22import fnmatch
33import os .path
44import sys
5+ import re
56
67def print_help ():
78 print (
8- """usage: python single_header_packer.py --macro <macro> [--intro <files>] --pub <files> --priv <files> [--outro <files>]
9+ """usage: python single_header_packer.py --macro <macro> [--intro <files>] --extern <files> -- pub <files> --priv1 <files> --priv2 <files> [--outro <files>]
910
1011 where <files> can be a comma-separated list of files. e.g. --priv *.c,inc/*.h
12+
13+ The 'extern' files are placed between 'priv1' and 'priv2'.
1114
1215 The resulting code is packed as follows:
1316
@@ -70,6 +73,9 @@ def omit_includes(str, files):
7073 str = str .replace ("#include <" + fname + ">" , "" );
7174 return str
7275
76+ def fix_comments (str ):
77+ return re .sub (r"//(.*)(\n|$)" , "/* \\ 1 */\\ 2" , str )
78+
7379# Main start
7480# ==========
7581
@@ -79,8 +85,9 @@ def omit_includes(str, files):
7985
8086intro_files = []
8187pub_files = []
82- priv_files = []
83- outro_files = []
88+ priv_files1 = []
89+ outro_files2 = []
90+ extern_files = []
8491cur_arg = 1
8592macro = ""
8693
@@ -99,9 +106,15 @@ def omit_includes(str, files):
99106 elif sys .argv [cur_arg ] == "--pub" :
100107 cur_arg += 1
101108 pub_files = parse_files (sys .argv [cur_arg ])
102- elif sys .argv [cur_arg ] == "--priv" :
109+ elif sys .argv [cur_arg ] == "--priv1" :
110+ cur_arg += 1
111+ priv_files1 = parse_files (sys .argv [cur_arg ])
112+ elif sys .argv [cur_arg ] == "--priv2" :
103113 cur_arg += 1
104- priv_files = parse_files (sys .argv [cur_arg ])
114+ priv_files2 = parse_files (sys .argv [cur_arg ])
115+ elif sys .argv [cur_arg ] == "--extern" :
116+ cur_arg += 1
117+ extern_files = parse_files (sys .argv [cur_arg ])
105118 elif sys .argv [cur_arg ] == "--outro" :
106119 cur_arg += 1
107120 outro_files = parse_files (sys .argv [cur_arg ])
@@ -134,9 +147,17 @@ def omit_includes(str, files):
134147
135148print (os .linesep + "#ifdef " + macro + "_IMPLEMENTATION" );
136149print ("" );
137- for f in priv_files :
150+
151+ for f in priv_files1 :
138152 print (omit_includes (open (f , 'r' ).read (),
139- pub_files + priv_files ))
153+ pub_files + priv_files1 + priv_files2 + extern_files ))
154+ for f in extern_files :
155+ print (fix_comments (open (f , 'r' ).read ()))
156+
157+ for f in priv_files2 :
158+ print (omit_includes (open (f , 'r' ).read (),
159+ pub_files + priv_files1 + priv_files2 + extern_files ))
160+
140161print ("#endif /* " + macro + "_IMPLEMENTATION */" );
141162
142163print (os .linesep + "/*" )
0 commit comments