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