Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
find_sdk.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Prints the lowest locally available SDK version greater than or equal to a
6given minimum sdk version to standard output.
7
8Usage:
9 python3 find_sdk.py 10.6 # Ignores SDKs < 10.6
10"""
11
12import os
13import re
14import subprocess
15import sys
16
17from optparse import OptionParser
18
19
20# sdk/build/xcode_links
21ROOT_SRC_DIR = os.path.join(
22 os.path.dirname(
23 os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
24
25
27 """
28 Create symlink to Xcode directory at target location, which can be absolute or
29 relative to `ROOT_SRC_DIR`.
30 """
31
32 # If `dst` is relative, it is assumed to be relative to src root.
33 if not os.path.isabs(dst):
34 dst = os.path.join(ROOT_SRC_DIR, dst)
35
36 if not os.path.isdir(dst):
37 os.makedirs(dst)
38
39 dst = os.path.join(dst, os.path.basename(src))
40
41 # Update the symlink if exists.
42 if os.path.islink(dst):
43 current_src = os.readlink(dst)
44 if current_src == src:
45 return dst
46
47 os.unlink(dst)
48 sys.stderr.write('existing symlink %s points %s; want %s. Removed.' %
49 (dst, current_src, src))
50
51 os.symlink(src, dst)
52 return dst
53
54
55def parse_version(version_str):
56 """'10.6' => [10, 6]"""
57 return list(map(int, re.findall(r'(\d+)', version_str)))
58
59
60def main():
61 parser = OptionParser()
62 parser.add_option(
63 "--verify",
64 action="store_true",
65 dest="verify",
66 default=False,
67 help="return the sdk argument and warn if it doesn't exist")
68 parser.add_option(
69 "--sdk_path",
70 action="store",
71 type="string",
72 dest="sdk_path",
73 default="",
74 help="user-specified SDK path; bypasses verification")
75 parser.add_option(
76 "--print_sdk_path",
77 action="store_true",
78 dest="print_sdk_path",
79 default=False,
80 help="Additionally print the path the SDK (appears first).")
81 parser.add_option(
82 "--create_symlink_at",
83 action="store",
84 dest="create_symlink_at",
85 help=
86 "Create symlink to SDK at given location and return symlink path as SDK "
87 "info instead of the original location.")
88 (options, args) = parser.parse_args()
89 min_sdk_version = args[0]
90
91 job = subprocess.Popen(['xcode-select', '-print-path'],
92 stdout=subprocess.PIPE,
93 stderr=subprocess.STDOUT,
94 universal_newlines=True)
95 out, err = job.communicate()
96 if job.returncode != 0:
97 print(out, file=sys.stderr)
98 print(err, file=sys.stderr)
99 raise Exception('Error %d running xcode-select' % job.returncode)
100 sdk_dir = os.path.join(out.rstrip(),
101 'Platforms/MacOSX.platform/Developer/SDKs')
102 if not os.path.isdir(sdk_dir):
103 raise Exception(
104 'Install Xcode, launch it, accept the license ' +
105 'agreement, and run `sudo xcode-select -s /path/to/Xcode.app` ' +
106 'to continue.')
107 sdks = [
108 re.findall('^MacOSX(\d+\.\d+)\.sdk$', s) for s in os.listdir(sdk_dir)
109 ]
110 sdks = [s[0] for s in sdks if s] # [['10.5'], ['10.6']] => ['10.5', '10.6']
111 sdks = [
112 s for s in sdks # ['10.5', '10.6'] => ['10.6']
113 if parse_version(s) >= parse_version(min_sdk_version)
114 ]
115 if not sdks:
116 raise Exception('No %s+ SDK found' % min_sdk_version)
117 best_sdk = sorted(sdks, key=parse_version)[0]
118
119 if options.verify and best_sdk != min_sdk_version and not options.sdk_path:
120 print('''
121 vvvvvvv
122
123This build requires the %s SDK, but it was not found on your system.
124
125Either install it, or explicitly set mac_sdk in your GYP_DEFINES.
126
127 ^^^^^^^'
128''' % min_sdk_version,
129 file=sys.stderr)
130 return min_sdk_version
131
132 if options.print_sdk_path:
133 sdk_path = subprocess.check_output(
134 ['xcodebuild', '-version', '-sdk', 'macosx' + best_sdk, 'Path'],
135 universal_newlines=True).strip()
136 if options.create_symlink_at:
137 print(CreateSymlinkForSDKAt(sdk_path, options.create_symlink_at))
138 else:
139 print(sdk_path)
140
141 return best_sdk
142
143
144if __name__ == '__main__':
145 if sys.platform != 'darwin':
146 raise Exception("This script only runs on Mac")
147 print(main())
void print(void *str)
Definition bridge.cpp:126
CreateSymlinkForSDKAt(src, dst)
Definition find_sdk.py:26
parse_version(version_str)
Definition find_sdk.py:55
Definition main.py:1