Merge branch 'package2module' into wip
[nit.git] / src / compiling / compiling_base.nit
index 9745021..24d4fcb 100644 (file)
@@ -37,6 +37,90 @@ redef class ToolContext
        readable writable var _ext_prefix: String = ""
 end
 
+# A program that is compiled to C
+class CProgram
+       init(p: Program)
+       do
+               _program = p
+               _compdir = p.tc.compdir.as(not null)
+               _build_file = "{compdir}/{program.main_module.name}._build.sh"
+       end
+
+       # The Nit program compiled to C
+       readable var _program: Program
+
+       # C files (full path) required to compile
+       readable var _files: Array[String] = new Array[String]
+
+       # Includes paths (gcc -I) required to find the headers (.h) of native modules
+       readable var _include_dirs: ArraySet[String] = new ArraySet[String]
+
+       # The path of the building script
+       readable var _build_file: String
+
+       # The directory where all files are generated
+       readable var _compdir: String
+
+       # Return the basename of the public header file (.h) of a module
+       fun module_header_name(m: MMModule): String
+       do
+               if _module_include.has_key(m) then
+                       return _module_include[m]
+               end
+               var filename = "{m.name}.{get_file_ending}.h"
+               _module_include[m] = filename
+               return filename
+       end
+
+       # Cache for module_header_name
+       var _module_include: Map[MMModule, String] = new HashMap[MMModule, String]
+
+       # When we are using global compilation, we generate _glob files instead
+       # of _sep files so that we do not corrupt separate compilation
+       fun get_file_ending: String do return if program.tc.global then "_glob" else "_sep"
+
+       # Generate the shell script that build the program by calling gccx
+       fun generate_build_file
+       do
+               var f = new OFStream.open(_build_file)
+               var verbose = ""
+               var tc = program.tc
+
+               if tc.verbose_level == 1 then
+                       verbose = "-v"
+               else if tc.verbose_level >= 2 then
+                       # We catch tc.verbose_level >= 2, since 3+ is not valid with gccx
+                       verbose = "-vv"
+               end
+
+               f.write("#!/bin/sh\n")
+               f.write("# This shell script is generated by NIT to compile the program {program.main_module.name}.\n")
+               f.write("CLIBDIR=\"{tc.clibdir.as(not null)}\"\n")
+               f.write("{tc.bindir.as(not null)}/gccx {verbose} -d {compdir} -I $CLIBDIR {include_dirs.join(" ")}")
+               if tc.output_file != null then
+                       f.write(" -o {tc.output_file.as(not null)}")
+               else if tc.ext_prefix.is_empty then
+                       f.write(" -o {program.main_module.name}")
+               else
+                       f.write(" -o {program.main_module.name}_{tc.ext_prefix}")
+               end
+               if tc.boost then f.write(" -O")
+               if not tc.cc_link then f.write(" -x \"-c\"")
+               for l in tc.cc_libs do f.write(" -x \"-l {l}\"")
+               for lp in tc.cc_lib_paths do f.write(" -x \"-L {lp}\"")
+               for ip in tc.cc_include_paths do f.write(" -x \"-I {ip}\"")
+               f.write(" \"$@\" \\\n  {files.join("\\\n  ")}\n")
+               f.close
+       end
+
+       # Invoke the build_file
+       fun run_c_compiler
+       do
+               program.tc.info("Building",1)
+               sys.system("sh {_build_file}")
+       end
+end
+
 # Class used to generate files.
 # Note that in fact it is not a visitor.
 # Note also that this class is unefficient and poorly designed thus requires love.
@@ -106,8 +190,8 @@ class CompilerVisitor
                if _indent_level < 0 then _indent_level = 0
        end
 
-       # The processed module
-       readable var _module: MMModule
+       # The processed mmmodule
+       readable var _mmmodule: MMModule
 
        # Where header decl are stored (public stuff)
        readable writable var _header_writer: Writer
@@ -130,11 +214,15 @@ class CompilerVisitor
        # The program we are compiling
        readable var _program: Program
 
+       # The cprogram associed with program
+       readable var _cprogram: CProgram
+
        # Create a new CompilerVisitor based on a module
-       init(module: MMModule, p: Program)
+       init(mod: MMModule, cp: CProgram)
        do
-               _module = module
-               _program = p
+               _mmmodule = mod
+               _cprogram = cp
+               _program = cp.program
 
                var w = new Writer
                _header_writer = w
@@ -189,7 +277,7 @@ redef class MMLocalProperty
        do
                var cname = _cname_cache
                if cname == null then
-                       cname = cmangle(module.name, local_class.name, name)
+                       cname = cmangle(mmmodule.name, local_class.name, name)
                        _cname_cache = cname
                end
                return cname