1#!/usr/bin/env python3 2# Copyright 2014 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import argparse 7import doctest 8import itertools 9import os 10import plistlib 11import re 12import subprocess 13import sys 14 15# This script prints information about the build system, the operating 16# system and the iOS or Mac SDK (depending on the platform "iphonesimulator", 17# "iphoneos" or "macosx" generally). 18 19 20def SplitVersion(version): 21 """Splits the Xcode version to 3 values. 22 23 >>> list(SplitVersion('8.2.1.1')) 24 ['8', '2', '1'] 25 >>> list(SplitVersion('9.3')) 26 ['9', '3', '0'] 27 >>> list(SplitVersion('10.0')) 28 ['10', '0', '0'] 29 """ 30 version = version.split('.') 31 return itertools.islice(itertools.chain(version, itertools.repeat('0')), 0, 3) 32 33 34def FormatVersion(version): 35 """Converts Xcode version to a format required for DTXcode in Info.plist 36 37 >>> FormatVersion('8.2.1') 38 '0821' 39 >>> FormatVersion('9.3') 40 '0930' 41 >>> FormatVersion('10.0') 42 '1000' 43 """ 44 major, minor, patch = SplitVersion(version) 45 return ('%2s%s%s' % (major, minor, patch)).replace(' ', '0') 46 47 48def FillXcodeVersion(settings, developer_dir): 49 """Fills the Xcode version and build number into |settings|.""" 50 if developer_dir: 51 xcode_version_plist_path = os.path.join(developer_dir, 52 'Contents/version.plist') 53 with open(xcode_version_plist_path, 'rb') as f: 54 version_plist = plistlib.load(f) 55 settings['xcode_version'] = FormatVersion( 56 version_plist['CFBundleShortVersionString']) 57 settings['xcode_version_int'] = int(settings['xcode_version'], 10) 58 settings['xcode_build'] = version_plist['ProductBuildVersion'] 59 return 60 61 lines = subprocess.check_output(['xcodebuild', 62 '-version']).decode('UTF-8').splitlines() 63 settings['xcode_version'] = FormatVersion(lines[0].split()[-1]) 64 settings['xcode_version_int'] = int(settings['xcode_version'], 10) 65 settings['xcode_build'] = lines[-1].split()[-1] 66 67 68def FillMachineOSBuild(settings): 69 """Fills OS build number into |settings|.""" 70 machine_os_build = subprocess.check_output(['sw_vers', '-buildVersion' 71 ]).decode('UTF-8').strip() 72 settings['machine_os_build'] = machine_os_build 73 74 75def FillSDKPathAndVersion(settings, platform, xcode_version): 76 """Fills the SDK path and version for |platform| into |settings|.""" 77 settings['sdk_path'] = subprocess.check_output( 78 ['xcrun', '-sdk', platform, '--show-sdk-path']).decode('UTF-8').strip() 79 settings['sdk_version'] = subprocess.check_output( 80 ['xcrun', '-sdk', platform, 81 '--show-sdk-version']).decode('UTF-8').strip() 82 settings['sdk_platform_path'] = subprocess.check_output( 83 ['xcrun', '-sdk', platform, 84 '--show-sdk-platform-path']).decode('UTF-8').strip() 85 settings['sdk_build'] = subprocess.check_output( 86 ['xcrun', '-sdk', platform, 87 '--show-sdk-build-version']).decode('UTF-8').strip() 88 settings['toolchains_path'] = os.path.join( 89 subprocess.check_output(['xcode-select', 90 '-print-path']).decode('UTF-8').strip(), 91 'Toolchains/XcodeDefault.xctoolchain') 92 93 94def CreateXcodeSymlinkAt(src, dst, root_build_dir): 95 """Create symlink to Xcode directory at target location.""" 96 97 if not os.path.isdir(dst): 98 os.makedirs(dst) 99 100 dst = os.path.join(dst, os.path.basename(src)) 101 updated_value = os.path.join(root_build_dir, dst) 102 103 # Update the symlink only if it is different from the current destination. 104 if os.path.islink(dst): 105 current_src = os.readlink(dst) 106 if current_src == src: 107 return updated_value 108 os.unlink(dst) 109 sys.stderr.write('existing symlink %s points %s; want %s. Removed.' % 110 (dst, current_src, src)) 111 os.symlink(src, dst) 112 return updated_value 113 114 115def main(): 116 doctest.testmod() 117 118 parser = argparse.ArgumentParser() 119 parser.add_argument('--developer_dir') 120 parser.add_argument('--get_sdk_info', 121 action='store_true', 122 default=False, 123 help='Returns SDK info in addition to xcode info.') 124 parser.add_argument('--get_machine_info', 125 action='store_true', 126 default=False, 127 help='Returns machine info in addition to xcode info.') 128 parser.add_argument('--create_symlink_at', 129 help='Create symlink of SDK at given location and ' 130 'returns the symlinked paths as SDK info instead ' 131 'of the original location.') 132 parser.add_argument('--root_build_dir', 133 default='.', 134 help='Value of gn $root_build_dir') 135 parser.add_argument('platform', 136 choices=[ 137 'appletvos', 138 'appletvsimulator', 139 'iphoneos', 140 'iphonesimulator', 141 'macosx', 142 'watchos', 143 'watchsimulator', 144 ]) 145 args = parser.parse_args() 146 if args.developer_dir: 147 os.environ['DEVELOPER_DIR'] = args.developer_dir 148 149 settings = {} 150 if args.get_machine_info: 151 FillMachineOSBuild(settings) 152 FillXcodeVersion(settings, args.developer_dir) 153 if args.get_sdk_info: 154 FillSDKPathAndVersion(settings, args.platform, settings['xcode_version']) 155 156 for key in sorted(settings): 157 value = settings[key] 158 if args.create_symlink_at and '_path' in key: 159 value = CreateXcodeSymlinkAt(value, args.create_symlink_at, 160 args.root_build_dir) 161 if isinstance(value, str): 162 value = '"%s"' % value 163 print('%s=%s' % (key, value)) 164 165 166if __name__ == '__main__': 167 sys.exit(main()) 168