| OLD | NEW | 
 | (Empty) | 
|    1 #!/usr/bin/env python |  | 
|    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 |  | 
|    4 # found in the LICENSE file. |  | 
|    5  |  | 
|    6 """A helper script for setting up forwarding headers.""" |  | 
|    7  |  | 
|    8 import errno |  | 
|    9 import os |  | 
|   10 import sys |  | 
|   11  |  | 
|   12  |  | 
|   13 def GetHeaderFilesInDir(dir_path): |  | 
|   14   """Return a list of all header files under dir_path |  | 
|   15   (as absolute native paths).""" |  | 
|   16   all_files = [] |  | 
|   17   for root, dirs, files in os.walk(dir_path): |  | 
|   18     all_files.extend([os.path.join(root, f) for f in files if f.endswith('.h')]) |  | 
|   19   return all_files |  | 
|   20  |  | 
|   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  |  | 
|   37 def Inputs(args): |  | 
|   38   """List the files in the provided input dir. |  | 
|   39  |  | 
|   40   args: A list with 1 value, the input dir. |  | 
|   41   Returns: 0 on success, other value on error.""" |  | 
|   42   if len(args) != 1: |  | 
|   43     print "'inputs' expects only one input directory." |  | 
|   44     return -1 |  | 
|   45  |  | 
|   46   for filename in GetHeaderFilesInDir(args[0]): |  | 
|   47     print PathForGyp(filename) |  | 
|   48   return 0 |  | 
|   49  |  | 
|   50  |  | 
|   51 def Outputs(args): |  | 
|   52   """Takes an input dir and an output dir and figures out new output files |  | 
|   53   based on copying from the input dir to the output dir. |  | 
|   54  |  | 
|   55   args: A list with 2 values, the input dir and the output dir. |  | 
|   56   Returns: 0 on success, other value on error.""" |  | 
|   57   if len(args) != 2: |  | 
|   58     print "'outputs' expects an input directory and an output directory." |  | 
|   59     return -1 |  | 
|   60  |  | 
|   61   base_input_dir = NativePath(args[0]) |  | 
|   62   output_dir = NativePath(args[1]) |  | 
|   63   input_files = GetHeaderFilesInDir(base_input_dir) |  | 
|   64   for filename in input_files: |  | 
|   65     rel_path = os.path.relpath(filename, base_input_dir) |  | 
|   66     print PathForGyp(os.path.join(output_dir, rel_path)) |  | 
|   67  |  | 
|   68  |  | 
|   69 def SetupHeaders(args): |  | 
|   70   """Takes an input dir and an output dir and sets up forwarding headers |  | 
|   71   from output dir to files in input 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.. |  | 
|   74   Returns: 0 on success, other value on error.""" |  | 
|   75   if len(args) != 3: |  | 
|   76     print ("'setup_headers' expects an input directory, an output directory, ." |  | 
|   77         "and a directory to make includes relative to.") |  | 
|   78     return -1 |  | 
|   79  |  | 
|   80   base_input_dir = NativePath(args[0]) |  | 
|   81   output_dir = NativePath(args[1]) |  | 
|   82   relative_to_dir = NativePath(args[2]) |  | 
|   83   input_files = GetHeaderFilesInDir(base_input_dir) |  | 
|   84   for input_filename in input_files: |  | 
|   85     rel_path = os.path.relpath(input_filename, base_input_dir) |  | 
|   86     out_filename = os.path.join(output_dir, rel_path) |  | 
|   87     TryToMakeDir(os.path.split(out_filename)[0]) |  | 
|   88     WriteForwardingHeader(input_filename, out_filename, relative_to_dir) |  | 
|   89  |  | 
|   90  |  | 
|   91 def TryToMakeDir(dir_name): |  | 
|   92   """Create the directory dir_name if it doesn't exist.""" |  | 
|   93   try: |  | 
|   94     os.makedirs(dir_name) |  | 
|   95   except OSError, e: |  | 
|   96     if e.errno != errno.EEXIST: |  | 
|   97       raise e |  | 
|   98  |  | 
|   99  |  | 
|  100 def WriteForwardingHeader(input_filename, out_filename, relative_to_dir): |  | 
|  101   """Create a forwarding header from out_filename to input_filename.""" |  | 
|  102   # Windows has a file path limit of 260 characters, which can be hit when |  | 
|  103   # generating these forwarding headers.  Instead of using an include |  | 
|  104   # that specifies the path relative to out_filename's dir, we compute a path |  | 
|  105   # relative to relative_to_dir, which must be included in gyp's include_dirs |  | 
|  106   # settings for this to work. Even those this is really only needed on |  | 
|  107   # Windows, we do this on all platforms to be consistent. |  | 
|  108   rel_path = os.path.relpath(input_filename, relative_to_dir) |  | 
|  109   out_file = open(out_filename, 'w') |  | 
|  110   out_file.write("""// This file is generated.  Do not edit. |  | 
|  111 // The include is relative to "%s". |  | 
|  112 #include "%s" |  | 
|  113 """ % (os.path.abspath(relative_to_dir), PathForInclude(rel_path))) |  | 
|  114   out_file.close() |  | 
|  115  |  | 
|  116  |  | 
|  117 def Main(argv): |  | 
|  118   commands = { |  | 
|  119     'inputs': Inputs, |  | 
|  120     'outputs': Outputs, |  | 
|  121     'setup_headers': SetupHeaders, |  | 
|  122   } |  | 
|  123   command = argv[1] |  | 
|  124   args = argv[2:] |  | 
|  125   return commands[command](args) |  | 
|  126  |  | 
|  127  |  | 
|  128 if __name__ == '__main__': |  | 
|  129   sys.exit(Main(sys.argv)) |  | 
| OLD | NEW |