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