From afe3fc43b58fe304944bd2f6c2158a5747317916 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Tue, 1 Sep 2015 14:00:16 -0400 Subject: [PATCH] contrib/objcwrapper: move file preprocessing to Nit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/objcwrapper/src/objcwrapper.nit | 9 ++--- contrib/objcwrapper/src/preprocessing.nit | 56 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 contrib/objcwrapper/src/preprocessing.nit diff --git a/contrib/objcwrapper/src/objcwrapper.nit b/contrib/objcwrapper/src/objcwrapper.nit index 0e3cb18..0b8bb72 100644 --- a/contrib/objcwrapper/src/objcwrapper.nit +++ b/contrib/objcwrapper/src/objcwrapper.nit @@ -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 index 0000000..643ef1f --- /dev/null +++ b/contrib/objcwrapper/src/preprocessing.nit @@ -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 -- 1.7.9.5