Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/lamont/src/compiler/c-util/run-cc.sml
ViewVC logotype

Annotation of /branches/lamont/src/compiler/c-util/run-cc.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2081 - (view) (download)

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

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0