gyp-mac-tool 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #!/usr/bin/env python
  2. # Generated by gyp. Do not edit.
  3. # Copyright (c) 2012 Google Inc. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Utility functions to perform Xcode-style build steps.
  7. These functions are executed via gyp-mac-tool when using the Makefile generator.
  8. """
  9. import fcntl
  10. import fnmatch
  11. import glob
  12. import json
  13. import os
  14. import plistlib
  15. import re
  16. import shutil
  17. import string
  18. import subprocess
  19. import sys
  20. import tempfile
  21. def main(args):
  22. executor = MacTool()
  23. exit_code = executor.Dispatch(args)
  24. if exit_code is not None:
  25. sys.exit(exit_code)
  26. class MacTool(object):
  27. """This class performs all the Mac tooling steps. The methods can either be
  28. executed directly, or dispatched from an argument list."""
  29. def Dispatch(self, args):
  30. """Dispatches a string command to a method."""
  31. if len(args) < 1:
  32. raise Exception("Not enough arguments")
  33. method = "Exec%s" % self._CommandifyName(args[0])
  34. return getattr(self, method)(*args[1:])
  35. def _CommandifyName(self, name_string):
  36. """Transforms a tool name like copy-info-plist to CopyInfoPlist"""
  37. return name_string.title().replace('-', '')
  38. def ExecCopyBundleResource(self, source, dest):
  39. """Copies a resource file to the bundle/Resources directory, performing any
  40. necessary compilation on each resource."""
  41. extension = os.path.splitext(source)[1].lower()
  42. if os.path.isdir(source):
  43. # Copy tree.
  44. # TODO(thakis): This copies file attributes like mtime, while the
  45. # single-file branch below doesn't. This should probably be changed to
  46. # be consistent with the single-file branch.
  47. if os.path.exists(dest):
  48. shutil.rmtree(dest)
  49. shutil.copytree(source, dest)
  50. elif extension == '.xib':
  51. return self._CopyXIBFile(source, dest)
  52. elif extension == '.storyboard':
  53. return self._CopyXIBFile(source, dest)
  54. elif extension == '.strings':
  55. self._CopyStringsFile(source, dest)
  56. else:
  57. shutil.copy(source, dest)
  58. def _CopyXIBFile(self, source, dest):
  59. """Compiles a XIB file with ibtool into a binary plist in the bundle."""
  60. # ibtool sometimes crashes with relative paths. See crbug.com/314728.
  61. base = os.path.dirname(os.path.realpath(__file__))
  62. if os.path.relpath(source):
  63. source = os.path.join(base, source)
  64. if os.path.relpath(dest):
  65. dest = os.path.join(base, dest)
  66. args = ['xcrun', 'ibtool', '--errors', '--warnings', '--notices',
  67. '--output-format', 'human-readable-text', '--compile', dest, source]
  68. ibtool_section_re = re.compile(r'/\*.*\*/')
  69. ibtool_re = re.compile(r'.*note:.*is clipping its content')
  70. ibtoolout = subprocess.Popen(args, stdout=subprocess.PIPE)
  71. current_section_header = None
  72. for line in ibtoolout.stdout:
  73. if ibtool_section_re.match(line):
  74. current_section_header = line
  75. elif not ibtool_re.match(line):
  76. if current_section_header:
  77. sys.stdout.write(current_section_header)
  78. current_section_header = None
  79. sys.stdout.write(line)
  80. return ibtoolout.returncode
  81. def _CopyStringsFile(self, source, dest):
  82. """Copies a .strings file using iconv to reconvert the input into UTF-16."""
  83. input_code = self._DetectInputEncoding(source) or "UTF-8"
  84. # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call
  85. # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints
  86. # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing
  87. # semicolon in dictionary.
  88. # on invalid files. Do the same kind of validation.
  89. import CoreFoundation
  90. s = open(source, 'rb').read()
  91. d = CoreFoundation.CFDataCreate(None, s, len(s))
  92. _, error = CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None)
  93. if error:
  94. return
  95. fp = open(dest, 'wb')
  96. fp.write(s.decode(input_code).encode('UTF-16'))
  97. fp.close()
  98. def _DetectInputEncoding(self, file_name):
  99. """Reads the first few bytes from file_name and tries to guess the text
  100. encoding. Returns None as a guess if it can't detect it."""
  101. fp = open(file_name, 'rb')
  102. try:
  103. header = fp.read(3)
  104. except e:
  105. fp.close()
  106. return None
  107. fp.close()
  108. if header.startswith("\xFE\xFF"):
  109. return "UTF-16"
  110. elif header.startswith("\xFF\xFE"):
  111. return "UTF-16"
  112. elif header.startswith("\xEF\xBB\xBF"):
  113. return "UTF-8"
  114. else:
  115. return None
  116. def ExecCopyInfoPlist(self, source, dest, *keys):
  117. """Copies the |source| Info.plist to the destination directory |dest|."""
  118. # Read the source Info.plist into memory.
  119. fd = open(source, 'r')
  120. lines = fd.read()
  121. fd.close()
  122. # Insert synthesized key/value pairs (e.g. BuildMachineOSBuild).
  123. plist = plistlib.readPlistFromString(lines)
  124. if keys:
  125. plist = dict(plist.items() + json.loads(keys[0]).items())
  126. lines = plistlib.writePlistToString(plist)
  127. # Go through all the environment variables and replace them as variables in
  128. # the file.
  129. IDENT_RE = re.compile('[/\s]')
  130. for key in os.environ:
  131. if key.startswith('_'):
  132. continue
  133. evar = '${%s}' % key
  134. evalue = os.environ[key]
  135. lines = string.replace(lines, evar, evalue)
  136. # Xcode supports various suffices on environment variables, which are
  137. # all undocumented. :rfc1034identifier is used in the standard project
  138. # template these days, and :identifier was used earlier. They are used to
  139. # convert non-url characters into things that look like valid urls --
  140. # except that the replacement character for :identifier, '_' isn't valid
  141. # in a URL either -- oops, hence :rfc1034identifier was born.
  142. evar = '${%s:identifier}' % key
  143. evalue = IDENT_RE.sub('_', os.environ[key])
  144. lines = string.replace(lines, evar, evalue)
  145. evar = '${%s:rfc1034identifier}' % key
  146. evalue = IDENT_RE.sub('-', os.environ[key])
  147. lines = string.replace(lines, evar, evalue)
  148. # Remove any keys with values that haven't been replaced.
  149. lines = lines.split('\n')
  150. for i in range(len(lines)):
  151. if lines[i].strip().startswith("<string>${"):
  152. lines[i] = None
  153. lines[i - 1] = None
  154. lines = '\n'.join(filter(lambda x: x is not None, lines))
  155. # Write out the file with variables replaced.
  156. fd = open(dest, 'w')
  157. fd.write(lines)
  158. fd.close()
  159. # Now write out PkgInfo file now that the Info.plist file has been
  160. # "compiled".
  161. self._WritePkgInfo(dest)
  162. def _WritePkgInfo(self, info_plist):
  163. """This writes the PkgInfo file from the data stored in Info.plist."""
  164. plist = plistlib.readPlist(info_plist)
  165. if not plist:
  166. return
  167. # Only create PkgInfo for executable types.
  168. package_type = plist['CFBundlePackageType']
  169. if package_type != 'APPL':
  170. return
  171. # The format of PkgInfo is eight characters, representing the bundle type
  172. # and bundle signature, each four characters. If that is missing, four
  173. # '?' characters are used instead.
  174. signature_code = plist.get('CFBundleSignature', '????')
  175. if len(signature_code) != 4: # Wrong length resets everything, too.
  176. signature_code = '?' * 4
  177. dest = os.path.join(os.path.dirname(info_plist), 'PkgInfo')
  178. fp = open(dest, 'w')
  179. fp.write('%s%s' % (package_type, signature_code))
  180. fp.close()
  181. def ExecFlock(self, lockfile, *cmd_list):
  182. """Emulates the most basic behavior of Linux's flock(1)."""
  183. # Rely on exception handling to report errors.
  184. fd = os.open(lockfile, os.O_RDONLY|os.O_NOCTTY|os.O_CREAT, 0o666)
  185. fcntl.flock(fd, fcntl.LOCK_EX)
  186. return subprocess.call(cmd_list)
  187. def ExecFilterLibtool(self, *cmd_list):
  188. """Calls libtool and filters out '/path/to/libtool: file: foo.o has no
  189. symbols'."""
  190. libtool_re = re.compile(r'^.*libtool: file: .* has no symbols$')
  191. libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE)
  192. _, err = libtoolout.communicate()
  193. for line in err.splitlines():
  194. if not libtool_re.match(line):
  195. print >>sys.stderr, line
  196. return libtoolout.returncode
  197. def ExecPackageFramework(self, framework, version):
  198. """Takes a path to Something.framework and the Current version of that and
  199. sets up all the symlinks."""
  200. # Find the name of the binary based on the part before the ".framework".
  201. binary = os.path.basename(framework).split('.')[0]
  202. CURRENT = 'Current'
  203. RESOURCES = 'Resources'
  204. VERSIONS = 'Versions'
  205. if not os.path.exists(os.path.join(framework, VERSIONS, version, binary)):
  206. # Binary-less frameworks don't seem to contain symlinks (see e.g.
  207. # chromium's out/Debug/org.chromium.Chromium.manifest/ bundle).
  208. return
  209. # Move into the framework directory to set the symlinks correctly.
  210. pwd = os.getcwd()
  211. os.chdir(framework)
  212. # Set up the Current version.
  213. self._Relink(version, os.path.join(VERSIONS, CURRENT))
  214. # Set up the root symlinks.
  215. self._Relink(os.path.join(VERSIONS, CURRENT, binary), binary)
  216. self._Relink(os.path.join(VERSIONS, CURRENT, RESOURCES), RESOURCES)
  217. # Back to where we were before!
  218. os.chdir(pwd)
  219. def _Relink(self, dest, link):
  220. """Creates a symlink to |dest| named |link|. If |link| already exists,
  221. it is overwritten."""
  222. if os.path.lexists(link):
  223. os.remove(link)
  224. os.symlink(dest, link)
  225. def ExecCodeSignBundle(self, key, resource_rules, entitlements, provisioning):
  226. """Code sign a bundle.
  227. This function tries to code sign an iOS bundle, following the same
  228. algorithm as Xcode:
  229. 1. copy ResourceRules.plist from the user or the SDK into the bundle,
  230. 2. pick the provisioning profile that best match the bundle identifier,
  231. and copy it into the bundle as embedded.mobileprovision,
  232. 3. copy Entitlements.plist from user or SDK next to the bundle,
  233. 4. code sign the bundle.
  234. """
  235. resource_rules_path = self._InstallResourceRules(resource_rules)
  236. substitutions, overrides = self._InstallProvisioningProfile(
  237. provisioning, self._GetCFBundleIdentifier())
  238. entitlements_path = self._InstallEntitlements(
  239. entitlements, substitutions, overrides)
  240. subprocess.check_call([
  241. 'codesign', '--force', '--sign', key, '--resource-rules',
  242. resource_rules_path, '--entitlements', entitlements_path,
  243. os.path.join(
  244. os.environ['TARGET_BUILD_DIR'],
  245. os.environ['FULL_PRODUCT_NAME'])])
  246. def _InstallResourceRules(self, resource_rules):
  247. """Installs ResourceRules.plist from user or SDK into the bundle.
  248. Args:
  249. resource_rules: string, optional, path to the ResourceRules.plist file
  250. to use, default to "${SDKROOT}/ResourceRules.plist"
  251. Returns:
  252. Path to the copy of ResourceRules.plist into the bundle.
  253. """
  254. source_path = resource_rules
  255. target_path = os.path.join(
  256. os.environ['BUILT_PRODUCTS_DIR'],
  257. os.environ['CONTENTS_FOLDER_PATH'],
  258. 'ResourceRules.plist')
  259. if not source_path:
  260. source_path = os.path.join(
  261. os.environ['SDKROOT'], 'ResourceRules.plist')
  262. shutil.copy2(source_path, target_path)
  263. return target_path
  264. def _InstallProvisioningProfile(self, profile, bundle_identifier):
  265. """Installs embedded.mobileprovision into the bundle.
  266. Args:
  267. profile: string, optional, short name of the .mobileprovision file
  268. to use, if empty or the file is missing, the best file installed
  269. will be used
  270. bundle_identifier: string, value of CFBundleIdentifier from Info.plist
  271. Returns:
  272. A tuple containing two dictionary: variables substitutions and values
  273. to overrides when generating the entitlements file.
  274. """
  275. source_path, provisioning_data, team_id = self._FindProvisioningProfile(
  276. profile, bundle_identifier)
  277. target_path = os.path.join(
  278. os.environ['BUILT_PRODUCTS_DIR'],
  279. os.environ['CONTENTS_FOLDER_PATH'],
  280. 'embedded.mobileprovision')
  281. shutil.copy2(source_path, target_path)
  282. substitutions = self._GetSubstitutions(bundle_identifier, team_id + '.')
  283. return substitutions, provisioning_data['Entitlements']
  284. def _FindProvisioningProfile(self, profile, bundle_identifier):
  285. """Finds the .mobileprovision file to use for signing the bundle.
  286. Checks all the installed provisioning profiles (or if the user specified
  287. the PROVISIONING_PROFILE variable, only consult it) and select the most
  288. specific that correspond to the bundle identifier.
  289. Args:
  290. profile: string, optional, short name of the .mobileprovision file
  291. to use, if empty or the file is missing, the best file installed
  292. will be used
  293. bundle_identifier: string, value of CFBundleIdentifier from Info.plist
  294. Returns:
  295. A tuple of the path to the selected provisioning profile, the data of
  296. the embedded plist in the provisioning profile and the team identifier
  297. to use for code signing.
  298. Raises:
  299. SystemExit: if no .mobileprovision can be used to sign the bundle.
  300. """
  301. profiles_dir = os.path.join(
  302. os.environ['HOME'], 'Library', 'MobileDevice', 'Provisioning Profiles')
  303. if not os.path.isdir(profiles_dir):
  304. print >>sys.stderr, (
  305. 'cannot find mobile provisioning for %s' % bundle_identifier)
  306. sys.exit(1)
  307. provisioning_profiles = None
  308. if profile:
  309. profile_path = os.path.join(profiles_dir, profile + '.mobileprovision')
  310. if os.path.exists(profile_path):
  311. provisioning_profiles = [profile_path]
  312. if not provisioning_profiles:
  313. provisioning_profiles = glob.glob(
  314. os.path.join(profiles_dir, '*.mobileprovision'))
  315. valid_provisioning_profiles = {}
  316. for profile_path in provisioning_profiles:
  317. profile_data = self._LoadProvisioningProfile(profile_path)
  318. app_id_pattern = profile_data.get(
  319. 'Entitlements', {}).get('application-identifier', '')
  320. for team_identifier in profile_data.get('TeamIdentifier', []):
  321. app_id = '%s.%s' % (team_identifier, bundle_identifier)
  322. if fnmatch.fnmatch(app_id, app_id_pattern):
  323. valid_provisioning_profiles[app_id_pattern] = (
  324. profile_path, profile_data, team_identifier)
  325. if not valid_provisioning_profiles:
  326. print >>sys.stderr, (
  327. 'cannot find mobile provisioning for %s' % bundle_identifier)
  328. sys.exit(1)
  329. # If the user has multiple provisioning profiles installed that can be
  330. # used for ${bundle_identifier}, pick the most specific one (ie. the
  331. # provisioning profile whose pattern is the longest).
  332. selected_key = max(valid_provisioning_profiles, key=lambda v: len(v))
  333. return valid_provisioning_profiles[selected_key]
  334. def _LoadProvisioningProfile(self, profile_path):
  335. """Extracts the plist embedded in a provisioning profile.
  336. Args:
  337. profile_path: string, path to the .mobileprovision file
  338. Returns:
  339. Content of the plist embedded in the provisioning profile as a dictionary.
  340. """
  341. with tempfile.NamedTemporaryFile() as temp:
  342. subprocess.check_call([
  343. 'security', 'cms', '-D', '-i', profile_path, '-o', temp.name])
  344. return self._LoadPlistMaybeBinary(temp.name)
  345. def _LoadPlistMaybeBinary(self, plist_path):
  346. """Loads into a memory a plist possibly encoded in binary format.
  347. This is a wrapper around plistlib.readPlist that tries to convert the
  348. plist to the XML format if it can't be parsed (assuming that it is in
  349. the binary format).
  350. Args:
  351. plist_path: string, path to a plist file, in XML or binary format
  352. Returns:
  353. Content of the plist as a dictionary.
  354. """
  355. try:
  356. # First, try to read the file using plistlib that only supports XML,
  357. # and if an exception is raised, convert a temporary copy to XML and
  358. # load that copy.
  359. return plistlib.readPlist(plist_path)
  360. except:
  361. pass
  362. with tempfile.NamedTemporaryFile() as temp:
  363. shutil.copy2(plist_path, temp.name)
  364. subprocess.check_call(['plutil', '-convert', 'xml1', temp.name])
  365. return plistlib.readPlist(temp.name)
  366. def _GetSubstitutions(self, bundle_identifier, app_identifier_prefix):
  367. """Constructs a dictionary of variable substitutions for Entitlements.plist.
  368. Args:
  369. bundle_identifier: string, value of CFBundleIdentifier from Info.plist
  370. app_identifier_prefix: string, value for AppIdentifierPrefix
  371. Returns:
  372. Dictionary of substitutions to apply when generating Entitlements.plist.
  373. """
  374. return {
  375. 'CFBundleIdentifier': bundle_identifier,
  376. 'AppIdentifierPrefix': app_identifier_prefix,
  377. }
  378. def _GetCFBundleIdentifier(self):
  379. """Extracts CFBundleIdentifier value from Info.plist in the bundle.
  380. Returns:
  381. Value of CFBundleIdentifier in the Info.plist located in the bundle.
  382. """
  383. info_plist_path = os.path.join(
  384. os.environ['TARGET_BUILD_DIR'],
  385. os.environ['INFOPLIST_PATH'])
  386. info_plist_data = self._LoadPlistMaybeBinary(info_plist_path)
  387. return info_plist_data['CFBundleIdentifier']
  388. def _InstallEntitlements(self, entitlements, substitutions, overrides):
  389. """Generates and install the ${BundleName}.xcent entitlements file.
  390. Expands variables "$(variable)" pattern in the source entitlements file,
  391. add extra entitlements defined in the .mobileprovision file and the copy
  392. the generated plist to "${BundlePath}.xcent".
  393. Args:
  394. entitlements: string, optional, path to the Entitlements.plist template
  395. to use, defaults to "${SDKROOT}/Entitlements.plist"
  396. substitutions: dictionary, variable substitutions
  397. overrides: dictionary, values to add to the entitlements
  398. Returns:
  399. Path to the generated entitlements file.
  400. """
  401. source_path = entitlements
  402. target_path = os.path.join(
  403. os.environ['BUILT_PRODUCTS_DIR'],
  404. os.environ['PRODUCT_NAME'] + '.xcent')
  405. if not source_path:
  406. source_path = os.path.join(
  407. os.environ['SDKROOT'],
  408. 'Entitlements.plist')
  409. shutil.copy2(source_path, target_path)
  410. data = self._LoadPlistMaybeBinary(target_path)
  411. data = self._ExpandVariables(data, substitutions)
  412. if overrides:
  413. for key in overrides:
  414. if key not in data:
  415. data[key] = overrides[key]
  416. plistlib.writePlist(data, target_path)
  417. return target_path
  418. def _ExpandVariables(self, data, substitutions):
  419. """Expands variables "$(variable)" in data.
  420. Args:
  421. data: object, can be either string, list or dictionary
  422. substitutions: dictionary, variable substitutions to perform
  423. Returns:
  424. Copy of data where each references to "$(variable)" has been replaced
  425. by the corresponding value found in substitutions, or left intact if
  426. the key was not found.
  427. """
  428. if isinstance(data, str):
  429. for key, value in substitutions.iteritems():
  430. data = data.replace('$(%s)' % key, value)
  431. return data
  432. if isinstance(data, list):
  433. return [self._ExpandVariables(v, substitutions) for v in data]
  434. if isinstance(data, dict):
  435. return dict((k, self._ExpandVariables(data[k],
  436. substitutions)) for k in data)
  437. return data
  438. if __name__ == '__main__':
  439. sys.exit(main(sys.argv[1:]))