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.
6 """A helper script for setting up forwarding headers."""
13 def GetHeaderFilesInDir(dir_path
):
14 """Return a list of all header files in dir_path."""
16 for root
, dirs
, files
in os
.walk(dir_path
):
17 # Backslashes get shell escaped by gyp, so force forward slash for
19 all_files
.extend([os
.path
.join(root
, f
).replace(os
.sep
, '/')
20 for f
in files
if f
.endswith('.h')])
25 """List the files in the provided input dir.
27 args: A list with 1 value, the input dir.
28 Returns: 0 on success, other value on error."""
30 print "'inputs' expects only one input directory."
33 for filename
in GetHeaderFilesInDir(args
[0]):
39 """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.
42 args: A list with 2 values, the input dir and the output dir.
43 Returns: 0 on success, other value on error."""
45 print "'outputs' expects an input directory and an output directory."
48 base_input_dir
= args
[0]
50 input_files
= GetHeaderFilesInDir(base_input_dir
)
51 for filename
in input_files
:
52 rel_path
= filename
[len(base_input_dir
) + 1:]
53 # Backslashes get shell escaped by gyp, so force forward slash for
55 print os
.path
.join(output_dir
, rel_path
).replace(os
.sep
, '/')
58 def SetupHeaders(args
):
59 """Takes an input dir and an output dir and sets up forwarding headers
60 from output dir to files in input dir.
61 args: A list with 2 values, the input dir and the output dir.
62 Returns: 0 on success, other value on error."""
63 if len(args
) != 2 and len(args
) != 3:
64 print ("'setup_headers' expects an input directory, an output directory, ."
65 "and an optional directory to make includes relative to.")
68 base_input_dir
= args
[0]
70 relative_to_dir
= None
72 relative_to_dir
= args
[2]
73 input_files
= GetHeaderFilesInDir(base_input_dir
)
74 for input_filename
in input_files
:
75 rel_path
= input_filename
[len(base_input_dir
) + 1:]
76 out_filename
= os
.path
.join(output_dir
, rel_path
)
77 TryToMakeDir(os
.path
.split(out_filename
)[0])
78 WriteSetupFilename(input_filename
, out_filename
, relative_to_dir
)
81 def TryToMakeDir(dir_name
):
82 """Create the directory dir_name if it doesn't exist."""
86 if e
.errno
!= errno
.EEXIST
:
90 def NormalizePath(path
):
91 """Normalize path for use with os.path.commonprefix.
92 On windows, this makes sure that the drive letters are always in the
94 abs_path
= os
.path
.abspath(path
)
95 drive
, rest
= os
.path
.splitdrive(abs_path
)
96 return os
.path
.join(drive
.lower(), rest
)
99 def WriteSetupFilename(input_filename
, out_filename
, relative_to_dir
):
100 """Create a forwarding header from out_filename to input_filename."""
101 # Figure out the relative path from out_filename to input_filename.
102 # We can't use os.path.relpath since that's a python2.6 feature and we
103 # support python 2.5.
104 input_filename
= NormalizePath(input_filename
)
105 out_filename
= NormalizePath(out_filename
)
106 ancestor
= os
.path
.commonprefix([input_filename
, out_filename
])
108 assert os
.path
.isdir(ancestor
)
110 out_dir
= os
.path
.split(out_filename
)[0]
111 while os
.path
.normpath(ancestor
) != os
.path
.normpath(out_dir
):
113 out_dir
= os
.path
.split(out_dir
)[0]
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
):]
122 rel_path
= os
.path
.join(relative_to_dir
, rel_path
)
124 rel_path
= os
.path
.join('/'.join(['..'] * num_parent_dirs
),
125 input_filename
[len(ancestor
):])
127 out_file
= open(out_filename
, 'w')
128 out_file
.write("""// This file is generated. Do not edit.
138 'setup_headers': SetupHeaders
,
142 return commands
[command
](args
)
145 if __name__
== '__main__':
146 sys
.exit(Main(sys
.argv
))