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