• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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: str or bytes):
29    """
30    Splits the Xcode version to 3 values.
31
32    >>> list(split_version('8.2.1.1'))
33    ['8', '2', '1']
34    >>> list(split_version('9.3'))
35    ['9', '3', '0']
36    >>> list(split_version('10.0'))
37    ['10', '0', '0']
38    """
39    if isinstance(version, bytes):
40        version = version.decode()
41    version = version.split('.')
42    return itertools.islice(itertools.chain(version, itertools.repeat('0')), 0, 3)
43
44
45def format_version(version: str or bytes):
46    """
47    Converts Xcode version to a format required for DTXcode in Info.plist
48
49    >>> format_version('8.2.1')
50    '0821'
51    >>> format_version('9.3')
52    '0930'
53    >>> format_version('10.0')
54    '1000'
55    """
56    major, minor, patch = split_version(version)
57    return ('%2s%s%s' % (major, minor, patch)).replace(' ', '0')
58
59
60def fill_xcode_version(fill_settings: dict) -> dict:
61    """Fills the Xcode version and build number into |fill_settings|."""
62
63    try:
64        lines = subprocess.check_output(['xcodebuild', '-version']).splitlines()
65        fill_settings['xcode_version'] = format_version(lines[0].split()[-1])
66        fill_settings['xcode_version_int'] = int(fill_settings['xcode_version'], 10)
67        fill_settings['xcode_build'] = lines[-1].split()[-1]
68    except subprocess.CalledProcessError as cpe:
69        print(f"Failed to run xcodebuild -version: {cpe}")
70
71
72def fill_machine_os_build(fill_settings: dict):
73    """Fills OS build number into |fill_settings|."""
74    fill_settings['machine_os_build'] = subprocess.check_output(
75        ['sw_vers', '-buildVersion']).strip()
76
77
78def fill_sdk_path_and_version(fill_settings: dict, platform: str, xcode_version: str or bytes):
79    """Fills the SDK path and version for |platform| into |fill_settings|."""
80    fill_settings['sdk_path'] = subprocess.check_output([
81        'xcrun', '-sdk', platform, '--show-sdk-path']).strip()
82    fill_settings['sdk_version'] = subprocess.check_output([
83        'xcrun', '-sdk', platform, '--show-sdk-version']).strip()
84    fill_settings['sdk_platform_path'] = subprocess.check_output([
85        'xcrun', '-sdk', platform, '--show-sdk-platform-path']).strip()
86    if xcode_version >= '0720':
87        fill_settings['sdk_build'] = subprocess.check_output([
88          'xcrun', '-sdk', platform, '--show-sdk-build-version']).strip()
89    else:
90        fill_settings['sdk_build'] = fill_settings['sdk_version']
91
92
93if __name__ == '__main__':
94    doctest.testmod()
95
96    parser = argparse.ArgumentParser()
97    parser.add_argument("--developer_dir", required=False)
98    args, unknownargs = parser.parse_known_args()
99    if args.developer_dir:
100        os.environ['DEVELOPER_DIR'] = args.developer_dir
101
102    if len(unknownargs) != 1:
103        sys.stderr.write(
104          'usage: %s [iphoneos|iphonesimulator|macosx]\n' %
105          os.path.basename(sys.argv[0]))
106        sys.exit(1)
107
108    settings = {}
109    fill_machine_os_build(settings)
110    fill_xcode_version(settings)
111    try:
112        fill_sdk_path_and_version(settings, unknownargs[0], settings.get('xcode_version'))
113    except ValueError as vle:
114        print(f"Error: {vle}")
115
116    for key in sorted(settings):
117        value = settings.get(key)
118        if isinstance(value, bytes):
119            value = value.decode()
120        if key != 'xcode_version_int':
121            value = '"%s"' % value
122            print('%s=%s' % (key, value))
123        else:
124            print('%s=%d' % (key, value))
125