OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2016 The Chromium Authors. All rights reserved. | 3 # Copyright 2016 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """This script creates a "jumbo" file which merges all incoming files | 7 """This script creates a "jumbo" file which merges all incoming files |
8 for compiling. | 8 for compiling. |
9 | 9 |
10 """ | 10 """ |
11 | 11 |
12 from __future__ import print_function | 12 from __future__ import print_function |
13 | 13 |
14 import argparse | 14 import argparse |
| 15 import cStringIO |
| 16 import os |
| 17 |
| 18 |
| 19 def write_jumbo_files(inputs, outputs, written_input_set, written_output_set): |
| 20 output_count = len(outputs) |
| 21 input_count = len(inputs) |
| 22 |
| 23 written_inputs = 0 |
| 24 for output_index, output_file in enumerate(outputs): |
| 25 written_output_set.add(output_file) |
| 26 if os.path.isfile(output_file): |
| 27 with open(output_file, "r") as current: |
| 28 current_jumbo_file = current.read() |
| 29 else: |
| 30 current_jumbo_file = None |
| 31 |
| 32 out = cStringIO.StringIO() |
| 33 out.write("/* This is a Jumbo file. Don't edit. */\n\n") |
| 34 out.write("/* Generated with merge_for_jumbo.py. */\n\n") |
| 35 input_limit = (output_index + 1) * input_count / output_count |
| 36 while written_inputs < input_limit: |
| 37 filename = inputs[written_inputs] |
| 38 written_inputs += 1 |
| 39 out.write("#include \"%s\"\n" % filename) |
| 40 written_input_set.add(filename) |
| 41 new_jumbo_file = out.getvalue() |
| 42 out.close() |
| 43 |
| 44 if new_jumbo_file != current_jumbo_file: |
| 45 with open(output_file, "w") as out: |
| 46 out.write(new_jumbo_file) |
15 | 47 |
16 | 48 |
17 def main(): | 49 def main(): |
18 parser = argparse.ArgumentParser() | 50 parser = argparse.ArgumentParser() |
19 parser.add_argument("--outputs", nargs="+", required=True, | 51 parser.add_argument("--outputs", nargs="+", required=True, |
20 help='List of output files to split input into') | 52 help='List of output files to split input into') |
21 parser.add_argument("--file-list", required=True) | 53 parser.add_argument("--file-list", required=True) |
22 parser.add_argument("--verbose", action="store_true") | 54 parser.add_argument("--verbose", action="store_true") |
23 args = parser.parse_args() | 55 args = parser.parse_args() |
24 | 56 |
25 output_count = len(args.outputs) | |
26 | |
27 lines = [] | 57 lines = [] |
28 # If written with gn |write_file| each file is on its own line. | 58 # If written with gn |write_file| each file is on its own line. |
29 with open(args.file_list) as file_list_file: | 59 with open(args.file_list) as file_list_file: |
30 lines = [line.strip() for line in file_list_file if line.strip()] | 60 lines = [line.strip() for line in file_list_file if line.strip()] |
31 # If written with gn |response_file_contents| the files are space separated. | 61 # If written with gn |response_file_contents| the files are space separated. |
32 inputs = [] | 62 all_inputs = [] |
33 for line in lines: | 63 for line in lines: |
34 inputs.extend(line.split()) | 64 all_inputs.extend(line.split()) |
35 input_count = len(inputs) | |
36 | 65 |
37 written_inputs = 0 | 66 written_output_set = set() # Just for double checking |
38 for output_index, output_file in enumerate(args.outputs): | 67 written_input_set = set() # Just for double checking |
39 # TODO: Check if the file is right already and then do not update it. | 68 for language_ext in (".cc", ".c", ".mm"): |
40 with open(output_file, "w") as out: | 69 if language_ext == ".cc": |
41 out.write("/* This is a Jumbo file. Don't edit. */\n\n") | 70 ext_pattern = (".cc", ".cpp") |
42 out.write("/* Generated with merge_for_jumbo.py. */\n\n") | 71 else: |
43 input_limit = (output_index + 1) * input_count / output_count | 72 ext_pattern = tuple([language_ext]) |
44 while written_inputs < input_limit: | |
45 filename = inputs[written_inputs] | |
46 written_inputs += 1 | |
47 # The source list includes headers which should not be | |
48 # compiled, and Objective C files which will be special cased | |
49 # later since they will not compile correctly if included in a | |
50 # C++ file. We will just skip them here for now. | |
51 if filename.endswith((".h", ".mm")): | |
52 continue | |
53 | 73 |
54 out.write("#include \"%s\"\n" % filename) | 74 outputs = [x for x in args.outputs if x.endswith(ext_pattern)] |
| 75 inputs = [x for x in all_inputs if x.endswith(ext_pattern)] |
55 | 76 |
| 77 if not outputs: |
| 78 assert not inputs |
| 79 continue |
| 80 |
| 81 write_jumbo_files(inputs, outputs, written_input_set, written_output_set) |
| 82 |
| 83 header_files = set([x for x in all_inputs if x.endswith(".h")]) |
| 84 assert set(args.outputs) == written_output_set, "Did not fill all outputs" |
| 85 files_not_included = set(all_inputs) - written_input_set - header_files |
| 86 assert not files_not_included, "Did not include files: " + files_not_included |
56 if args.verbose: | 87 if args.verbose: |
57 print("Generated %s (%d files) based on %s" % (str(args.outputs), | 88 print("Generated %s (%d files) based on %s" % ( |
58 written_inputs, | 89 str(args.outputs), len(written_input_set), args.file_list)) |
59 args.file_list)) | |
60 | 90 |
61 if __name__ == "__main__": | 91 if __name__ == "__main__": |
62 main() | 92 main() |
OLD | NEW |