Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(439)

Side by Side Diff: webkit/support/setup_third_party.py

Issue 12224087: fix third_party_headers to work properly on Windows w/ ninja. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: improve comment in WriteForwardingHeader Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 2 values, the input dir and the output dir.
tony 2013/02/11 18:16:50 Nit: Can you update this docstring?
Dirk Pranke 2013/02/11 23:42:30 good catch. done.
62 Returns: 0 on success, other value on error.""" 73 Returns: 0 on success, other value on error."""
63 if len(args) != 2 and len(args) != 3: 74 if len(args) != 3:
64 print ("'setup_headers' expects an input directory, an output directory, ." 75 print ("'setup_headers' expects an input directory, an output directory, ."
65 "and an optional directory to make includes relative to.") 76 "and a directory to make includes relative to.")
66 return -1 77 return -1
67 78
68 base_input_dir = args[0] 79 base_input_dir = NativePath(args[0])
69 output_dir = args[1] 80 output_dir = NativePath(args[1])
70 relative_to_dir = None 81 relative_to_dir = NativePath(args[2])
71 if len(args) == 3:
72 relative_to_dir = args[2]
73 input_files = GetHeaderFilesInDir(base_input_dir) 82 input_files = GetHeaderFilesInDir(base_input_dir)
74 for input_filename in input_files: 83 for input_filename in input_files:
75 rel_path = input_filename[len(base_input_dir) + 1:] 84 rel_path = os.path.relpath(input_filename, base_input_dir)
76 out_filename = os.path.join(output_dir, rel_path) 85 out_filename = os.path.join(output_dir, rel_path)
77 TryToMakeDir(os.path.split(out_filename)[0]) 86 TryToMakeDir(os.path.split(out_filename)[0])
78 WriteSetupFilename(input_filename, out_filename, relative_to_dir) 87 WriteForwardingHeader(input_filename, out_filename, relative_to_dir)
79 88
80 89
81 def TryToMakeDir(dir_name): 90 def TryToMakeDir(dir_name):
82 """Create the directory dir_name if it doesn't exist.""" 91 """Create the directory dir_name if it doesn't exist."""
83 try: 92 try:
84 os.makedirs(dir_name) 93 os.makedirs(dir_name)
85 except OSError, e: 94 except OSError, e:
86 if e.errno != errno.EEXIST: 95 if e.errno != errno.EEXIST:
87 raise e 96 raise e
88 97
89 98
90 def NormalizePath(path): 99 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.""" 100 """Create a forwarding header from out_filename to input_filename."""
101 # Figure out the relative path from out_filename to input_filename. 101 # 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 102 # generating these forwarding headers. Instead of using an include
103 # support python 2.5. 103 # that specifies the path relative to out_filename's dir, we compute a path
104 input_filename = NormalizePath(input_filename) 104 # relative to relative_to_dir, which must be included in gyp's include_dirs
105 out_filename = NormalizePath(out_filename) 105 # settings for this to work. Even those this is really only needed on
106 ancestor = os.path.commonprefix([input_filename, out_filename]) 106 # Windows, we do this on all platforms to be consistent.
107 107 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') 108 out_file = open(out_filename, 'w')
128 out_file.write("""// This file is generated. Do not edit. 109 out_file.write("""// This file is generated. Do not edit.
110 // The include is relative to "%s".
129 #include "%s" 111 #include "%s"
130 """ % rel_path) 112 """ % (os.path.abspath(relative_to_dir), PathForInclude(rel_path)))
131 out_file.close() 113 out_file.close()
132 114
133 115
134 def Main(argv): 116 def Main(argv):
135 commands = { 117 commands = {
136 'inputs': Inputs, 118 'inputs': Inputs,
137 'outputs': Outputs, 119 'outputs': Outputs,
138 'setup_headers': SetupHeaders, 120 'setup_headers': SetupHeaders,
139 } 121 }
140 command = argv[1] 122 command = argv[1]
141 args = argv[2:] 123 args = argv[2:]
142 return commands[command](args) 124 return commands[command](args)
143 125
144 126
145 if __name__ == '__main__': 127 if __name__ == '__main__':
146 sys.exit(Main(sys.argv)) 128 sys.exit(Main(sys.argv))
OLDNEW
« webkit/support/setup_third_party.gyp ('K') | « webkit/support/setup_third_party.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698