var time0 = get_time
self.toolcontext.info("*** WRITING C ***", 1)
- var compile_dir = toolcontext.opt_compile_dir.value
- if compile_dir == null then compile_dir = ".nit_compile"
-
compile_dir.mkdir
+ var cfiles = new Array[String]
+ write_files(compiler, compile_dir, cfiles)
+
+ # Generate the Makefile
+
+ write_makefile(compiler, compile_dir, cfiles)
+
+ var time1 = get_time
+ self.toolcontext.info("*** END WRITING C: {time1-time0} ***", 2)
+
+ # Execute the Makefile
+
+ if self.toolcontext.opt_no_cc.value then return
+
+ time0 = time1
+ self.toolcontext.info("*** COMPILING C ***", 1)
+
+ compile_c_code(compiler, compile_dir)
+
+ time1 = get_time
+ self.toolcontext.info("*** END COMPILING C: {time1-time0} ***", 2)
+ end
+
+ fun write_files(compiler: AbstractCompiler, compile_dir: String, cfiles: Array[String])
+ do
if self.toolcontext.opt_stacktrace.value then compiler.build_c_to_nit_bindings
- var orig_dir=".." # FIXME only works if `compile_dir` is a subdirectory of cwd
+ # Add gc_choser.h to aditionnal bodies
+ var gc_chooser = new ExternCFile("gc_chooser.c", "-DWITH_LIBGC")
+ compiler.extern_bodies.add(gc_chooser)
+ compiler.files_to_copy.add "{cc_paths.first}/gc_chooser.c"
+ compiler.files_to_copy.add "{cc_paths.first}/gc_chooser.h"
- var outname = self.toolcontext.opt_output.value
- if outname == null then
- outname = "{mainmodule.name}"
++ # FFI
++ for m in compiler.mainmodule.in_importation.greaters do if mmodule2nmodule.keys.has(m) then
++ var amodule = mmodule2nmodule[m]
++ if m.uses_ffi or amodule.uses_legacy_ni then
++ compiler.finalize_ffi_for_module(amodule)
++ end
++ end
++
+ # Copy original .[ch] files to compile_dir
+ for src in compiler.files_to_copy do
+ var basename = src.basename("")
+ var dst = "{compile_dir}/{basename}"
+ src.file_copy_to dst
end
- var outpath = orig_dir.join_path(outname).simplify_path
var hfilename = compiler.header.file.name + ".h"
var hfilepath = "{compile_dir}/{hfilename}"
var cc_includes = ""
for p in cc_paths do
- p = orig_dir.join_path(p).simplify_path
cc_includes += " -I \"" + p + "\""
end
- if toolcontext.opt_no_stacktrace.value then
- makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
- else
- makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lunwind -lm -lgc\n\n")
+
+ var linker_options = new HashSet[String]
- for m in mainmodule.in_importation.greaters do if mmodule2nmodule.keys.has(m) then
++ for m in compiler.mainmodule.in_importation.greaters do if mmodule2nmodule.keys.has(m) then
+ var amod = mmodule2nmodule[m]
+ linker_options.add(amod.c_linker_options)
end
+
- if not toolcontext.opt_no_stacktrace.value then
- linker_options.add("-lunwind")
- end
++ if not toolcontext.opt_no_stacktrace.value then linker_options.add("-lunwind")
+
+ makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc {linker_options.join(" ")}\n\n")
makefile.write("all: {outpath}\n\n")
var ofiles = new Array[String]
ofiles.add(o)
end
- # Add gc_choser.h to aditionnal bodies
- var gc_chooser = new ExternCFile("{cc_paths.first}/gc_chooser.c", "-DWITH_LIBGC")
- compiler.extern_bodies.add(gc_chooser)
-
# Compile each required extern body into a specific .o
for f in compiler.extern_bodies do
- var basename = f.filename.basename(".c")
- var o = "{basename}.extern.o"
- var ff = f.filename
- makefile.write("{o}: {ff}\n\t$(CC) $(CFLAGS) -D NONITCNI {f.cflags} -c -o {o} {ff}\n\n")
- ofiles.add(o)
+ if f isa ExternCFile then
+ var basename = f.filename.basename(".c")
+ var o = "{basename}.extern.o"
- var ff = orig_dir.join_path(f.filename).simplify_path
++ var ff = f.filename.basename("")
+ makefile.write("{o}: {ff}\n\t$(CC) $(CFLAGS) -D NONITCNI {f.cflags} -c -o {o} {ff}\n\n")
+ ofiles.add(o)
+ end
end
# Link edition
self.header.add_decl("#include <stdlib.h>")
self.header.add_decl("#include <stdio.h>")
self.header.add_decl("#include <string.h>")
- self.header.add_decl("#include <gc_chooser.h>")
+ self.header.add_decl("#include \"gc_chooser.h\"")
compile_header_structs
+ compile_nitni_structs
# Signal handler function prototype
self.header.add_decl("void show_backtrace(int);")
v.add("\}")
end
- # List of additional .c files required to compile (native interface)
- var extern_bodies = new Array[ExternCFile]
+ # List of additional files required to compile (FFI)
+ var extern_bodies = new Array[ExternFile]
+ # List of source files to copy over to the compile dir
+ var files_to_copy = new Array[String]
+
# This is used to avoid adding an extern file more than once
private var seen_extern = new ArraySet[String]