"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode.py" (8 Mar 2017, 58344 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) 2012 Google Inc. All rights reserved.
    2 # Use of this source code is governed by a BSD-style license that can be
    3 # found in the LICENSE file.
    4 
    5 import filecmp
    6 import gyp.common
    7 import gyp.xcodeproj_file
    8 import gyp.xcode_ninja
    9 import errno
   10 import os
   11 import sys
   12 import posixpath
   13 import re
   14 import shutil
   15 import subprocess
   16 import tempfile
   17 
   18 
   19 # Project files generated by this module will use _intermediate_var as a
   20 # custom Xcode setting whose value is a DerivedSources-like directory that's
   21 # project-specific and configuration-specific.  The normal choice,
   22 # DERIVED_FILE_DIR, is target-specific, which is thought to be too restrictive
   23 # as it is likely that multiple targets within a single project file will want
   24 # to access the same set of generated files.  The other option,
   25 # PROJECT_DERIVED_FILE_DIR, is unsuitable because while it is project-specific,
   26 # it is not configuration-specific.  INTERMEDIATE_DIR is defined as
   27 # $(PROJECT_DERIVED_FILE_DIR)/$(CONFIGURATION).
   28 _intermediate_var = 'INTERMEDIATE_DIR'
   29 
   30 # SHARED_INTERMEDIATE_DIR is the same, except that it is shared among all
   31 # targets that share the same BUILT_PRODUCTS_DIR.
   32 _shared_intermediate_var = 'SHARED_INTERMEDIATE_DIR'
   33 
   34 _library_search_paths_var = 'LIBRARY_SEARCH_PATHS'
   35 
   36 generator_default_variables = {
   37   'EXECUTABLE_PREFIX': '',
   38   'EXECUTABLE_SUFFIX': '',
   39   'STATIC_LIB_PREFIX': 'lib',
   40   'SHARED_LIB_PREFIX': 'lib',
   41   'STATIC_LIB_SUFFIX': '.a',
   42   'SHARED_LIB_SUFFIX': '.dylib',
   43   # INTERMEDIATE_DIR is a place for targets to build up intermediate products.
   44   # It is specific to each build environment.  It is only guaranteed to exist
   45   # and be constant within the context of a project, corresponding to a single
   46   # input file.  Some build environments may allow their intermediate directory
   47   # to be shared on a wider scale, but this is not guaranteed.
   48   'INTERMEDIATE_DIR': '$(%s)' % _intermediate_var,
   49   'OS': 'mac',
   50   'PRODUCT_DIR': '$(BUILT_PRODUCTS_DIR)',
   51   'LIB_DIR': '$(BUILT_PRODUCTS_DIR)',
   52   'RULE_INPUT_ROOT': '$(INPUT_FILE_BASE)',
   53   'RULE_INPUT_EXT': '$(INPUT_FILE_SUFFIX)',
   54   'RULE_INPUT_NAME': '$(INPUT_FILE_NAME)',
   55   'RULE_INPUT_PATH': '$(INPUT_FILE_PATH)',
   56   'RULE_INPUT_DIRNAME': '$(INPUT_FILE_DIRNAME)',
   57   'SHARED_INTERMEDIATE_DIR': '$(%s)' % _shared_intermediate_var,
   58   'CONFIGURATION_NAME': '$(CONFIGURATION)',
   59 }
   60 
   61 # The Xcode-specific sections that hold paths.
   62 generator_additional_path_sections = [
   63   'mac_bundle_resources',
   64   'mac_framework_headers',
   65   'mac_framework_private_headers',
   66   # 'mac_framework_dirs', input already handles _dirs endings.
   67 ]
   68 
   69 # The Xcode-specific keys that exist on targets and aren't moved down to
   70 # configurations.
   71 generator_additional_non_configuration_keys = [
   72   'ios_app_extension',
   73   'ios_watch_app',
   74   'ios_watchkit_extension',
   75   'mac_bundle',
   76   'mac_bundle_resources',
   77   'mac_framework_headers',
   78   'mac_framework_private_headers',
   79   'mac_xctest_bundle',
   80   'xcode_create_dependents_test_runner',
   81 ]
   82 
   83 # We want to let any rules apply to files that are resources also.
   84 generator_extra_sources_for_rules = [
   85   'mac_bundle_resources',
   86   'mac_framework_headers',
   87   'mac_framework_private_headers',
   88 ]
   89 
   90 generator_filelist_paths = None
   91 
   92 # Xcode's standard set of library directories, which don't need to be duplicated
   93 # in LIBRARY_SEARCH_PATHS. This list is not exhaustive, but that's okay.
   94 xcode_standard_library_dirs = frozenset([
   95   '$(SDKROOT)/usr/lib',
   96   '$(SDKROOT)/usr/local/lib',
   97 ])
   98 
   99 def CreateXCConfigurationList(configuration_names):
  100   xccl = gyp.xcodeproj_file.XCConfigurationList({'buildConfigurations': []})
  101   if len(configuration_names) == 0:
  102     configuration_names = ['Default']
  103   for configuration_name in configuration_names:
  104     xcbc = gyp.xcodeproj_file.XCBuildConfiguration({
  105         'name': configuration_name})
  106     xccl.AppendProperty('buildConfigurations', xcbc)
  107   xccl.SetProperty('defaultConfigurationName', configuration_names[0])
  108   return xccl
  109 
  110 
  111 class XcodeProject(object):
  112   def __init__(self, gyp_path, path, build_file_dict):
  113     self.gyp_path = gyp_path
  114     self.path = path
  115     self.project = gyp.xcodeproj_file.PBXProject(path=path)
  116     projectDirPath = gyp.common.RelativePath(
  117                          os.path.dirname(os.path.abspath(self.gyp_path)),
  118                          os.path.dirname(path) or '.')
  119     self.project.SetProperty('projectDirPath', projectDirPath)
  120     self.project_file = \
  121         gyp.xcodeproj_file.XCProjectFile({'rootObject': self.project})
  122     self.build_file_dict = build_file_dict
  123 
  124     # TODO(mark): add destructor that cleans up self.path if created_dir is
  125     # True and things didn't complete successfully.  Or do something even
  126     # better with "try"?
  127     self.created_dir = False
  128     try:
  129       os.makedirs(self.path)
  130       self.created_dir = True
  131     except OSError, e:
  132       if e.errno != errno.EEXIST:
  133         raise
  134 
  135   def Finalize1(self, xcode_targets, serialize_all_tests):
  136     # Collect a list of all of the build configuration names used by the
  137     # various targets in the file.  It is very heavily advised to keep each
  138     # target in an entire project (even across multiple project files) using
  139     # the same set of configuration names.
  140     configurations = []
  141     for xct in self.project.GetProperty('targets'):
  142       xccl = xct.GetProperty('buildConfigurationList')
  143       xcbcs = xccl.GetProperty('buildConfigurations')
  144       for xcbc in xcbcs:
  145         name = xcbc.GetProperty('name')
  146         if name not in configurations:
  147           configurations.append(name)
  148 
  149     # Replace the XCConfigurationList attached to the PBXProject object with
  150     # a new one specifying all of the configuration names used by the various
  151     # targets.
  152     try:
  153       xccl = CreateXCConfigurationList(configurations)
  154       self.project.SetProperty('buildConfigurationList', xccl)
  155     except:
  156       sys.stderr.write("Problem with gyp file %s\n" % self.gyp_path)
  157       raise
  158 
  159     # The need for this setting is explained above where _intermediate_var is
  160     # defined.  The comments below about wanting to avoid project-wide build
  161     # settings apply here too, but this needs to be set on a project-wide basis
  162     # so that files relative to the _intermediate_var setting can be displayed
  163     # properly in the Xcode UI.
  164     #
  165     # Note that for configuration-relative files such as anything relative to
  166     # _intermediate_var, for the purposes of UI tree view display, Xcode will
  167     # only resolve the configuration name once, when the project file is
  168     # opened.  If the active build configuration is changed, the project file
  169     # must be closed and reopened if it is desired for the tree view to update.
  170     # This is filed as Apple radar 6588391.
  171     xccl.SetBuildSetting(_intermediate_var,
  172                          '$(PROJECT_DERIVED_FILE_DIR)/$(CONFIGURATION)')
  173     xccl.SetBuildSetting(_shared_intermediate_var,
  174                          '$(SYMROOT)/DerivedSources/$(CONFIGURATION)')
  175 
  176     # Set user-specified project-wide build settings and config files.  This
  177     # is intended to be used very sparingly.  Really, almost everything should
  178     # go into target-specific build settings sections.  The project-wide
  179     # settings are only intended to be used in cases where Xcode attempts to
  180     # resolve variable references in a project context as opposed to a target
  181     # context, such as when resolving sourceTree references while building up
  182     # the tree tree view for UI display.
  183     # Any values set globally are applied to all configurations, then any
  184     # per-configuration values are applied.
  185     for xck, xcv in self.build_file_dict.get('xcode_settings', {}).iteritems():
  186       xccl.SetBuildSetting(xck, xcv)
  187     if 'xcode_config_file' in self.build_file_dict:
  188       config_ref = self.project.AddOrGetFileInRootGroup(
  189           self.build_file_dict['xcode_config_file'])
  190       xccl.SetBaseConfiguration(config_ref)
  191     build_file_configurations = self.build_file_dict.get('configurations', {})
  192     if build_file_configurations:
  193       for config_name in configurations:
  194         build_file_configuration_named = \
  195             build_file_configurations.get(config_name, {})
  196         if build_file_configuration_named:
  197           xcc = xccl.ConfigurationNamed(config_name)
  198           for xck, xcv in build_file_configuration_named.get('xcode_settings',
  199                                                              {}).iteritems():
  200             xcc.SetBuildSetting(xck, xcv)
  201           if 'xcode_config_file' in build_file_configuration_named:
  202             config_ref = self.project.AddOrGetFileInRootGroup(
  203                 build_file_configurations[config_name]['xcode_config_file'])
  204             xcc.SetBaseConfiguration(config_ref)
  205 
  206     # Sort the targets based on how they appeared in the input.
  207     # TODO(mark): Like a lot of other things here, this assumes internal
  208     # knowledge of PBXProject - in this case, of its "targets" property.
  209 
  210     # ordinary_targets are ordinary targets that are already in the project
  211     # file. run_test_targets are the targets that run unittests and should be
  212     # used for the Run All Tests target.  support_targets are the action/rule
  213     # targets used by GYP file targets, just kept for the assert check.
  214     ordinary_targets = []
  215     run_test_targets = []
  216     support_targets = []
  217 
  218     # targets is full list of targets in the project.
  219     targets = []
  220 
  221     # does the it define it's own "all"?
  222     has_custom_all = False
  223 
  224     # targets_for_all is the list of ordinary_targets that should be listed
  225     # in this project's "All" target.  It includes each non_runtest_target
  226     # that does not have suppress_wildcard set.
  227     targets_for_all = []
  228 
  229     for target in self.build_file_dict['targets']:
  230       target_name = target['target_name']
  231       toolset = target['toolset']
  232       qualified_target = gyp.common.QualifiedTarget(self.gyp_path, target_name,
  233                                                     toolset)
  234       xcode_target = xcode_targets[qualified_target]
  235       # Make sure that the target being added to the sorted list is already in
  236       # the unsorted list.
  237       assert xcode_target in self.project._properties['targets']
  238       targets.append(xcode_target)
  239       ordinary_targets.append(xcode_target)
  240       if xcode_target.support_target:
  241         support_targets.append(xcode_target.support_target)
  242         targets.append(xcode_target.support_target)
  243 
  244       if not int(target.get('suppress_wildcard', False)):
  245         targets_for_all.append(xcode_target)
  246 
  247       if target_name.lower() == 'all':
  248         has_custom_all = True;
  249 
  250       # If this target has a 'run_as' attribute, add its target to the
  251       # targets, and add it to the test targets.
  252       if target.get('run_as'):
  253         # Make a target to run something.  It should have one
  254         # dependency, the parent xcode target.
  255         xccl = CreateXCConfigurationList(configurations)
  256         run_target = gyp.xcodeproj_file.PBXAggregateTarget({
  257               'name':                   'Run ' + target_name,
  258               'productName':            xcode_target.GetProperty('productName'),
  259               'buildConfigurationList': xccl,
  260             },
  261             parent=self.project)
  262         run_target.AddDependency(xcode_target)
  263 
  264         command = target['run_as']
  265         script = ''
  266         if command.get('working_directory'):
  267           script = script + 'cd "%s"\n' % \
  268                    gyp.xcodeproj_file.ConvertVariablesToShellSyntax(
  269                        command.get('working_directory'))
  270 
  271         if command.get('environment'):
  272           script = script + "\n".join(
  273             ['export %s="%s"' %
  274              (key, gyp.xcodeproj_file.ConvertVariablesToShellSyntax(val))
  275              for (key, val) in command.get('environment').iteritems()]) + "\n"
  276 
  277         # Some test end up using sockets, files on disk, etc. and can get
  278         # confused if more then one test runs at a time.  The generator
  279         # flag 'xcode_serialize_all_test_runs' controls the forcing of all
  280         # tests serially.  It defaults to True.  To get serial runs this
  281         # little bit of python does the same as the linux flock utility to
  282         # make sure only one runs at a time.
  283         command_prefix = ''
  284         if serialize_all_tests:
  285           command_prefix = \
  286 """python -c "import fcntl, subprocess, sys
  287 file = open('$TMPDIR/GYP_serialize_test_runs', 'a')
  288 fcntl.flock(file.fileno(), fcntl.LOCK_EX)
  289 sys.exit(subprocess.call(sys.argv[1:]))" """
  290 
  291         # If we were unable to exec for some reason, we want to exit
  292         # with an error, and fixup variable references to be shell
  293         # syntax instead of xcode syntax.
  294         script = script + 'exec ' + command_prefix + '%s\nexit 1\n' % \
  295                  gyp.xcodeproj_file.ConvertVariablesToShellSyntax(
  296                      gyp.common.EncodePOSIXShellList(command.get('action')))
  297 
  298         ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({
  299               'shellScript':      script,
  300               'showEnvVarsInLog': 0,
  301             })
  302         run_target.AppendProperty('buildPhases', ssbp)
  303 
  304         # Add the run target to the project file.
  305         targets.append(run_target)
  306         run_test_targets.append(run_target)
  307         xcode_target.test_runner = run_target
  308 
  309 
  310     # Make sure that the list of targets being replaced is the same length as
  311     # the one replacing it, but allow for the added test runner targets.
  312     assert len(self.project._properties['targets']) == \
  313       len(ordinary_targets) + len(support_targets)
  314 
  315     self.project._properties['targets'] = targets
  316 
  317     # Get rid of unnecessary levels of depth in groups like the Source group.
  318     self.project.RootGroupsTakeOverOnlyChildren(True)
  319 
  320     # Sort the groups nicely.  Do this after sorting the targets, because the
  321     # Products group is sorted based on the order of the targets.
  322     self.project.SortGroups()
  323 
  324     # Create an "All" target if there's more than one target in this project
  325     # file and the project didn't define its own "All" target.  Put a generated
  326     # "All" target first so that people opening up the project for the first
  327     # time will build everything by default.
  328     if len(targets_for_all) > 1 and not has_custom_all:
  329       xccl = CreateXCConfigurationList(configurations)
  330       all_target = gyp.xcodeproj_file.PBXAggregateTarget(
  331           {
  332             'buildConfigurationList': xccl,
  333             'name':                   'All',
  334           },
  335           parent=self.project)
  336 
  337       for target in targets_for_all:
  338         all_target.AddDependency(target)
  339 
  340       # TODO(mark): This is evil because it relies on internal knowledge of
  341       # PBXProject._properties.  It's important to get the "All" target first,
  342       # though.
  343       self.project._properties['targets'].insert(0, all_target)
  344 
  345     # The same, but for run_test_targets.
  346     if len(run_test_targets) > 1:
  347       xccl = CreateXCConfigurationList(configurations)
  348       run_all_tests_target = gyp.xcodeproj_file.PBXAggregateTarget(
  349           {
  350             'buildConfigurationList': xccl,
  351             'name':                   'Run All Tests',
  352           },
  353           parent=self.project)
  354       for run_test_target in run_test_targets:
  355         run_all_tests_target.AddDependency(run_test_target)
  356 
  357       # Insert after the "All" target, which must exist if there is more than
  358       # one run_test_target.
  359       self.project._properties['targets'].insert(1, run_all_tests_target)
  360 
  361   def Finalize2(self, xcode_targets, xcode_target_to_target_dict):
  362     # Finalize2 needs to happen in a separate step because the process of
  363     # updating references to other projects depends on the ordering of targets
  364     # within remote project files.  Finalize1 is responsible for sorting duty,
  365     # and once all project files are sorted, Finalize2 can come in and update
  366     # these references.
  367 
  368     # To support making a "test runner" target that will run all the tests
  369     # that are direct dependents of any given target, we look for
  370     # xcode_create_dependents_test_runner being set on an Aggregate target,
  371     # and generate a second target that will run the tests runners found under
  372     # the marked target.
  373     for bf_tgt in self.build_file_dict['targets']:
  374       if int(bf_tgt.get('xcode_create_dependents_test_runner', 0)):
  375         tgt_name = bf_tgt['target_name']
  376         toolset = bf_tgt['toolset']
  377         qualified_target = gyp.common.QualifiedTarget(self.gyp_path,
  378                                                       tgt_name, toolset)
  379         xcode_target = xcode_targets[qualified_target]
  380         if isinstance(xcode_target, gyp.xcodeproj_file.PBXAggregateTarget):
  381           # Collect all the run test targets.
  382           all_run_tests = []
  383           pbxtds = xcode_target.GetProperty('dependencies')
  384           for pbxtd in pbxtds:
  385             pbxcip = pbxtd.GetProperty('targetProxy')
  386             dependency_xct = pbxcip.GetProperty('remoteGlobalIDString')
  387             if hasattr(dependency_xct, 'test_runner'):
  388               all_run_tests.append(dependency_xct.test_runner)
  389 
  390           # Directly depend on all the runners as they depend on the target
  391           # that builds them.
  392           if len(all_run_tests) > 0:
  393             run_all_target = gyp.xcodeproj_file.PBXAggregateTarget({
  394                   'name':        'Run %s Tests' % tgt_name,
  395                   'productName': tgt_name,
  396                 },
  397                 parent=self.project)
  398             for run_test_target in all_run_tests:
  399               run_all_target.AddDependency(run_test_target)
  400 
  401             # Insert the test runner after the related target.
  402             idx = self.project._properties['targets'].index(xcode_target)
  403             self.project._properties['targets'].insert(idx + 1, run_all_target)
  404 
  405     # Update all references to other projects, to make sure that the lists of
  406     # remote products are complete.  Otherwise, Xcode will fill them in when
  407     # it opens the project file, which will result in unnecessary diffs.
  408     # TODO(mark): This is evil because it relies on internal knowledge of
  409     # PBXProject._other_pbxprojects.
  410     for other_pbxproject in self.project._other_pbxprojects.keys():
  411       self.project.AddOrGetProjectReference(other_pbxproject)
  412 
  413     self.project.SortRemoteProductReferences()
  414 
  415     # Give everything an ID.
  416     self.project_file.ComputeIDs()
  417 
  418     # Make sure that no two objects in the project file have the same ID.  If
  419     # multiple objects wind up with the same ID, upon loading the file, Xcode
  420     # will only recognize one object (the last one in the file?) and the
  421     # results are unpredictable.
  422     self.project_file.EnsureNoIDCollisions()
  423 
  424   def Write(self):
  425     # Write the project file to a temporary location first.  Xcode watches for
  426     # changes to the project file and presents a UI sheet offering to reload
  427     # the project when it does change.  However, in some cases, especially when
  428     # multiple projects are open or when Xcode is busy, things don't work so
  429     # seamlessly.  Sometimes, Xcode is able to detect that a project file has
  430     # changed but can't unload it because something else is referencing it.
  431     # To mitigate this problem, and to avoid even having Xcode present the UI
  432     # sheet when an open project is rewritten for inconsequential changes, the
  433     # project file is written to a temporary file in the xcodeproj directory
  434     # first.  The new temporary file is then compared to the existing project
  435     # file, if any.  If they differ, the new file replaces the old; otherwise,
  436     # the new project file is simply deleted.  Xcode properly detects a file
  437     # being renamed over an open project file as a change and so it remains
  438     # able to present the "project file changed" sheet under this system.
  439     # Writing to a temporary file first also avoids the possible problem of
  440     # Xcode rereading an incomplete project file.
  441     (output_fd, new_pbxproj_path) = \
  442         tempfile.mkstemp(suffix='.tmp', prefix='project.pbxproj.gyp.',
  443                          dir=self.path)
  444 
  445     try:
  446       output_file = os.fdopen(output_fd, 'wb')
  447 
  448       self.project_file.Print(output_file)
  449       output_file.close()
  450 
  451       pbxproj_path = os.path.join(self.path, 'project.pbxproj')
  452 
  453       same = False
  454       try:
  455         same = filecmp.cmp(pbxproj_path, new_pbxproj_path, False)
  456       except OSError, e:
  457         if e.errno != errno.ENOENT:
  458           raise
  459 
  460       if same:
  461         # The new file is identical to the old one, just get rid of the new
  462         # one.
  463         os.unlink(new_pbxproj_path)
  464       else:
  465         # The new file is different from the old one, or there is no old one.
  466         # Rename the new file to the permanent name.
  467         #
  468         # tempfile.mkstemp uses an overly restrictive mode, resulting in a
  469         # file that can only be read by the owner, regardless of the umask.
  470         # There's no reason to not respect the umask here, which means that
  471         # an extra hoop is required to fetch it and reset the new file's mode.
  472         #
  473         # No way to get the umask without setting a new one?  Set a safe one
  474         # and then set it back to the old value.
  475         umask = os.umask(077)
  476         os.umask(umask)
  477 
  478         os.chmod(new_pbxproj_path, 0666 & ~umask)
  479         os.rename(new_pbxproj_path, pbxproj_path)
  480 
  481     except Exception:
  482       # Don't leave turds behind.  In fact, if this code was responsible for
  483       # creating the xcodeproj directory, get rid of that too.
  484       os.unlink(new_pbxproj_path)
  485       if self.created_dir:
  486         shutil.rmtree(self.path, True)
  487       raise
  488 
  489 
  490 def AddSourceToTarget(source, type, pbxp, xct):
  491   # TODO(mark): Perhaps source_extensions and library_extensions can be made a
  492   # little bit fancier.
  493   source_extensions = ['c', 'cc', 'cpp', 'cxx', 'm', 'mm', 's', 'swift']
  494 
  495   # .o is conceptually more of a "source" than a "library," but Xcode thinks
  496   # of "sources" as things to compile and "libraries" (or "frameworks") as
  497   # things to link with. Adding an object file to an Xcode target's frameworks
  498   # phase works properly.
  499   library_extensions = ['a', 'dylib', 'framework', 'o']
  500 
  501   basename = posixpath.basename(source)
  502   (root, ext) = posixpath.splitext(basename)
  503   if ext:
  504     ext = ext[1:].lower()
  505 
  506   if ext in source_extensions and type != 'none':
  507     xct.SourcesPhase().AddFile(source)
  508   elif ext in library_extensions and type != 'none':
  509     xct.FrameworksPhase().AddFile(source)
  510   else:
  511     # Files that aren't added to a sources or frameworks build phase can still
  512     # go into the project file, just not as part of a build phase.
  513     pbxp.AddOrGetFileInRootGroup(source)
  514 
  515 
  516 def AddResourceToTarget(resource, pbxp, xct):
  517   # TODO(mark): Combine with AddSourceToTarget above?  Or just inline this call
  518   # where it's used.
  519   xct.ResourcesPhase().AddFile(resource)
  520 
  521 
  522 def AddHeaderToTarget(header, pbxp, xct, is_public):
  523   # TODO(mark): Combine with AddSourceToTarget above?  Or just inline this call
  524   # where it's used.
  525   settings = '{ATTRIBUTES = (%s, ); }' % ('Private', 'Public')[is_public]
  526   xct.HeadersPhase().AddFile(header, settings)
  527 
  528 
  529 _xcode_variable_re = re.compile(r'(\$\((.*?)\))')
  530 def ExpandXcodeVariables(string, expansions):
  531   """Expands Xcode-style $(VARIABLES) in string per the expansions dict.
  532 
  533   In some rare cases, it is appropriate to expand Xcode variables when a
  534   project file is generated.  For any substring $(VAR) in string, if VAR is a
  535   key in the expansions dict, $(VAR) will be replaced with expansions[VAR].
  536   Any $(VAR) substring in string for which VAR is not a key in the expansions
  537   dict will remain in the returned string.
  538   """
  539 
  540   matches = _xcode_variable_re.findall(string)
  541   if matches == None:
  542     return string
  543 
  544   matches.reverse()
  545   for match in matches:
  546     (to_replace, variable) = match
  547     if not variable in expansions:
  548       continue
  549 
  550     replacement = expansions[variable]
  551     string = re.sub(re.escape(to_replace), replacement, string)
  552 
  553   return string
  554 
  555 
  556 _xcode_define_re = re.compile(r'([\\\"\' ])')
  557 def EscapeXcodeDefine(s):
  558   """We must escape the defines that we give to XCode so that it knows not to
  559      split on spaces and to respect backslash and quote literals. However, we
  560      must not quote the define, or Xcode will incorrectly intepret variables
  561      especially $(inherited)."""
  562   return re.sub(_xcode_define_re, r'\\\1', s)
  563 
  564 
  565 def PerformBuild(data, configurations, params):
  566   options = params['options']
  567 
  568   for build_file, build_file_dict in data.iteritems():
  569     (build_file_root, build_file_ext) = os.path.splitext(build_file)
  570     if build_file_ext != '.gyp':
  571       continue
  572     xcodeproj_path = build_file_root + options.suffix + '.xcodeproj'
  573     if options.generator_output:
  574       xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path)
  575 
  576   for config in configurations:
  577     arguments = ['xcodebuild', '-project', xcodeproj_path]
  578     arguments += ['-configuration', config]
  579     print "Building [%s]: %s" % (config, arguments)
  580     subprocess.check_call(arguments)
  581 
  582 
  583 def CalculateGeneratorInputInfo(params):
  584   toplevel = params['options'].toplevel_dir
  585   if params.get('flavor') == 'ninja':
  586     generator_dir = os.path.relpath(params['options'].generator_output or '.')
  587     output_dir = params.get('generator_flags', {}).get('output_dir', 'out')
  588     output_dir = os.path.normpath(os.path.join(generator_dir, output_dir))
  589     qualified_out_dir = os.path.normpath(os.path.join(
  590         toplevel, output_dir, 'gypfiles-xcode-ninja'))
  591   else:
  592     output_dir = os.path.normpath(os.path.join(toplevel, 'xcodebuild'))
  593     qualified_out_dir = os.path.normpath(os.path.join(
  594         toplevel, output_dir, 'gypfiles'))
  595 
  596   global generator_filelist_paths
  597   generator_filelist_paths = {
  598       'toplevel': toplevel,
  599       'qualified_out_dir': qualified_out_dir,
  600   }
  601 
  602 
  603 def GenerateOutput(target_list, target_dicts, data, params):
  604   # Optionally configure each spec to use ninja as the external builder.
  605   ninja_wrapper = params.get('flavor') == 'ninja'
  606   if ninja_wrapper:
  607     (target_list, target_dicts, data) = \
  608         gyp.xcode_ninja.CreateWrapper(target_list, target_dicts, data, params)
  609 
  610   options = params['options']
  611   generator_flags = params.get('generator_flags', {})
  612   parallel_builds = generator_flags.get('xcode_parallel_builds', True)
  613   serialize_all_tests = \
  614       generator_flags.get('xcode_serialize_all_test_runs', True)
  615   upgrade_check_project_version = \
  616       generator_flags.get('xcode_upgrade_check_project_version', None)
  617 
  618   # Format upgrade_check_project_version with leading zeros as needed.
  619   if upgrade_check_project_version:
  620     upgrade_check_project_version = str(upgrade_check_project_version)
  621     while len(upgrade_check_project_version) < 4:
  622       upgrade_check_project_version = '0' + upgrade_check_project_version
  623 
  624   skip_excluded_files = \
  625       not generator_flags.get('xcode_list_excluded_files', True)
  626   xcode_projects = {}
  627   for build_file, build_file_dict in data.iteritems():
  628     (build_file_root, build_file_ext) = os.path.splitext(build_file)
  629     if build_file_ext != '.gyp':
  630       continue
  631     xcodeproj_path = build_file_root + options.suffix + '.xcodeproj'
  632     if options.generator_output:
  633       xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path)
  634     xcp = XcodeProject(build_file, xcodeproj_path, build_file_dict)
  635     xcode_projects[build_file] = xcp
  636     pbxp = xcp.project
  637 
  638     # Set project-level attributes from multiple options
  639     project_attributes = {};
  640     if parallel_builds:
  641       project_attributes['BuildIndependentTargetsInParallel'] = 'YES'
  642     if upgrade_check_project_version:
  643       project_attributes['LastUpgradeCheck'] = upgrade_check_project_version
  644       project_attributes['LastTestingUpgradeCheck'] = \
  645           upgrade_check_project_version
  646       project_attributes['LastSwiftUpdateCheck'] = \
  647           upgrade_check_project_version
  648     pbxp.SetProperty('attributes', project_attributes)
  649 
  650     # Add gyp/gypi files to project
  651     if not generator_flags.get('standalone'):
  652       main_group = pbxp.GetProperty('mainGroup')
  653       build_group = gyp.xcodeproj_file.PBXGroup({'name': 'Build'})
  654       main_group.AppendChild(build_group)
  655       for included_file in build_file_dict['included_files']:
  656         build_group.AddOrGetFileByPath(included_file, False)
  657 
  658   xcode_targets = {}
  659   xcode_target_to_target_dict = {}
  660   for qualified_target in target_list:
  661     [build_file, target_name, toolset] = \
  662         gyp.common.ParseQualifiedTarget(qualified_target)
  663 
  664     spec = target_dicts[qualified_target]
  665     if spec['toolset'] != 'target':
  666       raise Exception(
  667           'Multiple toolsets not supported in xcode build (target %s)' %
  668           qualified_target)
  669     configuration_names = [spec['default_configuration']]
  670     for configuration_name in sorted(spec['configurations'].keys()):
  671       if configuration_name not in configuration_names:
  672         configuration_names.append(configuration_name)
  673     xcp = xcode_projects[build_file]
  674     pbxp = xcp.project
  675 
  676     # Set up the configurations for the target according to the list of names
  677     # supplied.
  678     xccl = CreateXCConfigurationList(configuration_names)
  679 
  680     # Create an XCTarget subclass object for the target. The type with
  681     # "+bundle" appended will be used if the target has "mac_bundle" set.
  682     # loadable_modules not in a mac_bundle are mapped to
  683     # com.googlecode.gyp.xcode.bundle, a pseudo-type that xcode.py interprets
  684     # to create a single-file mh_bundle.
  685     _types = {
  686       'executable':                  'com.apple.product-type.tool',
  687       'loadable_module':             'com.googlecode.gyp.xcode.bundle',
  688       'shared_library':              'com.apple.product-type.library.dynamic',
  689       'static_library':              'com.apple.product-type.library.static',
  690       'mac_kernel_extension':        'com.apple.product-type.kernel-extension',
  691       'executable+bundle':           'com.apple.product-type.application',
  692       'loadable_module+bundle':      'com.apple.product-type.bundle',
  693       'loadable_module+xctest':      'com.apple.product-type.bundle.unit-test',
  694       'shared_library+bundle':       'com.apple.product-type.framework',
  695       'executable+extension+bundle': 'com.apple.product-type.app-extension',
  696       'executable+watch+extension+bundle':
  697           'com.apple.product-type.watchkit-extension',
  698       'executable+watch+bundle':
  699           'com.apple.product-type.application.watchapp',
  700       'mac_kernel_extension+bundle': 'com.apple.product-type.kernel-extension',
  701     }
  702 
  703     target_properties = {
  704       'buildConfigurationList': xccl,
  705       'name':                   target_name,
  706     }
  707 
  708     type = spec['type']
  709     is_xctest = int(spec.get('mac_xctest_bundle', 0))
  710     is_bundle = int(spec.get('mac_bundle', 0)) or is_xctest
  711     is_app_extension = int(spec.get('ios_app_extension', 0))
  712     is_watchkit_extension = int(spec.get('ios_watchkit_extension', 0))
  713     is_watch_app = int(spec.get('ios_watch_app', 0))
  714     if type != 'none':
  715       type_bundle_key = type
  716       if is_xctest:
  717         type_bundle_key += '+xctest'
  718         assert type == 'loadable_module', (
  719             'mac_xctest_bundle targets must have type loadable_module '
  720             '(target %s)' % target_name)
  721       elif is_app_extension:
  722         assert is_bundle, ('ios_app_extension flag requires mac_bundle '
  723             '(target %s)' % target_name)
  724         type_bundle_key += '+extension+bundle'
  725       elif is_watchkit_extension:
  726         assert is_bundle, ('ios_watchkit_extension flag requires mac_bundle '
  727             '(target %s)' % target_name)
  728         type_bundle_key += '+watch+extension+bundle'
  729       elif is_watch_app:
  730         assert is_bundle, ('ios_watch_app flag requires mac_bundle '
  731             '(target %s)' % target_name)
  732         type_bundle_key += '+watch+bundle'
  733       elif is_bundle:
  734         type_bundle_key += '+bundle'
  735 
  736       xctarget_type = gyp.xcodeproj_file.PBXNativeTarget
  737       try:
  738         target_properties['productType'] = _types[type_bundle_key]
  739       except KeyError, e:
  740         gyp.common.ExceptionAppend(e, "-- unknown product type while "
  741                                    "writing target %s" % target_name)
  742         raise
  743     else:
  744       xctarget_type = gyp.xcodeproj_file.PBXAggregateTarget
  745       assert not is_bundle, (
  746           'mac_bundle targets cannot have type none (target "%s")' %
  747           target_name)
  748       assert not is_xctest, (
  749           'mac_xctest_bundle targets cannot have type none (target "%s")' %
  750           target_name)
  751 
  752     target_product_name = spec.get('product_name')
  753     if target_product_name is not None:
  754       target_properties['productName'] = target_product_name
  755 
  756     xct = xctarget_type(target_properties, parent=pbxp,
  757                         force_outdir=spec.get('product_dir'),
  758                         force_prefix=spec.get('product_prefix'),
  759                         force_extension=spec.get('product_extension'))
  760     pbxp.AppendProperty('targets', xct)
  761     xcode_targets[qualified_target] = xct
  762     xcode_target_to_target_dict[xct] = spec
  763 
  764     spec_actions = spec.get('actions', [])
  765     spec_rules = spec.get('rules', [])
  766 
  767     # Xcode has some "issues" with checking dependencies for the "Compile
  768     # sources" step with any source files/headers generated by actions/rules.
  769     # To work around this, if a target is building anything directly (not
  770     # type "none"), then a second target is used to run the GYP actions/rules
  771     # and is made a dependency of this target.  This way the work is done
  772     # before the dependency checks for what should be recompiled.
  773     support_xct = None
  774     # The Xcode "issues" don't affect xcode-ninja builds, since the dependency
  775     # logic all happens in ninja.  Don't bother creating the extra targets in
  776     # that case.
  777     if type != 'none' and (spec_actions or spec_rules) and not ninja_wrapper:
  778       support_xccl = CreateXCConfigurationList(configuration_names);
  779       support_target_suffix = generator_flags.get(
  780           'support_target_suffix', ' Support')
  781       support_target_properties = {
  782         'buildConfigurationList': support_xccl,
  783         'name':                   target_name + support_target_suffix,
  784       }
  785       if target_product_name:
  786         support_target_properties['productName'] = \
  787             target_product_name + ' Support'
  788       support_xct = \
  789           gyp.xcodeproj_file.PBXAggregateTarget(support_target_properties,
  790                                                 parent=pbxp)
  791       pbxp.AppendProperty('targets', support_xct)
  792       xct.AddDependency(support_xct)
  793     # Hang the support target off the main target so it can be tested/found
  794     # by the generator during Finalize.
  795     xct.support_target = support_xct
  796 
  797     prebuild_index = 0
  798 
  799     # Add custom shell script phases for "actions" sections.
  800     for action in spec_actions:
  801       # There's no need to write anything into the script to ensure that the
  802       # output directories already exist, because Xcode will look at the
  803       # declared outputs and automatically ensure that they exist for us.
  804 
  805       # Do we have a message to print when this action runs?
  806       message = action.get('message')
  807       if message:
  808         message = 'echo note: ' + gyp.common.EncodePOSIXShellArgument(message)
  809       else:
  810         message = ''
  811 
  812       # Turn the list into a string that can be passed to a shell.
  813       action_string = gyp.common.EncodePOSIXShellList(action['action'])
  814 
  815       # Convert Xcode-type variable references to sh-compatible environment
  816       # variable references.
  817       message_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax(message)
  818       action_string_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax(
  819         action_string)
  820 
  821       script = ''
  822       # Include the optional message
  823       if message_sh:
  824         script += message_sh + '\n'
  825       # Be sure the script runs in exec, and that if exec fails, the script
  826       # exits signalling an error.
  827       script += 'exec ' + action_string_sh + '\nexit 1\n'
  828       ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({
  829             'inputPaths': action['inputs'],
  830             'name': 'Action "' + action['action_name'] + '"',
  831             'outputPaths': action['outputs'],
  832             'shellScript': script,
  833             'showEnvVarsInLog': 0,
  834           })
  835 
  836       if support_xct:
  837         support_xct.AppendProperty('buildPhases', ssbp)
  838       else:
  839         # TODO(mark): this assumes too much knowledge of the internals of
  840         # xcodeproj_file; some of these smarts should move into xcodeproj_file
  841         # itself.
  842         xct._properties['buildPhases'].insert(prebuild_index, ssbp)
  843         prebuild_index = prebuild_index + 1
  844 
  845       # TODO(mark): Should verify that at most one of these is specified.
  846       if int(action.get('process_outputs_as_sources', False)):
  847         for output in action['outputs']:
  848           AddSourceToTarget(output, type, pbxp, xct)
  849 
  850       if int(action.get('process_outputs_as_mac_bundle_resources', False)):
  851         for output in action['outputs']:
  852           AddResourceToTarget(output, pbxp, xct)
  853 
  854     # tgt_mac_bundle_resources holds the list of bundle resources so
  855     # the rule processing can check against it.
  856     if is_bundle:
  857       tgt_mac_bundle_resources = spec.get('mac_bundle_resources', [])
  858     else:
  859       tgt_mac_bundle_resources = []
  860 
  861     # Add custom shell script phases driving "make" for "rules" sections.
  862     #
  863     # Xcode's built-in rule support is almost powerful enough to use directly,
  864     # but there are a few significant deficiencies that render them unusable.
  865     # There are workarounds for some of its inadequacies, but in aggregate,
  866     # the workarounds added complexity to the generator, and some workarounds
  867     # actually require input files to be crafted more carefully than I'd like.
  868     # Consequently, until Xcode rules are made more capable, "rules" input
  869     # sections will be handled in Xcode output by shell script build phases
  870     # performed prior to the compilation phase.
  871     #
  872     # The following problems with Xcode rules were found.  The numbers are
  873     # Apple radar IDs.  I hope that these shortcomings are addressed, I really
  874     # liked having the rules handled directly in Xcode during the period that
  875     # I was prototyping this.
  876     #
  877     # 6588600 Xcode compiles custom script rule outputs too soon, compilation
  878     #         fails.  This occurs when rule outputs from distinct inputs are
  879     #         interdependent.  The only workaround is to put rules and their
  880     #         inputs in a separate target from the one that compiles the rule
  881     #         outputs.  This requires input file cooperation and it means that
  882     #         process_outputs_as_sources is unusable.
  883     # 6584932 Need to declare that custom rule outputs should be excluded from
  884     #         compilation.  A possible workaround is to lie to Xcode about a
  885     #         rule's output, giving it a dummy file it doesn't know how to
  886     #         compile.  The rule action script would need to touch the dummy.
  887     # 6584839 I need a way to declare additional inputs to a custom rule.
  888     #         A possible workaround is a shell script phase prior to
  889     #         compilation that touches a rule's primary input files if any
  890     #         would-be additional inputs are newer than the output.  Modifying
  891     #         the source tree - even just modification times - feels dirty.
  892     # 6564240 Xcode "custom script" build rules always dump all environment
  893     #         variables.  This is a low-prioroty problem and is not a
  894     #         show-stopper.
  895     rules_by_ext = {}
  896     for rule in spec_rules:
  897       rules_by_ext[rule['extension']] = rule
  898 
  899       # First, some definitions:
  900       #
  901       # A "rule source" is a file that was listed in a target's "sources"
  902       # list and will have a rule applied to it on the basis of matching the
  903       # rule's "extensions" attribute.  Rule sources are direct inputs to
  904       # rules.
  905       #
  906       # Rule definitions may specify additional inputs in their "inputs"
  907       # attribute.  These additional inputs are used for dependency tracking
  908       # purposes.
  909       #
  910       # A "concrete output" is a rule output with input-dependent variables
  911       # resolved.  For example, given a rule with:
  912       #   'extension': 'ext', 'outputs': ['$(INPUT_FILE_BASE).cc'],
  913       # if the target's "sources" list contained "one.ext" and "two.ext",
  914       # the "concrete output" for rule input "two.ext" would be "two.cc".  If
  915       # a rule specifies multiple outputs, each input file that the rule is
  916       # applied to will have the same number of concrete outputs.
  917       #
  918       # If any concrete outputs are outdated or missing relative to their
  919       # corresponding rule_source or to any specified additional input, the
  920       # rule action must be performed to generate the concrete outputs.
  921 
  922       # concrete_outputs_by_rule_source will have an item at the same index
  923       # as the rule['rule_sources'] that it corresponds to.  Each item is a
  924       # list of all of the concrete outputs for the rule_source.
  925       concrete_outputs_by_rule_source = []
  926 
  927       # concrete_outputs_all is a flat list of all concrete outputs that this
  928       # rule is able to produce, given the known set of input files
  929       # (rule_sources) that apply to it.
  930       concrete_outputs_all = []
  931 
  932       # messages & actions are keyed by the same indices as rule['rule_sources']
  933       # and concrete_outputs_by_rule_source.  They contain the message and
  934       # action to perform after resolving input-dependent variables.  The
  935       # message is optional, in which case None is stored for each rule source.
  936       messages = []
  937       actions = []
  938 
  939       for rule_source in rule.get('rule_sources', []):
  940         rule_source_dirname, rule_source_basename = \
  941             posixpath.split(rule_source)
  942         (rule_source_root, rule_source_ext) = \
  943             posixpath.splitext(rule_source_basename)
  944 
  945         # These are the same variable names that Xcode uses for its own native
  946         # rule support.  Because Xcode's rule engine is not being used, they
  947         # need to be expanded as they are written to the makefile.
  948         rule_input_dict = {
  949           'INPUT_FILE_BASE':   rule_source_root,
  950           'INPUT_FILE_SUFFIX': rule_source_ext,
  951           'INPUT_FILE_NAME':   rule_source_basename,
  952           'INPUT_FILE_PATH':   rule_source,
  953           'INPUT_FILE_DIRNAME': rule_source_dirname,
  954         }
  955 
  956         concrete_outputs_for_this_rule_source = []
  957         for output in rule.get('outputs', []):
  958           # Fortunately, Xcode and make both use $(VAR) format for their
  959           # variables, so the expansion is the only transformation necessary.
  960           # Any remaning $(VAR)-type variables in the string can be given
  961           # directly to make, which will pick up the correct settings from
  962           # what Xcode puts into the environment.
  963           concrete_output = ExpandXcodeVariables(output, rule_input_dict)
  964           concrete_outputs_for_this_rule_source.append(concrete_output)
  965 
  966           # Add all concrete outputs to the project.
  967           pbxp.AddOrGetFileInRootGroup(concrete_output)
  968 
  969         concrete_outputs_by_rule_source.append( \
  970             concrete_outputs_for_this_rule_source)
  971         concrete_outputs_all.extend(concrete_outputs_for_this_rule_source)
  972 
  973         # TODO(mark): Should verify that at most one of these is specified.
  974         if int(rule.get('process_outputs_as_sources', False)):
  975           for output in concrete_outputs_for_this_rule_source:
  976             AddSourceToTarget(output, type, pbxp, xct)
  977 
  978         # If the file came from the mac_bundle_resources list or if the rule
  979         # is marked to process outputs as bundle resource, do so.
  980         was_mac_bundle_resource = rule_source in tgt_mac_bundle_resources
  981         if was_mac_bundle_resource or \
  982             int(rule.get('process_outputs_as_mac_bundle_resources', False)):
  983           for output in concrete_outputs_for_this_rule_source:
  984             AddResourceToTarget(output, pbxp, xct)
  985 
  986         # Do we have a message to print when this rule runs?
  987         message = rule.get('message')
  988         if message:
  989           message = gyp.common.EncodePOSIXShellArgument(message)
  990           message = ExpandXcodeVariables(message, rule_input_dict)
  991         messages.append(message)
  992 
  993         # Turn the list into a string that can be passed to a shell.
  994         action_string = gyp.common.EncodePOSIXShellList(rule['action'])
  995 
  996         action = ExpandXcodeVariables(action_string, rule_input_dict)
  997         actions.append(action)
  998 
  999       if len(concrete_outputs_all) > 0:
 1000         # TODO(mark): There's a possibilty for collision here.  Consider
 1001         # target "t" rule "A_r" and target "t_A" rule "r".
 1002         makefile_name = '%s.make' % re.sub(
 1003             '[^a-zA-Z0-9_]', '_' , '%s_%s' % (target_name, rule['rule_name']))
 1004         makefile_path = os.path.join(xcode_projects[build_file].path,
 1005                                      makefile_name)
 1006         # TODO(mark): try/close?  Write to a temporary file and swap it only
 1007         # if it's got changes?
 1008         makefile = open(makefile_path, 'wb')
 1009 
 1010         # make will build the first target in the makefile by default.  By
 1011         # convention, it's called "all".  List all (or at least one)
 1012         # concrete output for each rule source as a prerequisite of the "all"
 1013         # target.
 1014         makefile.write('all: \\\n')
 1015         for concrete_output_index in \
 1016             xrange(0, len(concrete_outputs_by_rule_source)):
 1017           # Only list the first (index [0]) concrete output of each input
 1018           # in the "all" target.  Otherwise, a parallel make (-j > 1) would
 1019           # attempt to process each input multiple times simultaneously.
 1020           # Otherwise, "all" could just contain the entire list of
 1021           # concrete_outputs_all.
 1022           concrete_output = \
 1023               concrete_outputs_by_rule_source[concrete_output_index][0]
 1024           if concrete_output_index == len(concrete_outputs_by_rule_source) - 1:
 1025             eol = ''
 1026           else:
 1027             eol = ' \\'
 1028           makefile.write('    %s%s\n' % (concrete_output, eol))
 1029 
 1030         for (rule_source, concrete_outputs, message, action) in \
 1031             zip(rule['rule_sources'], concrete_outputs_by_rule_source,
 1032                 messages, actions):
 1033           makefile.write('\n')
 1034 
 1035           # Add a rule that declares it can build each concrete output of a
 1036           # rule source.  Collect the names of the directories that are
 1037           # required.
 1038           concrete_output_dirs = []
 1039           for concrete_output_index in xrange(0, len(concrete_outputs)):
 1040             concrete_output = concrete_outputs[concrete_output_index]
 1041             if concrete_output_index == 0:
 1042               bol = ''
 1043             else:
 1044               bol = '    '
 1045             makefile.write('%s%s \\\n' % (bol, concrete_output))
 1046 
 1047             concrete_output_dir = posixpath.dirname(concrete_output)
 1048             if (concrete_output_dir and
 1049                 concrete_output_dir not in concrete_output_dirs):
 1050               concrete_output_dirs.append(concrete_output_dir)
 1051 
 1052           makefile.write('    : \\\n')
 1053 
 1054           # The prerequisites for this rule are the rule source itself and
 1055           # the set of additional rule inputs, if any.
 1056           prerequisites = [rule_source]
 1057           prerequisites.extend(rule.get('inputs', []))
 1058           for prerequisite_index in xrange(0, len(prerequisites)):
 1059             prerequisite = prerequisites[prerequisite_index]
 1060             if prerequisite_index == len(prerequisites) - 1:
 1061               eol = ''
 1062             else:
 1063               eol = ' \\'
 1064             makefile.write('    %s%s\n' % (prerequisite, eol))
 1065 
 1066           # Make sure that output directories exist before executing the rule
 1067           # action.
 1068           if len(concrete_output_dirs) > 0:
 1069             makefile.write('\t@mkdir -p "%s"\n' %
 1070                            '" "'.join(concrete_output_dirs))
 1071 
 1072           # The rule message and action have already had the necessary variable
 1073           # substitutions performed.
 1074           if message:
 1075             # Mark it with note: so Xcode picks it up in build output.
 1076             makefile.write('\t@echo note: %s\n' % message)
 1077           makefile.write('\t%s\n' % action)
 1078 
 1079         makefile.close()
 1080 
 1081         # It might be nice to ensure that needed output directories exist
 1082         # here rather than in each target in the Makefile, but that wouldn't
 1083         # work if there ever was a concrete output that had an input-dependent
 1084         # variable anywhere other than in the leaf position.
 1085 
 1086         # Don't declare any inputPaths or outputPaths.  If they're present,
 1087         # Xcode will provide a slight optimization by only running the script
 1088         # phase if any output is missing or outdated relative to any input.
 1089         # Unfortunately, it will also assume that all outputs are touched by
 1090         # the script, and if the outputs serve as files in a compilation
 1091         # phase, they will be unconditionally rebuilt.  Since make might not
 1092         # rebuild everything that could be declared here as an output, this
 1093         # extra compilation activity is unnecessary.  With inputPaths and
 1094         # outputPaths not supplied, make will always be called, but it knows
 1095         # enough to not do anything when everything is up-to-date.
 1096 
 1097         # To help speed things up, pass -j COUNT to make so it does some work
 1098         # in parallel.  Don't use ncpus because Xcode will build ncpus targets
 1099         # in parallel and if each target happens to have a rules step, there
 1100         # would be ncpus^2 things going.  With a machine that has 2 quad-core
 1101         # Xeons, a build can quickly run out of processes based on
 1102         # scheduling/other tasks, and randomly failing builds are no good.
 1103         script = \
 1104 """JOB_COUNT="$(/usr/sbin/sysctl -n hw.ncpu)"
 1105 if [ "${JOB_COUNT}" -gt 4 ]; then
 1106   JOB_COUNT=4
 1107 fi
 1108 exec xcrun make -f "${PROJECT_FILE_PATH}/%s" -j "${JOB_COUNT}"
 1109 exit 1
 1110 """ % makefile_name
 1111         ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({
 1112               'name': 'Rule "' + rule['rule_name'] + '"',
 1113               'shellScript': script,
 1114               'showEnvVarsInLog': 0,
 1115             })
 1116 
 1117         if support_xct:
 1118           support_xct.AppendProperty('buildPhases', ssbp)
 1119         else:
 1120           # TODO(mark): this assumes too much knowledge of the internals of
 1121           # xcodeproj_file; some of these smarts should move into xcodeproj_file
 1122           # itself.
 1123           xct._properties['buildPhases'].insert(prebuild_index, ssbp)
 1124           prebuild_index = prebuild_index + 1
 1125 
 1126       # Extra rule inputs also go into the project file.  Concrete outputs were
 1127       # already added when they were computed.
 1128       groups = ['inputs', 'inputs_excluded']
 1129       if skip_excluded_files:
 1130         groups = [x for x in groups if not x.endswith('_excluded')]
 1131       for group in groups:
 1132         for item in rule.get(group, []):
 1133           pbxp.AddOrGetFileInRootGroup(item)
 1134 
 1135     # Add "sources".
 1136     for source in spec.get('sources', []):
 1137       (source_root, source_extension) = posixpath.splitext(source)
 1138       if source_extension[1:] not in rules_by_ext:
 1139         # AddSourceToTarget will add the file to a root group if it's not
 1140         # already there.
 1141         AddSourceToTarget(source, type, pbxp, xct)
 1142       else:
 1143         pbxp.AddOrGetFileInRootGroup(source)
 1144 
 1145     # Add "mac_bundle_resources" and "mac_framework_private_headers" if
 1146     # it's a bundle of any type.
 1147     if is_bundle:
 1148       for resource in tgt_mac_bundle_resources:
 1149         (resource_root, resource_extension) = posixpath.splitext(resource)
 1150         if resource_extension[1:] not in rules_by_ext:
 1151           AddResourceToTarget(resource, pbxp, xct)
 1152         else:
 1153           pbxp.AddOrGetFileInRootGroup(resource)
 1154 
 1155       for header in spec.get('mac_framework_private_headers', []):
 1156         AddHeaderToTarget(header, pbxp, xct, False)
 1157 
 1158     # Add "mac_framework_headers". These can be valid for both frameworks
 1159     # and static libraries.
 1160     if is_bundle or type == 'static_library':
 1161       for header in spec.get('mac_framework_headers', []):
 1162         AddHeaderToTarget(header, pbxp, xct, True)
 1163 
 1164     # Add "copies".
 1165     pbxcp_dict = {}
 1166     for copy_group in spec.get('copies', []):
 1167       dest = copy_group['destination']
 1168       if dest[0] not in ('/', '$'):
 1169         # Relative paths are relative to $(SRCROOT).
 1170         dest = '$(SRCROOT)/' + dest
 1171 
 1172       code_sign = int(copy_group.get('xcode_code_sign', 0))
 1173       settings = (None, '{ATTRIBUTES = (CodeSignOnCopy, ); }')[code_sign];
 1174 
 1175       # Coalesce multiple "copies" sections in the same target with the same
 1176       # "destination" property into the same PBXCopyFilesBuildPhase, otherwise
 1177       # they'll wind up with ID collisions.
 1178       pbxcp = pbxcp_dict.get(dest, None)
 1179       if pbxcp is None:
 1180         pbxcp = gyp.xcodeproj_file.PBXCopyFilesBuildPhase({
 1181               'name': 'Copy to ' + copy_group['destination']
 1182             },
 1183             parent=xct)
 1184         pbxcp.SetDestination(dest)
 1185 
 1186         # TODO(mark): The usual comment about this knowing too much about
 1187         # gyp.xcodeproj_file internals applies.
 1188         xct._properties['buildPhases'].insert(prebuild_index, pbxcp)
 1189 
 1190         pbxcp_dict[dest] = pbxcp
 1191 
 1192       for file in copy_group['files']:
 1193         pbxcp.AddFile(file, settings)
 1194 
 1195     # Excluded files can also go into the project file.
 1196     if not skip_excluded_files:
 1197       for key in ['sources', 'mac_bundle_resources', 'mac_framework_headers',
 1198                   'mac_framework_private_headers']:
 1199         excluded_key = key + '_excluded'
 1200         for item in spec.get(excluded_key, []):
 1201           pbxp.AddOrGetFileInRootGroup(item)
 1202 
 1203     # So can "inputs" and "outputs" sections of "actions" groups.
 1204     groups = ['inputs', 'inputs_excluded', 'outputs', 'outputs_excluded']
 1205     if skip_excluded_files:
 1206       groups = [x for x in groups if not x.endswith('_excluded')]
 1207     for action in spec.get('actions', []):
 1208       for group in groups:
 1209         for item in action.get(group, []):
 1210           # Exclude anything in BUILT_PRODUCTS_DIR.  They're products, not
 1211           # sources.
 1212           if not item.startswith('$(BUILT_PRODUCTS_DIR)/'):
 1213             pbxp.AddOrGetFileInRootGroup(item)
 1214 
 1215     for postbuild in spec.get('postbuilds', []):
 1216       action_string_sh = gyp.common.EncodePOSIXShellList(postbuild['action'])
 1217       script = 'exec ' + action_string_sh + '\nexit 1\n'
 1218 
 1219       # Make the postbuild step depend on the output of ld or ar from this
 1220       # target. Apparently putting the script step after the link step isn't
 1221       # sufficient to ensure proper ordering in all cases. With an input
 1222       # declared but no outputs, the script step should run every time, as
 1223       # desired.
 1224       ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({
 1225             'inputPaths': ['$(BUILT_PRODUCTS_DIR)/$(EXECUTABLE_PATH)'],
 1226             'name': 'Postbuild "' + postbuild['postbuild_name'] + '"',
 1227             'shellScript': script,
 1228             'showEnvVarsInLog': 0,
 1229           })
 1230       xct.AppendProperty('buildPhases', ssbp)
 1231 
 1232     # Add dependencies before libraries, because adding a dependency may imply
 1233     # adding a library.  It's preferable to keep dependencies listed first
 1234     # during a link phase so that they can override symbols that would
 1235     # otherwise be provided by libraries, which will usually include system
 1236     # libraries.  On some systems, ld is finicky and even requires the
 1237     # libraries to be ordered in such a way that unresolved symbols in
 1238     # earlier-listed libraries may only be resolved by later-listed libraries.
 1239     # The Mac linker doesn't work that way, but other platforms do, and so
 1240     # their linker invocations need to be constructed in this way.  There's
 1241     # no compelling reason for Xcode's linker invocations to differ.
 1242 
 1243     if 'dependencies' in spec:
 1244       for dependency in spec['dependencies']:
 1245         xct.AddDependency(xcode_targets[dependency])
 1246         # The support project also gets the dependencies (in case they are
 1247         # needed for the actions/rules to work).
 1248         if support_xct:
 1249           support_xct.AddDependency(xcode_targets[dependency])
 1250 
 1251     if 'libraries' in spec:
 1252       for library in spec['libraries']:
 1253         xct.FrameworksPhase().AddFile(library)
 1254         # Add the library's directory to LIBRARY_SEARCH_PATHS if necessary.
 1255         # I wish Xcode handled this automatically.
 1256         library_dir = posixpath.dirname(library)
 1257         if library_dir not in xcode_standard_library_dirs and (
 1258             not xct.HasBuildSetting(_library_search_paths_var) or
 1259             library_dir not in xct.GetBuildSetting(_library_search_paths_var)):
 1260           xct.AppendBuildSetting(_library_search_paths_var, library_dir)
 1261 
 1262     for configuration_name in configuration_names:
 1263       configuration = spec['configurations'][configuration_name]
 1264       xcbc = xct.ConfigurationNamed(configuration_name)
 1265       for include_dir in configuration.get('mac_framework_dirs', []):
 1266         xcbc.AppendBuildSetting('FRAMEWORK_SEARCH_PATHS', include_dir)
 1267       for include_dir in configuration.get('include_dirs', []):
 1268         xcbc.AppendBuildSetting('HEADER_SEARCH_PATHS', include_dir)
 1269       for library_dir in configuration.get('library_dirs', []):
 1270         if library_dir not in xcode_standard_library_dirs and (
 1271             not xcbc.HasBuildSetting(_library_search_paths_var) or
 1272             library_dir not in xcbc.GetBuildSetting(_library_search_paths_var)):
 1273           xcbc.AppendBuildSetting(_library_search_paths_var, library_dir)
 1274 
 1275       if 'defines' in configuration:
 1276         for define in configuration['defines']:
 1277           set_define = EscapeXcodeDefine(define)
 1278           xcbc.AppendBuildSetting('GCC_PREPROCESSOR_DEFINITIONS', set_define)
 1279       if 'xcode_settings' in configuration:
 1280         for xck, xcv in configuration['xcode_settings'].iteritems():
 1281           xcbc.SetBuildSetting(xck, xcv)
 1282       if 'xcode_config_file' in configuration:
 1283         config_ref = pbxp.AddOrGetFileInRootGroup(
 1284             configuration['xcode_config_file'])
 1285         xcbc.SetBaseConfiguration(config_ref)
 1286 
 1287   build_files = []
 1288   for build_file, build_file_dict in data.iteritems():
 1289     if build_file.endswith('.gyp'):
 1290       build_files.append(build_file)
 1291 
 1292   for build_file in build_files:
 1293     xcode_projects[build_file].Finalize1(xcode_targets, serialize_all_tests)
 1294 
 1295   for build_file in build_files:
 1296     xcode_projects[build_file].Finalize2(xcode_targets,
 1297                                          xcode_target_to_target_dict)
 1298 
 1299   for build_file in build_files:
 1300     xcode_projects[build_file].Write()