1--------------------------------------------------------------------------
2-- Copyright (c) 2007-2010, ETH Zurich.
3-- All rights reserved.
4--
5-- This file is distributed under the terms in the attached LICENSE file.
6-- If you do not find this file, copies can be found by writing to:
7-- ETH Zurich D-INFK, Universitaetstasse 6, CH-8092 Zurich. Attn: Systems Group.
8--
9-- Default architecture-specific definitions for Barrelfish
10--
11--------------------------------------------------------------------------
12
13module ArchDefaults where
14
15import Data.List
16import HakeTypes
17import System.FilePath
18import qualified Config
19
20commonFlags = [ Str s | s <- [ "-fno-builtin",
21                                "-nostdinc",
22                                "-U__linux__",
23                                "-Ulinux",
24                                "-Wall",
25                                "-Wshadow",
26                                "-Wmissing-declarations",
27                                "-Wmissing-field-initializers",
28                                "-Wtype-limits",
29                                "-Wredundant-decls",
30                                "-Werror" ] ]
31
32commonCFlags = [ Str s | s <- [ "-std=c99",
33                                "-Wstrict-prototypes",
34                                "-Wold-style-definition",
35                                "-Wmissing-prototypes" ] ]
36                 ++ [ Str (if Config.use_fp then "-fno-omit-frame-pointer" else "") ]
37
38commonCxxFlags = [ Str s | s <- [ "-nostdinc++",
39                                  "-fexceptions",
40                                  "-nodefaultlibs",
41                                  "-fasynchronous-unwind-tables",
42                                  "-DLIBCXX_CXX_ABI=libcxxabi",
43                                  "-I" ] ]
44                 ++ [ NoDep SrcTree "src" "/include/cxx" ]
45                 ++ [ Str (if Config.use_fp then "-fno-omit-frame-pointer" else "") ]
46
47cFlags = [ Str s | s <- [ "-Wno-packed-bitfield-compat" ] ]
48       ++ commonCFlags
49
50cxxFlags = [ Str s | s <- [ "-Wno-packed-bitfield-compat" ] ]
51       ++ commonCxxFlags
52
53cDefines options = [ Str ("-D"++s) | s <- [ "BARRELFISH",
54                                            "BF_BINARY_PREFIX=\\\"\\\""
55                                          ]
56                   ]
57                   ++ Config.defines
58                   ++ Config.arch_defines options
59
60cStdIncs arch archFamily =
61    [ NoDep BFSrcTree "src" "/include",
62      NoDep BFSrcTree "src" ("/include/arch" </> archFamily),
63      NoDep BFSrcTree "src" ("/include/target" </> archFamily),
64      NoDep InstallTree arch "/include",
65      NoDep BFSrcTree "src" ".",
66      NoDep SrcTree "src" ".",
67      NoDep BuildTree arch "." ]
68
69ldFlags arch =
70    map Str Config.cOptFlags ++
71    [ In InstallTree arch "/lib/crt0.o",
72      In InstallTree arch "/lib/crtbegin.o",
73      Str "-fno-builtin",
74      Str "-nostdlib" ]
75
76ldCxxFlags arch =
77    map Str Config.cOptFlags ++
78    [ In InstallTree arch "/lib/crt0.o",
79      In InstallTree arch "/lib/crtbegin.o",
80      Str "-fno-builtin",
81      Str "-nostdlib" ]
82
83kernelLibs arch =
84    [ In InstallTree arch "/lib/libcompiler-rt.a" ]
85
86-- Libraries that are linked to all applications.
87stdLibs arch =
88    [ In InstallTree arch "/lib/libbarrelfish.a",
89      In InstallTree arch "/lib/libterm_client.a",
90      In InstallTree arch "/lib/liboctopus_parser.a", -- XXX: For NS client in libbarrelfish
91      In InstallTree arch "/errors/errno.o",
92      In InstallTree arch ("/lib/libc.a"),
93      In InstallTree arch "/lib/libcompiler-rt.a",
94      --In InstallTree arch "/lib/libposixcompat.a",
95      --In InstallTree arch "/lib/libvfs.a",
96      --In InstallTree arch "/lib/libnfs.a",
97      --In InstallTree arch "/lib/liblwip.a",
98      --In InstallTree arch "/lib/libbarrelfish.a",
99      --In InstallTree arch "/lib/libcontmng.a",
100      --In InstallTree arch "/lib/libprocon.a",
101      In InstallTree arch "/lib/crtend.o" ,
102      In InstallTree arch "/lib/libcollections.a" ]
103
104stdCxxLibs arch =
105    [ In InstallTree arch "/lib/libcxx.a" ]
106    ++ stdLibs arch
107
108options arch archFamily = Options {
109            optArch = arch,
110            optArchFamily = archFamily,
111            optFlags = cFlags,
112            optCxxFlags = cxxFlags,
113            optDefines = [ Str "-DBARRELFISH" ] ++ Config.defines,
114            optIncludes = cStdIncs arch archFamily,
115            optDependencies =
116                [ Dep InstallTree arch "/include/errors/errno.h",
117                  Dep InstallTree arch "/include/barrelfish_kpi/capbits.h",
118                  Dep InstallTree arch "/include/asmoffsets.h",
119                  Dep InstallTree arch "/include/trace_definitions/trace_defs.h" ],
120            optLdFlags = ldFlags arch,
121            optLdCxxFlags = ldCxxFlags arch,
122            optLibs = stdLibs arch,
123            optCxxLibs = stdCxxLibs arch,
124            optInterconnectDrivers = ["lmp", "ump", "multihop", "local"],
125            optFlounderBackends = ["lmp", "ump", "multihop", "local"],
126            extraFlags = [],
127            extraCxxFlags = [],
128            extraDefines = [],
129            extraIncludes = [],
130            extraDependencies = [],
131            extraLdFlags = [],
132            optSuffix = [],
133            optInstallPath = OptionsPath {
134                optPathBin = "/sbin",
135                optPathLib = "/lib"
136            }
137          }
138
139------------------------------------------------------------------------
140--
141-- Now, commands to actually do something
142--
143------------------------------------------------------------------------
144
145--
146-- C compiler
147--
148cCompiler :: String -> String -> [String] -> Options -> String ->
149             String -> String -> [RuleToken]
150cCompiler arch compiler opt_flags opts phase src obj =
151    let incls = (extraIncludes opts) ++ (optIncludes opts)
152        flags = (optFlags opts)
153                ++ (optDefines opts)
154                ++ [ Str f | f <- extraFlags opts ]
155                ++ [ Str f | f <- extraDefines opts ]
156        deps = (optDependencies opts) ++ (extraDependencies opts)
157    in
158      [ Str compiler ] ++ flags ++ (map Str opt_flags)
159      ++ concat [ [ NStr "-I", i ] | i <- incls ]
160      ++ [ Str "-o", Out arch obj,
161           Str "-c", In (if phase == "src" then SrcTree else BuildTree) phase src ]
162      ++ deps
163
164--
165-- the C preprocessor, like C compiler but with -E
166--
167cPreprocessor :: String -> String -> [String] -> Options -> String ->
168                 String -> String -> [RuleToken]
169cPreprocessor arch compiler opt_flags opts phase src obj =
170    let incls = (extraIncludes opts) ++ (optIncludes opts)
171        flags = (optFlags opts)
172                ++ (optDefines opts)
173                ++ [ Str f | f <- extraFlags opts ]
174                ++ [ Str f | f <- extraDefines opts ]
175        deps = (optDependencies opts) ++ (extraDependencies opts)
176        cOptFlags = opt_flags \\ ["-g"]
177    in
178      [ Str compiler ] ++ flags ++ (map Str cOptFlags)
179      ++ concat [ [ NStr "-I", i ] | i <- incls ]
180      ++ [ Str "-o", Out arch obj,
181           Str "-E", In (if phase == "src" then SrcTree else BuildTree) phase src ]
182      ++ deps
183
184--
185-- C++ compiler
186--
187cxxCompiler arch cxxcompiler opt_flags opts phase src obj =
188    let incls = (extraIncludes opts) ++ (optIncludes opts)
189        flags = (optCxxFlags opts)
190                ++ (optDefines opts)
191                ++ [ Str f | f <- extraCxxFlags opts ]
192                ++ [ Str f | f <- extraDefines opts ]
193        deps = (optDependencies opts) ++ (extraDependencies opts)
194    in
195      [ Str cxxcompiler ] ++ flags ++ (map Str opt_flags)
196      ++ concat [ [ NStr "-I", i ] | i <- incls ]
197      ++ [ Str "-o", Out arch obj,
198           Str "-c", In (if phase == "src" then SrcTree else BuildTree) phase src ]
199      ++ deps
200
201--
202-- Create C file dependencies
203--
204makeDepend arch compiler opts phase src obj depfile =
205    let incls = (extraIncludes opts) ++ (optIncludes opts)
206        flags = (optFlags opts)
207                ++ (optDefines opts)
208                ++ [ Str f | f <- extraFlags opts ]
209                ++ [ Str f | f <- extraDefines opts ]
210    in
211      [ Str ('@':compiler) ] ++ flags
212      ++ concat [ [ NStr "-I", i ] | i <- incls ]
213      ++ (optDependencies opts) ++ (extraDependencies opts)
214      ++ [ Str "-M -MF",
215           Out arch depfile,
216           Str "-MQ", NoDep BuildTree arch obj,
217           Str "-MQ", NoDep BuildTree arch depfile,
218           Str "-c", In (if phase == "src" then SrcTree else BuildTree) phase src
219         ]
220
221--
222-- Create C++ file dependencies
223--
224makeCxxDepend arch cxxcompiler opts phase src obj depfile =
225    let incls = (extraIncludes opts) ++ (optIncludes opts)
226        flags = (optCxxFlags opts)
227                ++ (optDefines opts)
228                ++ [ Str f | f <- extraCxxFlags opts ]
229                ++ [ Str f | f <- extraDefines opts ]
230    in
231      [ Str ('@':cxxcompiler) ] ++ flags
232      ++ concat [ [ NStr "-I", i ] | i <- incls ]
233      ++ (optDependencies opts) ++ (extraDependencies opts)
234      ++ [ Str "-M -MF",
235           Out arch depfile,
236           Str "-MQ", NoDep BuildTree arch obj,
237           Str "-MQ", NoDep BuildTree arch depfile,
238           Str "-c", In (if phase == "src" then SrcTree else BuildTree) phase src
239         ]
240
241--
242-- Compile a C program to assembler
243--
244cToAssembler :: String -> String -> [String] -> Options -> String -> String ->
245                String -> String -> [ RuleToken ]
246cToAssembler arch compiler opt_flags opts phase src afile objdepfile =
247    let incls = (extraIncludes opts) ++ (optIncludes opts)
248        flags = (optFlags opts)
249                ++ (optDefines opts)
250                ++ [ Str f | f <- extraFlags opts ]
251                ++ [ Str f | f <- extraDefines opts ]
252        deps = [ Dep BuildTree arch objdepfile ] ++
253               (optDependencies opts) ++
254               (extraDependencies opts)
255    in
256      [ Str compiler ] ++ flags ++ (map Str opt_flags)
257      ++ concat [ [ NStr "-I", i ] | i <- incls ]
258      ++ [ Str "-o ", Out arch afile,
259           Str "-S ", In (if phase == "src" then SrcTree else BuildTree) phase src ]
260      ++ deps
261
262--
263-- Assemble an assembly language file
264--
265assembler :: String -> String -> [ String ] -> Options -> String ->
266             String -> [ RuleToken ]
267assembler arch compiler opt_flags opts src obj =
268    let incls = (extraIncludes opts) ++ (optIncludes opts)
269        flags = (optFlags opts)
270                ++ (optDefines opts)
271                ++ [ Str f | f <- extraFlags opts ]
272                ++ [ Str f | f <- extraDefines opts ]
273        deps = (optDependencies opts) ++ (extraDependencies opts)
274    in
275      [ Str compiler ] ++ flags ++ (map Str opt_flags)
276      ++ concat [ [ NStr "-I", i ] | i <- incls ]
277      ++ [ Str "-o ", Out arch obj, Str "-c ", In SrcTree "src" src ]
278      ++ deps
279
280--
281-- Create a library from a set of object files
282--
283archive :: String -> Options -> [String] -> [String] -> String -> String -> [ RuleToken ]
284archive arch opts objs libs name libname =
285    [ Str "rm -f ", Out arch libname ]
286    ++
287    [ NL, Str "ar crT ", Out arch libname ]
288    ++
289    [ In BuildTree arch o | o <- objs ]
290    ++
291    if libs == [] then []
292                  else [ In BuildTree arch a | a <- libs ]
293    ++
294    [ NL, Str "ranlib ", Out arch libname ]
295
296--
297-- Link an executable
298--
299linker :: String -> String -> Options -> [String] -> [String] -> [String] -> String -> [RuleToken]
300linker arch compiler opts objs libs mods bin =
301    [ Str compiler ]
302    ++ (optLdFlags opts)
303    ++
304    (extraLdFlags opts)
305    ++
306    [ Str "-o", Out arch bin ]
307    ++
308    [ In BuildTree arch o | o <- objs ]
309    ++
310    [Str "-Wl,--start-group"]
311    ++
312    [ In BuildTree arch l | l <- libs ]
313    ++
314    [Str "-Wl,--whole-archive"] ++ [ In BuildTree arch l | l <- mods ] ++ [Str "-Wl,--no-whole-archive"]
315    ++
316    [ In BuildTree arch l | l <- libs ]
317    ++
318    (optLibs opts)
319    ++
320    [Str "-Wl,--end-group"]
321
322
323--
324-- Link an executable
325--
326cxxlinker :: String -> String -> Options -> [String] -> [String] -> [String] -> String -> [RuleToken]
327cxxlinker arch cxxcompiler opts objs libs mods bin =
328    [ Str cxxcompiler ]
329    ++ (optLdCxxFlags opts)
330    ++
331    (extraLdFlags opts)
332    ++
333    [ Str "-o", Out arch bin ]
334    ++
335    [ In BuildTree arch o | o <- objs ]
336    ++
337    [ In BuildTree arch l | l <- libs ]
338    ++
339    [Str "-Wl,--start-group -Wl,--whole-archive"] ++ [ In BuildTree arch l | l <- mods ] ++ [Str "-Wl,--no-whole-archive"]
340    ++
341    (optCxxLibs opts)
342    ++
343    [Str "-Wl,--end-group"]
344
345--
346-- Strip debug symbols from an executable
347--
348strip :: String -> String -> Options -> String -> String ->
349         String -> [RuleToken]
350strip arch objcopy opts src debuglink target =
351    [ Str objcopy,
352      Str "-g",
353      NStr "--add-gnu-debuglink=", In BuildTree arch debuglink,
354      In BuildTree arch src,
355      Out arch target
356    ]
357
358--
359-- Extract debug symbols from an executable
360--
361debug :: String -> String -> Options -> String -> String -> [RuleToken]
362debug arch objcopy opts src target =
363    [ Str objcopy,
364      Str "--only-keep-debug",
365      In BuildTree arch src,
366      Out arch target
367    ]
368