]> gitweb @ CieloNegro.org - build-hc-pkg.git/blob - build-hc-pkg
Various changes to support 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 2>/dev/null; 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 detect_gnu_sed () {
40     if which gsed >/dev/null 2>&1; then
41         echo "gsed"
42     elif sed --version | grep -qF GNU; then
43         echo "sed"
44     else
45         echo "We need GNU sed to be installed." >&2
46         exit 1
47     fi
48 }
49
50 function say_hello () {
51     local -r self=$(basename "$0")
52     local -r platform=$(detect_platform_type)
53     local -r n_cpus=$(detect_number_of_cpus)
54
55     echo -n "This is $self, running on $platform with $n_cpus "
56     if (( $n_cpus == 1 )); then
57         echo "processor."
58     else
59         echo "processors."
60     fi
61 }
62
63 function srcpath () {
64     local -r archive="work/archive"
65     local srcpath=""
66
67     if [[ -e "$archive" ]]; then
68         srcpath=$(cat "$archive")
69     fi
70
71     if [[ -z "$srcpath" || ! -e "$srcpath" ]]; then
72         mkdir -p work
73         read -e -p "Enter the file path to ghc-x.y.z-src.tar.bz2: " srcpath
74         if [[ -e "$srcpath" ]]; then
75             mkdir -p work
76             echo "$srcpath" > "$archive"
77         else
78             echo "$srcpath not found." >&2
79             exit 1
80         fi
81     fi
82
83     echo "$srcpath"
84 }
85
86 function build_hc_pkg () {
87     local -r srcpath="$(srcpath)"
88
89     gmake \
90         -j $(($(detect_number_of_cpus) + 1)) \
91         -f mk/main.mk \
92         SRCPATH="$srcpath" \
93         PLATFORM="$(detect_platform_type)" \
94         GNUTAR="$(detect_gnu_tar)" \
95         GNUSED="$(detect_gnu_sed)" \
96         SHELL="$SHELL"
97
98     echo "If you are done, run \"$0 clean\" to cleanup the working directory."
99 }
100
101 function main () {
102     case "$1" in
103         "")
104             say_hello
105             build_hc_pkg
106             exit
107             ;;
108         "clean")
109             echo "Cleaning..."
110             rm -rf work
111             exit
112             ;;
113         *)
114             echo "Usage: $0 [clean]" >&2
115             exit 1
116             ;;
117     esac
118 }
119
120 main $@