contrib/objcwrapper: move file preprocessing to Nit
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 1 Sep 2015 18:00:16 +0000 (14:00 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 3 Sep 2015 14:32:31 +0000 (10:32 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/objcwrapper/src/objcwrapper.nit
contrib/objcwrapper/src/preprocessing.nit [new file with mode: 0644]

index 0e3cb18..0b8bb72 100644 (file)
@@ -23,12 +23,13 @@ import objc_model
 import objc_generator
 import objc_lexer
 import objc_parser
+import preprocessing
 
 var opt_help = new OptionBool("Show this help message", "-h", "--help")
 
 var opts = new OptionContext
-opts.add_option(opt_help, opt_output, opt_init_as_methods)
-opts.parse(args)
+opts.add_option(opt_help, opt_output, opt_init_as_methods, opt_gcc_options)
+opts.parse args
 
 if opts.errors.not_empty or opts.rest.is_empty or opt_help.value then
        print """
@@ -42,9 +43,9 @@ end
 
 var v = new ObjcVisitor
 
-for arg in opts.rest do
+for src in opts.rest do
        # Read input
-       var content = arg.to_path.read_all
+       var content = src.preprocess_content
 
        # Parse
        var lexer = new Lexer_objc(content)
diff --git a/contrib/objcwrapper/src/preprocessing.nit b/contrib/objcwrapper/src/preprocessing.nit
new file mode 100644 (file)
index 0000000..643ef1f
--- /dev/null
@@ -0,0 +1,56 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Preprocessing of input files
+module preprocessing
+
+import header_static
+import header_keeper
+
+import opts
+
+redef class Sys
+       # Options for the C preprocessor
+       var opt_gcc_options = new OptionString("C Preprocessor options to add to `gcc -E file`", "-p")
+end
+
+redef class Text
+       # Read file at `self` and preprocess its content through `gcc -E`, `header_keeper` and `header_static`
+       fun preprocess_content: String
+       do
+               var path = to_s
+
+               # GCC C preprocessor, to apply macros and includes
+               var gcc_opts = opt_gcc_options.value or else ""
+               var cmd = "gcc -E {path} {gcc_opts}"
+               var gcc_proc = new ProcessReader("sh", "-c", cmd)
+
+               # header_keeper, to keep only the target file (path)
+               var o = new StringWriter
+               header_keeper(gcc_proc, o, path)
+
+               gcc_proc.wait
+               if gcc_proc.status != 0 then
+                       print_error "Fatal Error: call to gcc failed, the command was: {cmd}"
+                       exit 1
+               end
+
+               # header_static, strip lines unsupported by our parser
+               var i = new StringReader(o.to_s)
+               o = new StringWriter
+               header_static(i, o)
+
+               return o.to_s
+       end
+end