test_parser: add option `-x` to output XML
[nit.git] / src / c_tools.nit
index a44d7e5..932f03d 100644 (file)
@@ -49,9 +49,9 @@ class CCompilationUnit
 
        fun add_local_function( efc : CFunction )
        do
-               body_decl.add( "{efc.signature};\n" )
-               body_impl.add( "\n" )
-               body_impl.add( efc.to_writer )
+               body_decl.add "static {efc.signature};\n"
+               body_impl.add "\n"
+               body_impl.add efc.to_writer
        end
 
        fun add_exported_function( efc : CFunction )
@@ -61,7 +61,7 @@ class CCompilationUnit
                body_impl.add( efc.to_writer )
        end
 
-       fun compile_header_core( stream : OStream )
+       fun compile_header_core( stream : Writer )
        do
                header_c_base.write_to( stream )
                header_custom.write_to( stream )
@@ -69,7 +69,7 @@ class CCompilationUnit
                header_decl.write_to( stream )
        end
 
-       fun compile_body_core( stream : OStream )
+       fun compile_body_core( stream : Writer )
        do
                body_decl.write_to( stream )
                body_custom.write_to( stream )
@@ -104,25 +104,49 @@ class ExternFile
        # The filename of the file
        var filename: String
 
+       # The name of the target in the Makefile
+       # Usually the produced .o file
        fun makefile_rule_name: String is abstract
+
+       # The content of the rule in the make
+       # Usually the one-line shell command after the tabulation
        fun makefile_rule_content: String is abstract
+
+       fun compiles_to_o_file: Bool do return false
+
+       fun add_to_jar: Bool do return false
+
+       # Additional libraries needed for the compilation
+       # Will be used with pkg-config
+       var pkgconfigs = new Array[String]
 end
 
 # An extern C file to compile
 class ExternCFile
        super ExternFile
 
-       init (filename, cflags: String)
-       do
-               super filename
-
-               self.cflags = cflags
-       end
-
-       # Additionnal specific CC compiler -c flags
+       # Custom options for the C compiler (CFLAGS)
        var cflags: String
 
        redef fun hash do return filename.hash
        redef fun ==(o) do return o isa ExternCFile and filename == o.filename
+
+       redef fun makefile_rule_name do
+               var basename = filename.basename(".c")
+               var res = "{basename}.extern.o"
+               return res
+       end
+
+       redef fun makefile_rule_content do
+               var ff = filename.basename("")
+               var o = makefile_rule_name
+               var pkg = ""
+               if not pkgconfigs.is_empty then
+                       pkg = "`pkg-config --cflags {pkgconfigs.join(" ")}`"
+               end
+               return "$(CC) $(CFLAGS) {self.cflags} {pkg} -c -o {o} {ff}"
+       end
+
+       redef fun compiles_to_o_file do return true
 end