9bb4fb7f692a8c7dde3b895dfb4c6713b5f459cfd9631a80d36bb8287308a0b040a16390a7563ba8d3a46bce01b51a981da18cc8f09992b7211afb655f52b6 64 KB

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