9"""usage: python single_header_packer.py --macro <macro> [--intro <files>] --extern <files> --pub <files> --priv1 <files> --priv2 <files> [--outro <files>]
11 where <files> can be a comma-separated list of files. e.g. --priv *.c,inc/*.h
12 or a space separated list encapsulated in quotes. e.g. --priv1 "file.c file2.c file3.c"
14 The 'extern' files are placed between 'priv1' and 'priv2'.
16 The resulting code is packed as follows:
20 #ifndef <macro>_SINGLE_HEADER
21 #define <macro>_SINGLE_HEADER
22 [public header file contents]
23 #endif /* <macro>_SINGLE_HEADER */
25 #ifdef <macro>_IMPLEMENTATION
26 [private header and source file contents]
27 #endif /* <macro>_IMPLEMENTATION */
35 paths = re.split(
r'[,\s]', arg)
40 d = os.path.dirname(path)
43 if not os.path.exists(d):
44 print(d +
" does not exist.")
47 wildcard = os.path.basename(path)
49 for file
in os.listdir(d):
50 if fnmatch.fnmatch(file, wildcard):
51 unsorted.append(os.path.join(d, file))
53 files.extend(unsorted)
57 if not os.path.exists(path):
58 print(path +
" does not exist.")
60 elif os.path.isdir(path):
61 print(path +
" is a directory. Expected a file name.")
68def omit_includes(str, files):
70 fname = os.path.basename(file)
72 str = str.replace(
"#include \"" + fname +
"\"",
"");
73 str = str.replace(
"#include <" + fname +
">",
"");
77 return re.sub(
r"//(.*)(\n|$)",
"/* \\1 */\\2", str)
96while cur_arg < len(sys.argv):
97 if sys.argv[cur_arg] ==
"--help":
100 elif sys.argv[cur_arg] ==
"--macro":
102 macro = sys.argv[cur_arg]
103 elif sys.argv[cur_arg] ==
"--intro":
105 intro_files = parse_files(sys.argv[cur_arg])
106 elif sys.argv[cur_arg] ==
"--pub":
108 pub_files = parse_files(sys.argv[cur_arg])
109 elif sys.argv[cur_arg] ==
"--priv1":
111 priv_files1 = parse_files(sys.argv[cur_arg])
112 elif sys.argv[cur_arg] ==
"--priv2":
114 priv_files2 = parse_files(sys.argv[cur_arg])
115 elif sys.argv[cur_arg] ==
"--extern":
117 extern_files = parse_files(sys.argv[cur_arg])
118 elif sys.argv[cur_arg] ==
"--outro":
120 outro_files = parse_files(sys.argv[cur_arg])
122 print(
"Unknown argument " + sys.argv[cur_arg])
127 print(
"Option --macro <macro> is mandatory")
133 sys.stdout.write(open(f,
'r').read())
137print(
"#ifndef NK_SINGLE_FILE");
138print(
" #define NK_SINGLE_FILE");
143 sys.stdout.write(open(f,
'r').read())
146print(
"\n#ifdef " + macro +
"_IMPLEMENTATION");
150 print(omit_includes(open(f,
'r').read(),
151 pub_files + priv_files1 + priv_files2 + extern_files))
152for f
in extern_files:
153 print(fix_comments(open(f,
'r').read()))
156 print(omit_includes(open(f,
'r').read(),
157 pub_files + priv_files1 + priv_files2 + extern_files))
159print(
"#endif /* " + macro +
"_IMPLEMENTATION */");
163 sys.stdout.write(open(f,
'r').read())