1#!/bin/sh
2
3##########################################################################
4# Copyright (c) 2009, 2011, 2013, 2015, ETH Zurich.
5# All rights reserved.
6#
7# This file is distributed under the terms in the attached LICENSE file.
8# If you do not find this file, copies can be found by writing to:
9# ETH Zurich D-INFK, Universitaetstasse 6, CH-8092 Zurich. Attn: Systems Group.
10##########################################################################
11
12DFLTARCHS="\"x86_64\""
13RUN_HAKE="Yes"
14HAKEDIR=$(dirname $0)
15DEFAULT_JOBS=4
16JOBS="$DEFAULT_JOBS"
17CACHEDIR="$HOME/.cache/barrelfish"
18HAGFISH_LOCATION="/home/netos/tftpboot/Hagfish.efi"
19
20# Don't override the default toolchain unless asked to.
21TOOLROOT=Nothing
22ARM_TOOLSPEC=Nothing
23AARCH64_TOOLSPEC=Nothing
24THUMB_TOOLSPEC=Nothing
25ARMEB_TOOLSPEC=Nothing
26X86_TOOLSPEC=Nothing
27K1OM_TOOLSPEC=Nothing
28
29usage() {
30    echo "Usage: $0 <options>"
31    echo "   -s|--source-dir: path to source tree (required)"
32    echo "   -i|--install-dir: path to install directory (defaults to \`pwd\`)"
33    echo "   -a|--architecture: specify archtitecture to build for (can be"
34    echo "       given multiple times, default architectures are"
35    echo "       $DFLTARCHS"
36    echo "   -n|--no-hake: just rebuild hake itself, don't run it (only useful"
37    echo "       for debugging hake)"
38    echo "   -t|--toolchain <arch> <toolchain>: use <toolchain> to build for"
39    echo "       <arch>."
40    echo "   -r|--toolroot <path>: where should I look for toolchains (instead"
41    echo "       of (/home/netos/tools)"
42    echo "   -j|--jobs: Number of parallel jobs to run (default $DEFAULT_JOBS)."
43    echo "   --hagfish: Location of Hagfish boot loader (default $HAGFISH_LOCATION)."
44    echo "   --cachedir: Cache directory (default $CACHEDIR)."
45    echo "   --help: Print this help."
46    echo ""
47    echo "  The way you use this script is to create a new directory for your"
48    echo "  build tree, cd into it, and run this script with the --source-dir"
49    echo "  argument specifying the top of the source tree."
50    echo ""
51    echo "  Known architectures may include: "
52    echo "     x86_64 armv7 armv8 k1om"
53    exit 1;
54}
55
56#
57# Legacy compatibility to avoid breaking the harness...
58#
59if test $# -eq 1 && test $1 != "-"*; then
60    echo "WARNING: old usage of hake.sh (sole argument gives the source directory) is"
61    echo "deprecated: please use --source-dir instead."
62    SRCDIR="$1"
63    shift
64fi
65
66#
67# Parse args
68#
69while test $# -ne 0; do
70    case $1 in
71	"-a"|"--architecture")
72	    if test -z "$NEWARCHS"; then
73		    NEWARCHS="\"$2\""
74	    else
75		    NEWARCHS="$NEWARCHS, \"$2\""
76	    fi
77        shift
78	    ;;
79	"-i"|"--install-dir")
80	    INSTALLDIR="$2"
81        shift
82	    ;;
83	"-s"|"--source-dir")
84	    SRCDIR="$2"
85        shift
86	    ;;
87	"-n"|"--no-hake")
88	    RUN_HAKE="No"
89	    ;;
90    "-d"|"--no-deps")
91        NO_DEPS="No"
92        ;;
93    "-t"|"--toolchain")
94        TC_ARCH="$2"
95        shift
96        TOOLSPEC="$2"
97        shift
98        case ${TC_ARCH} in
99        "arm")
100            ARM_TOOLSPEC="Just Tools.$TOOLSPEC"
101            ;;
102        "aarch64")
103            AARCH64_TOOLSPEC="Just Tools.$TOOLSPEC"
104            ;;
105        "thumb")
106            THUMB_TOOLSPEC="Just Tools.$TOOLSPEC"
107            ;;
108        "armeb")
109            ARMEB_TOOLSPEC="Just Tools.$TOOLSPEC"
110            ;;
111        "x86")
112            X86_TOOLSPEC="Just Tools.$TOOLSPEC"
113            ;;
114        "k1om")
115            K1OM_TOOLSPEC="Just Tools.$TOOLSPEC"
116            ;;
117	    *)
118            echo "Unknown toolchain architecture: $TC_ARCH"
119            exit 1
120            ;;
121        esac
122        ;;
123    "-r"|"--toolroot")
124        TOOLROOT="Just \"$2\""
125        shift
126        ;;
127	"-j"|"--jobs")
128	    JOBS="$2"
129        shift
130        ;;
131    "--cachedir")
132        CACHEDIR="$2"
133        shift
134        ;;
135    "--hagfish")
136        HAGFISH_LOCATION="$2"
137        shift
138        ;;
139    "--help")
140        usage
141        ;;
142	*)
143	    usage
144	    ;;
145    esac
146    shift
147done
148
149if test -z "$INSTALLDIR"; then
150    echo "Install directory defaulting to '.'"
151    INSTALLDIR="."
152else
153    echo "Install directory is $INSTALLDIR"
154fi
155cd $INSTALLDIR
156
157if test -z "$SRCDIR"; then
158    usage
159fi
160
161if test ! -f "$SRCDIR"/hake/Main.hs; then
162    echo "Can't find Hake in the source directory $SRCDIR."
163    echo "Did you specify the source directory correctly?"
164    usage
165fi
166echo "Source directory is $SRCDIR"
167
168if test ! -z "$NEWARCHS"; then
169    ARCHS="$NEWARCHS"
170else
171    ARCHS="$DFLTARCHS"
172fi
173echo "Architectures to build: $ARCHS"
174
175if test ! -d "$CACHEDIR"; then
176    mkdir -p "$CACHEDIR"
177    chmod g+rw "$CACHEDIR"
178fi
179
180if test ! -d hake; then
181    echo "Creating a local hake directory..."
182    mkdir -p hake
183    touch hake/.marker
184fi
185
186echo "Setting up hake build directory..."
187if test ! -f hake/Config.hs; then
188    echo "Bootstrapping Config.hs"
189    cp $SRCDIR/hake/Config.hs.template hake/Config.hs
190    cat >> hake/Config.hs <<EOF
191
192-- Automatically added by hake.sh. Do NOT copy these definitions to the defaults
193source_dir       = "$SRCDIR"
194architectures    = [ $ARCHS ]
195install_dir      = "$INSTALLDIR"
196toolroot         = $TOOLROOT
197arm_toolspec     = $ARM_TOOLSPEC
198aarch64_toolspec = $AARCH64_TOOLSPEC
199thumb_toolspec   = $THUMB_TOOLSPEC
200armeb_toolspec   = $ARMEB_TOOLSPEC
201x86_toolspec     = $X86_TOOLSPEC
202k1om_toolspec    = $K1OM_TOOLSPEC
203cache_dir        = "$CACHEDIR"
204hagfish_location = "$HAGFISH_LOCATION"
205EOF
206else
207    echo "You already have Config.hs, leaving it as-is."
208fi
209
210
211# FIXME: do we really need this; doesn't ghc get the dependencies right? -AB
212#rm -f hake/*.hi hake/*.o
213
214# RTS parameters for hake.  Tuned to perform well for the mid-2015 Barrelfish
215# tree, building over NFS.  Maximum heap size sits around 64MB, or a little
216# more, so 128MB minimises collections.  Parallelism is a balancing act
217# between performance in the tree walk on NFS systems (which benefits heavily
218# from parallelising the IO operations), and performance in Hakefile
219# evaluation, which generally gets *slower* with more threads, as the GHC
220# garbage collector ends up thrashing a lot.
221HAKE_RTSOPTS="-H1024M -A4M -N4"
222
223echo "Building hake..."
224ghc -O2 --make \
225    -XDeriveDataTypeable \
226    -XStandaloneDeriving \
227    -XScopedTypeVariables \
228    -package ghc \
229    -package ghc-mtl \
230    -package ghc-paths \
231    -package bytestring-trie \
232    -o hake/hake \
233    -outputdir hake \
234    -i$SRCDIR/hake \
235    -ihake \
236    -rtsopts=all \
237    -with-rtsopts="$HAKE_RTSOPTS" \
238    -threaded \
239    $SRCDIR/hake/Main.hs $LDFLAGS || exit 1
240
241    # -eventlog \
242
243if test "$RUN_HAKE" = "No"; then
244    echo "Not running hake as per your request."
245    exit
246fi
247
248echo "Running hake..."
249./hake/hake --output-filename Makefile --source-dir "$SRCDIR/" --ghc-libdir "$(ghc --print-libdir)" || exit
250
251if test "$NO_DEPS" = "No"; then
252    echo "Not running dependencies as per your request."
253    exit
254fi
255
256echo "Now running initial make to build dependencies."
257echo "Running $JOBS jobs at once (-j N to change this)."
258make -j "$JOBS" help
259