3ce3798890deded352a36b9472dbaf7631e23f76
[nit.git] / src / compiling / compiling.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Compute and generate tables for classes and modules.
18 package compiling
19
20 import compiling_base
21 private import compiling_methods
22 private import compiling_global
23 private import syntax
24
25 redef class MMSrcModule
26 # Compile the program
27 # Generate all sep files (_sep.[ch]), the main file (_table.c) and the build file (_build.sh)
28 # Then execute the build.sh
29 meth compile_prog_to_c(tc: ToolContext)
30 do
31 for m in mhe.greaters_and_self do
32 assert m isa MMSrcModule
33 m.local_analysis(tc)
34 end
35
36 var ga = global_analysis(tc)
37
38 tc.compdir.mkdir
39
40 var files = new Array[String]
41 var includes = new ArraySet[String]
42 files.add("$CLIBDIR/nit_main.c")
43 for m in mhe.greaters_and_self do
44 assert m isa MMSrcModule
45 files.add("{tc.compdir}/{m.name}._sep.c")
46 m.compile_separate_module(tc, ga)
47 var native_name = m.filename.strip_extension(".nit")
48 if (native_name + "_nit.h").file_exists then
49 includes.add("-I {native_name.dirname}")
50 end
51 native_name += "_nit.c"
52 if native_name.file_exists then files.add(native_name)
53 end
54
55 files.add("{tc.compdir}/{name}._tables.c")
56 compile_main(tc, ga)
57
58 var fn = "{tc.compdir}/{name}._build.sh"
59 var f = new OFStream.open(fn)
60 f.write("#!/bin/sh\n")
61 f.write("# This shell script is generated by NIT to compile the program {name}.\n")
62 f.write("CLIBDIR=\"{tc.clibdir}\"\n")
63 f.write("{tc.bindir}/gccx -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
64 if tc.output_file != null then
65 f.write(" -o {tc.output_file}")
66 else if tc.ext_prefix.is_empty then
67 f.write(" -o {name}")
68 else
69 f.write(" -o {name}_{tc.ext_prefix}")
70 end
71 if tc.boost then f.write(" -O")
72 f.write(" \"$@\" \\\n {files.join("\\\n ")}\n")
73 f.close
74
75 if not tc.no_cc then
76 sys.system("sh {fn}")
77 end
78 end
79
80 # Compile the main file
81 private meth compile_main(tc: ToolContext, ga: GlobalAnalysis)
82 do
83 var v = new GlobalCompilerVisitor(self, tc, ga)
84 v.add_decl("#include <nit_common.h>")
85 compile_tables_to_c(v)
86 compile_main_part(v)
87 var f = new OFStream.open("{tc.compdir}/{name}._tables.c")
88 f.write("/* This C file is generated by NIT to compile program {name}. */\n")
89 for m in mhe.greaters_and_self do
90 f.write("#include \"{m.name}._sep.h\"\n")
91 end
92 f.write(v.to_s)
93 f.close
94 end
95
96 # Compile the sep files (of the current module only)
97 private meth compile_separate_module(tc: ToolContext, ga: GlobalAnalysis)
98 do
99 var v = new GlobalCompilerVisitor(self, tc, ga)
100 v.add_decl("#include <nit_common.h>")
101 var native_name = filename.strip_extension(".nit")
102 native_name += ("_nit.h")
103 if native_name.file_exists then v.add_decl("#include <{native_name.basename("")}>")
104 declare_class_tables_to_c(v)
105 compile_mod_to_c(v)
106 var f = new OFStream.open("{tc.compdir}/{name}._sep.h")
107 f.write("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
108 f.write("#ifndef {name}_sep\n")
109 f.write("#define {name}_sep\n")
110 for m in mhe.direct_greaters do f.write("#include \"{m.name}._sep.h\"\n")
111 f.write(v.ctx.decls.join("\n"))
112 f.write("\n#endif\n")
113 f.close
114 var f = new OFStream.open("{tc.compdir}/{name}._sep.c")
115 f.write("/* This C file is generated by NIT to compile module {name}. */\n")
116 f.write("#include \"{name}._sep.h\"\n")
117 f.write(v.ctx.instrs.join("\n"))
118 f.write("\n")
119 f.close
120 end
121 end
122