niti FFI: delete the nit_compile directory after execution
[nit.git] / src / interpreter / dynamic_loading_ffi / on_demand_compiler.nit
index eba0678..aabecdb 100644 (file)
@@ -20,6 +20,15 @@ import c_tools
 import nitni
 import ffi
 import naive_interpreter
+import debugger_socket # To linearize `ToolContext::init`
+
+redef class ToolContext
+
+       # --compile-dir
+       var opt_compile_dir = new OptionString("Directory used to generate temporary files", "--compile-dir")
+
+       init do option_context.add_option opt_compile_dir
+end
 
 redef class AMethPropdef
        # Does this method definition use the FFI and is it supported by the interpreter?
@@ -45,10 +54,25 @@ redef class AMethPropdef
 end
 
 redef class NaiveInterpreter
+       redef fun start(mainmodule)
+       do
+               super
+
+               # Delete temporary files
+               var compile_dir = compile_dir
+               if compile_dir.file_exists then compile_dir.rmdir
+       end
+
        # Where to store generated C and extracted code
-       #
-       # TODO make customizable and delete when execution completes
-       private var compile_dir = "nit_compile"
+       private var compile_dir: String is lazy do
+               # Prioritize the user supplied directory
+               var opt = modelbuilder.toolcontext.opt_compile_dir.value
+               if opt != null then return opt
+               return "/tmp/niti_ffi_{process_id}"
+       end
+
+       # Identifier for this process, unique between running interpreters
+       private fun process_id: Int `{ return getpid(); `}
 
        # Path of the compiled foreign code library
        #
@@ -109,7 +133,7 @@ redef class AModule
 
                # Link everything in a shared library
                # TODO customize the compiler
-               var cmd = "{v.c_compiler} -Wall -shared -Wl,-soname,{mmodule.name}.so -g -o {foreign_code_lib_path} {object_files.join(" ")} {ldflags}"
+               var cmd = "{v.c_compiler} -Wall -shared -o {foreign_code_lib_path} {object_files.join(" ")} {ldflags}"
                if sys.system(cmd) != 0 then
                        v.fatal "FFI Error: Failed to link native code using `{cmd}`"
                        return false
@@ -262,7 +286,7 @@ redef class MMethodDef
                for param in msignature.mparameters do params.add param.mtype.cname_blind
 
                # Declare the implementation function as extern
-               var impl_cname = mproperty.build_cname(mclassdef.mclass.mclass_type,
+               var impl_cname = mproperty.build_cname(mclassdef.bound_mtype,
                        mclassdef.mmodule, "___impl", long_signature)
                ecc.body_decl.add "extern {c_return_type} {impl_cname}({params.join(", ")});\n"