1 #!/usr/bin/env python 2 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. 6 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. 9 10 import glob 11 import gyp_helper 12 import os 13 import pipes 14 import shlex 15 import subprocess 16 import string 17 import sys 18 19 script_dir = os.path.dirname(os.path.realpath(__file__)) 20 chrome_src = os.path.abspath(os.path.join(script_dir, os.pardir)) 21 22 sys.path.insert(0, os.path.join(chrome_src, 'tools', 'gyp', 'pylib')) 23 import gyp 24 25 # Assume this file is in a one-level-deep subdirectory of the source root. 26 SRC_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 27 28 # Add paths so that pymod_do_main(...) can import files. 29 sys.path.insert(1, os.path.join(chrome_src, 'tools', 'generate_shim_headers')) 30 sys.path.insert(1, os.path.join(chrome_src, 'tools', 'grit')) 31 sys.path.insert(1, os.path.join(chrome_src, 'chrome', 'tools', 'build')) 32 sys.path.insert(1, os.path.join(chrome_src, 'native_client', 'build')) 33 sys.path.insert(1, os.path.join(chrome_src, 'native_client_sdk', 'src', 34 'build_tools')) 35 sys.path.insert(1, os.path.join(chrome_src, 'remoting', 'tools', 'build')) 36 sys.path.insert(1, os.path.join(chrome_src, 'third_party', 'liblouis')) 37 sys.path.insert(1, os.path.join(chrome_src, 'third_party', 'WebKit', 38 'Source', 'build', 'scripts')) 39 40 # On Windows, Psyco shortens warm runs of build/gyp_chromium by about 41 # 20 seconds on a z600 machine with 12 GB of RAM, from 90 down to 70 42 # seconds. Conversely, memory usage of build/gyp_chromium with Psyco 43 # maxes out at about 158 MB vs. 132 MB without it. 44 # 45 # Psyco uses native libraries, so we need to load a different 46 # installation depending on which OS we are running under. It has not 47 # been tested whether using Psyco on our Mac and Linux builds is worth 48 # it (the GYP running time is a lot shorter, so the JIT startup cost 49 # may not be worth it). 50 if sys.platform == 'win32': 51 try: 52 sys.path.insert(0, os.path.join(chrome_src, 'third_party', 'psyco_win32')) 53 import psyco 54 except: 55 psyco = None 56 else: 57 psyco = None 58 59 60 def GetSupplementalFiles(): 61 """Returns a list of the supplemental files that are included in all GYP 62 sources.""" 63 return glob.glob(os.path.join(chrome_src, '*', 'supplement.gypi')) 64 65 66 def FormatKeyForGN(key): 67 """Returns the given GYP key reformatted for GN. 68 69 GYP dictionary keys can be almost anything, but in GN they are identifiers 70 and must follow the same rules. This reformats such keys to be valid GN 71 identifiers.""" 72 return ''.join([c if c in string.ascii_letters else '_' for c in key]) 73 74 75 def EscapeStringForGN(s): 76 """Converts a string to a GN string literal.""" 77 # Escape $ characters which have special meaning to GN. 78 return '"' + s.replace('$', '\\$').replace('"', '\\"') + '"' 79 80 81 def GetGypVarsForGN(supplemental_files): 82 """Returns a dictionary of all GYP vars that we will be passing to GN.""" 83 vars_dict = {} 84 85 for supplement in supplemental_files: 86 with open(supplement, 'r') as f: 87 try: 88 file_data = eval(f.read(), {'__builtins__': None}, None) 89 except SyntaxError, e: 90 e.filename = os.path.abspath(supplement) 91 raise 92 variables = file_data.get('variables', []) 93 for v in variables: 94 vars_dict[FormatKeyForGN(v)] = EscapeStringForGN(str(variables[v])) 95 96 env_string = os.environ.get('GYP_DEFINES', '') 97 items = shlex.split(env_string) 98 for item in items: 99 tokens = item.split('=', 1) 100 # Some GYP variables have hyphens, which we don't support. 101 key = FormatKeyForGN(tokens[0]) 102 if len(tokens) == 2: 103 vars_dict[key] = tokens[1] 104 else: 105 # No value supplied, treat it as a boolean and set it. 106 vars_dict[key] = 'true' 107 108 return vars_dict 109 110 111 def GetArgsStringForGN(supplemental_files): 112 """Returns the args to pass to GN. 113 Based on a subset of the GYP variables that have been rewritten a bit.""" 114 115 vars_dict = GetGypVarsForGN(supplemental_files) 116 gn_args = '' 117 118 # These tuples of (key, value, gn_arg_string) use the gn_arg_string for 119 # gn when the key is set to the given value in the GYP arguments. 120 remap_cases = [ 121 ('branding', 'Chrome', 'is_chrome_branded=true'), 122 ('buildtype', 'Official', 'is_official_build=true'), 123 ('component', 'shared_library', 'is_component_build=true'), 124 ] 125 for i in remap_cases: 126 if i[0] in vars_dict and vars_dict[i[0]] == i[1]: 127 gn_args += ' ' + i[2] 128 129 # These string arguments get passed directly. 130 for v in ['windows_sdk_path']: 131 if v in vars_dict: 132 gn_args += ' ' + v + '=' + EscapeStringForGN(vars_dict[v]) 133 134 # Set the GYP flag so BUILD files know they're being invoked in GYP mode. 135 gn_args += ' is_gyp=true' 136 return gn_args.strip() 137 138 139 def additional_include_files(supplemental_files, args=[]): 140 """ 141 Returns a list of additional (.gypi) files to include, without duplicating 142 ones that are already specified on the command line. The list of supplemental 143 include files is passed in as an argument. 144 """ 145 # Determine the include files specified on the command line. 146 # This doesn't cover all the different option formats you can use, 147 # but it's mainly intended to avoid duplicating flags on the automatic 148 # makefile regeneration which only uses this format. 149 specified_includes = set() 150 for arg in args: 151 if arg.startswith('-I') and len(arg) > 2: 152 specified_includes.add(os.path.realpath(arg[2:])) 153 154 result = [] 155 def AddInclude(path): 156 if os.path.realpath(path) not in specified_includes: 157 result.append(path) 158 159 # Always include common.gypi. 160 AddInclude(os.path.join(script_dir, 'common.gypi')) 161 162 # Optionally add supplemental .gypi files if present. 163 for supplement in supplemental_files: 164 AddInclude(supplement) 165 166 return result 167 168 169 def RunGN(supplemental_includes): 170 """Runs GN, returning True if it succeeded, printing an error and returning 171 false if not.""" 172 173 # The binaries in platform-specific subdirectories in src/tools/gn/bin. 174 gnpath = SRC_DIR + '/tools/gn/bin/' 175 if sys.platform in ('cygwin', 'win32'): 176 gnpath += 'win/gn.exe' 177 elif sys.platform.startswith('linux'): 178 # On Linux we have 32-bit and 64-bit versions. 179 if subprocess.check_output(["getconf", "LONG_BIT"]).find("64") >= 0: 180 gnpath += 'linux/gn' 181 else: 182 gnpath += 'linux/gn32' 183 elif sys.platform == 'darwin': 184 gnpath += 'mac/gn' 185 else: 186 print 'Unknown platform for GN: ', sys.platform 187 return False 188 189 print 'Generating gyp files from GN...' 190 191 # Need to pass both the source root (the bots don't run this command from 192 # within the source tree) as well as set the is_gyp value so the BUILD files 193 # to know they're being run under GYP. 194 args = [gnpath, 'gyp', '-q', 195 '--root=' + chrome_src, 196 '--args=' + GetArgsStringForGN(supplemental_includes)] 197 return subprocess.call(args) == 0 198 199 200 if __name__ == '__main__': 201 args = sys.argv[1:] 202 203 if int(os.environ.get('GYP_CHROMIUM_NO_ACTION', 0)): 204 print 'Skipping gyp_chromium due to GYP_CHROMIUM_NO_ACTION env var.' 205 sys.exit(0) 206 207 # Use the Psyco JIT if available. 208 if psyco: 209 psyco.profile() 210 print "Enabled Psyco JIT." 211 212 # Fall back on hermetic python if we happen to get run under cygwin. 213 # TODO(bradnelson): take this out once this issue is fixed: 214 # http://code.google.com/p/gyp/issues/detail?id=177 215 if sys.platform == 'cygwin': 216 python_dir = os.path.join(chrome_src, 'third_party', 'python_26') 217 env = os.environ.copy() 218 env['PATH'] = python_dir + os.pathsep + env.get('PATH', '') 219 p = subprocess.Popen( 220 [os.path.join(python_dir, 'python.exe')] + sys.argv, 221 env=env, shell=False) 222 p.communicate() 223 sys.exit(p.returncode) 224 225 gyp_helper.apply_chromium_gyp_env() 226 227 # This could give false positives since it doesn't actually do real option 228 # parsing. Oh well. 229 gyp_file_specified = False 230 for arg in args: 231 if arg.endswith('.gyp'): 232 gyp_file_specified = True 233 break 234 235 # If we didn't get a file, check an env var, and then fall back to 236 # assuming 'all.gyp' from the same directory as the script. 237 if not gyp_file_specified: 238 gyp_file = os.environ.get('CHROMIUM_GYP_FILE') 239 if gyp_file: 240 # Note that CHROMIUM_GYP_FILE values can't have backslashes as 241 # path separators even on Windows due to the use of shlex.split(). 242 args.extend(shlex.split(gyp_file)) 243 else: 244 args.append(os.path.join(script_dir, 'all.gyp')) 245 246 supplemental_includes = GetSupplementalFiles() 247 248 if not RunGN(supplemental_includes): 249 sys.exit(1) 250 251 args.extend( 252 ['-I' + i for i in additional_include_files(supplemental_includes, args)]) 253 254 # There shouldn't be a circular dependency relationship between .gyp files, 255 # but in Chromium's .gyp files, on non-Mac platforms, circular relationships 256 # currently exist. The check for circular dependencies is currently 257 # bypassed on other platforms, but is left enabled on the Mac, where a 258 # violation of the rule causes Xcode to misbehave badly. 259 # TODO(mark): Find and kill remaining circular dependencies, and remove this 260 # option. http://crbug.com/35878. 261 # TODO(tc): Fix circular dependencies in ChromiumOS then add linux2 to the 262 # list. 263 if sys.platform not in ('darwin',): 264 args.append('--no-circular-check') 265 266 # Default to ninja on linux, but only if no generator has explicitly been set. 267 # Also default to ninja on mac, but only when not building chrome/ios. 268 # . -f / --format has precedence over the env var, no need to check for it 269 # . set the env var only if it hasn't been set yet 270 # . chromium.gyp_env has been applied to os.environ at this point already 271 if sys.platform.startswith('linux') and not os.environ.get('GYP_GENERATORS'): 272 os.environ['GYP_GENERATORS'] = 'ninja' 273 elif sys.platform == 'darwin' and not os.environ.get('GYP_GENERATORS') and \ 274 not 'OS=ios' in os.environ.get('GYP_DEFINES', []): 275 os.environ['GYP_GENERATORS'] = 'ninja' 276 277 # If using ninja on windows, and not opting out of the the automatic 278 # toolchain, then set up variables for the automatic toolchain. Opt-out is 279 # on by default, for now. 280 if (sys.platform in ('win32', 'cygwin') and 281 os.environ.get('GYP_GENERATORS') == 'ninja' and 282 os.environ.get('GYP_MSVS_USE_SYSTEM_TOOLCHAIN', '1') != '1'): 283 # For now, call the acquisition script here so that there's only one 284 # opt-in step required. This will be moved to a separate DEPS step once 285 # it's on by default. 286 subprocess.check_call([ 287 sys.executable, 288 os.path.normpath(os.path.join(script_dir, '..', 'tools', 'win', 289 'toolchain', 290 'get_toolchain_if_necessary.py'))]) 291 toolchain = os.path.normpath(os.path.join( 292 script_dir, '..', 'third_party', 'win_toolchain', 'files')) 293 os.environ['GYP_MSVS_OVERRIDE_PATH'] = toolchain 294 os.environ['GYP_MSVS_VERSION'] = '2013' 295 # We need to make sure windows_sdk_path is set to the automated toolchain 296 # values in GYP_DEFINES, but don't want to override any other values there. 297 gyp_defines_dict = gyp.NameValueListToDict(gyp.ShlexEnv('GYP_DEFINES')) 298 win8sdk = os.path.join(toolchain, 'win8sdk') 299 gyp_defines_dict['windows_sdk_path'] = win8sdk 300 os.environ['WINDOWSSDKDIR'] = win8sdk 301 os.environ['GYP_DEFINES'] = ' '.join('%s=%s' % (k, pipes.quote(str(v))) 302 for k, v in gyp_defines_dict.iteritems()) 303 # Include the VS runtime in the PATH in case it's not machine-installed. 304 runtime_path = ';'.join( 305 os.path.normpath(os.path.join( 306 script_dir, '..', 'third_party', 'win_toolchain', 'files', s)) 307 for s in ('sys64', 'sys32')) 308 os.environ['PATH'] = runtime_path + os.environ['PATH'] 309 print('Using automatic toolchain in %s.' % toolchain) 310 311 # If CHROMIUM_GYP_SYNTAX_CHECK is set to 1, it will invoke gyp with --check 312 # to enfore syntax checking. 313 syntax_check = os.environ.get('CHROMIUM_GYP_SYNTAX_CHECK') 314 if syntax_check and int(syntax_check): 315 args.append('--check') 316 317 print 'Updating projects from gyp files...' 318 sys.stdout.flush() 319 320 # Off we go... 321 sys.exit(gyp.main(args)) 322