PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/front/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py

https://gitlab.com/boxnia/NFU_MOVIL
Python | 1256 lines | 1233 code | 7 blank | 16 comment | 5 complexity | 788cb5178de89c6a6e47e0203b66d74a MD5 | raw file
  1. # Copyright (c) 2012 Google Inc. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """
  5. This module contains classes that help to emulate xcodebuild behavior on top of
  6. other build systems, such as make and ninja.
  7. """
  8. import copy
  9. import gyp.common
  10. import os
  11. import os.path
  12. import re
  13. import shlex
  14. import subprocess
  15. import sys
  16. import tempfile
  17. from gyp.common import GypError
  18. # Populated lazily by XcodeVersion, for efficiency, and to fix an issue when
  19. # "xcodebuild" is called too quickly (it has been found to return incorrect
  20. # version number).
  21. XCODE_VERSION_CACHE = None
  22. # Populated lazily by GetXcodeArchsDefault, to an |XcodeArchsDefault| instance
  23. # corresponding to the installed version of Xcode.
  24. XCODE_ARCHS_DEFAULT_CACHE = None
  25. def XcodeArchsVariableMapping(archs, archs_including_64_bit=None):
  26. """Constructs a dictionary with expansion for $(ARCHS_STANDARD) variable,
  27. and optionally for $(ARCHS_STANDARD_INCLUDING_64_BIT)."""
  28. mapping = {'$(ARCHS_STANDARD)': archs}
  29. if archs_including_64_bit:
  30. mapping['$(ARCHS_STANDARD_INCLUDING_64_BIT)'] = archs_including_64_bit
  31. return mapping
  32. class XcodeArchsDefault(object):
  33. """A class to resolve ARCHS variable from xcode_settings, resolving Xcode
  34. macros and implementing filtering by VALID_ARCHS. The expansion of macros
  35. depends on the SDKROOT used ("macosx", "iphoneos", "iphonesimulator") and
  36. on the version of Xcode.
  37. """
  38. # Match variable like $(ARCHS_STANDARD).
  39. variable_pattern = re.compile(r'\$\([a-zA-Z_][a-zA-Z0-9_]*\)$')
  40. def __init__(self, default, mac, iphonesimulator, iphoneos):
  41. self._default = (default,)
  42. self._archs = {'mac': mac, 'ios': iphoneos, 'iossim': iphonesimulator}
  43. def _VariableMapping(self, sdkroot):
  44. """Returns the dictionary of variable mapping depending on the SDKROOT."""
  45. sdkroot = sdkroot.lower()
  46. if 'iphoneos' in sdkroot:
  47. return self._archs['ios']
  48. elif 'iphonesimulator' in sdkroot:
  49. return self._archs['iossim']
  50. else:
  51. return self._archs['mac']
  52. def _ExpandArchs(self, archs, sdkroot):
  53. """Expands variables references in ARCHS, and remove duplicates."""
  54. variable_mapping = self._VariableMapping(sdkroot)
  55. expanded_archs = []
  56. for arch in archs:
  57. if self.variable_pattern.match(arch):
  58. variable = arch
  59. try:
  60. variable_expansion = variable_mapping[variable]
  61. for arch in variable_expansion:
  62. if arch not in expanded_archs:
  63. expanded_archs.append(arch)
  64. except KeyError as e:
  65. print 'Warning: Ignoring unsupported variable "%s".' % variable
  66. elif arch not in expanded_archs:
  67. expanded_archs.append(arch)
  68. return expanded_archs
  69. def ActiveArchs(self, archs, valid_archs, sdkroot):
  70. """Expands variables references in ARCHS, and filter by VALID_ARCHS if it
  71. is defined (if not set, Xcode accept any value in ARCHS, otherwise, only
  72. values present in VALID_ARCHS are kept)."""
  73. expanded_archs = self._ExpandArchs(archs or self._default, sdkroot or '')
  74. if valid_archs:
  75. filtered_archs = []
  76. for arch in expanded_archs:
  77. if arch in valid_archs:
  78. filtered_archs.append(arch)
  79. expanded_archs = filtered_archs
  80. return expanded_archs
  81. def GetXcodeArchsDefault():
  82. """Returns the |XcodeArchsDefault| object to use to expand ARCHS for the
  83. installed version of Xcode. The default values used by Xcode for ARCHS
  84. and the expansion of the variables depends on the version of Xcode used.
  85. For all version anterior to Xcode 5.0 or posterior to Xcode 5.1 included
  86. uses $(ARCHS_STANDARD) if ARCHS is unset, while Xcode 5.0 to 5.0.2 uses
  87. $(ARCHS_STANDARD_INCLUDING_64_BIT). This variable was added to Xcode 5.0
  88. and deprecated with Xcode 5.1.
  89. For "macosx" SDKROOT, all version starting with Xcode 5.0 includes 64-bit
  90. architecture as part of $(ARCHS_STANDARD) and default to only building it.
  91. For "iphoneos" and "iphonesimulator" SDKROOT, 64-bit architectures are part
  92. of $(ARCHS_STANDARD_INCLUDING_64_BIT) from Xcode 5.0. From Xcode 5.1, they
  93. are also part of $(ARCHS_STANDARD).
  94. All thoses rules are coded in the construction of the |XcodeArchsDefault|
  95. object to use depending on the version of Xcode detected. The object is
  96. for performance reason."""
  97. global XCODE_ARCHS_DEFAULT_CACHE
  98. if XCODE_ARCHS_DEFAULT_CACHE:
  99. return XCODE_ARCHS_DEFAULT_CACHE
  100. xcode_version, _ = XcodeVersion()
  101. if xcode_version < '0500':
  102. XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault(
  103. '$(ARCHS_STANDARD)',
  104. XcodeArchsVariableMapping(['i386']),
  105. XcodeArchsVariableMapping(['i386']),
  106. XcodeArchsVariableMapping(['armv7']))
  107. elif xcode_version < '0510':
  108. XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault(
  109. '$(ARCHS_STANDARD_INCLUDING_64_BIT)',
  110. XcodeArchsVariableMapping(['x86_64'], ['x86_64']),
  111. XcodeArchsVariableMapping(['i386'], ['i386', 'x86_64']),
  112. XcodeArchsVariableMapping(
  113. ['armv7', 'armv7s'],
  114. ['armv7', 'armv7s', 'arm64']))
  115. else:
  116. XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault(
  117. '$(ARCHS_STANDARD)',
  118. XcodeArchsVariableMapping(['x86_64'], ['x86_64']),
  119. XcodeArchsVariableMapping(['i386', 'x86_64'], ['i386', 'x86_64']),
  120. XcodeArchsVariableMapping(
  121. ['armv7', 'armv7s', 'arm64'],
  122. ['armv7', 'armv7s', 'arm64']))
  123. return XCODE_ARCHS_DEFAULT_CACHE
  124. class XcodeSettings(object):
  125. """A class that understands the gyp 'xcode_settings' object."""
  126. # Populated lazily by _SdkPath(). Shared by all XcodeSettings, so cached
  127. # at class-level for efficiency.
  128. _sdk_path_cache = {}
  129. _sdk_root_cache = {}
  130. # Populated lazily by GetExtraPlistItems(). Shared by all XcodeSettings, so
  131. # cached at class-level for efficiency.
  132. _plist_cache = {}
  133. # Populated lazily by GetIOSPostbuilds. Shared by all XcodeSettings, so
  134. # cached at class-level for efficiency.
  135. _codesigning_key_cache = {}
  136. def __init__(self, spec):
  137. self.spec = spec
  138. self.isIOS = False
  139. # Per-target 'xcode_settings' are pushed down into configs earlier by gyp.
  140. # This means self.xcode_settings[config] always contains all settings
  141. # for that config -- the per-target settings as well. Settings that are
  142. # the same for all configs are implicitly per-target settings.
  143. self.xcode_settings = {}
  144. configs = spec['configurations']
  145. for configname, config in configs.iteritems():
  146. self.xcode_settings[configname] = config.get('xcode_settings', {})
  147. self._ConvertConditionalKeys(configname)
  148. if self.xcode_settings[configname].get('IPHONEOS_DEPLOYMENT_TARGET',
  149. None):
  150. self.isIOS = True
  151. # This is only non-None temporarily during the execution of some methods.
  152. self.configname = None
  153. # Used by _AdjustLibrary to match .a and .dylib entries in libraries.
  154. self.library_re = re.compile(r'^lib([^/]+)\.(a|dylib)$')
  155. def _ConvertConditionalKeys(self, configname):
  156. """Converts or warns on conditional keys. Xcode supports conditional keys,
  157. such as CODE_SIGN_IDENTITY[sdk=iphoneos*]. This is a partial implementation
  158. with some keys converted while the rest force a warning."""
  159. settings = self.xcode_settings[configname]
  160. conditional_keys = [key for key in settings if key.endswith(']')]
  161. for key in conditional_keys:
  162. # If you need more, speak up at http://crbug.com/122592
  163. if key.endswith("[sdk=iphoneos*]"):
  164. if configname.endswith("iphoneos"):
  165. new_key = key.split("[")[0]
  166. settings[new_key] = settings[key]
  167. else:
  168. print 'Warning: Conditional keys not implemented, ignoring:', \
  169. ' '.join(conditional_keys)
  170. del settings[key]
  171. def _Settings(self):
  172. assert self.configname
  173. return self.xcode_settings[self.configname]
  174. def _Test(self, test_key, cond_key, default):
  175. return self._Settings().get(test_key, default) == cond_key
  176. def _Appendf(self, lst, test_key, format_str, default=None):
  177. if test_key in self._Settings():
  178. lst.append(format_str % str(self._Settings()[test_key]))
  179. elif default:
  180. lst.append(format_str % str(default))
  181. def _WarnUnimplemented(self, test_key):
  182. if test_key in self._Settings():
  183. print 'Warning: Ignoring not yet implemented key "%s".' % test_key
  184. def IsBinaryOutputFormat(self, configname):
  185. default = "binary" if self.isIOS else "xml"
  186. format = self.xcode_settings[configname].get('INFOPLIST_OUTPUT_FORMAT',
  187. default)
  188. return format == "binary"
  189. def _IsBundle(self):
  190. return int(self.spec.get('mac_bundle', 0)) != 0
  191. def _IsIosAppExtension(self):
  192. return int(self.spec.get('ios_app_extension', 0)) != 0
  193. def _IsIosWatchKitExtension(self):
  194. return int(self.spec.get('ios_watchkit_extension', 0)) != 0
  195. def _IsIosWatchApp(self):
  196. return int(self.spec.get('ios_watch_app', 0)) != 0
  197. def GetFrameworkVersion(self):
  198. """Returns the framework version of the current target. Only valid for
  199. bundles."""
  200. assert self._IsBundle()
  201. return self.GetPerTargetSetting('FRAMEWORK_VERSION', default='A')
  202. def GetWrapperExtension(self):
  203. """Returns the bundle extension (.app, .framework, .plugin, etc). Only
  204. valid for bundles."""
  205. assert self._IsBundle()
  206. if self.spec['type'] in ('loadable_module', 'shared_library'):
  207. default_wrapper_extension = {
  208. 'loadable_module': 'bundle',
  209. 'shared_library': 'framework',
  210. }[self.spec['type']]
  211. wrapper_extension = self.GetPerTargetSetting(
  212. 'WRAPPER_EXTENSION', default=default_wrapper_extension)
  213. return '.' + self.spec.get('product_extension', wrapper_extension)
  214. elif self.spec['type'] == 'executable':
  215. if self._IsIosAppExtension() or self._IsIosWatchKitExtension():
  216. return '.' + self.spec.get('product_extension', 'appex')
  217. else:
  218. return '.' + self.spec.get('product_extension', 'app')
  219. else:
  220. assert False, "Don't know extension for '%s', target '%s'" % (
  221. self.spec['type'], self.spec['target_name'])
  222. def GetProductName(self):
  223. """Returns PRODUCT_NAME."""
  224. return self.spec.get('product_name', self.spec['target_name'])
  225. def GetFullProductName(self):
  226. """Returns FULL_PRODUCT_NAME."""
  227. if self._IsBundle():
  228. return self.GetWrapperName()
  229. else:
  230. return self._GetStandaloneBinaryPath()
  231. def GetWrapperName(self):
  232. """Returns the directory name of the bundle represented by this target.
  233. Only valid for bundles."""
  234. assert self._IsBundle()
  235. return self.GetProductName() + self.GetWrapperExtension()
  236. def GetBundleContentsFolderPath(self):
  237. """Returns the qualified path to the bundle's contents folder. E.g.
  238. Chromium.app/Contents or Foo.bundle/Versions/A. Only valid for bundles."""
  239. if self.isIOS:
  240. return self.GetWrapperName()
  241. assert self._IsBundle()
  242. if self.spec['type'] == 'shared_library':
  243. return os.path.join(
  244. self.GetWrapperName(), 'Versions', self.GetFrameworkVersion())
  245. else:
  246. # loadable_modules have a 'Contents' folder like executables.
  247. return os.path.join(self.GetWrapperName(), 'Contents')
  248. def GetBundleResourceFolder(self):
  249. """Returns the qualified path to the bundle's resource folder. E.g.
  250. Chromium.app/Contents/Resources. Only valid for bundles."""
  251. assert self._IsBundle()
  252. if self.isIOS:
  253. return self.GetBundleContentsFolderPath()
  254. return os.path.join(self.GetBundleContentsFolderPath(), 'Resources')
  255. def GetBundlePlistPath(self):
  256. """Returns the qualified path to the bundle's plist file. E.g.
  257. Chromium.app/Contents/Info.plist. Only valid for bundles."""
  258. assert self._IsBundle()
  259. if self.spec['type'] in ('executable', 'loadable_module'):
  260. return os.path.join(self.GetBundleContentsFolderPath(), 'Info.plist')
  261. else:
  262. return os.path.join(self.GetBundleContentsFolderPath(),
  263. 'Resources', 'Info.plist')
  264. def GetProductType(self):
  265. """Returns the PRODUCT_TYPE of this target."""
  266. if self._IsIosAppExtension():
  267. assert self._IsBundle(), ('ios_app_extension flag requires mac_bundle '
  268. '(target %s)' % self.spec['target_name'])
  269. return 'com.apple.product-type.app-extension'
  270. if self._IsIosWatchKitExtension():
  271. assert self._IsBundle(), ('ios_watchkit_extension flag requires '
  272. 'mac_bundle (target %s)' % self.spec['target_name'])
  273. return 'com.apple.product-type.watchkit-extension'
  274. if self._IsIosWatchApp():
  275. assert self._IsBundle(), ('ios_watch_app flag requires mac_bundle '
  276. '(target %s)' % self.spec['target_name'])
  277. return 'com.apple.product-type.application.watchapp'
  278. if self._IsBundle():
  279. return {
  280. 'executable': 'com.apple.product-type.application',
  281. 'loadable_module': 'com.apple.product-type.bundle',
  282. 'shared_library': 'com.apple.product-type.framework',
  283. }[self.spec['type']]
  284. else:
  285. return {
  286. 'executable': 'com.apple.product-type.tool',
  287. 'loadable_module': 'com.apple.product-type.library.dynamic',
  288. 'shared_library': 'com.apple.product-type.library.dynamic',
  289. 'static_library': 'com.apple.product-type.library.static',
  290. }[self.spec['type']]
  291. def GetMachOType(self):
  292. """Returns the MACH_O_TYPE of this target."""
  293. # Weird, but matches Xcode.
  294. if not self._IsBundle() and self.spec['type'] == 'executable':
  295. return ''
  296. return {
  297. 'executable': 'mh_execute',
  298. 'static_library': 'staticlib',
  299. 'shared_library': 'mh_dylib',
  300. 'loadable_module': 'mh_bundle',
  301. }[self.spec['type']]
  302. def _GetBundleBinaryPath(self):
  303. """Returns the name of the bundle binary of by this target.
  304. E.g. Chromium.app/Contents/MacOS/Chromium. Only valid for bundles."""
  305. assert self._IsBundle()
  306. if self.spec['type'] in ('shared_library') or self.isIOS:
  307. path = self.GetBundleContentsFolderPath()
  308. elif self.spec['type'] in ('executable', 'loadable_module'):
  309. path = os.path.join(self.GetBundleContentsFolderPath(), 'MacOS')
  310. return os.path.join(path, self.GetExecutableName())
  311. def _GetStandaloneExecutableSuffix(self):
  312. if 'product_extension' in self.spec:
  313. return '.' + self.spec['product_extension']
  314. return {
  315. 'executable': '',
  316. 'static_library': '.a',
  317. 'shared_library': '.dylib',
  318. 'loadable_module': '.so',
  319. }[self.spec['type']]
  320. def _GetStandaloneExecutablePrefix(self):
  321. return self.spec.get('product_prefix', {
  322. 'executable': '',
  323. 'static_library': 'lib',
  324. 'shared_library': 'lib',
  325. # Non-bundled loadable_modules are called foo.so for some reason
  326. # (that is, .so and no prefix) with the xcode build -- match that.
  327. 'loadable_module': '',
  328. }[self.spec['type']])
  329. def _GetStandaloneBinaryPath(self):
  330. """Returns the name of the non-bundle binary represented by this target.
  331. E.g. hello_world. Only valid for non-bundles."""
  332. assert not self._IsBundle()
  333. assert self.spec['type'] in (
  334. 'executable', 'shared_library', 'static_library', 'loadable_module'), (
  335. 'Unexpected type %s' % self.spec['type'])
  336. target = self.spec['target_name']
  337. if self.spec['type'] == 'static_library':
  338. if target[:3] == 'lib':
  339. target = target[3:]
  340. elif self.spec['type'] in ('loadable_module', 'shared_library'):
  341. if target[:3] == 'lib':
  342. target = target[3:]
  343. target_prefix = self._GetStandaloneExecutablePrefix()
  344. target = self.spec.get('product_name', target)
  345. target_ext = self._GetStandaloneExecutableSuffix()
  346. return target_prefix + target + target_ext
  347. def GetExecutableName(self):
  348. """Returns the executable name of the bundle represented by this target.
  349. E.g. Chromium."""
  350. if self._IsBundle():
  351. return self.spec.get('product_name', self.spec['target_name'])
  352. else:
  353. return self._GetStandaloneBinaryPath()
  354. def GetExecutablePath(self):
  355. """Returns the directory name of the bundle represented by this target. E.g.
  356. Chromium.app/Contents/MacOS/Chromium."""
  357. if self._IsBundle():
  358. return self._GetBundleBinaryPath()
  359. else:
  360. return self._GetStandaloneBinaryPath()
  361. def GetActiveArchs(self, configname):
  362. """Returns the architectures this target should be built for."""
  363. config_settings = self.xcode_settings[configname]
  364. xcode_archs_default = GetXcodeArchsDefault()
  365. return xcode_archs_default.ActiveArchs(
  366. config_settings.get('ARCHS'),
  367. config_settings.get('VALID_ARCHS'),
  368. config_settings.get('SDKROOT'))
  369. def _GetSdkVersionInfoItem(self, sdk, infoitem):
  370. # xcodebuild requires Xcode and can't run on Command Line Tools-only
  371. # systems from 10.7 onward.
  372. # Since the CLT has no SDK paths anyway, returning None is the
  373. # most sensible route and should still do the right thing.
  374. try:
  375. return GetStdout(['xcodebuild', '-version', '-sdk', sdk, infoitem])
  376. except:
  377. pass
  378. def _SdkRoot(self, configname):
  379. if configname is None:
  380. configname = self.configname
  381. return self.GetPerConfigSetting('SDKROOT', configname, default='')
  382. def _SdkPath(self, configname=None):
  383. sdk_root = self._SdkRoot(configname)
  384. if sdk_root.startswith('/'):
  385. return sdk_root
  386. return self._XcodeSdkPath(sdk_root)
  387. def _XcodeSdkPath(self, sdk_root):
  388. if sdk_root not in XcodeSettings._sdk_path_cache:
  389. sdk_path = self._GetSdkVersionInfoItem(sdk_root, 'Path')
  390. XcodeSettings._sdk_path_cache[sdk_root] = sdk_path
  391. if sdk_root:
  392. XcodeSettings._sdk_root_cache[sdk_path] = sdk_root
  393. return XcodeSettings._sdk_path_cache[sdk_root]
  394. def _AppendPlatformVersionMinFlags(self, lst):
  395. self._Appendf(lst, 'MACOSX_DEPLOYMENT_TARGET', '-mmacosx-version-min=%s')
  396. if 'IPHONEOS_DEPLOYMENT_TARGET' in self._Settings():
  397. # TODO: Implement this better?
  398. sdk_path_basename = os.path.basename(self._SdkPath())
  399. if sdk_path_basename.lower().startswith('iphonesimulator'):
  400. self._Appendf(lst, 'IPHONEOS_DEPLOYMENT_TARGET',
  401. '-mios-simulator-version-min=%s')
  402. else:
  403. self._Appendf(lst, 'IPHONEOS_DEPLOYMENT_TARGET',
  404. '-miphoneos-version-min=%s')
  405. def GetCflags(self, configname, arch=None):
  406. """Returns flags that need to be added to .c, .cc, .m, and .mm
  407. compilations."""
  408. # This functions (and the similar ones below) do not offer complete
  409. # emulation of all xcode_settings keys. They're implemented on demand.
  410. self.configname = configname
  411. cflags = []
  412. sdk_root = self._SdkPath()
  413. if 'SDKROOT' in self._Settings() and sdk_root:
  414. cflags.append('-isysroot %s' % sdk_root)
  415. if self._Test('CLANG_WARN_CONSTANT_CONVERSION', 'YES', default='NO'):
  416. cflags.append('-Wconstant-conversion')
  417. if self._Test('GCC_CHAR_IS_UNSIGNED_CHAR', 'YES', default='NO'):
  418. cflags.append('-funsigned-char')
  419. if self._Test('GCC_CW_ASM_SYNTAX', 'YES', default='YES'):
  420. cflags.append('-fasm-blocks')
  421. if 'GCC_DYNAMIC_NO_PIC' in self._Settings():
  422. if self._Settings()['GCC_DYNAMIC_NO_PIC'] == 'YES':
  423. cflags.append('-mdynamic-no-pic')
  424. else:
  425. pass
  426. # TODO: In this case, it depends on the target. xcode passes
  427. # mdynamic-no-pic by default for executable and possibly static lib
  428. # according to mento
  429. if self._Test('GCC_ENABLE_PASCAL_STRINGS', 'YES', default='YES'):
  430. cflags.append('-mpascal-strings')
  431. self._Appendf(cflags, 'GCC_OPTIMIZATION_LEVEL', '-O%s', default='s')
  432. if self._Test('GCC_GENERATE_DEBUGGING_SYMBOLS', 'YES', default='YES'):
  433. dbg_format = self._Settings().get('DEBUG_INFORMATION_FORMAT', 'dwarf')
  434. if dbg_format == 'dwarf':
  435. cflags.append('-gdwarf-2')
  436. elif dbg_format == 'stabs':
  437. raise NotImplementedError('stabs debug format is not supported yet.')
  438. elif dbg_format == 'dwarf-with-dsym':
  439. cflags.append('-gdwarf-2')
  440. else:
  441. raise NotImplementedError('Unknown debug format %s' % dbg_format)
  442. if self._Settings().get('GCC_STRICT_ALIASING') == 'YES':
  443. cflags.append('-fstrict-aliasing')
  444. elif self._Settings().get('GCC_STRICT_ALIASING') == 'NO':
  445. cflags.append('-fno-strict-aliasing')
  446. if self._Test('GCC_SYMBOLS_PRIVATE_EXTERN', 'YES', default='NO'):
  447. cflags.append('-fvisibility=hidden')
  448. if self._Test('GCC_TREAT_WARNINGS_AS_ERRORS', 'YES', default='NO'):
  449. cflags.append('-Werror')
  450. if self._Test('GCC_WARN_ABOUT_MISSING_NEWLINE', 'YES', default='NO'):
  451. cflags.append('-Wnewline-eof')
  452. # In Xcode, this is only activated when GCC_COMPILER_VERSION is clang or
  453. # llvm-gcc. It also requires a fairly recent libtool, and
  454. # if the system clang isn't used, DYLD_LIBRARY_PATH needs to contain the
  455. # path to the libLTO.dylib that matches the used clang.
  456. if self._Test('LLVM_LTO', 'YES', default='NO'):
  457. cflags.append('-flto')
  458. self._AppendPlatformVersionMinFlags(cflags)
  459. # TODO:
  460. if self._Test('COPY_PHASE_STRIP', 'YES', default='NO'):
  461. self._WarnUnimplemented('COPY_PHASE_STRIP')
  462. self._WarnUnimplemented('GCC_DEBUGGING_SYMBOLS')
  463. self._WarnUnimplemented('GCC_ENABLE_OBJC_EXCEPTIONS')
  464. # TODO: This is exported correctly, but assigning to it is not supported.
  465. self._WarnUnimplemented('MACH_O_TYPE')
  466. self._WarnUnimplemented('PRODUCT_TYPE')
  467. if arch is not None:
  468. archs = [arch]
  469. else:
  470. assert self.configname
  471. archs = self.GetActiveArchs(self.configname)
  472. if len(archs) != 1:
  473. # TODO: Supporting fat binaries will be annoying.
  474. self._WarnUnimplemented('ARCHS')
  475. archs = ['i386']
  476. cflags.append('-arch ' + archs[0])
  477. if archs[0] in ('i386', 'x86_64'):
  478. if self._Test('GCC_ENABLE_SSE3_EXTENSIONS', 'YES', default='NO'):
  479. cflags.append('-msse3')
  480. if self._Test('GCC_ENABLE_SUPPLEMENTAL_SSE3_INSTRUCTIONS', 'YES',
  481. default='NO'):
  482. cflags.append('-mssse3') # Note 3rd 's'.
  483. if self._Test('GCC_ENABLE_SSE41_EXTENSIONS', 'YES', default='NO'):
  484. cflags.append('-msse4.1')
  485. if self._Test('GCC_ENABLE_SSE42_EXTENSIONS', 'YES', default='NO'):
  486. cflags.append('-msse4.2')
  487. cflags += self._Settings().get('WARNING_CFLAGS', [])
  488. if sdk_root:
  489. framework_root = sdk_root
  490. else:
  491. framework_root = ''
  492. config = self.spec['configurations'][self.configname]
  493. framework_dirs = config.get('mac_framework_dirs', [])
  494. for directory in framework_dirs:
  495. cflags.append('-F' + directory.replace('$(SDKROOT)', framework_root))
  496. self.configname = None
  497. return cflags
  498. def GetCflagsC(self, configname):
  499. """Returns flags that need to be added to .c, and .m compilations."""
  500. self.configname = configname
  501. cflags_c = []
  502. if self._Settings().get('GCC_C_LANGUAGE_STANDARD', '') == 'ansi':
  503. cflags_c.append('-ansi')
  504. else:
  505. self._Appendf(cflags_c, 'GCC_C_LANGUAGE_STANDARD', '-std=%s')
  506. cflags_c += self._Settings().get('OTHER_CFLAGS', [])
  507. self.configname = None
  508. return cflags_c
  509. def GetCflagsCC(self, configname):
  510. """Returns flags that need to be added to .cc, and .mm compilations."""
  511. self.configname = configname
  512. cflags_cc = []
  513. clang_cxx_language_standard = self._Settings().get(
  514. 'CLANG_CXX_LANGUAGE_STANDARD')
  515. # Note: Don't make c++0x to c++11 so that c++0x can be used with older
  516. # clangs that don't understand c++11 yet (like Xcode 4.2's).
  517. if clang_cxx_language_standard:
  518. cflags_cc.append('-std=%s' % clang_cxx_language_standard)
  519. self._Appendf(cflags_cc, 'CLANG_CXX_LIBRARY', '-stdlib=%s')
  520. if self._Test('GCC_ENABLE_CPP_RTTI', 'NO', default='YES'):
  521. cflags_cc.append('-fno-rtti')
  522. if self._Test('GCC_ENABLE_CPP_EXCEPTIONS', 'NO', default='YES'):
  523. cflags_cc.append('-fno-exceptions')
  524. if self._Test('GCC_INLINES_ARE_PRIVATE_EXTERN', 'YES', default='NO'):
  525. cflags_cc.append('-fvisibility-inlines-hidden')
  526. if self._Test('GCC_THREADSAFE_STATICS', 'NO', default='YES'):
  527. cflags_cc.append('-fno-threadsafe-statics')
  528. # Note: This flag is a no-op for clang, it only has an effect for gcc.
  529. if self._Test('GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO', 'NO', default='YES'):
  530. cflags_cc.append('-Wno-invalid-offsetof')
  531. other_ccflags = []
  532. for flag in self._Settings().get('OTHER_CPLUSPLUSFLAGS', ['$(inherited)']):
  533. # TODO: More general variable expansion. Missing in many other places too.
  534. if flag in ('$inherited', '$(inherited)', '${inherited}'):
  535. flag = '$OTHER_CFLAGS'
  536. if flag in ('$OTHER_CFLAGS', '$(OTHER_CFLAGS)', '${OTHER_CFLAGS}'):
  537. other_ccflags += self._Settings().get('OTHER_CFLAGS', [])
  538. else:
  539. other_ccflags.append(flag)
  540. cflags_cc += other_ccflags
  541. self.configname = None
  542. return cflags_cc
  543. def _AddObjectiveCGarbageCollectionFlags(self, flags):
  544. gc_policy = self._Settings().get('GCC_ENABLE_OBJC_GC', 'unsupported')
  545. if gc_policy == 'supported':
  546. flags.append('-fobjc-gc')
  547. elif gc_policy == 'required':
  548. flags.append('-fobjc-gc-only')
  549. def _AddObjectiveCARCFlags(self, flags):
  550. if self._Test('CLANG_ENABLE_OBJC_ARC', 'YES', default='NO'):
  551. flags.append('-fobjc-arc')
  552. def _AddObjectiveCMissingPropertySynthesisFlags(self, flags):
  553. if self._Test('CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS',
  554. 'YES', default='NO'):
  555. flags.append('-Wobjc-missing-property-synthesis')
  556. def GetCflagsObjC(self, configname):
  557. """Returns flags that need to be added to .m compilations."""
  558. self.configname = configname
  559. cflags_objc = []
  560. self._AddObjectiveCGarbageCollectionFlags(cflags_objc)
  561. self._AddObjectiveCARCFlags(cflags_objc)
  562. self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objc)
  563. self.configname = None
  564. return cflags_objc
  565. def GetCflagsObjCC(self, configname):
  566. """Returns flags that need to be added to .mm compilations."""
  567. self.configname = configname
  568. cflags_objcc = []
  569. self._AddObjectiveCGarbageCollectionFlags(cflags_objcc)
  570. self._AddObjectiveCARCFlags(cflags_objcc)
  571. self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objcc)
  572. if self._Test('GCC_OBJC_CALL_CXX_CDTORS', 'YES', default='NO'):
  573. cflags_objcc.append('-fobjc-call-cxx-cdtors')
  574. self.configname = None
  575. return cflags_objcc
  576. def GetInstallNameBase(self):
  577. """Return DYLIB_INSTALL_NAME_BASE for this target."""
  578. # Xcode sets this for shared_libraries, and for nonbundled loadable_modules.
  579. if (self.spec['type'] != 'shared_library' and
  580. (self.spec['type'] != 'loadable_module' or self._IsBundle())):
  581. return None
  582. install_base = self.GetPerTargetSetting(
  583. 'DYLIB_INSTALL_NAME_BASE',
  584. default='/Library/Frameworks' if self._IsBundle() else '/usr/local/lib')
  585. return install_base
  586. def _StandardizePath(self, path):
  587. """Do :standardizepath processing for path."""
  588. # I'm not quite sure what :standardizepath does. Just call normpath(),
  589. # but don't let @executable_path/../foo collapse to foo.
  590. if '/' in path:
  591. prefix, rest = '', path
  592. if path.startswith('@'):
  593. prefix, rest = path.split('/', 1)
  594. rest = os.path.normpath(rest) # :standardizepath
  595. path = os.path.join(prefix, rest)
  596. return path
  597. def GetInstallName(self):
  598. """Return LD_DYLIB_INSTALL_NAME for this target."""
  599. # Xcode sets this for shared_libraries, and for nonbundled loadable_modules.
  600. if (self.spec['type'] != 'shared_library' and
  601. (self.spec['type'] != 'loadable_module' or self._IsBundle())):
  602. return None
  603. default_install_name = \
  604. '$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)'
  605. install_name = self.GetPerTargetSetting(
  606. 'LD_DYLIB_INSTALL_NAME', default=default_install_name)
  607. # Hardcode support for the variables used in chromium for now, to
  608. # unblock people using the make build.
  609. if '$' in install_name:
  610. assert install_name in ('$(DYLIB_INSTALL_NAME_BASE:standardizepath)/'
  611. '$(WRAPPER_NAME)/$(PRODUCT_NAME)', default_install_name), (
  612. 'Variables in LD_DYLIB_INSTALL_NAME are not generally supported '
  613. 'yet in target \'%s\' (got \'%s\')' %
  614. (self.spec['target_name'], install_name))
  615. install_name = install_name.replace(
  616. '$(DYLIB_INSTALL_NAME_BASE:standardizepath)',
  617. self._StandardizePath(self.GetInstallNameBase()))
  618. if self._IsBundle():
  619. # These are only valid for bundles, hence the |if|.
  620. install_name = install_name.replace(
  621. '$(WRAPPER_NAME)', self.GetWrapperName())
  622. install_name = install_name.replace(
  623. '$(PRODUCT_NAME)', self.GetProductName())
  624. else:
  625. assert '$(WRAPPER_NAME)' not in install_name
  626. assert '$(PRODUCT_NAME)' not in install_name
  627. install_name = install_name.replace(
  628. '$(EXECUTABLE_PATH)', self.GetExecutablePath())
  629. return install_name
  630. def _MapLinkerFlagFilename(self, ldflag, gyp_to_build_path):
  631. """Checks if ldflag contains a filename and if so remaps it from
  632. gyp-directory-relative to build-directory-relative."""
  633. # This list is expanded on demand.
  634. # They get matched as:
  635. # -exported_symbols_list file
  636. # -Wl,exported_symbols_list file
  637. # -Wl,exported_symbols_list,file
  638. LINKER_FILE = r'(\S+)'
  639. WORD = r'\S+'
  640. linker_flags = [
  641. ['-exported_symbols_list', LINKER_FILE], # Needed for NaCl.
  642. ['-unexported_symbols_list', LINKER_FILE],
  643. ['-reexported_symbols_list', LINKER_FILE],
  644. ['-sectcreate', WORD, WORD, LINKER_FILE], # Needed for remoting.
  645. ]
  646. for flag_pattern in linker_flags:
  647. regex = re.compile('(?:-Wl,)?' + '[ ,]'.join(flag_pattern))
  648. m = regex.match(ldflag)
  649. if m:
  650. ldflag = ldflag[:m.start(1)] + gyp_to_build_path(m.group(1)) + \
  651. ldflag[m.end(1):]
  652. # Required for ffmpeg (no idea why they don't use LIBRARY_SEARCH_PATHS,
  653. # TODO(thakis): Update ffmpeg.gyp):
  654. if ldflag.startswith('-L'):
  655. ldflag = '-L' + gyp_to_build_path(ldflag[len('-L'):])
  656. return ldflag
  657. def GetLdflags(self, configname, product_dir, gyp_to_build_path, arch=None):
  658. """Returns flags that need to be passed to the linker.
  659. Args:
  660. configname: The name of the configuration to get ld flags for.
  661. product_dir: The directory where products such static and dynamic
  662. libraries are placed. This is added to the library search path.
  663. gyp_to_build_path: A function that converts paths relative to the
  664. current gyp file to paths relative to the build direcotry.
  665. """
  666. self.configname = configname
  667. ldflags = []
  668. # The xcode build is relative to a gyp file's directory, and OTHER_LDFLAGS
  669. # can contain entries that depend on this. Explicitly absolutify these.
  670. for ldflag in self._Settings().get('OTHER_LDFLAGS', []):
  671. ldflags.append(self._MapLinkerFlagFilename(ldflag, gyp_to_build_path))
  672. if self._Test('DEAD_CODE_STRIPPING', 'YES', default='NO'):
  673. ldflags.append('-Wl,-dead_strip')
  674. if self._Test('PREBINDING', 'YES', default='NO'):
  675. ldflags.append('-Wl,-prebind')
  676. self._Appendf(
  677. ldflags, 'DYLIB_COMPATIBILITY_VERSION', '-compatibility_version %s')
  678. self._Appendf(
  679. ldflags, 'DYLIB_CURRENT_VERSION', '-current_version %s')
  680. self._AppendPlatformVersionMinFlags(ldflags)
  681. if 'SDKROOT' in self._Settings() and self._SdkPath():
  682. ldflags.append('-isysroot ' + self._SdkPath())
  683. for library_path in self._Settings().get('LIBRARY_SEARCH_PATHS', []):
  684. ldflags.append('-L' + gyp_to_build_path(library_path))
  685. if 'ORDER_FILE' in self._Settings():
  686. ldflags.append('-Wl,-order_file ' +
  687. '-Wl,' + gyp_to_build_path(
  688. self._Settings()['ORDER_FILE']))
  689. if arch is not None:
  690. archs = [arch]
  691. else:
  692. assert self.configname
  693. archs = self.GetActiveArchs(self.configname)
  694. if len(archs) != 1:
  695. # TODO: Supporting fat binaries will be annoying.
  696. self._WarnUnimplemented('ARCHS')
  697. archs = ['i386']
  698. ldflags.append('-arch ' + archs[0])
  699. # Xcode adds the product directory by default.
  700. ldflags.append('-L' + product_dir)
  701. install_name = self.GetInstallName()
  702. if install_name and self.spec['type'] != 'loadable_module':
  703. ldflags.append('-install_name ' + install_name.replace(' ', r'\ '))
  704. for rpath in self._Settings().get('LD_RUNPATH_SEARCH_PATHS', []):
  705. ldflags.append('-Wl,-rpath,' + rpath)
  706. sdk_root = self._SdkPath()
  707. if not sdk_root:
  708. sdk_root = ''
  709. config = self.spec['configurations'][self.configname]
  710. framework_dirs = config.get('mac_framework_dirs', [])
  711. for directory in framework_dirs:
  712. ldflags.append('-F' + directory.replace('$(SDKROOT)', sdk_root))
  713. is_extension = self._IsIosAppExtension() or self._IsIosWatchKitExtension()
  714. if sdk_root and is_extension:
  715. # Adds the link flags for extensions. These flags are common for all
  716. # extensions and provide loader and main function.
  717. # These flags reflect the compilation options used by xcode to compile
  718. # extensions.
  719. ldflags.append('-lpkstart')
  720. if XcodeVersion() < '0900':
  721. ldflags.append(sdk_root +
  722. '/System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit')
  723. ldflags.append('-fapplication-extension')
  724. ldflags.append('-Xlinker -rpath '
  725. '-Xlinker @executable_path/../../Frameworks')
  726. self._Appendf(ldflags, 'CLANG_CXX_LIBRARY', '-stdlib=%s')
  727. self.configname = None
  728. return ldflags
  729. def GetLibtoolflags(self, configname):
  730. """Returns flags that need to be passed to the static linker.
  731. Args:
  732. configname: The name of the configuration to get ld flags for.
  733. """
  734. self.configname = configname
  735. libtoolflags = []
  736. for libtoolflag in self._Settings().get('OTHER_LDFLAGS', []):
  737. libtoolflags.append(libtoolflag)
  738. # TODO(thakis): ARCHS?
  739. self.configname = None
  740. return libtoolflags
  741. def GetPerTargetSettings(self):
  742. """Gets a list of all the per-target settings. This will only fetch keys
  743. whose values are the same across all configurations."""
  744. first_pass = True
  745. result = {}
  746. for configname in sorted(self.xcode_settings.keys()):
  747. if first_pass:
  748. result = dict(self.xcode_settings[configname])
  749. first_pass = False
  750. else:
  751. for key, value in self.xcode_settings[configname].iteritems():
  752. if key not in result:
  753. continue
  754. elif result[key] != value:
  755. del result[key]
  756. return result
  757. def GetPerConfigSetting(self, setting, configname, default=None):
  758. if configname in self.xcode_settings:
  759. return self.xcode_settings[configname].get(setting, default)
  760. else:
  761. return self.GetPerTargetSetting(setting, default)
  762. def GetPerTargetSetting(self, setting, default=None):
  763. """Tries to get xcode_settings.setting from spec. Assumes that the setting
  764. has the same value in all configurations and throws otherwise."""
  765. is_first_pass = True
  766. result = None
  767. for configname in sorted(self.xcode_settings.keys()):
  768. if is_first_pass:
  769. result = self.xcode_settings[configname].get(setting, None)
  770. is_first_pass = False
  771. else:
  772. assert result == self.xcode_settings[configname].get(setting, None), (
  773. "Expected per-target setting for '%s', got per-config setting "
  774. "(target %s)" % (setting, self.spec['target_name']))
  775. if result is None:
  776. return default
  777. return result
  778. def _GetStripPostbuilds(self, configname, output_binary, quiet):
  779. """Returns a list of shell commands that contain the shell commands
  780. neccessary to strip this target's binary. These should be run as postbuilds
  781. before the actual postbuilds run."""
  782. self.configname = configname
  783. result = []
  784. if (self._Test('DEPLOYMENT_POSTPROCESSING', 'YES', default='NO') and
  785. self._Test('STRIP_INSTALLED_PRODUCT', 'YES', default='NO')):
  786. default_strip_style = 'debugging'
  787. if self.spec['type'] == 'loadable_module' and self._IsBundle():
  788. default_strip_style = 'non-global'
  789. elif self.spec['type'] == 'executable':
  790. default_strip_style = 'all'
  791. strip_style = self._Settings().get('STRIP_STYLE', default_strip_style)
  792. strip_flags = {
  793. 'all': '',
  794. 'non-global': '-x',
  795. 'debugging': '-S',
  796. }[strip_style]
  797. explicit_strip_flags = self._Settings().get('STRIPFLAGS', '')
  798. if explicit_strip_flags:
  799. strip_flags += ' ' + _NormalizeEnvVarReferences(explicit_strip_flags)
  800. if not quiet:
  801. result.append('echo STRIP\\(%s\\)' % self.spec['target_name'])
  802. result.append('strip %s %s' % (strip_flags, output_binary))
  803. self.configname = None
  804. return result
  805. def _GetDebugInfoPostbuilds(self, configname, output, output_binary, quiet):
  806. """Returns a list of shell commands that contain the shell commands
  807. neccessary to massage this target's debug information. These should be run
  808. as postbuilds before the actual postbuilds run."""
  809. self.configname = configname
  810. # For static libraries, no dSYMs are created.
  811. result = []
  812. if (self._Test('GCC_GENERATE_DEBUGGING_SYMBOLS', 'YES', default='YES') and
  813. self._Test(
  814. 'DEBUG_INFORMATION_FORMAT', 'dwarf-with-dsym', default='dwarf') and
  815. self.spec['type'] != 'static_library'):
  816. if not quiet:
  817. result.append('echo DSYMUTIL\\(%s\\)' % self.spec['target_name'])
  818. result.append('dsymutil %s -o %s' % (output_binary, output + '.dSYM'))
  819. self.configname = None
  820. return result
  821. def _GetTargetPostbuilds(self, configname, output, output_binary,
  822. quiet=False):
  823. """Returns a list of shell commands that contain the shell commands
  824. to run as postbuilds for this target, before the actual postbuilds."""
  825. # dSYMs need to build before stripping happens.
  826. return (
  827. self._GetDebugInfoPostbuilds(configname, output, output_binary, quiet) +
  828. self._GetStripPostbuilds(configname, output_binary, quiet))
  829. def _GetIOSPostbuilds(self, configname, output_binary):
  830. """Return a shell command to codesign the iOS output binary so it can
  831. be deployed to a device. This should be run as the very last step of the
  832. build."""
  833. if not (self.isIOS and self.spec['type'] == 'executable'):
  834. return []
  835. settings = self.xcode_settings[configname]
  836. key = self._GetIOSCodeSignIdentityKey(settings)
  837. if not key:
  838. return []
  839. # Warn for any unimplemented signing xcode keys.
  840. unimpl = ['OTHER_CODE_SIGN_FLAGS']
  841. unimpl = set(unimpl) & set(self.xcode_settings[configname].keys())
  842. if unimpl:
  843. print 'Warning: Some codesign keys not implemented, ignoring: %s' % (
  844. ', '.join(sorted(unimpl)))
  845. return ['%s code-sign-bundle "%s" "%s" "%s" "%s"' % (
  846. os.path.join('${TARGET_BUILD_DIR}', 'gyp-mac-tool'), key,
  847. settings.get('CODE_SIGN_RESOURCE_RULES_PATH', ''),
  848. settings.get('CODE_SIGN_ENTITLEMENTS', ''),
  849. settings.get('PROVISIONING_PROFILE', ''))
  850. ]
  851. def _GetIOSCodeSignIdentityKey(self, settings):
  852. identity = settings.get('CODE_SIGN_IDENTITY')
  853. if not identity:
  854. return None
  855. if identity not in XcodeSettings._codesigning_key_cache:
  856. output = subprocess.check_output(
  857. ['security', 'find-identity', '-p', 'codesigning', '-v'])
  858. for line in output.splitlines():
  859. if identity in line:
  860. fingerprint = line.split()[1]
  861. cache = XcodeSettings._codesigning_key_cache
  862. assert identity not in cache or fingerprint == cache[identity], (
  863. "Multiple codesigning fingerprints for identity: %s" % identity)
  864. XcodeSettings._codesigning_key_cache[identity] = fingerprint
  865. return XcodeSettings._codesigning_key_cache.get(identity, '')
  866. def AddImplicitPostbuilds(self, configname, output, output_binary,
  867. postbuilds=[], quiet=False):
  868. """Returns a list of shell commands that should run before and after
  869. |postbuilds|."""
  870. assert output_binary is not None
  871. pre = self._GetTargetPostbuilds(configname, output, output_binary, quiet)
  872. post = self._GetIOSPostbuilds(configname, output_binary)
  873. return pre + postbuilds + post
  874. def _AdjustLibrary(self, library, config_name=None):
  875. if library.endswith('.framework'):
  876. l = '-framework ' + os.path.splitext(os.path.basename(library))[0]
  877. else:
  878. m = self.library_re.match(library)
  879. if m:
  880. l = '-l' + m.group(1)
  881. else:
  882. l = library
  883. sdk_root = self._SdkPath(config_name)
  884. if not sdk_root:
  885. sdk_root = ''
  886. # Xcode 7 started shipping with ".tbd" (text based stubs) files instead of
  887. # ".dylib" without providing a real support for them. What it does, for
  888. # "/usr/lib" libraries, is do "-L/usr/lib -lname" which is dependent on the
  889. # library order and cause collision when building Chrome.
  890. #
  891. # Instead substitude ".tbd" to ".dylib" in the generated project when the
  892. # following conditions are both true:
  893. # - library is referenced in the gyp file as "$(SDKROOT)/**/*.dylib",
  894. # - the ".dylib" file does not exists but a ".tbd" file do.
  895. library = l.replace('$(SDKROOT)', sdk_root)
  896. if l.startswith('$(SDKROOT)'):
  897. basename, ext = os.path.splitext(library)
  898. if ext == '.dylib' and not os.path.exists(library):
  899. tbd_library = basename + '.tbd'
  900. if os.path.exists(tbd_library):
  901. library = tbd_library
  902. return library
  903. def AdjustLibraries(self, libraries, config_name=None):
  904. """Transforms entries like 'Cocoa.framework' in libraries into entries like
  905. '-framework Cocoa', 'libcrypto.dylib' into '-lcrypto', etc.
  906. """
  907. libraries = [self._AdjustLibrary(library, config_name)
  908. for library in libraries]
  909. return libraries
  910. def _BuildMachineOSBuild(self):
  911. return GetStdout(['sw_vers', '-buildVersion'])
  912. def _XcodeIOSDeviceFamily(self, configname):
  913. family = self.xcode_settings[configname].get('TARGETED_DEVICE_FAMILY', '1')
  914. return [int(x) for x in family.split(',')]
  915. def GetExtraPlistItems(self, configname=None):
  916. """Returns a dictionary with extra items to insert into Info.plist."""
  917. if configname not in XcodeSettings._plist_cache:
  918. cache = {}
  919. cache['BuildMachineOSBuild'] = self._BuildMachineOSBuild()
  920. xcode, xcode_build = XcodeVersion()
  921. cache['DTXcode'] = xcode
  922. cache['DTXcodeBuild'] = xcode_build
  923. sdk_root = self._SdkRoot(configname)
  924. if not sdk_root:
  925. sdk_root = self._DefaultSdkRoot()
  926. cache['DTSDKName'] = sdk_root
  927. if xcode >= '0430':
  928. cache['DTSDKBuild'] = self._GetSdkVersionInfoItem(
  929. sdk_root, 'ProductBuildVersion')
  930. else:
  931. cache['DTSDKBuild'] = cache['BuildMachineOSBuild']
  932. if self.isIOS:
  933. cache['DTPlatformName'] = cache['DTSDKName']
  934. if configname.endswith("iphoneos"):
  935. cache['DTPlatformVersion'] = self._GetSdkVersionInfoItem(
  936. sdk_root, 'ProductVersion')
  937. cache['CFBundleSupportedPlatforms'] = ['iPhoneOS']
  938. else:
  939. cache['CFBundleSupportedPlatforms'] = ['iPhoneSimulator']
  940. XcodeSettings._plist_cache[configname] = cache
  941. # Include extra plist items that are per-target, not per global
  942. # XcodeSettings.
  943. items = dict(XcodeSettings._plist_cache[configname])
  944. if self.isIOS:
  945. items['UIDeviceFamily'] = self._XcodeIOSDeviceFamily(configname)
  946. return items
  947. def _DefaultSdkRoot(self):
  948. """Returns the default SDKROOT to use.
  949. Prior to version 5.0.0, if SDKROOT was not explicitly set in the Xcode
  950. project, then the environment variable was empty. Starting with this
  951. version, Xcode uses the name of the newest SDK installed.
  952. """
  953. xcode_version, xcode_build = XcodeVersion()
  954. if xcode_version < '0500':
  955. return ''
  956. default_sdk_path = self._XcodeSdkPath('')
  957. default_sdk_root = XcodeSettings._sdk_root_cache.get(default_sdk_path)
  958. if default_sdk_root:
  959. return default_sdk_root
  960. try:
  961. all_sdks = GetStdout(['xcodebuild', '-showsdks'])
  962. except:
  963. # If xcodebuild fails, there will be no valid SDKs
  964. return ''
  965. for line in all_sdks.splitlines():
  966. items = line.split()
  967. if len(items) >= 3 and items[-2] == '-sdk':
  968. sdk_root = items[-1]
  969. sdk_path = self._XcodeSdkPath(sdk_root)
  970. if sdk_path == default_sdk_path:
  971. return sdk_root
  972. return ''
  973. class MacPrefixHeader(object):
  974. """A class that helps with emulating Xcode's GCC_PREFIX_HEADER feature.
  975. This feature consists of several pieces:
  976. * If GCC_PREFIX_HEADER is present, all compilations in that project get an
  977. additional |-include path_to_prefix_header| cflag.
  978. * If GCC_PRECOMPILE_PREFIX_HEADER is present too, then the prefix header is
  979. instead compiled, and all other compilations in the project get an
  980. additional |-include path_to_compiled_header| instead.
  981. + Compiled prefix headers have the extension gch. There is one gch file for
  982. every language used in the project (c, cc, m, mm), since gch files for
  983. different languages aren't compatible.
  984. + gch files themselves are built with the target's normal cflags, but they
  985. obviously don't get the |-include| flag. Instead, they need a -x flag that
  986. describes their language.
  987. + All o files in the target need to depend on the gch file, to make sure
  988. it's built before any o file is built.
  989. This class helps with some of these tasks, but it needs help from the build
  990. system for writing dependencies to the gch files, for writing build commands
  991. for the gch files, and for figuring out the location of the gch files.
  992. """
  993. def __init__(self, xcode_settings,
  994. gyp_path_to_build_path, gyp_path_to_build_output):
  995. """If xcode_settings is None, all methods on this class are no-ops.
  996. Args:
  997. gyp_path_to_build_path: A function that takes a gyp-relative path,
  998. and returns a path relative to the build directory.
  999. gyp_path_to_build_output: A function that takes a gyp-relative path and
  1000. a language code ('c', 'cc', 'm', or 'mm'), and that returns a path
  1001. to where the output of precompiling that path for that language
  1002. should be placed (without the trailing '.gch').
  1003. """
  1004. # This doesn't support per-configuration prefix headers. Good enough
  1005. # for now.
  1006. self.header = None
  1007. self.compile_headers = False
  1008. if xcode_settings:
  1009. self.header = xcode_settings.GetPerTargetSetting('GCC_PREFIX_HEADER')
  1010. self.compile_headers = xcode_settings.GetPerTargetSetting(
  1011. 'GCC_PRECOMPILE_PREFIX_HEADER', default='NO') != 'NO'
  1012. self.compiled_headers = {}
  1013. if self.header:
  1014. if self.compile_headers:
  1015. for lang in ['c', 'cc', 'm', 'mm']:
  1016. self.compiled_headers[lang] = gyp_path_to_build_output(
  1017. self.header, lang)
  1018. self.header = gyp_path_to_build_path(self.header)
  1019. def _CompiledHeader(self, lang, arch):
  1020. assert self.compile_headers
  1021. h = self.compiled_headers[lang]
  1022. if arch:
  1023. h += '.' + arch
  1024. return h
  1025. def GetInclude(self, lang, arch=None):
  1026. """Gets the cflags to include the prefix header for language |lang|."""
  1027. if self.compile_headers and lang in self.compiled_headers:
  1028. return '-include %s' % self._CompiledHeader(lang, arch)
  1029. elif self.header:
  1030. return '-include %s' % self.header
  1031. else:
  1032. return ''
  1033. def _Gch(self, lang, arch):
  1034. """Returns the actual file name of the prefix header for language |lang|."""
  1035. assert self.compile_headers
  1036. return self._CompiledHeader(lang, arch) + '.gch'
  1037. def GetObjDependencies(self, sources, objs, arch=None):
  1038. """Given a list of source files and the corresponding object files, returns
  1039. a list of (source, object, gch) tuples, where |gch| is the build-directory
  1040. relative path to the gch file each object file depends on. |compilable[i]|
  1041. has to be the source file belonging to |objs[i]|."""
  1042. if not self.header or not self.compile_headers:
  1043. return []
  1044. result = []
  1045. for source, obj in zip(sources, objs):
  1046. ext = os.path.splitext(source)[1]
  1047. lang = {
  1048. '.c': 'c',
  1049. '.cpp': 'cc', '.cc': 'cc', '.cxx': 'cc',
  1050. '.m': 'm',
  1051. '.mm': 'mm',
  1052. }.get(ext, None)
  1053. if lang:
  1054. result.append((source, obj, self._Gch(lang, arch)))
  1055. return result
  1056. def GetPchBuildCommands(self, arch=None):
  1057. """Returns [(path_to_gch, language_flag, language, header)].
  1058. |path_to_gch| and |header| are relative to the build directory.
  1059. """
  1060. if not self.header or not self.compile_headers:
  1061. return []
  1062. return [
  1063. (self._Gch('c', arch), '-x c-header', 'c', self.header),
  1064. (self._Gch('cc', arch), '-x c++-header', 'cc', self.header),
  1065. (self._Gch('m', arch), '-x objective-c-header', 'm', self.header),
  1066. (self._Gch('mm', arch), '-x objective-c++-header', 'mm', self.header),
  1067. ]
  1068. def XcodeVersion():
  1069. """Returns a tuple of version and build version of installed Xcode."""
  1070. # `xcodebuild -version` output looks like
  1071. # Xcode 4.6.3
  1072. # Build version 4H1503
  1073. # or like
  1074. # Xcode 3.2.6
  1075. # Component versions: DevToolsCore-1809.0; DevToolsSupport-1806.0
  1076. # BuildVersion: 10M2518
  1077. # Convert that to '0463', '4H1503'.
  1078. global XCODE_VERSION_CACHE
  1079. if XCODE_VERSION_CACHE:
  1080. return XCODE_VERSION_CACHE
  1081. try:
  1082. version_list = GetStdout(['xcodebuild', '-version']).splitlines()
  1083. # In some circumstances xcodebuild exits 0 but doesn't return
  1084. # the