ee098b9c28395dc67d0df68d0345866ee582fd2655f0ec49ed995cff7402131fc3fefb731897c9da43e475371978f2ac19a0e5c3b9198f302d41f3e2d4e0bf 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # Copyright (c) 2014 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. """Xcode-ninja wrapper project file generator.
  5. This updates the data structures passed to the Xcode gyp generator to build
  6. with ninja instead. The Xcode project itself is transformed into a list of
  7. executable targets, each with a build step to build with ninja, and a target
  8. with every source and resource file. This appears to sidestep some of the
  9. major performance headaches experienced using complex projects and large number
  10. of targets within Xcode.
  11. """
  12. import errno
  13. import gyp.generator.ninja
  14. import os
  15. import re
  16. import xml.sax.saxutils
  17. def _WriteWorkspace(main_gyp, sources_gyp, params):
  18. """ Create a workspace to wrap main and sources gyp paths. """
  19. (build_file_root, build_file_ext) = os.path.splitext(main_gyp)
  20. workspace_path = build_file_root + ".xcworkspace"
  21. options = params["options"]
  22. if options.generator_output:
  23. workspace_path = os.path.join(options.generator_output, workspace_path)
  24. try:
  25. os.makedirs(workspace_path)
  26. except OSError as e:
  27. if e.errno != errno.EEXIST:
  28. raise
  29. output_string = (
  30. '<?xml version="1.0" encoding="UTF-8"?>\n' + '<Workspace version = "1.0">\n'
  31. )
  32. for gyp_name in [main_gyp, sources_gyp]:
  33. name = os.path.splitext(os.path.basename(gyp_name))[0] + ".xcodeproj"
  34. name = xml.sax.saxutils.quoteattr("group:" + name)
  35. output_string += " <FileRef location = %s></FileRef>\n" % name
  36. output_string += "</Workspace>\n"
  37. workspace_file = os.path.join(workspace_path, "contents.xcworkspacedata")
  38. try:
  39. with open(workspace_file) as input_file:
  40. input_string = input_file.read()
  41. if input_string == output_string:
  42. return
  43. except OSError:
  44. # Ignore errors if the file doesn't exist.
  45. pass
  46. with open(workspace_file, "w") as output_file:
  47. output_file.write(output_string)
  48. def _TargetFromSpec(old_spec, params):
  49. """ Create fake target for xcode-ninja wrapper. """
  50. # Determine ninja top level build dir (e.g. /path/to/out).
  51. ninja_toplevel = None
  52. jobs = 0
  53. if params:
  54. options = params["options"]
  55. ninja_toplevel = os.path.join(
  56. options.toplevel_dir, gyp.generator.ninja.ComputeOutputDir(params)
  57. )
  58. jobs = params.get("generator_flags", {}).get("xcode_ninja_jobs", 0)
  59. target_name = old_spec.get("target_name")
  60. product_name = old_spec.get("product_name", target_name)
  61. product_extension = old_spec.get("product_extension")
  62. ninja_target = {}
  63. ninja_target["target_name"] = target_name
  64. ninja_target["product_name"] = product_name
  65. if product_extension:
  66. ninja_target["product_extension"] = product_extension
  67. ninja_target["toolset"] = old_spec.get("toolset")
  68. ninja_target["default_configuration"] = old_spec.get("default_configuration")
  69. ninja_target["configurations"] = {}
  70. # Tell Xcode to look in |ninja_toplevel| for build products.
  71. new_xcode_settings = {}
  72. if ninja_toplevel:
  73. new_xcode_settings["CONFIGURATION_BUILD_DIR"] = (
  74. "%s/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)" % ninja_toplevel
  75. )
  76. if "configurations" in old_spec:
  77. for config in old_spec["configurations"]:
  78. old_xcode_settings = old_spec["configurations"][config].get(
  79. "xcode_settings", {}
  80. )
  81. if "IPHONEOS_DEPLOYMENT_TARGET" in old_xcode_settings:
  82. new_xcode_settings["CODE_SIGNING_REQUIRED"] = "NO"
  83. new_xcode_settings["IPHONEOS_DEPLOYMENT_TARGET"] = old_xcode_settings[
  84. "IPHONEOS_DEPLOYMENT_TARGET"
  85. ]
  86. for key in ["BUNDLE_LOADER", "TEST_HOST"]:
  87. if key in old_xcode_settings:
  88. new_xcode_settings[key] = old_xcode_settings[key]
  89. ninja_target["configurations"][config] = {}
  90. ninja_target["configurations"][config][
  91. "xcode_settings"
  92. ] = new_xcode_settings
  93. ninja_target["mac_bundle"] = old_spec.get("mac_bundle", 0)
  94. ninja_target["mac_xctest_bundle"] = old_spec.get("mac_xctest_bundle", 0)
  95. ninja_target["ios_app_extension"] = old_spec.get("ios_app_extension", 0)
  96. ninja_target["ios_watchkit_extension"] = old_spec.get("ios_watchkit_extension", 0)
  97. ninja_target["ios_watchkit_app"] = old_spec.get("ios_watchkit_app", 0)
  98. ninja_target["type"] = old_spec["type"]
  99. if ninja_toplevel:
  100. ninja_target["actions"] = [
  101. {
  102. "action_name": "Compile and copy %s via ninja" % target_name,
  103. "inputs": [],
  104. "outputs": [],
  105. "action": [
  106. "env",
  107. "PATH=%s" % os.environ["PATH"],
  108. "ninja",
  109. "-C",
  110. new_xcode_settings["CONFIGURATION_BUILD_DIR"],
  111. target_name,
  112. ],
  113. "message": "Compile and copy %s via ninja" % target_name,
  114. },
  115. ]
  116. if jobs > 0:
  117. ninja_target["actions"][0]["action"].extend(("-j", jobs))
  118. return ninja_target
  119. def IsValidTargetForWrapper(target_extras, executable_target_pattern, spec):
  120. """Limit targets for Xcode wrapper.
  121. Xcode sometimes performs poorly with too many targets, so only include
  122. proper executable targets, with filters to customize.
  123. Arguments:
  124. target_extras: Regular expression to always add, matching any target.
  125. executable_target_pattern: Regular expression limiting executable targets.
  126. spec: Specifications for target.
  127. """
  128. target_name = spec.get("target_name")
  129. # Always include targets matching target_extras.
  130. if target_extras is not None and re.search(target_extras, target_name):
  131. return True
  132. # Otherwise just show executable targets and xc_tests.
  133. if int(spec.get("mac_xctest_bundle", 0)) != 0 or (
  134. spec.get("type", "") == "executable"
  135. and spec.get("product_extension", "") != "bundle"
  136. ):
  137. # If there is a filter and the target does not match, exclude the target.
  138. if executable_target_pattern is not None:
  139. if not re.search(executable_target_pattern, target_name):
  140. return False
  141. return True
  142. return False
  143. def CreateWrapper(target_list, target_dicts, data, params):
  144. """Initialize targets for the ninja wrapper.
  145. This sets up the necessary variables in the targets to generate Xcode projects
  146. that use ninja as an external builder.
  147. Arguments:
  148. target_list: List of target pairs: 'base/base.gyp:base'.
  149. target_dicts: Dict of target properties keyed on target pair.
  150. data: Dict of flattened build files keyed on gyp path.
  151. params: Dict of global options for gyp.
  152. """
  153. orig_gyp = params["build_files"][0]
  154. for gyp_name, gyp_dict in data.items():
  155. if gyp_name == orig_gyp:
  156. depth = gyp_dict["_DEPTH"]
  157. # Check for custom main gyp name, otherwise use the default CHROMIUM_GYP_FILE
  158. # and prepend .ninja before the .gyp extension.
  159. generator_flags = params.get("generator_flags", {})
  160. main_gyp = generator_flags.get("xcode_ninja_main_gyp", None)
  161. if main_gyp is None:
  162. (build_file_root, build_file_ext) = os.path.splitext(orig_gyp)
  163. main_gyp = build_file_root + ".ninja" + build_file_ext
  164. # Create new |target_list|, |target_dicts| and |data| data structures.
  165. new_target_list = []
  166. new_target_dicts = {}
  167. new_data = {}
  168. # Set base keys needed for |data|.
  169. new_data[main_gyp] = {}
  170. new_data[main_gyp]["included_files"] = []
  171. new_data[main_gyp]["targets"] = []
  172. new_data[main_gyp]["xcode_settings"] = data[orig_gyp].get("xcode_settings", {})
  173. # Normally the xcode-ninja generator includes only valid executable targets.
  174. # If |xcode_ninja_executable_target_pattern| is set, that list is reduced to
  175. # executable targets that match the pattern. (Default all)
  176. executable_target_pattern = generator_flags.get(
  177. "xcode_ninja_executable_target_pattern", None
  178. )
  179. # For including other non-executable targets, add the matching target name
  180. # to the |xcode_ninja_target_pattern| regular expression. (Default none)
  181. target_extras = generator_flags.get("xcode_ninja_target_pattern", None)
  182. for old_qualified_target in target_list:
  183. spec = target_dicts[old_qualified_target]
  184. if IsValidTargetForWrapper(target_extras, executable_target_pattern, spec):
  185. # Add to new_target_list.
  186. target_name = spec.get("target_name")
  187. new_target_name = f"{main_gyp}:{target_name}#target"
  188. new_target_list.append(new_target_name)
  189. # Add to new_target_dicts.
  190. new_target_dicts[new_target_name] = _TargetFromSpec(spec, params)
  191. # Add to new_data.
  192. for old_target in data[old_qualified_target.split(":")[0]]["targets"]:
  193. if old_target["target_name"] == target_name:
  194. new_data_target = {}
  195. new_data_target["target_name"] = old_target["target_name"]
  196. new_data_target["toolset"] = old_target["toolset"]
  197. new_data[main_gyp]["targets"].append(new_data_target)
  198. # Create sources target.
  199. sources_target_name = "sources_for_indexing"
  200. sources_target = _TargetFromSpec(
  201. {
  202. "target_name": sources_target_name,
  203. "toolset": "target",
  204. "default_configuration": "Default",
  205. "mac_bundle": "0",
  206. "type": "executable",
  207. },
  208. None,
  209. )
  210. # Tell Xcode to look everywhere for headers.
  211. sources_target["configurations"] = {"Default": {"include_dirs": [depth]}}
  212. # Put excluded files into the sources target so they can be opened in Xcode.
  213. skip_excluded_files = not generator_flags.get(
  214. "xcode_ninja_list_excluded_files", True
  215. )
  216. sources = []
  217. for target, target_dict in target_dicts.items():
  218. base = os.path.dirname(target)
  219. files = target_dict.get("sources", []) + target_dict.get(
  220. "mac_bundle_resources", []
  221. )
  222. if not skip_excluded_files:
  223. files.extend(
  224. target_dict.get("sources_excluded", [])
  225. + target_dict.get("mac_bundle_resources_excluded", [])
  226. )
  227. for action in target_dict.get("actions", []):
  228. files.extend(action.get("inputs", []))
  229. if not skip_excluded_files:
  230. files.extend(action.get("inputs_excluded", []))
  231. # Remove files starting with $. These are mostly intermediate files for the
  232. # build system.
  233. files = [file for file in files if not file.startswith("$")]
  234. # Make sources relative to root build file.
  235. relative_path = os.path.dirname(main_gyp)
  236. sources += [
  237. os.path.relpath(os.path.join(base, file), relative_path) for file in files
  238. ]
  239. sources_target["sources"] = sorted(set(sources))
  240. # Put sources_to_index in it's own gyp.
  241. sources_gyp = os.path.join(os.path.dirname(main_gyp), sources_target_name + ".gyp")
  242. fully_qualified_target_name = f"{sources_gyp}:{sources_target_name}#target"
  243. # Add to new_target_list, new_target_dicts and new_data.
  244. new_target_list.append(fully_qualified_target_name)
  245. new_target_dicts[fully_qualified_target_name] = sources_target
  246. new_data_target = {}
  247. new_data_target["target_name"] = sources_target["target_name"]
  248. new_data_target["_DEPTH"] = depth
  249. new_data_target["toolset"] = "target"
  250. new_data[sources_gyp] = {}
  251. new_data[sources_gyp]["targets"] = []
  252. new_data[sources_gyp]["included_files"] = []
  253. new_data[sources_gyp]["xcode_settings"] = data[orig_gyp].get("xcode_settings", {})
  254. new_data[sources_gyp]["targets"].append(new_data_target)
  255. # Write workspace to file.
  256. _WriteWorkspace(main_gyp, sources_gyp, params)
  257. return (new_target_list, new_target_dicts, new_data)