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