3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 # This script is wrapper for Chromium that adds some support for how GYP
8 # is invoked by Chromium beyond what can be done in the gclient hooks.
20 script_dir
= os
.path
.dirname(os
.path
.realpath(__file__
))
21 chrome_src
= os
.path
.abspath(os
.path
.join(script_dir
, os
.pardir
))
23 sys
.path
.insert(0, os
.path
.join(chrome_src
, 'tools', 'gyp', 'pylib'))
26 # Assume this file is in a one-level-deep subdirectory of the source root.
27 SRC_DIR
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
29 # Add paths so that pymod_do_main(...) can import files.
30 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'tools'))
31 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'tools', 'generate_shim_headers'))
32 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'tools', 'grit'))
33 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'chrome', 'tools', 'build'))
34 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'native_client', 'build'))
35 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'native_client_sdk', 'src',
37 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'remoting', 'tools', 'build'))
38 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'third_party', 'liblouis'))
39 sys
.path
.insert(1, os
.path
.join(chrome_src
, 'third_party', 'WebKit',
40 'Source', 'build', 'scripts'))
42 # On Windows, Psyco shortens warm runs of build/gyp_chromium by about
43 # 20 seconds on a z600 machine with 12 GB of RAM, from 90 down to 70
44 # seconds. Conversely, memory usage of build/gyp_chromium with Psyco
45 # maxes out at about 158 MB vs. 132 MB without it.
47 # Psyco uses native libraries, so we need to load a different
48 # installation depending on which OS we are running under. It has not
49 # been tested whether using Psyco on our Mac and Linux builds is worth
50 # it (the GYP running time is a lot shorter, so the JIT startup cost
51 # may not be worth it).
52 if sys
.platform
== 'win32':
54 sys
.path
.insert(0, os
.path
.join(chrome_src
, 'third_party', 'psyco_win32'))
62 def GetSupplementalFiles():
63 """Returns a list of the supplemental files that are included in all GYP
65 return glob
.glob(os
.path
.join(chrome_src
, '*', 'supplement.gypi'))
68 def ProcessGypDefinesItems(items
):
69 """Converts a list of strings to a list of key-value pairs."""
72 tokens
= item
.split('=', 1)
73 # Some GYP variables have hyphens, which we don't support.
75 result
+= [(tokens
[0], tokens
[1])]
77 # No value supplied, treat it as a boolean and set it. Note that we
78 # use the string '1' here so we have a consistent definition whether
79 # you do 'foo=1' or 'foo'.
80 result
+= [(tokens
[0], '1')]
84 def GetGypVars(supplemental_files
):
85 """Returns a dictionary of all GYP vars."""
86 # Find the .gyp directory in the user's home directory.
87 home_dot_gyp
= os
.environ
.get('GYP_CONFIG_DIR', None)
89 home_dot_gyp
= os
.path
.expanduser(home_dot_gyp
)
92 if sys
.platform
in ('cygwin', 'win32'):
93 home_vars
.append('USERPROFILE')
94 for home_var
in home_vars
:
95 home
= os
.getenv(home_var
)
97 home_dot_gyp
= os
.path
.join(home
, '.gyp')
98 if not os
.path
.exists(home_dot_gyp
):
104 include_gypi
= os
.path
.join(home_dot_gyp
, "include.gypi")
105 if os
.path
.exists(include_gypi
):
106 supplemental_files
+= [include_gypi
]
108 # GYP defines from the supplemental.gypi files.
110 for supplement
in supplemental_files
:
111 with
open(supplement
, 'r') as f
:
113 file_data
= eval(f
.read(), {'__builtins__': None}, None)
114 except SyntaxError, e
:
115 e
.filename
= os
.path
.abspath(supplement
)
117 variables
= file_data
.get('variables', [])
119 supp_items
+= [(v
, str(variables
[v
]))]
121 # GYP defines from the environment.
122 env_items
= ProcessGypDefinesItems(
123 shlex
.split(os
.environ
.get('GYP_DEFINES', '')))
125 # GYP defines from the command line. We can't use optparse since we want
126 # to ignore all arguments other than "-D".
127 cmdline_input_items
= []
128 for i
in range(len(sys
.argv
))[1:]:
129 if sys
.argv
[i
].startswith('-D'):
130 if sys
.argv
[i
] == '-D' and i
+ 1 < len(sys
.argv
):
131 cmdline_input_items
+= [sys
.argv
[i
+ 1]]
132 elif len(sys
.argv
[i
]) > 2:
133 cmdline_input_items
+= [sys
.argv
[i
][2:]]
134 cmdline_items
= ProcessGypDefinesItems(cmdline_input_items
)
136 vars_dict
= dict(supp_items
+ env_items
+ cmdline_items
)
140 def GetOutputDirectory():
141 """Returns the output directory that GYP will use."""
142 # GYP generator flags from the command line. We can't use optparse since we
143 # want to ignore all arguments other than "-G".
144 needle
= '-Goutput_dir='
145 cmdline_input_items
= []
146 for item
in sys
.argv
[1:]:
147 if item
.startswith(needle
):
148 return item
[len(needle
):]
150 env_items
= shlex
.split(os
.environ
.get('GYP_GENERATOR_FLAGS', ''))
151 needle
= 'output_dir='
152 for item
in env_items
:
153 if item
.startswith(needle
):
154 return item
[len(needle
):]
159 def additional_include_files(supplemental_files
, args
=[]):
161 Returns a list of additional (.gypi) files to include, without duplicating
162 ones that are already specified on the command line. The list of supplemental
163 include files is passed in as an argument.
165 # Determine the include files specified on the command line.
166 # This doesn't cover all the different option formats you can use,
167 # but it's mainly intended to avoid duplicating flags on the automatic
168 # makefile regeneration which only uses this format.
169 specified_includes
= set()
171 if arg
.startswith('-I') and len(arg
) > 2:
172 specified_includes
.add(os
.path
.realpath(arg
[2:]))
175 def AddInclude(path
):
176 if os
.path
.realpath(path
) not in specified_includes
:
179 # Always include common.gypi.
180 AddInclude(os
.path
.join(script_dir
, 'common.gypi'))
182 # Optionally add supplemental .gypi files if present.
183 for supplement
in supplemental_files
:
184 AddInclude(supplement
)
189 if __name__
== '__main__':
192 if int(os
.environ
.get('GYP_CHROMIUM_NO_ACTION', 0)):
193 print 'Skipping gyp_chromium due to GYP_CHROMIUM_NO_ACTION env var.'
196 # Use the Psyco JIT if available.
199 print "Enabled Psyco JIT."
201 # Fall back on hermetic python if we happen to get run under cygwin.
202 # TODO(bradnelson): take this out once this issue is fixed:
203 # http://code.google.com/p/gyp/issues/detail?id=177
204 if sys
.platform
== 'cygwin':
205 import find_depot_tools
206 depot_tools_path
= find_depot_tools
.add_depot_tools_to_path()
207 python_dir
= sorted(glob
.glob(os
.path
.join(depot_tools_path
,
208 'python2*_bin')))[-1]
209 env
= os
.environ
.copy()
210 env
['PATH'] = python_dir
+ os
.pathsep
+ env
.get('PATH', '')
211 p
= subprocess
.Popen(
212 [os
.path
.join(python_dir
, 'python.exe')] + sys
.argv
,
213 env
=env
, shell
=False)
215 sys
.exit(p
.returncode
)
217 gyp_helper
.apply_chromium_gyp_env()
219 # This could give false positives since it doesn't actually do real option
221 gyp_file_specified
= False
223 if arg
.endswith('.gyp'):
224 gyp_file_specified
= True
227 # If we didn't get a file, check an env var, and then fall back to
228 # assuming 'all.gyp' from the same directory as the script.
229 if not gyp_file_specified
:
230 gyp_file
= os
.environ
.get('CHROMIUM_GYP_FILE')
232 # Note that CHROMIUM_GYP_FILE values can't have backslashes as
233 # path separators even on Windows due to the use of shlex.split().
234 args
.extend(shlex
.split(gyp_file
))
236 args
.append(os
.path
.join(script_dir
, 'all.gyp'))
238 # There shouldn't be a circular dependency relationship between .gyp files,
239 # but in Chromium's .gyp files, on non-Mac platforms, circular relationships
240 # currently exist. The check for circular dependencies is currently
241 # bypassed on other platforms, but is left enabled on the Mac, where a
242 # violation of the rule causes Xcode to misbehave badly.
243 # TODO(mark): Find and kill remaining circular dependencies, and remove this
244 # option. http://crbug.com/35878.
245 # TODO(tc): Fix circular dependencies in ChromiumOS then add linux2 to the
247 if sys
.platform
not in ('darwin',):
248 args
.append('--no-circular-check')
250 # We explicitly don't support the make gyp generator (crbug.com/348686). Be
251 # nice and fail here, rather than choking in gyp.
252 if re
.search(r
'(^|,|\s)make($|,|\s)', os
.environ
.get('GYP_GENERATORS', '')):
253 print 'Error: make gyp generator not supported (check GYP_GENERATORS).'
256 # Default to ninja on linux and windows, but only if no generator has
257 # explicitly been set.
258 # Also default to ninja on mac, but only when not building chrome/ios.
259 # . -f / --format has precedence over the env var, no need to check for it
260 # . set the env var only if it hasn't been set yet
261 # . chromium.gyp_env has been applied to os.environ at this point already
262 if sys
.platform
.startswith(('linux', 'win', 'freebsd')) and \
263 not os
.environ
.get('GYP_GENERATORS'):
264 os
.environ
['GYP_GENERATORS'] = 'ninja'
265 elif sys
.platform
== 'darwin' and not os
.environ
.get('GYP_GENERATORS') and \
266 not 'OS=ios' in os
.environ
.get('GYP_DEFINES', []):
267 os
.environ
['GYP_GENERATORS'] = 'ninja'
269 vs2013_runtime_dll_dirs
= vs_toolchain
.SetEnvironmentAndGetRuntimeDllDirs()
271 # If CHROMIUM_GYP_SYNTAX_CHECK is set to 1, it will invoke gyp with --check
272 # to enfore syntax checking.
273 syntax_check
= os
.environ
.get('CHROMIUM_GYP_SYNTAX_CHECK')
274 if syntax_check
and int(syntax_check
):
275 args
.append('--check')
277 supplemental_includes
= GetSupplementalFiles()
278 gyp_vars_dict
= GetGypVars(supplemental_includes
)
280 # Automatically turn on crosscompile support for platforms that need it.
281 # (The Chrome OS build sets CC_host / CC_target which implicitly enables
283 if all(('ninja' in os
.environ
.get('GYP_GENERATORS', ''),
284 gyp_vars_dict
.get('OS') in ['android', 'ios'],
285 'GYP_CROSSCOMPILE' not in os
.environ
)):
286 os
.environ
['GYP_CROSSCOMPILE'] = '1'
287 if gyp_vars_dict
.get('OS') == 'android':
288 args
.append('--check')
291 ['-I' + i
for i
in additional_include_files(supplemental_includes
, args
)])
293 args
.extend(['-D', 'gyp_output_dir=' + GetOutputDirectory()])
295 print 'Updating projects from gyp files...'
299 gyp_rc
= gyp
.main(args
)
301 # Check for landmines (reasons to clobber the build). This must be run here,
302 # rather than a separate runhooks step so that any environment modifications
303 # from above are picked up.
304 print 'Running build/landmines.py...'
305 subprocess
.check_call(
306 [sys
.executable
, os
.path
.join(script_dir
, 'landmines.py')])
308 if vs2013_runtime_dll_dirs
:
309 x64_runtime
, x86_runtime
= vs2013_runtime_dll_dirs
310 vs_toolchain
.CopyVsRuntimeDlls(
311 os
.path
.join(chrome_src
, GetOutputDirectory()),
312 (x86_runtime
, x64_runtime
))