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:
22 #ifndef <macro>_SINGLE_HEADER
23 #define <macro>_SINGLE_HEADER
24 [public header file contents]
25 #endif /* <macro>_SINGLE_HEADER */
27 #ifdef <macro>_IMPLEMENTATION
28 [private header and source file contents]
29 #endif /* <macro>_IMPLEMENTATION */
37 paths = re.split(
r'[,\s]', arg)
42 d = os.path.dirname(path)
45 if not os.path.exists(d):
46 print(d +
" does not exist.")
49 wildcard = os.path.basename(path)
51 for file
in os.listdir(d):
52 if fnmatch.fnmatch(file, wildcard):
53 unsorted.append(os.path.join(d, file))
55 files.extend(unsorted)
59 if not os.path.exists(path):
60 print(path +
" does not exist.")
62 elif os.path.isdir(path):
63 print(path +
" is a directory. Expected a file name.")
70def omit_includes(str, files):
72 fname = os.path.basename(file)
74 str = str.replace(
"#include \"" + fname +
"\"",
"");
75 str = str.replace(
"#include <" + fname +
">",
"");
79 return re.sub(
r"//(.*)(\n|$)",
"/* \\1 */\\2", str)
98while cur_arg < len(sys.argv):
99 if sys.argv[cur_arg] ==
"--help":
102 elif sys.argv[cur_arg] ==
"--macro":
104 macro = sys.argv[cur_arg]
105 elif sys.argv[cur_arg] ==
"--intro":
107 intro_files = parse_files(sys.argv[cur_arg])
108 elif sys.argv[cur_arg] ==
"--pub":
110 pub_files = parse_files(sys.argv[cur_arg])
111 elif sys.argv[cur_arg] ==
"--priv1":
113 priv_files1 = parse_files(sys.argv[cur_arg])
114 elif sys.argv[cur_arg] ==
"--priv2":
116 priv_files2 = parse_files(sys.argv[cur_arg])
117 elif sys.argv[cur_arg] ==
"--extern":
119 extern_files = parse_files(sys.argv[cur_arg])
120 elif sys.argv[cur_arg] ==
"--outro":
122 outro_files = parse_files(sys.argv[cur_arg])
124 print(
"Unknown argument " + sys.argv[cur_arg])
129 print(
"Option --macro <macro> is mandatory")
136 sys.stdout.write(open(f,
'r').read())
141print(
"#ifndef NK_SINGLE_FILE");
142print(
" #define NK_SINGLE_FILE");
147 sys.stdout.write(open(f,
'r').read())
150print(
"\n#ifdef " + macro +
"_IMPLEMENTATION");
154 print(omit_includes(open(f,
'r').read(),
155 pub_files + priv_files1 + priv_files2 + extern_files))
156for f
in extern_files:
157 print(fix_comments(open(f,
'r').read()))
160 print(omit_includes(open(f,
'r').read(),
161 pub_files + priv_files1 + priv_files2 + extern_files))
163print(
"#endif /* " + macro +
"_IMPLEMENTATION */");
167 sys.stdout.write(open(f,
'r').read())