]> gitweb @ CieloNegro.org - build-hc-pkg.git/blob - build-hc-pkg
build-hc-pkg: Code reorganization
[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 say_hello () {
28     local -r platform=$(detect_platform_type)
29     local -r n_cpus=$(detect_number_of_cpus)
30     echo "This is build-hc-pkg, running on $platform with $n_cpus processors."
31 }
32
33 function srcpath () {
34     local -r archive="work/archive"
35     local srcpath=""
36
37     if [[ -e "$archive" ]]; then
38         srcpath=$(cat "$archive")
39     fi
40
41     if [[ -z "$srcpath" || ! -e "$srcpath" ]]; then
42         mkdir -p work
43         read -e -p "Enter the file path to ghc-x.y.z-src.tar.bz2: " srcpath
44         if [[ -e "$srcpath" ]]; then
45             mkdir -p work
46             echo "$srcpath" > "$archive"
47         else
48             echo "$srcpath not found." >&2
49             exit 1
50         fi
51     fi
52
53     echo "$srcpath"
54 }
55
56 function build_hc_pkg () {
57     local -r srcpath="$(srcpath)"
58
59     gmake \
60         -j $(($(detect_number_of_cpus) + 1)) \
61         -f mk/main.mk \
62         SRCPATH="$srcpath" \
63         PLATFORM="$(detect_platform_type)" \
64         SHELL="$SHELL"
65
66     echo "If you are done, run \"$0 clean\" to cleanup the working directory."
67 }
68
69 function main () {
70     case "$1" in
71         "")
72             say_hello
73             build_hc_pkg
74             exit
75             ;;
76         "clean")
77             echo "Cleaning..."
78             rm -rf work
79             exit
80             ;;
81         *)
82             echo "Usage: $0 [clean]" >&2
83             exit 1
84             ;;
85     esac
86 }
87
88 main $@