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 use_analyzer
= len(args
) and args
[0] == '--analyzer'
195 os
.environ
['GYP_GENERATORS'] = 'analyzer'
196 args
.append('-Gfile_path=' + args
.pop(0))
198 if int(os
.environ
.get('GYP_CHROMIUM_NO_ACTION', 0)):
199 # Check for landmines (reasons to clobber the build) in any case.
200 print 'Running build/landmines.py...'
201 subprocess
.check_call(
202 [sys
.executable
, os
.path
.join(script_dir
, 'landmines.py')])
203 print 'Skipping gyp_chromium due to GYP_CHROMIUM_NO_ACTION env var.'
206 # Use the Psyco JIT if available.
209 print "Enabled Psyco JIT."
211 # Fall back on hermetic python if we happen to get run under cygwin.
212 # TODO(bradnelson): take this out once this issue is fixed:
213 # http://code.google.com/p/gyp/issues/detail?id=177
214 if sys
.platform
== 'cygwin':
215 import find_depot_tools
216 depot_tools_path
= find_depot_tools
.add_depot_tools_to_path()
217 python_dir
= sorted(glob
.glob(os
.path
.join(depot_tools_path
,
218 'python2*_bin')))[-1]
219 env
= os
.environ
.copy()
220 env
['PATH'] = python_dir
+ os
.pathsep
+ env
.get('PATH', '')
221 p
= subprocess
.Popen(
222 [os
.path
.join(python_dir
, 'python.exe')] + sys
.argv
,
223 env
=env
, shell
=False)
225 sys
.exit(p
.returncode
)
227 gyp_helper
.apply_chromium_gyp_env()
229 # This could give false positives since it doesn't actually do real option
231 gyp_file_specified
= False
233 if arg
.endswith('.gyp'):
234 gyp_file_specified
= True
237 # If we didn't get a file, check an env var, and then fall back to
238 # assuming 'all.gyp' from the same directory as the script.
239 if not gyp_file_specified
:
240 gyp_file
= os
.environ
.get('CHROMIUM_GYP_FILE')
242 # Note that CHROMIUM_GYP_FILE values can't have backslashes as
243 # path separators even on Windows due to the use of shlex.split().
244 args
.extend(shlex
.split(gyp_file
))
246 args
.append(os
.path
.join(script_dir
, 'all.gyp'))
248 # There shouldn't be a circular dependency relationship between .gyp files,
249 # but in Chromium's .gyp files, on non-Mac platforms, circular relationships
250 # currently exist. The check for circular dependencies is currently
251 # bypassed on other platforms, but is left enabled on the Mac, where a
252 # violation of the rule causes Xcode to misbehave badly.
253 # TODO(mark): Find and kill remaining circular dependencies, and remove this
254 # option. http://crbug.com/35878.
255 # TODO(tc): Fix circular dependencies in ChromiumOS then add linux2 to the
257 if sys
.platform
not in ('darwin',):
258 args
.append('--no-circular-check')
260 # We explicitly don't support the make gyp generator (crbug.com/348686). Be
261 # nice and fail here, rather than choking in gyp.
262 if re
.search(r
'(^|,|\s)make($|,|\s)', os
.environ
.get('GYP_GENERATORS', '')):
263 print 'Error: make gyp generator not supported (check GYP_GENERATORS).'
266 # Default to ninja on linux and windows, but only if no generator has
267 # explicitly been set.
268 # Also default to ninja on mac, but only when not building chrome/ios.
269 # . -f / --format has precedence over the env var, no need to check for it
270 # . set the env var only if it hasn't been set yet
271 # . chromium.gyp_env has been applied to os.environ at this point already
272 if sys
.platform
.startswith(('linux', 'win', 'freebsd')) and \
273 not os
.environ
.get('GYP_GENERATORS'):
274 os
.environ
['GYP_GENERATORS'] = 'ninja'
275 elif sys
.platform
== 'darwin' and not os
.environ
.get('GYP_GENERATORS') and \
276 not 'OS=ios' in os
.environ
.get('GYP_DEFINES', []):
277 os
.environ
['GYP_GENERATORS'] = 'ninja'
279 vs2013_runtime_dll_dirs
= vs_toolchain
.SetEnvironmentAndGetRuntimeDllDirs()
281 # If CHROMIUM_GYP_SYNTAX_CHECK is set to 1, it will invoke gyp with --check
282 # to enfore syntax checking.
283 syntax_check
= os
.environ
.get('CHROMIUM_GYP_SYNTAX_CHECK')
284 if syntax_check
and int(syntax_check
):
285 args
.append('--check')
287 supplemental_includes
= GetSupplementalFiles()
288 gyp_vars_dict
= GetGypVars(supplemental_includes
)
290 # TODO(dmikurube): Remove these checks and messages after a while.
291 if ('linux_use_tcmalloc' in gyp_vars_dict
or
292 'android_use_tcmalloc' in gyp_vars_dict
):
293 print '*****************************************************************'
294 print '"linux_use_tcmalloc" and "android_use_tcmalloc" are deprecated!'
295 print '-----------------------------------------------------------------'
296 print 'You specify "linux_use_tcmalloc" or "android_use_tcmalloc" in'
297 print 'your GYP_DEFINES. Please switch them into "use_allocator" now.'
298 print 'See http://crbug.com/345554 for the details.'
299 print '*****************************************************************'
301 # Automatically turn on crosscompile support for platforms that need it.
302 # (The Chrome OS build sets CC_host / CC_target which implicitly enables
304 if all(('ninja' in os
.environ
.get('GYP_GENERATORS', ''),
305 gyp_vars_dict
.get('OS') in ['android', 'ios'],
306 'GYP_CROSSCOMPILE' not in os
.environ
)):
307 os
.environ
['GYP_CROSSCOMPILE'] = '1'
308 if gyp_vars_dict
.get('OS') == 'android':
309 args
.append('--check')
312 ['-I' + i
for i
in additional_include_files(supplemental_includes
, args
)])
314 args
.extend(['-D', 'gyp_output_dir=' + GetOutputDirectory()])
317 print 'Updating projects from gyp files...'
321 gyp_rc
= gyp
.main(args
)
324 # Check for landmines (reasons to clobber the build). This must be run here,
325 # rather than a separate runhooks step so that any environment modifications
326 # from above are picked up.
327 print 'Running build/landmines.py...'
328 subprocess
.check_call(
329 [sys
.executable
, os
.path
.join(script_dir
, 'landmines.py')])
331 if vs2013_runtime_dll_dirs
:
332 x64_runtime
, x86_runtime
= vs2013_runtime_dll_dirs
333 vs_toolchain
.CopyVsRuntimeDlls(
334 os
.path
.join(chrome_src
, GetOutputDirectory()),
335 (x86_runtime
, x64_runtime
))