Chromium Code Reviews| Index: webkit/support/setup_third_party.py |
| diff --git a/webkit/support/setup_third_party.py b/webkit/support/setup_third_party.py |
| index 50c7cc22b83ba31cde6f2fb7f74169557128eddd..4f3e8ce11fdc80a9d113d24290a81d3891ca0b63 100755 |
| --- a/webkit/support/setup_third_party.py |
| +++ b/webkit/support/setup_third_party.py |
| @@ -11,16 +11,29 @@ import sys |
| def GetHeaderFilesInDir(dir_path): |
| - """Return a list of all header files in dir_path.""" |
| + """Return a list of all header files under dir_path |
| + (as absolute native paths).""" |
| all_files = [] |
| for root, dirs, files in os.walk(dir_path): |
| - # Backslashes get shell escaped by gyp, so force forward slash for |
| - # path separators. |
| - all_files.extend([os.path.join(root, f).replace(os.sep, '/') |
| - for f in files if f.endswith('.h')]) |
| + all_files.extend([os.path.join(root, f) for f in files if f.endswith('.h')]) |
| return all_files |
| +def PathForInclude(path): |
| + # We should always use unix-style forward slashes in #includes. |
| + return path.replace(os.sep, '/') |
| + |
| + |
| +def NativePath(path): |
| + return path.replace('/', os.sep) |
| + |
| + |
| +def PathForGyp(path): |
| + # GYP will try to shell-escape backslashes, so we should always |
| + # return unix-style paths with forward slashes as the directory separators. |
| + return path.replace(os.sep, '/') |
| + |
| + |
| def Inputs(args): |
| """List the files in the provided input dir. |
| @@ -31,7 +44,7 @@ def Inputs(args): |
| return -1 |
| for filename in GetHeaderFilesInDir(args[0]): |
| - print filename |
| + print PathForGyp(filename) |
| return 0 |
| @@ -45,14 +58,12 @@ def Outputs(args): |
| print "'outputs' expects an input directory and an output directory." |
| return -1 |
| - base_input_dir = args[0] |
| - output_dir = args[1] |
| + base_input_dir = NativePath(args[0]) |
| + output_dir = NativePath(args[1]) |
| input_files = GetHeaderFilesInDir(base_input_dir) |
| for filename in input_files: |
| - rel_path = filename[len(base_input_dir) + 1:] |
| - # Backslashes get shell escaped by gyp, so force forward slash for |
| - # path separators. |
| - print os.path.join(output_dir, rel_path).replace(os.sep, '/') |
| + rel_path = os.path.relpath(filename, base_input_dir) |
| + print PathForGyp(os.path.join(output_dir, rel_path)) |
| def SetupHeaders(args): |
| @@ -60,22 +71,20 @@ def SetupHeaders(args): |
| from output dir to files in input dir. |
| 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.
|
| Returns: 0 on success, other value on error.""" |
| - if len(args) != 2 and len(args) != 3: |
| + if len(args) != 3: |
| print ("'setup_headers' expects an input directory, an output directory, ." |
| - "and an optional directory to make includes relative to.") |
| + "and a directory to make includes relative to.") |
| return -1 |
| - base_input_dir = args[0] |
| - output_dir = args[1] |
| - relative_to_dir = None |
| - if len(args) == 3: |
| - relative_to_dir = args[2] |
| + base_input_dir = NativePath(args[0]) |
| + output_dir = NativePath(args[1]) |
| + relative_to_dir = NativePath(args[2]) |
| input_files = GetHeaderFilesInDir(base_input_dir) |
| for input_filename in input_files: |
| - rel_path = input_filename[len(base_input_dir) + 1:] |
| + rel_path = os.path.relpath(input_filename, base_input_dir) |
| out_filename = os.path.join(output_dir, rel_path) |
| TryToMakeDir(os.path.split(out_filename)[0]) |
| - WriteSetupFilename(input_filename, out_filename, relative_to_dir) |
| + WriteForwardingHeader(input_filename, out_filename, relative_to_dir) |
| def TryToMakeDir(dir_name): |
| @@ -87,47 +96,20 @@ def TryToMakeDir(dir_name): |
| raise e |
| -def NormalizePath(path): |
| - """Normalize path for use with os.path.commonprefix. |
| - On windows, this makes sure that the drive letters are always in the |
| - same case.""" |
| - abs_path = os.path.abspath(path) |
| - drive, rest = os.path.splitdrive(abs_path) |
| - return os.path.join(drive.lower(), rest) |
| - |
| - |
| -def WriteSetupFilename(input_filename, out_filename, relative_to_dir): |
| +def WriteForwardingHeader(input_filename, out_filename, relative_to_dir): |
| """Create a forwarding header from out_filename to input_filename.""" |
| - # Figure out the relative path from out_filename to input_filename. |
| - # We can't use os.path.relpath since that's a python2.6 feature and we |
| - # support python 2.5. |
| - input_filename = NormalizePath(input_filename) |
| - out_filename = NormalizePath(out_filename) |
| - ancestor = os.path.commonprefix([input_filename, out_filename]) |
| - |
| - assert os.path.isdir(ancestor) |
| - num_parent_dirs = 0 |
| - out_dir = os.path.split(out_filename)[0] |
| - while os.path.normpath(ancestor) != os.path.normpath(out_dir): |
| - num_parent_dirs += 1 |
| - out_dir = os.path.split(out_dir)[0] |
| - |
| - if sys.platform == 'win32': |
| - # Windows has a file path limit of 260 characters that we can hit when |
| - # generating these forwarding headers. Instead of writing the full |
| - # relative path, just write the path relative to the WebKit/chromium dir, |
| - # which is in the include path. |
| - rel_path = input_filename[len(ancestor):] |
| - if relative_to_dir: |
| - rel_path = os.path.join(relative_to_dir, rel_path) |
| - else: |
| - rel_path = os.path.join('/'.join(['..'] * num_parent_dirs), |
| - input_filename[len(ancestor):]) |
| - |
| + # Windows has a file path limit of 260 characters, which can be hit when |
| + # generating these forwarding headers. Instead of using an include |
| + # that specifies the path relative to out_filename's dir, we compute a path |
| + # relative to relative_to_dir, which must be included in gyp's include_dirs |
| + # settings for this to work. Even those this is really only needed on |
| + # Windows, we do this on all platforms to be consistent. |
| + rel_path = os.path.relpath(input_filename, relative_to_dir) |
| out_file = open(out_filename, 'w') |
| out_file.write("""// This file is generated. Do not edit. |
| +// The include is relative to "%s". |
| #include "%s" |
| -""" % rel_path) |
| +""" % (os.path.abspath(relative_to_dir), PathForInclude(rel_path))) |
| out_file.close() |