4f58fbf1b9c34eacf92ea7db6c7efb7d28ec1abf
[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 table_computation
21 import compiling_base
22 import icode_generator
23 private import compiling_global
24 private import compiling_icode
25
26 redef class Program
27 # The type of code generation to use
28 readable writable var _output_format: String = "none"
29
30 # Compile the program depending on the output format
31 fun compile_prog
32 do
33 if output_format == "none" then
34 # Nothing to do
35 else
36 # Optimize all iroutines
37 with_each_iroutines !action(i, m) do i.optimize(m)
38
39 if output_format == "C" then
40 compile_prog_to_c
41 else if output_format == "icode" then
42 generate_icode_files
43 end
44 end
45 end
46
47 # Compile the program to C
48 # Generate all files (_sep.[ch] or _glob.[ch]), the main file (_table.c) and the build file (_build.sh)
49 # Then execute the build.sh
50 fun compile_prog_to_c
51 do
52 var compdir = tc.compdir.as(not null)
53 compdir.mkdir
54
55 var files = new Array[String]
56 var includes = new ArraySet[String]
57 files.add("$CLIBDIR/nit_main.c")
58 files.add("$CLIBDIR/gc.c")
59 files.add("$CLIBDIR/gc_static_objects_list.c")
60 tc.info("Generating C code",1)
61 for m in module.mhe.greaters_and_self do
62 files.add("{compdir}/{m.name}.{get_file_ending}.c")
63 tc.info("Generating C code for module: {m.name}",2)
64 m.compile_separate_module(self)
65 var native_name = m.location.file.strip_extension(".nit")
66 if (native_name + "_nit.h").file_exists then
67 includes.add("-I {native_name.dirname}")
68 end
69 native_name += "_nit.c"
70 if native_name.file_exists then files.add(native_name)
71 end
72
73 tc.info("Generating main, tables and makefile ...",1)
74 files.add("{compdir}/{module.name}._tables.c")
75 compile_main
76
77 var fn = "{compdir}/{module.name}._build.sh"
78 var f = new OFStream.open(fn)
79 var verbose = ""
80
81 if tc.verbose_level > 0 then
82 verbose = "-"
83 for i in [1..tc.verbose_level] do verbose = verbose + "v"
84 end
85
86 f.write("#!/bin/sh\n")
87 f.write("# This shell script is generated by NIT to compile the program {module.name}.\n")
88 f.write("CLIBDIR=\"{tc.clibdir.as(not null)}\"\n")
89 f.write("{tc.bindir.as(not null)}/gccx {verbose} -d {compdir} -I $CLIBDIR {includes.join(" ")}")
90 if tc.output_file != null then
91 f.write(" -o {tc.output_file.as(not null)}")
92 else if tc.ext_prefix.is_empty then
93 f.write(" -o {module.name}")
94 else
95 f.write(" -o {module.name}_{tc.ext_prefix}")
96 end
97 if tc.boost then f.write(" -O")
98 if not tc.cc_link then f.write(" -x \"-c\"")
99 for l in tc.cc_libs do f.write(" -x \"-l {l}\"")
100 for lp in tc.cc_lib_paths do f.write(" -x \"-L {lp}\"")
101 for ip in tc.cc_include_paths do f.write(" -x \"-I {ip}\"")
102 f.write(" \"$@\" \\\n {files.join("\\\n ")}\n")
103 f.close
104
105 if not tc.no_cc then
106 tc.info("Building",1)
107 sys.system("sh {fn}")
108 end
109 end
110
111 # Compile the main file
112 private fun compile_main
113 do
114 var v = new CompilerVisitor(module, self)
115 v.add_decl("#include <nit_common.h>")
116 compile_tables_to_c(v)
117 compile_main_part(v)
118 var f = new OFStream.open("{tc.compdir.as(not null)}/{module.name}._tables.c")
119 f.write("/* This C file is generated by NIT to compile program {module.name}. */\n")
120 for m in module.mhe.greaters_and_self do
121 f.write("#include \"{m.name}.{get_file_ending}.h\"\n")
122 end
123 v.header_writer.write_to_stream(f)
124 v.writer.write_to_stream(f)
125 f.close
126 end
127 end
128
129 redef class MMModule
130 # Compile the sep or glob files (of the current module only)
131 private fun compile_separate_module(program: Program)
132 do
133 var tc = program.tc
134 var v = new CompilerVisitor(self, program)
135 v.add_decl("#include <nit_common.h>")
136 var native_name = location.file.strip_extension(".nit")
137 native_name += ("_nit.h")
138 if native_name.file_exists then v.add_decl("#include <{native_name.basename("")}>")
139 declare_class_tables_to_c(v)
140 compile_mod_to_c(v)
141 var f = new OFStream.open("{tc.compdir.as(not null)}/{name}.{program.get_file_ending}.h")
142 f.write("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
143 f.write("#ifndef {name}{program.get_file_ending}\n")
144 f.write("#define {name}{program.get_file_ending}\n")
145 for m in mhe.direct_greaters do f.write("#include \"{m.name}.{program.get_file_ending}.h\"\n")
146 v.header_writer.write_to_stream(f)
147 f.write("#endif\n")
148 f.close
149
150 f = new OFStream.open("{tc.compdir.as(not null)}/{name}.{program.get_file_ending}.c")
151 f.write("/* This C file is generated by NIT to compile module {name}. */\n")
152 f.write("#include \"{name}.{program.get_file_ending}.h\"\n")
153 v.top_writer.write_to_stream(f)
154 f.close
155 end
156 end
157