]> gitweb @ CieloNegro.org - build-hc-pkg.git/blob - build-hc-pkg
tiny tweak
[build-hc-pkg.git] / build-hc-pkg
1 #!/usr/bin/env bash
2 set -e
3
4 function detect_platform_type () {
5     local -r canon_name="canon-name/src/canon-name"
6
7     if [[ ! -x "$canon_name" ]]; then
8         gmake -f mk/canon-name.mk
9     fi
10
11     "$canon_name"
12 }
13
14 function detect_number_of_cpus () {
15     local -r platform=$(detect_platform_type)
16
17     case "$platform" in
18         *-*-darwin)
19             ioreg -r -c IOCPU | fgrep -c IOCPUNumber
20             ;;
21         *)
22             echo "WARNING: I don't know how to detect the number of processors on this platform." >&2
23             echo "WARNING: GHC will be built using only 1 processor. Expect some slowdown." >&2
24             echo 1
25             ;;
26     esac
27 }
28
29 function detect_gnu_tar () {
30     if which gnutar >/dev/null 2>&1; then
31         echo "gnutar"
32     elif which gtar >/dev/null 2>&1; then
33         echo "gtar"
34     else
35         echo "We need GNU tar to be installed." >&2
36         exit 1
37     fi
38 }
39
40 function say_hello () {
41     local -r platform=$(detect_platform_type)
42     local -r n_cpus=$(detect_number_of_cpus)
43
44     echo "This is build-hc-pkg, running on $platform with $n_cpus processors."
45 }
46
47 function srcpath () {
48     local -r archive="work/archive"
49     local srcpath=""
50
51     if [[ -e "$archive" ]]; then
52         srcpath=$(cat "$archive")
53     fi
54
55     if [[ -z "$srcpath" || ! -e "$srcpath" ]]; then
56         mkdir -p work
57         read -e -p "Enter the file path to ghc-x.y.z-src.tar.bz2: " srcpath
58         if [[ -e "$srcpath" ]]; then
59             mkdir -p work
60             echo "$srcpath" > "$archive"
61         else
62             echo "$srcpath not found." >&2
63             exit 1
64         fi
65     fi
66
67     echo "$srcpath"
68 }
69
70 function build_hc_pkg () {
71     local -r srcpath="$(srcpath)"
72
73     gmake \
74         -j $(($(detect_number_of_cpus) + 1)) \
75         -f mk/main.mk \
76         SRCPATH="$srcpath" \
77         PLATFORM="$(detect_platform_type)" \
78         GNUTAR="$(detect_gnu_tar)" \
79         SHELL="$SHELL"
80
81     echo "If you are done, run \"$0 clean\" to cleanup the working directory."
82 }
83
84 function main () {
85     case "$1" in
86         "")
87             say_hello
88             build_hc_pkg
89             exit
90             ;;
91         "clean")
92             echo "Cleaning..."
93             rm -rf work
94             exit
95             ;;
96         *)
97             echo "Usage: $0 [clean]" >&2
98             exit 1
99             ;;
100     esac
101 }
102
103 main $@