michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: from __future__ import print_function michael@0: import os, re, string, sys michael@0: from mozbuild.util import FileAvoidWrite michael@0: michael@0: def find_in_path(file, searchpath): michael@0: for dir in searchpath.split(os.pathsep): michael@0: f = os.path.join(dir, file) michael@0: if os.path.exists(f): michael@0: return f michael@0: return '' michael@0: michael@0: def header_path(header, compiler): michael@0: if compiler == 'gcc': michael@0: # we use include_next on gcc michael@0: return header michael@0: elif compiler == 'msvc': michael@0: return find_in_path(header, os.environ.get('INCLUDE', '')) michael@0: else: michael@0: # hope someone notices this ... michael@0: raise NotImplementedError(compiler) michael@0: michael@0: def is_comment(line): michael@0: return re.match(r'\s*#.*', line) michael@0: michael@0: def main(outdir, compiler, template_file, header_list_file): michael@0: if not os.path.isdir(outdir): michael@0: os.mkdir(outdir) michael@0: michael@0: template = open(template_file, 'r').read() michael@0: path_to_new = header_path('new', compiler) michael@0: michael@0: for header in open(header_list_file, 'r'): michael@0: header = header.rstrip() michael@0: if 0 == len(header) or is_comment(header): michael@0: continue michael@0: michael@0: path = header_path(header, compiler) michael@0: with FileAvoidWrite(os.path.join(outdir, header)) as f: michael@0: f.write(string.Template(template).substitute(HEADER=header, michael@0: HEADER_PATH=path, michael@0: NEW_HEADER_PATH=path_to_new)) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: if 5 != len(sys.argv): michael@0: print("""Usage: michael@0: python {0} OUT_DIR ('msvc'|'gcc') TEMPLATE_FILE HEADER_LIST_FILE michael@0: """.format(sys.argv[0]), file=sys.stderr) michael@0: sys.exit(1) michael@0: michael@0: main(*sys.argv[1:])