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, Universit��tstasse 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 x86_32 armv7 armv8 k10m"
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    "-t"|"--toolchain")
91        TC_ARCH="$2"
92        shift
93        TOOLSPEC="$2"
94        shift
95        case ${TC_ARCH} in
96        "arm")
97            ARM_TOOLSPEC="Just Tools.$TOOLSPEC"
98            ;;
99        "aarch64")
100            AARCH64_TOOLSPEC="Just Tools.$TOOLSPEC"
101            ;;
102        "thumb")
103            THUMB_TOOLSPEC="Just Tools.$TOOLSPEC"
104            ;;
105        "armeb")
106            ARMEB_TOOLSPEC="Just Tools.$TOOLSPEC"
107            ;;
108        "x86")
109            X86_TOOLSPEC="Just Tools.$TOOLSPEC"
110            ;;
111        "k1om")
112            K1OM_TOOLSPEC="Just Tools.$TOOLSPEC"
113            ;;
114	    *) 
115            echo "Unknown toolchain architecture: $TC_ARCH"
116            exit 1
117            ;;
118        esac
119        ;;
120    "-r"|"--toolroot")
121        TOOLROOT="Just \"$2\""
122        shift
123        ;;
124	"-j"|"--jobs")
125	    JOBS="$2"
126        shift 
127        ;;
128    "--cachedir")
129        CACHEDIR="$2"
130        shift
131        ;;
132    "--hagfish")
133        HAGFISH_LOCATION="$2"
134        shift
135        ;;
136    "--help")
137        usage
138        ;;
139	*) 
140	    usage
141	    ;;
142    esac
143    shift
144done
145
146if test -z "$INSTALLDIR"; then
147    echo "Install directory defaulting to '.'"
148    INSTALLDIR="."
149else
150    echo "Install directory is $INSTALLDIR"
151fi
152cd $INSTALLDIR
153
154if test -z "$SRCDIR"; then
155    usage
156fi
157
158if test ! -f "$SRCDIR"/hake/Main.hs; then
159    echo "Can't find Hake in the source directory $SRCDIR."
160    echo "Did you specify the source directory correctly?"
161    usage
162fi
163echo "Source directory is $SRCDIR"
164
165if test ! -z "$NEWARCHS"; then
166    ARCHS="$NEWARCHS"
167else 
168    ARCHS="$DFLTARCHS"
169fi
170echo "Architectures to build: $ARCHS"
171
172if test ! -d "$CACHEDIR"; then
173    mkdir -p "$CACHEDIR"
174    chmod g+rw "$CACHEDIR"
175fi
176
177if test ! -d hake; then
178    echo "Creating a local hake directory..."
179    mkdir -p hake
180    touch hake/.marker
181fi
182
183echo "Setting up hake build directory..."
184if test ! -f hake/Config.hs; then
185    echo "Bootstrapping Config.hs"
186    cp $SRCDIR/hake/Config.hs.template hake/Config.hs
187    cat >> hake/Config.hs <<EOF
188
189-- Automatically added by hake.sh. Do NOT copy these definitions to the defaults
190source_dir       = "$SRCDIR"
191architectures    = [ $ARCHS ]
192install_dir      = "$INSTALLDIR"
193toolroot         = $TOOLROOT
194arm_toolspec     = $ARM_TOOLSPEC
195aarch64_toolspec = $AARCH64_TOOLSPEC
196thumb_toolspec   = $THUMB_TOOLSPEC
197armeb_toolspec   = $ARMEB_TOOLSPEC
198x86_toolspec     = $X86_TOOLSPEC
199k1om_toolspec    = $K1OM_TOOLSPEC
200cache_dir        = "$CACHEDIR"
201hagfish_location = "$HAGFISH_LOCATION"
202EOF
203else
204    echo "You already have Config.hs, leaving it as-is."
205fi
206
207
208# FIXME: do we really need this; doesn't ghc get the dependencies right? -AB
209#rm -f hake/*.hi hake/*.o 
210
211# RTS parameters for hake.  Tuned to perform well for the mid-2015 Barrelfish
212# tree, building over NFS.  Maximum heap size sits around 64MB, or a little
213# more, so 128MB minimises collections.  Parallelism is a balancing act
214# between performance in the tree walk on NFS systems (which benefits heavily
215# from parallelising the IO operations), and performance in Hakefile
216# evaluation, which generally gets *slower* with more threads, as the GHC
217# garbage collector ends up thrashing a lot.
218HAKE_RTSOPTS="-H128M -A4M -N4"
219
220echo "Building hake..."
221ghc -O --make \
222    -XDeriveDataTypeable \
223    -XStandaloneDeriving \
224    -XScopedTypeVariables \
225    -package ghc \
226    -package ghc-mtl \
227    -package ghc-paths \
228    -package bytestring-trie \
229    -o hake/hake \
230    -outputdir hake \
231    -i$SRCDIR/hake \
232    -ihake \
233    -rtsopts=all \
234    -with-rtsopts="$HAKE_RTSOPTS" \
235    -threaded \
236    $SRCDIR/hake/Main.hs $LDFLAGS || exit 1
237
238    # -eventlog \
239
240if test "$RUN_HAKE" = "No"; then
241    echo "Not running hake as per your request."
242    exit
243fi
244
245echo "Running hake..."
246./hake/hake --output-filename Makefile --source-dir "$SRCDIR/" --ghc-libdir "$(ghc --print-libdir)" || exit
247
248echo "Now running initial make to build dependencies."
249echo "Running $JOBS jobs at once (-j N to change this)."
250make -j "$JOBS" help
251