OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """A helper script for setting up forwarding headers.""" | 6 """A helper script for setting up forwarding headers.""" |
7 | 7 |
8 import errno | 8 import errno |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 | 11 |
12 | 12 |
13 def GetHeaderFilesInDir(dir_path): | 13 def GetHeaderFilesInDir(dir_path): |
14 """Return a list of all header files in dir_path.""" | 14 """Return a list of all header files under dir_path |
| 15 (as absolute native paths).""" |
15 all_files = [] | 16 all_files = [] |
16 for root, dirs, files in os.walk(dir_path): | 17 for root, dirs, files in os.walk(dir_path): |
17 # Backslashes get shell escaped by gyp, so force forward slash for | 18 all_files.extend([os.path.join(root, f) for f in files if f.endswith('.h')]) |
18 # path separators. | |
19 all_files.extend([os.path.join(root, f).replace(os.sep, '/') | |
20 for f in files if f.endswith('.h')]) | |
21 return all_files | 19 return all_files |
22 | 20 |
23 | 21 |
| 22 def PathForInclude(path): |
| 23 # We should always use unix-style forward slashes in #includes. |
| 24 return path.replace(os.sep, '/') |
| 25 |
| 26 |
| 27 def NativePath(path): |
| 28 return path.replace('/', os.sep) |
| 29 |
| 30 |
| 31 def PathForGyp(path): |
| 32 # GYP will try to shell-escape backslashes, so we should always |
| 33 # return unix-style paths with forward slashes as the directory separators. |
| 34 return path.replace(os.sep, '/') |
| 35 |
| 36 |
24 def Inputs(args): | 37 def Inputs(args): |
25 """List the files in the provided input dir. | 38 """List the files in the provided input dir. |
26 | 39 |
27 args: A list with 1 value, the input dir. | 40 args: A list with 1 value, the input dir. |
28 Returns: 0 on success, other value on error.""" | 41 Returns: 0 on success, other value on error.""" |
29 if len(args) != 1: | 42 if len(args) != 1: |
30 print "'inputs' expects only one input directory." | 43 print "'inputs' expects only one input directory." |
31 return -1 | 44 return -1 |
32 | 45 |
33 for filename in GetHeaderFilesInDir(args[0]): | 46 for filename in GetHeaderFilesInDir(args[0]): |
34 print filename | 47 print PathForGyp(filename) |
35 return 0 | 48 return 0 |
36 | 49 |
37 | 50 |
38 def Outputs(args): | 51 def Outputs(args): |
39 """Takes an input dir and an output dir and figures out new output files | 52 """Takes an input dir and an output dir and figures out new output files |
40 based on copying from the input dir to the output dir. | 53 based on copying from the input dir to the output dir. |
41 | 54 |
42 args: A list with 2 values, the input dir and the output dir. | 55 args: A list with 2 values, the input dir and the output dir. |
43 Returns: 0 on success, other value on error.""" | 56 Returns: 0 on success, other value on error.""" |
44 if len(args) != 2: | 57 if len(args) != 2: |
45 print "'outputs' expects an input directory and an output directory." | 58 print "'outputs' expects an input directory and an output directory." |
46 return -1 | 59 return -1 |
47 | 60 |
48 base_input_dir = args[0] | 61 base_input_dir = NativePath(args[0]) |
49 output_dir = args[1] | 62 output_dir = NativePath(args[1]) |
50 input_files = GetHeaderFilesInDir(base_input_dir) | 63 input_files = GetHeaderFilesInDir(base_input_dir) |
51 for filename in input_files: | 64 for filename in input_files: |
52 rel_path = filename[len(base_input_dir) + 1:] | 65 rel_path = os.path.relpath(filename, base_input_dir) |
53 # Backslashes get shell escaped by gyp, so force forward slash for | 66 print PathForGyp(os.path.join(output_dir, rel_path)) |
54 # path separators. | |
55 print os.path.join(output_dir, rel_path).replace(os.sep, '/') | |
56 | 67 |
57 | 68 |
58 def SetupHeaders(args): | 69 def SetupHeaders(args): |
59 """Takes an input dir and an output dir and sets up forwarding headers | 70 """Takes an input dir and an output dir and sets up forwarding headers |
60 from output dir to files in input dir. | 71 from output dir to files in input dir. |
61 args: A list with 2 values, the input dir and the output dir. | 72 args: A list with 3 values, the input dir, the output dir, and the dir |
| 73 that #include paths will be relative to.. |
62 Returns: 0 on success, other value on error.""" | 74 Returns: 0 on success, other value on error.""" |
63 if len(args) != 2 and len(args) != 3: | 75 if len(args) != 3: |
64 print ("'setup_headers' expects an input directory, an output directory, ." | 76 print ("'setup_headers' expects an input directory, an output directory, ." |
65 "and an optional directory to make includes relative to.") | 77 "and a directory to make includes relative to.") |
66 return -1 | 78 return -1 |
67 | 79 |
68 base_input_dir = args[0] | 80 base_input_dir = NativePath(args[0]) |
69 output_dir = args[1] | 81 output_dir = NativePath(args[1]) |
70 relative_to_dir = None | 82 relative_to_dir = NativePath(args[2]) |
71 if len(args) == 3: | |
72 relative_to_dir = args[2] | |
73 input_files = GetHeaderFilesInDir(base_input_dir) | 83 input_files = GetHeaderFilesInDir(base_input_dir) |
74 for input_filename in input_files: | 84 for input_filename in input_files: |
75 rel_path = input_filename[len(base_input_dir) + 1:] | 85 rel_path = os.path.relpath(input_filename, base_input_dir) |
76 out_filename = os.path.join(output_dir, rel_path) | 86 out_filename = os.path.join(output_dir, rel_path) |
77 TryToMakeDir(os.path.split(out_filename)[0]) | 87 TryToMakeDir(os.path.split(out_filename)[0]) |
78 WriteSetupFilename(input_filename, out_filename, relative_to_dir) | 88 WriteForwardingHeader(input_filename, out_filename, relative_to_dir) |
79 | 89 |
80 | 90 |
81 def TryToMakeDir(dir_name): | 91 def TryToMakeDir(dir_name): |
82 """Create the directory dir_name if it doesn't exist.""" | 92 """Create the directory dir_name if it doesn't exist.""" |
83 try: | 93 try: |
84 os.makedirs(dir_name) | 94 os.makedirs(dir_name) |
85 except OSError, e: | 95 except OSError, e: |
86 if e.errno != errno.EEXIST: | 96 if e.errno != errno.EEXIST: |
87 raise e | 97 raise e |
88 | 98 |
89 | 99 |
90 def NormalizePath(path): | 100 def WriteForwardingHeader(input_filename, out_filename, relative_to_dir): |
91 """Normalize path for use with os.path.commonprefix. | |
92 On windows, this makes sure that the drive letters are always in the | |
93 same case.""" | |
94 abs_path = os.path.abspath(path) | |
95 drive, rest = os.path.splitdrive(abs_path) | |
96 return os.path.join(drive.lower(), rest) | |
97 | |
98 | |
99 def WriteSetupFilename(input_filename, out_filename, relative_to_dir): | |
100 """Create a forwarding header from out_filename to input_filename.""" | 101 """Create a forwarding header from out_filename to input_filename.""" |
101 # Figure out the relative path from out_filename to input_filename. | 102 # Windows has a file path limit of 260 characters, which can be hit when |
102 # We can't use os.path.relpath since that's a python2.6 feature and we | 103 # generating these forwarding headers. Instead of using an include |
103 # support python 2.5. | 104 # that specifies the path relative to out_filename's dir, we compute a path |
104 input_filename = NormalizePath(input_filename) | 105 # relative to relative_to_dir, which must be included in gyp's include_dirs |
105 out_filename = NormalizePath(out_filename) | 106 # settings for this to work. Even those this is really only needed on |
106 ancestor = os.path.commonprefix([input_filename, out_filename]) | 107 # Windows, we do this on all platforms to be consistent. |
107 | 108 rel_path = os.path.relpath(input_filename, relative_to_dir) |
108 assert os.path.isdir(ancestor) | |
109 num_parent_dirs = 0 | |
110 out_dir = os.path.split(out_filename)[0] | |
111 while os.path.normpath(ancestor) != os.path.normpath(out_dir): | |
112 num_parent_dirs += 1 | |
113 out_dir = os.path.split(out_dir)[0] | |
114 | |
115 if sys.platform == 'win32': | |
116 # Windows has a file path limit of 260 characters that we can hit when | |
117 # generating these forwarding headers. Instead of writing the full | |
118 # relative path, just write the path relative to the WebKit/chromium dir, | |
119 # which is in the include path. | |
120 rel_path = input_filename[len(ancestor):] | |
121 if relative_to_dir: | |
122 rel_path = os.path.join(relative_to_dir, rel_path) | |
123 else: | |
124 rel_path = os.path.join('/'.join(['..'] * num_parent_dirs), | |
125 input_filename[len(ancestor):]) | |
126 | |
127 out_file = open(out_filename, 'w') | 109 out_file = open(out_filename, 'w') |
128 out_file.write("""// This file is generated. Do not edit. | 110 out_file.write("""// This file is generated. Do not edit. |
| 111 // The include is relative to "%s". |
129 #include "%s" | 112 #include "%s" |
130 """ % rel_path) | 113 """ % (os.path.abspath(relative_to_dir), PathForInclude(rel_path))) |
131 out_file.close() | 114 out_file.close() |
132 | 115 |
133 | 116 |
134 def Main(argv): | 117 def Main(argv): |
135 commands = { | 118 commands = { |
136 'inputs': Inputs, | 119 'inputs': Inputs, |
137 'outputs': Outputs, | 120 'outputs': Outputs, |
138 'setup_headers': SetupHeaders, | 121 'setup_headers': SetupHeaders, |
139 } | 122 } |
140 command = argv[1] | 123 command = argv[1] |
141 args = argv[2:] | 124 args = argv[2:] |
142 return commands[command](args) | 125 return commands[command](args) |
143 | 126 |
144 | 127 |
145 if __name__ == '__main__': | 128 if __name__ == '__main__': |
146 sys.exit(Main(sys.argv)) | 129 sys.exit(Main(sys.argv)) |
OLD | NEW |