• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import doctest
18import itertools
19import os
20import subprocess
21import sys
22
23# This script prints information about the build system, the operating
24# system and the iOS or Mac SDK (depending on the platform "iphonesimulator",
25# "iphoneos" or "macosx" generally).
26
27
28def split_version(version):
29    """Splits the Xcode version to 3 values.
30
31    >>> list(SplitVersion('8.2.1.1'))
32    ['8', '2', '1']
33    >>> list(SplitVersion('9.3'))
34    ['9', '3', '0']
35    >>> list(SplitVersion('10.0'))
36    ['10', '0', '0']
37    """
38    if isinstance(version, bytes):
39        version = version.decode()
40    version = version.split('.')
41    return itertools.islice(itertools.chain(version, itertools.repeat('0')), 0, 3)
42
43
44def format_version(version):
45    """Converts Xcode version to a format required for DTXcode in Info.plist
46
47    >>> FormatVersion('8.2.1')
48    '0821'
49    >>> FormatVersion('9.3')
50    '0930'
51    >>> FormatVersion('10.0')
52    '1000'
53    """
54    major, minor, patch = split_version(version)
55    return ('%2s%s%s' % (major, minor, patch)).replace(' ', '0')
56
57
58def fill_xcode_version(settings_xcode):
59    """Fills the Xcode version and build number into |settings_xcode|."""
60    lines = subprocess.check_output(['xcodebuild', '-version']).splitlines()
61    settings_xcode['xcode_version'] = format_version(lines[0].split()[-1])
62    settings_xcode['xcode_version_int'] = int(settings_xcode['xcode_version'], 10)
63    settings_xcode['xcode_build'] = lines[-1].split()[-1]
64
65
66def fill_machine_os_build(settings_os):
67    """Fills OS build number into |settings_os|."""
68    settings_os['machine_os_build'] = subprocess.check_output(
69        ['sw_vers', '-buildVersion']).strip()
70
71
72def fill_sdk_path_and_version(settings_sdk, platform, xcode_version):
73    """Fills the SDK path and version for |platform| into |settings|."""
74    settings_sdk['sdk_path'] = subprocess.check_output([
75        'xcrun', '-sdk', platform, '--show-sdk-path']).strip()
76    settings_sdk['sdk_version'] = subprocess.check_output([
77        'xcrun', '-sdk', platform, '--show-sdk-version']).strip()
78    settings_sdk['sdk_platform_path'] = subprocess.check_output([
79        'xcrun', '-sdk', platform, '--show-sdk-platform-path']).strip()
80    if xcode_version >= '0720':
81        settings_sdk['sdk_build'] = subprocess.check_output([
82            'xcrun', '-sdk', platform, '--show-sdk-build-version']).strip()
83    else:
84        settings_sdk['sdk_build'] = settings_sdk['sdk_version']
85
86
87if __name__ == '__main__':
88    doctest.testmod()
89
90    parser = argparse.ArgumentParser()
91    parser.add_argument("--developer_dir", required=False)
92    args, unknownargs = parser.parse_known_args()
93    if args.developer_dir:
94        os.environ['DEVELOPER_DIR'] = args.developer_dir
95
96    if len(unknownargs) != 1:
97        sys.stderr.write(
98            'usage: %s [iphoneos|iphonesimulator|macosx]\n' %
99            os.path.basename(sys.argv[0]))
100        sys.exit(1)
101
102    settings = {}
103    fill_machine_os_build(settings)
104    fill_xcode_version(settings)
105    test_xcode_version = settings.get('xcode_version')
106    if test_xcode_version is None:
107        raise ValueError("Xcode version is not set or invalid.")
108    fill_sdk_path_and_version(settings, unknownargs[0], test_xcode_version)
109
110    for key in sorted(settings):
111        value = settings[key]
112        if isinstance(value, bytes):
113            value = value.decode()
114        if key != 'xcode_version_int':
115            value = '"%s"' % value
116            print('%s=%s' % (key, value))
117        else:
118            print('%s=%d' % (key, value))
119