|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 from __future__ import print_function |
|
5 import os, re, string, sys |
|
6 from mozbuild.util import FileAvoidWrite |
|
7 |
|
8 def find_in_path(file, searchpath): |
|
9 for dir in searchpath.split(os.pathsep): |
|
10 f = os.path.join(dir, file) |
|
11 if os.path.exists(f): |
|
12 return f |
|
13 return '' |
|
14 |
|
15 def header_path(header, compiler): |
|
16 if compiler == 'gcc': |
|
17 # we use include_next on gcc |
|
18 return header |
|
19 elif compiler == 'msvc': |
|
20 return find_in_path(header, os.environ.get('INCLUDE', '')) |
|
21 else: |
|
22 # hope someone notices this ... |
|
23 raise NotImplementedError(compiler) |
|
24 |
|
25 def is_comment(line): |
|
26 return re.match(r'\s*#.*', line) |
|
27 |
|
28 def main(outdir, compiler, template_file, header_list_file): |
|
29 if not os.path.isdir(outdir): |
|
30 os.mkdir(outdir) |
|
31 |
|
32 template = open(template_file, 'r').read() |
|
33 path_to_new = header_path('new', compiler) |
|
34 |
|
35 for header in open(header_list_file, 'r'): |
|
36 header = header.rstrip() |
|
37 if 0 == len(header) or is_comment(header): |
|
38 continue |
|
39 |
|
40 path = header_path(header, compiler) |
|
41 with FileAvoidWrite(os.path.join(outdir, header)) as f: |
|
42 f.write(string.Template(template).substitute(HEADER=header, |
|
43 HEADER_PATH=path, |
|
44 NEW_HEADER_PATH=path_to_new)) |
|
45 |
|
46 |
|
47 if __name__ == '__main__': |
|
48 if 5 != len(sys.argv): |
|
49 print("""Usage: |
|
50 python {0} OUT_DIR ('msvc'|'gcc') TEMPLATE_FILE HEADER_LIST_FILE |
|
51 """.format(sys.argv[0]), file=sys.stderr) |
|
52 sys.exit(1) |
|
53 |
|
54 main(*sys.argv[1:]) |