1 # This file is part of NIT (http://www.nitlanguage.org).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Filters preprocessed C-like header files to remove included files
17 # This tool is used in the process of parsing header files to extract
18 # information on the declared services (the functions and structures).
19 # This information is then used to generate bindings for Nit code
20 # to access these services.
22 # The C preprocessor extends macros, inline files marked with `#include`,
23 # and more. This tool acts after the C preprocessor, in a way to keep
24 # everything but the included files. It searches for line pragmas
25 # to identify the source of each line. The result is printed to stdout.
27 # Typical usage on the output of `gcc -E` (it would be the same with `clang`):
30 # gcc -E /usr/include/SDL/SDL_image.h | header_keeper SDL_image.h > preprocessed_header.h
33 # This module can also be used as a library.
34 # The main service is the method `header_keeper`.
37 # Filters the preprocessed `input` to keep only files from `target` and write to the `output`
38 fun header_keeper
(input
: Reader, output
: Writer, target
: String)
41 while not input
.eof
do
42 var line
= input
.read_line
43 if not line
.is_empty
and line
[0] == '#' then
44 in_target
= line
.has
(target
)
48 if in_target
then output
.write line
+ "\n"
52 if args
.length
!= 1 then
53 print
"Usage: header_keeper header_name.h"
57 header_keeper
(sys
.stdin
, sys
.stdout
, args
.first
)