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