SCM Repository
Annotation of /trunk/src/compiler/c-util/run-cc.sml
Parent Directory
|
Revision Log
Revision 3349 - (view) (download)
1 : | jhr | 1115 | (* run-cc.sml |
2 : | * | ||
3 : | jhr | 3349 | * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 : | * | ||
5 : | * COPYRIGHT (c) 2015 The University of Chicago | ||
6 : | jhr | 1115 | * All rights reserved. |
7 : | * | ||
8 : | * Support for running the C compiler to compile and link the generated code. | ||
9 : | *) | ||
10 : | |||
11 : | structure RunCC : sig | ||
12 : | |||
13 : | jhr | 1232 | (* compile ("base", cflags) -- compiles the file "base.c" to produce "base.o". *) |
14 : | val compile : string * string list -> unit | ||
15 : | jhr | 1115 | |
16 : | jhr | 2471 | (* linkExec ("base", opts) -- links base.o to create an executable using the |
17 : | * given options (libraries, etc.) | ||
18 : | *) | ||
19 : | val linkExec : string * string list -> unit | ||
20 : | jhr | 1115 | |
21 : | jhr | 2471 | (* linkLib ("base", opts) -- links base.o to create using the given options (libraries, etc.) *) |
22 : | val linkLib : string * string list -> unit | ||
23 : | |||
24 : | jhr | 1115 | end = struct |
25 : | |||
26 : | fun system cmd = ( | ||
27 : | jhr | 1232 | Log.msg(cmd ^ "\n"); |
28 : | jhr | 1115 | if OS.Process.isSuccess(OS.Process.system cmd) |
29 : | then () | ||
30 : | else raise Fail "error compiling/linking") | ||
31 : | |||
32 : | jhr | 1232 | fun compile (baseName, cflags) = let |
33 : | jhr | 1115 | val cFile = OS.Path.joinBaseExt{base=baseName, ext=SOME"c"} |
34 : | jhr | 1232 | val cmd = String.concatWith " " ([Paths.cc, "-c"] @ cflags @ [cFile]) |
35 : | jhr | 1115 | in |
36 : | PhaseTimer.withTimer Timers.timeCC system cmd | ||
37 : | end | ||
38 : | |||
39 : | jhr | 2471 | fun linkExec (baseName, ldOpts) = let |
40 : | jhr | 1115 | val objFile = OS.Path.joinBaseExt{base=baseName, ext=SOME"o"} |
41 : | val exeFile = baseName | ||
42 : | jhr | 1232 | val cmd = String.concatWith " " ([Paths.cc, "-o", exeFile, objFile] @ ldOpts) |
43 : | jhr | 1115 | in |
44 : | PhaseTimer.withTimer Timers.timeCC system cmd | ||
45 : | end | ||
46 : | |||
47 : | jhr | 2471 | fun linkLib (baseName, ldOpts) = let |
48 : | val objFile = OS.Path.joinBaseExt{base=baseName, ext=SOME"o"} | ||
49 : | val tmpFile = let | ||
50 : | (* on Linux systems, the rename fails if the src and dst are on | ||
51 : | * different devices, so we create the temp file in the same | ||
52 : | * directory as the final target. | ||
53 : | *) | ||
54 : | val {file, ...} = OS.Path.splitDirFile(OS.FileSys.tmpName()) | ||
55 : | val {dir, ...} = OS.Path.splitDirFile baseName | ||
56 : | in | ||
57 : | OS.Path.joinDirFile{ | ||
58 : | dir = dir, | ||
59 : | file = OS.Path.joinBaseExt{ | ||
60 : | base = file, | ||
61 : | ext = SOME "o" | ||
62 : | } | ||
63 : | } | ||
64 : | end | ||
65 : | val cmd = String.concatWith " " ([Paths.ld, "-r", "-o", tmpFile, objFile] @ ldOpts) | ||
66 : | fun link () = ( | ||
67 : | system cmd; | ||
68 : | Log.msg(concat["rename ", tmpFile, " to ", objFile, "\n"]); | ||
69 : | OS.FileSys.rename{old=tmpFile, new=objFile} | ||
70 : | handle ex => (OS.FileSys.remove tmpFile; raise ex)) | ||
71 : | in | ||
72 : | PhaseTimer.withTimer Timers.timeCC link () | ||
73 : | end | ||
74 : | |||
75 : | jhr | 1115 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |