13 def include_dirs(self
):
14 return self
._include
_dirs
17 def include_dirs(self
, values
):
19 value
= os
.path
.relpath(value
)
20 self
._include
_dirs
.append(value
)
23 self
.name
= os
.path
.relpath(name
).replace("\\", "/")
24 with
open(self
.name
, "r") as f
:
25 self
.source
= f
.read()
28 def write(self
, name
):
29 with
open(name
, "wb") as f
:
30 f
.write(self
.source
.encode())
34 sys
.stdout
.write(self
.source
)
36 def __call__(self
, *args
):
42 name
= name
.replace("\\", "/")
43 if not os
.path
.exists(name
):
45 if name
in self
._include
_files
:
48 with
open(name
, "r") as f
:
52 include_dirs
= [os
.path
.dirname(name
)] + self
._include
_dirs
53 includes
= re
.findall(r
"#include \"([^
\"]+)\"", source)
54 for include in includes:
55 cur = '#include "{}"\n'.format(include)
56 for include_dir in include_dirs:
57 new = os.path.join(include_dir, include)
58 if not os.path.exists(new):
60 new = self.__call__(new)
61 source = source.replace(cur, new)
62 self._include_files.append(name)
64 for define_macro in self.define_macros:
65 macros += "#define %s\n" % define_macro
66 for undef_macro
in self
.undef_macros
:
67 macros
+= "#undef %s\n" % undef_macro
68 self
.include_files
[name
] = {
69 "header": tuple(self
._include
_files
),
72 self
.source
= macros
+ source
73 self
._include
_files
.clear()
77 if __name__
== "__main__":
81 parser
= argparse
.ArgumentParser()
82 parser
.add_argument("-v", "--verbose", action
="store_true")
83 parser
.add_argument("-D", default
=[], action
="append")
84 parser
.add_argument("-U", default
=[], action
="append")
85 parser
.add_argument("-I", default
=[], action
="append")
86 parser
.add_argument("-H")
87 parser
.add_argument("-C")
88 parser
.add_argument("-c")
89 parser
.add_argument("-O")
90 parser
.add_argument("-o")
91 args
= parser
.parse_known_args()
92 o
.define_macros
= args
[0].D
93 o
.undef_macros
= args
[0].U
94 o
.include_dirs
= args
[0].I
100 outname
= os
.path
.relpath(args
[0].c
)
101 outname
= os
.path
.join(args
[0].O
, outname
)
102 outpath
= os
.path
.dirname(outname
)
103 os
.makedirs(outpath
, exist_ok
=True)
107 if args
[0].O
and args
[0].H
:
108 for dirpath
, dirnames
, filenames
in os
.walk(args
[0].H
):
109 outpath
= os
.path
.relpath(dirpath
, args
[0].H
)
110 outpath
= os
.path
.join(args
[0].O
, outpath
)
111 os
.makedirs(outpath
, exist_ok
=True)
112 for filename
in filenames
:
113 header
= os
.path
.join(dirpath
, filename
)
114 output
= os
.path
.join(outpath
, filename
)
115 prefix
, suffix
= os
.path
.splitext(header
)
116 if suffix
in (".h", ".hh", ".hpp", ".hxx"):
119 if args
[0].O
and args
[0].C
:
120 for dirpath
, dirnames
, filenames
in os
.walk(args
[0].C
):
121 outpath
= os
.path
.relpath(dirpath
, args
[0].C
)
122 outpath
= os
.path
.join(args
[0].O
, outpath
)
123 os
.makedirs(outpath
, exist_ok
=True)
124 for filename
in filenames
:
125 source
= os
.path
.join(dirpath
, filename
)
126 output
= os
.path
.join(outpath
, filename
)
127 prefix
, suffix
= os
.path
.splitext(source
)
128 if suffix
in (".c", ".cc", ".cpp", ".cxx"):
132 sources
= " ".join(o
.include_files
)
133 objects
= ".o ".join(o
.include_files
) + ".o"
134 for key
in o
.include_files
:
135 item
= o
.include_files
[key
]
136 item
= " ".join(reversed(item
["header"]))
137 sys
.stderr
.write(key
+ ".o:%s\n" % item
)
138 sys
.stderr
.write("SOURCES = %s\n" % sources
)
139 sys
.stderr
.write("OBJECTS = %s\n" % objects
)