contrib/objcwrapper: move file preprocessing to Nit
[nit.git] / contrib / objcwrapper / src / preprocessing.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Preprocessing of input files
16 module preprocessing
17
18 import header_static
19 import header_keeper
20
21 import opts
22
23 redef class Sys
24 # Options for the C preprocessor
25 var opt_gcc_options = new OptionString("C Preprocessor options to add to `gcc -E file`", "-p")
26 end
27
28 redef class Text
29 # Read file at `self` and preprocess its content through `gcc -E`, `header_keeper` and `header_static`
30 fun preprocess_content: String
31 do
32 var path = to_s
33
34 # GCC C preprocessor, to apply macros and includes
35 var gcc_opts = opt_gcc_options.value or else ""
36 var cmd = "gcc -E {path} {gcc_opts}"
37 var gcc_proc = new ProcessReader("sh", "-c", cmd)
38
39 # header_keeper, to keep only the target file (path)
40 var o = new StringWriter
41 header_keeper(gcc_proc, o, path)
42
43 gcc_proc.wait
44 if gcc_proc.status != 0 then
45 print_error "Fatal Error: call to gcc failed, the command was: {cmd}"
46 exit 1
47 end
48
49 # header_static, strip lines unsupported by our parser
50 var i = new StringReader(o.to_s)
51 o = new StringWriter
52 header_static(i, o)
53
54 return o.to_s
55 end
56 end