OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 self.in_file = InFile.load_from_files(in_files, self.defaults, self.
valid_values, self.default_parameters) | 48 self.in_file = InFile.load_from_files(in_files, self.defaults, self.
valid_values, self.default_parameters) |
49 else: | 49 else: |
50 self.in_file = None | 50 self.in_file = None |
51 self._outputs = {} # file_name -> generator | 51 self._outputs = {} # file_name -> generator |
52 | 52 |
53 def wrap_with_condition(self, string, condition): | 53 def wrap_with_condition(self, string, condition): |
54 if not condition: | 54 if not condition: |
55 return string | 55 return string |
56 return "#if ENABLE(%(condition)s)\n%(string)s\n#endif" % { 'condition' :
condition, 'string' : string } | 56 return "#if ENABLE(%(condition)s)\n%(string)s\n#endif" % { 'condition' :
condition, 'string' : string } |
57 | 57 |
58 def _forcibly_create_text_file_at_path_with_contents(self, file_path, conten
ts): | 58 def _write_file_if_changed(self, output_dir, contents, file_name): |
59 # FIXME: This method can be made less force-full anytime after 6/1/2013. | 59 path = os.path.join(output_dir, file_name) |
60 # A gyp error was briefly checked into the tree, causing | 60 |
61 # a directory to have been generated in place of one of | |
62 # our output files. Clean up after that error so that | |
63 # all users don't need to clobber their output directories. | |
64 shutil.rmtree(file_path, ignore_errors=True) | |
65 # The build system should ensure our output directory exists, but just i
n case. | 61 # The build system should ensure our output directory exists, but just i
n case. |
66 directory = os.path.dirname(file_path) | 62 directory = os.path.dirname(path) |
67 if not os.path.exists(directory): | 63 if not os.path.exists(directory): |
68 os.makedirs(directory) | 64 os.makedirs(directory) |
69 | 65 |
70 with open(file_path, "w") as file_to_write: | 66 # Only write the file if the contents have changed. This allows ninja to |
71 file_to_write.write(contents) | 67 # skip rebuilding targets which depend on the output. |
72 | 68 with open(path, "a+") as output_file: |
73 def _write_file(self, output_dir, contents, file_name): | 69 output_file.seek(0) |
74 path = os.path.join(output_dir, file_name) | 70 if output_file.read() != contents: |
75 self._forcibly_create_text_file_at_path_with_contents(path, contents) | 71 output_file.truncate(0) |
| 72 output_file.write(contents) |
76 | 73 |
77 def write_files(self, output_dir): | 74 def write_files(self, output_dir): |
78 for file_name, generator in self._outputs.items(): | 75 for file_name, generator in self._outputs.items(): |
79 self._write_file(output_dir, generator(), file_name) | 76 self._write_file_if_changed(output_dir, generator(), file_name) |
80 | 77 |
81 def set_gperf_path(self, gperf_path): | 78 def set_gperf_path(self, gperf_path): |
82 self.gperf_path = gperf_path | 79 self.gperf_path = gperf_path |
83 | 80 |
84 | 81 |
85 class Maker(object): | 82 class Maker(object): |
86 def __init__(self, writer_class): | 83 def __init__(self, writer_class): |
87 self._writer_class = writer_class | 84 self._writer_class = writer_class |
88 | 85 |
89 def main(self, argv): | 86 def main(self, argv): |
90 script_name = os.path.basename(argv[0]) | 87 script_name = os.path.basename(argv[0]) |
91 args = argv[1:] | 88 args = argv[1:] |
92 if len(args) < 1: | 89 if len(args) < 1: |
93 print "USAGE: %s INPUT_FILES" % script_name | 90 print "USAGE: %s INPUT_FILES" % script_name |
94 exit(1) | 91 exit(1) |
95 | 92 |
96 parser = optparse.OptionParser() | 93 parser = optparse.OptionParser() |
97 parser.add_option("--gperf", default="gperf") | 94 parser.add_option("--gperf", default="gperf") |
98 parser.add_option("--output_dir", default=os.getcwd()) | 95 parser.add_option("--output_dir", default=os.getcwd()) |
99 options, args = parser.parse_args() | 96 options, args = parser.parse_args() |
100 | 97 |
101 writer = self._writer_class(args) | 98 writer = self._writer_class(args) |
102 writer.set_gperf_path(options.gperf) | 99 writer.set_gperf_path(options.gperf) |
103 writer.write_files(options.output_dir) | 100 writer.write_files(options.output_dir) |
OLD | NEW |