__init__.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 Google Inc. 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. import copy
  6. import gyp.input
  7. import optparse
  8. import os.path
  9. import re
  10. import shlex
  11. import sys
  12. import traceback
  13. from gyp.common import GypError
  14. # Default debug modes for GYP
  15. debug = {}
  16. # List of "official" debug modes, but you can use anything you like.
  17. DEBUG_GENERAL = 'general'
  18. DEBUG_VARIABLES = 'variables'
  19. DEBUG_INCLUDES = 'includes'
  20. def DebugOutput(mode, message, *args):
  21. if 'all' in gyp.debug or mode in gyp.debug:
  22. ctx = ('unknown', 0, 'unknown')
  23. try:
  24. f = traceback.extract_stack(limit=2)
  25. if f:
  26. ctx = f[0][:3]
  27. except:
  28. pass
  29. if args:
  30. message %= args
  31. print '%s:%s:%d:%s %s' % (mode.upper(), os.path.basename(ctx[0]),
  32. ctx[1], ctx[2], message)
  33. def FindBuildFiles():
  34. extension = '.gyp'
  35. files = os.listdir(os.getcwd())
  36. build_files = []
  37. for file in files:
  38. if file.endswith(extension):
  39. build_files.append(file)
  40. return build_files
  41. def Load(build_files, format, default_variables={},
  42. includes=[], depth='.', params=None, check=False,
  43. circular_check=True, duplicate_basename_check=True):
  44. """
  45. Loads one or more specified build files.
  46. default_variables and includes will be copied before use.
  47. Returns the generator for the specified format and the
  48. data returned by loading the specified build files.
  49. """
  50. if params is None:
  51. params = {}
  52. if '-' in format:
  53. format, params['flavor'] = format.split('-', 1)
  54. default_variables = copy.copy(default_variables)
  55. # Default variables provided by this program and its modules should be
  56. # named WITH_CAPITAL_LETTERS to provide a distinct "best practice" namespace,
  57. # avoiding collisions with user and automatic variables.
  58. default_variables['GENERATOR'] = format
  59. default_variables['GENERATOR_FLAVOR'] = params.get('flavor', '')
  60. # Format can be a custom python file, or by default the name of a module
  61. # within gyp.generator.
  62. if format.endswith('.py'):
  63. generator_name = os.path.splitext(format)[0]
  64. path, generator_name = os.path.split(generator_name)
  65. # Make sure the path to the custom generator is in sys.path
  66. # Don't worry about removing it once we are done. Keeping the path
  67. # to each generator that is used in sys.path is likely harmless and
  68. # arguably a good idea.
  69. path = os.path.abspath(path)
  70. if path not in sys.path:
  71. sys.path.insert(0, path)
  72. else:
  73. generator_name = 'gyp.generator.' + format
  74. # These parameters are passed in order (as opposed to by key)
  75. # because ActivePython cannot handle key parameters to __import__.
  76. generator = __import__(generator_name, globals(), locals(), generator_name)
  77. for (key, val) in generator.generator_default_variables.items():
  78. default_variables.setdefault(key, val)
  79. # Give the generator the opportunity to set additional variables based on
  80. # the params it will receive in the output phase.
  81. if getattr(generator, 'CalculateVariables', None):
  82. generator.CalculateVariables(default_variables, params)
  83. # Give the generator the opportunity to set generator_input_info based on
  84. # the params it will receive in the output phase.
  85. if getattr(generator, 'CalculateGeneratorInputInfo', None):
  86. generator.CalculateGeneratorInputInfo(params)
  87. # Fetch the generator specific info that gets fed to input, we use getattr
  88. # so we can default things and the generators only have to provide what
  89. # they need.
  90. generator_input_info = {
  91. 'non_configuration_keys':
  92. getattr(generator, 'generator_additional_non_configuration_keys', []),
  93. 'path_sections':
  94. getattr(generator, 'generator_additional_path_sections', []),
  95. 'extra_sources_for_rules':
  96. getattr(generator, 'generator_extra_sources_for_rules', []),
  97. 'generator_supports_multiple_toolsets':
  98. getattr(generator, 'generator_supports_multiple_toolsets', False),
  99. 'generator_wants_static_library_dependencies_adjusted':
  100. getattr(generator,
  101. 'generator_wants_static_library_dependencies_adjusted', True),
  102. 'generator_wants_sorted_dependencies':
  103. getattr(generator, 'generator_wants_sorted_dependencies', False),
  104. 'generator_filelist_paths':
  105. getattr(generator, 'generator_filelist_paths', None),
  106. }
  107. # Process the input specific to this generator.
  108. result = gyp.input.Load(build_files, default_variables, includes[:],
  109. depth, generator_input_info, check, circular_check,
  110. duplicate_basename_check,
  111. params['parallel'], params['root_targets'])
  112. return [generator] + result
  113. def NameValueListToDict(name_value_list):
  114. """
  115. Takes an array of strings of the form 'NAME=VALUE' and creates a dictionary
  116. of the pairs. If a string is simply NAME, then the value in the dictionary
  117. is set to True. If VALUE can be converted to an integer, it is.
  118. """
  119. result = { }
  120. for item in name_value_list:
  121. tokens = item.split('=', 1)
  122. if len(tokens) == 2:
  123. # If we can make it an int, use that, otherwise, use the string.
  124. try:
  125. token_value = int(tokens[1])
  126. except ValueError:
  127. token_value = tokens[1]
  128. # Set the variable to the supplied value.
  129. result[tokens[0]] = token_value
  130. else:
  131. # No value supplied, treat it as a boolean and set it.
  132. result[tokens[0]] = True
  133. return result
  134. def ShlexEnv(env_name):
  135. flags = os.environ.get(env_name, [])
  136. if flags:
  137. flags = shlex.split(flags)
  138. return flags
  139. def FormatOpt(opt, value):
  140. if opt.startswith('--'):
  141. return '%s=%s' % (opt, value)
  142. return opt + value
  143. def RegenerateAppendFlag(flag, values, predicate, env_name, options):
  144. """Regenerate a list of command line flags, for an option of action='append'.
  145. The |env_name|, if given, is checked in the environment and used to generate
  146. an initial list of options, then the options that were specified on the
  147. command line (given in |values|) are appended. This matches the handling of
  148. environment variables and command line flags where command line flags override
  149. the environment, while not requiring the environment to be set when the flags
  150. are used again.
  151. """
  152. flags = []
  153. if options.use_environment and env_name:
  154. for flag_value in ShlexEnv(env_name):
  155. value = FormatOpt(flag, predicate(flag_value))
  156. if value in flags:
  157. flags.remove(value)
  158. flags.append(value)
  159. if values:
  160. for flag_value in values:
  161. flags.append(FormatOpt(flag, predicate(flag_value)))
  162. return flags
  163. def RegenerateFlags(options):
  164. """Given a parsed options object, and taking the environment variables into
  165. account, returns a list of flags that should regenerate an equivalent options
  166. object (even in the absence of the environment variables.)
  167. Any path options will be normalized relative to depth.
  168. The format flag is not included, as it is assumed the calling generator will
  169. set that as appropriate.
  170. """
  171. def FixPath(path):
  172. path = gyp.common.FixIfRelativePath(path, options.depth)
  173. if not path:
  174. return os.path.curdir
  175. return path
  176. def Noop(value):
  177. return value
  178. # We always want to ignore the environment when regenerating, to avoid
  179. # duplicate or changed flags in the environment at the time of regeneration.
  180. flags = ['--ignore-environment']
  181. for name, metadata in options._regeneration_metadata.iteritems():
  182. opt = metadata['opt']
  183. value = getattr(options, name)
  184. value_predicate = metadata['type'] == 'path' and FixPath or Noop
  185. action = metadata['action']
  186. env_name = metadata['env_name']
  187. if action == 'append':
  188. flags.extend(RegenerateAppendFlag(opt, value, value_predicate,
  189. env_name, options))
  190. elif action in ('store', None): # None is a synonym for 'store'.
  191. if value:
  192. flags.append(FormatOpt(opt, value_predicate(value)))
  193. elif options.use_environment and env_name and os.environ.get(env_name):
  194. flags.append(FormatOpt(opt, value_predicate(os.environ.get(env_name))))
  195. elif action in ('store_true', 'store_false'):
  196. if ((action == 'store_true' and value) or
  197. (action == 'store_false' and not value)):
  198. flags.append(opt)
  199. elif options.use_environment and env_name:
  200. print >>sys.stderr, ('Warning: environment regeneration unimplemented '
  201. 'for %s flag %r env_name %r' % (action, opt,
  202. env_name))
  203. else:
  204. print >>sys.stderr, ('Warning: regeneration unimplemented for action %r '
  205. 'flag %r' % (action, opt))
  206. return flags
  207. class RegeneratableOptionParser(optparse.OptionParser):
  208. def __init__(self):
  209. self.__regeneratable_options = {}
  210. optparse.OptionParser.__init__(self)
  211. def add_option(self, *args, **kw):
  212. """Add an option to the parser.
  213. This accepts the same arguments as OptionParser.add_option, plus the
  214. following:
  215. regenerate: can be set to False to prevent this option from being included
  216. in regeneration.
  217. env_name: name of environment variable that additional values for this
  218. option come from.
  219. type: adds type='path', to tell the regenerator that the values of
  220. this option need to be made relative to options.depth
  221. """
  222. env_name = kw.pop('env_name', None)
  223. if 'dest' in kw and kw.pop('regenerate', True):
  224. dest = kw['dest']
  225. # The path type is needed for regenerating, for optparse we can just treat
  226. # it as a string.
  227. type = kw.get('type')
  228. if type == 'path':
  229. kw['type'] = 'string'
  230. self.__regeneratable_options[dest] = {
  231. 'action': kw.get('action'),
  232. 'type': type,
  233. 'env_name': env_name,
  234. 'opt': args[0],
  235. }
  236. optparse.OptionParser.add_option(self, *args, **kw)
  237. def parse_args(self, *args):
  238. values, args = optparse.OptionParser.parse_args(self, *args)
  239. values._regeneration_metadata = self.__regeneratable_options
  240. return values, args
  241. def gyp_main(args):
  242. my_name = os.path.basename(sys.argv[0])
  243. parser = RegeneratableOptionParser()
  244. usage = 'usage: %s [options ...] [build_file ...]'
  245. parser.set_usage(usage.replace('%s', '%prog'))
  246. parser.add_option('--build', dest='configs', action='append',
  247. help='configuration for build after project generation')
  248. parser.add_option('--check', dest='check', action='store_true',
  249. help='check format of gyp files')
  250. parser.add_option('--config-dir', dest='config_dir', action='store',
  251. env_name='GYP_CONFIG_DIR', default=None,
  252. help='The location for configuration files like '
  253. 'include.gypi.')
  254. parser.add_option('-d', '--debug', dest='debug', metavar='DEBUGMODE',
  255. action='append', default=[], help='turn on a debugging '
  256. 'mode for debugging GYP. Supported modes are "variables", '
  257. '"includes" and "general" or "all" for all of them.')
  258. parser.add_option('-D', dest='defines', action='append', metavar='VAR=VAL',
  259. env_name='GYP_DEFINES',
  260. help='sets variable VAR to value VAL')
  261. parser.add_option('--depth', dest='depth', metavar='PATH', type='path',
  262. help='set DEPTH gyp variable to a relative path to PATH')
  263. parser.add_option('-f', '--format', dest='formats', action='append',
  264. env_name='GYP_GENERATORS', regenerate=False,
  265. help='output formats to generate')
  266. parser.add_option('-G', dest='generator_flags', action='append', default=[],
  267. metavar='FLAG=VAL', env_name='GYP_GENERATOR_FLAGS',
  268. help='sets generator flag FLAG to VAL')
  269. parser.add_option('--generator-output', dest='generator_output',
  270. action='store', default=None, metavar='DIR', type='path',
  271. env_name='GYP_GENERATOR_OUTPUT',
  272. help='puts generated build files under DIR')
  273. parser.add_option('--ignore-environment', dest='use_environment',
  274. action='store_false', default=True, regenerate=False,
  275. help='do not read options from environment variables')
  276. parser.add_option('-I', '--include', dest='includes', action='append',
  277. metavar='INCLUDE', type='path',
  278. help='files to include in all loaded .gyp files')
  279. # --no-circular-check disables the check for circular relationships between
  280. # .gyp files. These relationships should not exist, but they've only been
  281. # observed to be harmful with the Xcode generator. Chromium's .gyp files
  282. # currently have some circular relationships on non-Mac platforms, so this
  283. # option allows the strict behavior to be used on Macs and the lenient
  284. # behavior to be used elsewhere.
  285. # TODO(mark): Remove this option when http://crbug.com/35878 is fixed.
  286. parser.add_option('--no-circular-check', dest='circular_check',
  287. action='store_false', default=True, regenerate=False,
  288. help="don't check for circular relationships between files")
  289. # --no-duplicate-basename-check disables the check for duplicate basenames
  290. # in a static_library/shared_library project. Visual C++ 2008 generator
  291. # doesn't support this configuration. Libtool on Mac also generates warnings
  292. # when duplicate basenames are passed into Make generator on Mac.
  293. # TODO(yukawa): Remove this option when these legacy generators are
  294. # deprecated.
  295. parser.add_option('--no-duplicate-basename-check',
  296. dest='duplicate_basename_check', action='store_false',
  297. default=True, regenerate=False,
  298. help="don't check for duplicate basenames")
  299. parser.add_option('--no-parallel', action='store_true', default=False,
  300. help='Disable multiprocessing')
  301. parser.add_option('-S', '--suffix', dest='suffix', default='',
  302. help='suffix to add to generated files')
  303. parser.add_option('--toplevel-dir', dest='toplevel_dir', action='store',
  304. default=None, metavar='DIR', type='path',
  305. help='directory to use as the root of the source tree')
  306. parser.add_option('-R', '--root-target', dest='root_targets',
  307. action='append', metavar='TARGET',
  308. help='include only TARGET and its deep dependencies')
  309. options, build_files_arg = parser.parse_args(args)
  310. build_files = build_files_arg
  311. # Set up the configuration directory (defaults to ~/.gyp)
  312. if not options.config_dir:
  313. home = None
  314. home_dot_gyp = None
  315. if options.use_environment:
  316. home_dot_gyp = os.environ.get('GYP_CONFIG_DIR', None)
  317. if home_dot_gyp:
  318. home_dot_gyp = os.path.expanduser(home_dot_gyp)
  319. if not home_dot_gyp:
  320. home_vars = ['HOME']
  321. if sys.platform in ('cygwin', 'win32'):
  322. home_vars.append('USERPROFILE')
  323. for home_var in home_vars:
  324. home = os.getenv(home_var)
  325. if home != None:
  326. home_dot_gyp = os.path.join(home, '.gyp')
  327. if not os.path.exists(home_dot_gyp):
  328. home_dot_gyp = None
  329. else:
  330. break
  331. else:
  332. home_dot_gyp = os.path.expanduser(options.config_dir)
  333. if home_dot_gyp and not os.path.exists(home_dot_gyp):
  334. home_dot_gyp = None
  335. if not options.formats:
  336. # If no format was given on the command line, then check the env variable.
  337. generate_formats = []
  338. if options.use_environment:
  339. generate_formats = os.environ.get('GYP_GENERATORS', [])
  340. if generate_formats:
  341. generate_formats = re.split(r'[\s,]', generate_formats)
  342. if generate_formats:
  343. options.formats = generate_formats
  344. else:
  345. # Nothing in the variable, default based on platform.
  346. if sys.platform == 'darwin':
  347. options.formats = ['xcode']
  348. elif sys.platform in ('win32', 'cygwin'):
  349. options.formats = ['msvs']
  350. else:
  351. options.formats = ['make']
  352. if not options.generator_output and options.use_environment:
  353. g_o = os.environ.get('GYP_GENERATOR_OUTPUT')
  354. if g_o:
  355. options.generator_output = g_o
  356. options.parallel = not options.no_parallel
  357. for mode in options.debug:
  358. gyp.debug[mode] = 1
  359. # Do an extra check to avoid work when we're not debugging.
  360. if DEBUG_GENERAL in gyp.debug:
  361. DebugOutput(DEBUG_GENERAL, 'running with these options:')
  362. for option, value in sorted(options.__dict__.items()):
  363. if option[0] == '_':
  364. continue
  365. if isinstance(value, basestring):
  366. DebugOutput(DEBUG_GENERAL, " %s: '%s'", option, value)
  367. else:
  368. DebugOutput(DEBUG_GENERAL, " %s: %s", option, value)
  369. if not build_files:
  370. build_files = FindBuildFiles()
  371. if not build_files:
  372. raise GypError((usage + '\n\n%s: error: no build_file') %
  373. (my_name, my_name))
  374. # TODO(mark): Chromium-specific hack!
  375. # For Chromium, the gyp "depth" variable should always be a relative path
  376. # to Chromium's top-level "src" directory. If no depth variable was set
  377. # on the command line, try to find a "src" directory by looking at the
  378. # absolute path to each build file's directory. The first "src" component
  379. # found will be treated as though it were the path used for --depth.
  380. if not options.depth:
  381. for build_file in build_files:
  382. build_file_dir = os.path.abspath(os.path.dirname(build_file))
  383. build_file_dir_components = build_file_dir.split(os.path.sep)
  384. components_len = len(build_file_dir_components)
  385. for index in xrange(components_len - 1, -1, -1):
  386. if build_file_dir_components[index] == 'src':
  387. options.depth = os.path.sep.join(build_file_dir_components)
  388. break
  389. del build_file_dir_components[index]
  390. # If the inner loop found something, break without advancing to another
  391. # build file.
  392. if options.depth:
  393. break
  394. if not options.depth:
  395. raise GypError('Could not automatically locate src directory. This is'
  396. 'a temporary Chromium feature that will be removed. Use'
  397. '--depth as a workaround.')
  398. # If toplevel-dir is not set, we assume that depth is the root of our source
  399. # tree.
  400. if not options.toplevel_dir:
  401. options.toplevel_dir = options.depth
  402. # -D on the command line sets variable defaults - D isn't just for define,
  403. # it's for default. Perhaps there should be a way to force (-F?) a
  404. # variable's value so that it can't be overridden by anything else.
  405. cmdline_default_variables = {}
  406. defines = []
  407. if options.use_environment:
  408. defines += ShlexEnv('GYP_DEFINES')
  409. if options.defines:
  410. defines += options.defines
  411. cmdline_default_variables = NameValueListToDict(defines)
  412. if DEBUG_GENERAL in gyp.debug:
  413. DebugOutput(DEBUG_GENERAL,
  414. "cmdline_default_variables: %s", cmdline_default_variables)
  415. # Set up includes.
  416. includes = []
  417. # If ~/.gyp/include.gypi exists, it'll be forcibly included into every
  418. # .gyp file that's loaded, before anything else is included.
  419. if home_dot_gyp != None:
  420. default_include = os.path.join(home_dot_gyp, 'include.gypi')
  421. if os.path.exists(default_include):
  422. print 'Using overrides found in ' + default_include
  423. includes.append(default_include)
  424. # Command-line --include files come after the default include.
  425. if options.includes:
  426. includes.extend(options.includes)
  427. # Generator flags should be prefixed with the target generator since they
  428. # are global across all generator runs.
  429. gen_flags = []
  430. if options.use_environment:
  431. gen_flags += ShlexEnv('GYP_GENERATOR_FLAGS')
  432. if options.generator_flags:
  433. gen_flags += options.generator_flags
  434. generator_flags = NameValueListToDict(gen_flags)
  435. if DEBUG_GENERAL in gyp.debug.keys():
  436. DebugOutput(DEBUG_GENERAL, "generator_flags: %s", generator_flags)
  437. # Generate all requested formats (use a set in case we got one format request
  438. # twice)
  439. for format in set(options.formats):
  440. params = {'options': options,
  441. 'build_files': build_files,
  442. 'generator_flags': generator_flags,
  443. 'cwd': os.getcwd(),
  444. 'build_files_arg': build_files_arg,
  445. 'gyp_binary': sys.argv[0],
  446. 'home_dot_gyp': home_dot_gyp,
  447. 'parallel': options.parallel,
  448. 'root_targets': options.root_targets,
  449. 'target_arch': cmdline_default_variables.get('target_arch', '')}
  450. # Start with the default variables from the command line.
  451. [generator, flat_list, targets, data] = Load(
  452. build_files, format, cmdline_default_variables, includes, options.depth,
  453. params, options.check, options.circular_check,
  454. options.duplicate_basename_check)
  455. # TODO(mark): Pass |data| for now because the generator needs a list of
  456. # build files that came in. In the future, maybe it should just accept
  457. # a list, and not the whole data dict.
  458. # NOTE: flat_list is the flattened dependency graph specifying the order
  459. # that targets may be built. Build systems that operate serially or that
  460. # need to have dependencies defined before dependents reference them should
  461. # generate targets in the order specified in flat_list.
  462. generator.GenerateOutput(flat_list, targets, data, params)
  463. if options.configs:
  464. valid_configs = targets[flat_list[0]]['configurations'].keys()
  465. for conf in options.configs:
  466. if conf not in valid_configs:
  467. raise GypError('Invalid config specified via --build: %s' % conf)
  468. generator.PerformBuild(data, options.configs, params)
  469. # Done
  470. return 0
  471. def main(args):
  472. try:
  473. return gyp_main(args)
  474. except GypError, e:
  475. sys.stderr.write("gyp: %s\n" % e)
  476. return 1
  477. # NOTE: setuptools generated console_scripts calls function with no arguments
  478. def script_main():
  479. return main(sys.argv[1:])
  480. if __name__ == '__main__':
  481. sys.exit(script_main())