-
Notifications
You must be signed in to change notification settings - Fork 150
/
project_precommit_check
executable file
·259 lines (241 loc) · 11.2 KB
/
project_precommit_check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
# ===--- project_precommit_check ------------------------------------------===
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ===----------------------------------------------------------------------===
import sys
import os
import argparse
import platform
import json
import common
script_path = os.path.dirname(os.path.realpath(__file__))
project_list = os.path.join(script_path, 'projects.json')
runner_path = os.path.join(script_path, 'runner.py')
supported_configs = {
'Darwin': {
'3.0': {
'version': 'Apple Swift version 3.0 '
'(swiftlang-800.0.46.2 clang-800.0.38)\n'
'Target: x86_64-apple-macosx10.9\n',
'description': 'Xcode 8.0 (contains Swift 3.0)',
'branch': 'swift-3.0-branch'
},
'3.1': {
'version': 'Apple Swift version 3.1 '
'(swiftlang-802.0.53 clang-802.0.42)\n'
'Target: x86_64-apple-macosx10.9\n',
'description': 'Xcode 8.3.2 (contains Swift 3.1)',
'branch': 'swift-3.0-branch'
},
'4.0': {
'version': 'Apple Swift version 4.0 '
'(swiftlang-900.0.65.2 clang-900.0.37)\n'
'Target: x86_64-apple-macosx10.9\n',
'description': 'Xcode 9.0.1 (contains Swift 4.0)',
'branch': 'swift-4.0-branch'
},
'3.2': {
'version': 'Apple Swift version 4.0.3 '
'(swiftlang-900.0.74.1 clang-900.0.39.2)\n'
'Target: x86_64-apple-macosx10.9\n',
'description': 'Xcode 9.2 (contains Swift 3.2 compatibility mode)',
'branch': 'swift-4.0-branch'
},
'4.0.3': {
'version': 'Apple Swift version 4.0.3 '
'(swiftlang-900.0.74.1 clang-900.0.39.2)\n'
'Target: x86_64-apple-macosx10.9\n',
'description': 'Xcode 9.2 (contains Swift 4.0.3)',
'branch': 'swift-4.0-branch'
},
'4.1': {
'version': 'Apple Swift version 4.1 '
'(swiftlang-902.0.48 clang-902.0.37.1)\n'
'Target: x86_64-apple-darwin17.7.0\n',
'description': 'Xcode 9.3 (contains Swift 4.1)',
'branch': 'swift-4.1-branch'
},
'4.2': {
'version': 'Apple Swift version 4.2 '
'(swiftlang-1000.11.37.1 clang-1000.11.45.1)\n'
'Target: x86_64-apple-darwin17.7.0\n',
'description': 'Xcode 10.0 (contains Swift 4.2)',
'branch': 'swift-4.2-branch'
},
'4.2.1': {
'version': 'Apple Swift version 4.2.1 '
'(swiftlang-1000.11.42 clang-1000.11.45.1)\n'
'Target: x86_64-apple-darwin17.7.0\n',
'description': 'Xcode 10.1 (contains Swift 4.2.1)',
'branch': 'swift-4.2-branch'
},
'5.0': {
'version': 'Apple Swift version 5.0 '
'(swiftlang-1001.0.69.5 clang-1001.0.46.3)\n'
'Target: x86_64-apple-darwin18.2.0\n',
'description': 'Xcode 10.2 (contains Swift 5.0)',
'branch': 'swift-5.0-branch'
},
'5.1': {
'version': 'Apple Swift version 5.1 '
'(swiftlang-1100.0.270.6 clang-1100.0.32.1)\n'
'Target: x86_64-apple-darwin18.2.0\n',
'description': 'Xcode 11 Beta 6 (contains Swift 5.1)',
'branch': 'swift-5.1-branch'
},
'5.2': {
'version': 'Apple Swift version 5.2.4 '
'(swiftlang-1103.0.32.9 clang-1103.0.32.53)\n'
'Target: x86_64-apple-darwin18.2.0\n',
'description': 'Xcode 11.5 (contains Swift 5.2.4)',
'branch': 'swift-5.2-branch'
},
'5.3': {
'version': 'Apple Swift version 5.3.2 '
'(swiftlang-1200.0.45 clang-1200.0.32.28)\n'
'Target: x86_64-apple-darwin20.3.0\n',
'description': 'Xcode 12.4 (contains Swift 5.3.2)',
'branch': 'swift-5.3-branch'
},
'5.7': {
'version': 'Apple Swift version 5.7.0 '
'(swiftlang-5.7.0.123.8 clang-1400.0.29.50)\n'
'Target: x86_64-apple-darwin22.3.0\n',
'description': 'Xcode 14.0 (contains Swift 5.7.0)',
'branch': 'swift-5.7-branch'
},
'5.9': {
'version': 'Apple Swift version 5.9 '
'(swiftlang-5.9.0.128.108 clang-1500.0.40.1)\n'
'Target: x86_64-apple-macosx14.0\n',
'description': 'Xcode 15.0 (contains Swift 5.9.0)',
'branch': 'swift-5.9-branch'
},
'5.10': {
'version': 'Apple Swift version 5.10 '
'(swiftlang-5.10.0.13 clang-1500.3.9.4)\n'
'Target: x86_64-apple-macosx14.0\n',
'description': 'Xcode 15.4 (contains Swift 5.10.0)',
'branch': 'swift-5.10-branch'
}
},
# NOTE: Linux isn't fully supported yet
'Linux': {
'3.0': {
'version': 'Swift version 3.0 '
'(swift-3.0-RELEASE)\n'
'Target: x86_64-unknown-linux-gnu\n',
'description': 'swift-3.0-RELEASE toolchain',
'branch': 'swift-3.0-branch'
},
'3.1': {
'version': 'Swift version 3.1 '
'(swift-3.1-RELEASE)\n'
'Target: x86_64-unknown-linux-gnu\n',
'description': 'swift-3.1-RELEASE toolchain',
'branch': 'swift-3.0-branch'
}
}
}
def parse_args():
parser = argparse.ArgumentParser(
description='Check a project for inclusion in the Swift source compatibility test suite'
)
parser.add_argument('project',
help='the project to test, specified by the "path" index field')
parser.add_argument('--earliest-compatible-swift-version',
metavar='VERSION',
help='the Swift version mode to check, including decimal point (e.g. 3.0)',
required=True)
parser.add_argument('--swiftc',
metavar='PATH',
help='swiftc executable')
return parser.parse_args()
def validate(project_path, compatibility_version):
common.debug_print("--- Validating %s Swift version %s compatibility ---" % (project_path, compatibility_version))
with open(project_list) as projects:
index = json.loads(projects.read())
project = next((p for p in index if p['path'] == project_path), None)
if not project:
common.debug_print("error: Unable to find %s in project index ---" % project_path)
exit(1)
try:
compatibility_dict = {version['version']: version for version in project['compatibility']}
compatibility_dict[compatibility_version]
except (IndexError, KeyError, TypeError) as e:
common.debug_print("error: Unable to find %s Swift version %s compatibility in project index ---" % (project_path, compatibility_version))
common.debug_print("note: Please add a version entry for %s to the `compatibility` field for %s in the project index or check against a version that has already been added ---" % (compatibility_version, project_path))
raise
common.debug_print("--- Project configured to be compatible with Swift %s ---" % compatibility_version)
common.debug_print("--- Checking %s platform compatibility with %s ---" % (project_path, platform.system()))
if not platform.system() in project['platforms']:
common.debug_print("error: %s is not configured to be compatible with %s ---" % (project_path, platform.system()))
common.debug_print("-- Add %s to the platforms list for %s ---" % (platform.system(), project_path))
exit(1)
common.debug_print("--- Platform compatibility check succeeded ---")
def is_correct_swift_version(swiftc, compatibility_version):
common.debug_print("--- Checking installed Swift version ---")
swift_version = common.check_execute_output([swiftc, '--version'])
try:
expected_swift_version = supported_configs[platform.system()][compatibility_version]['version']
except KeyError:
common.debug_print("error: Project configured for unsupported platform or Swift version")
raise
if swift_version.split("\n")[0] != expected_swift_version.split("\n")[0]:
common.debug_print("--- Version check failed ---")
common.debug_print("Expected version:\n" + expected_swift_version)
common.debug_print("Current version:\n" + swift_version)
common.debug_print("warning: Unexpected swiftc version. "\
"Expected swiftc for Swift {compat_vers} can be found in {description}.".format(
description=supported_configs[platform.system()][compatibility_version]['description'],
compat_vers=compatibility_version)
)
return False
common.debug_print("--- Version check succeeded ---")
return True
def check(project, swift_branch, swiftc, compatibility_version):
if not swiftc:
if platform.system() == 'Darwin':
common.debug_print("--- Locating swiftc executable ---")
swiftc = common.check_execute_output(['xcrun', '-f', 'swiftc']).strip()
else:
common.debug_print('error: please specify --swiftc for non-Darwin platforms.')
return 1
swiftc_path = os.path.abspath(swiftc)
if not is_correct_swift_version(swiftc_path, compatibility_version):
common.debug_print("warning: Continuing to build with unexpected swiftc version.\n")
runner_command = [
runner_path,
'--swift-branch', swift_branch,
'--swiftc', swiftc_path,
'--projects', project_list,
'--include-repos', 'path == "%s"' % project,
'--include-versions', 'version == "%s"' % compatibility_version,
'--include-actions', 'action.startswith("Build")',
]
common.debug_print("--- Executing build actions ---")
try:
common.check_execute(runner_command, timeout=3600)
except common.ExecuteCommandFailure:
common.debug_print("--- Encountered failures. Check .log files in current directory for details ---")
return 1
common.debug_print("--- %s checked successfully against Swift %s ---" % (project, compatibility_version))
return 0
def main():
os.chdir(os.path.dirname(__file__))
args = parse_args()
common.debug_print('** CHECK **')
validate(args.project, args.earliest_compatible_swift_version)
config = supported_configs[platform.system()][args.earliest_compatible_swift_version]
common.set_swift_branch(config['branch'])
return check(args.project, config['branch'], args.swiftc, args.earliest_compatible_swift_version)
if __name__ == '__main__':
sys.exit(main())