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