- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathswift-api-checker.py
executable file
·322 lines (270 loc) · 11.6 KB
/
swift-api-checker.py
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python3
importargparse
importos
importsubprocess
importsys
importtempfile
INFER_IMPORT_DIR= \
os.path.dirname(os.path.realpath(__file__)) +'/sdk-module-lists'
INFER_IMPORT_PATH=INFER_IMPORT_DIR+'/infer-imports.py'
defprinterr(message):
print(message, file=sys.stderr)
deffatal_error(message):
printerr(message)
sys.exit(1)
defescapeCmdArg(arg):
if'"'inargor' 'inarg:
return'"%s"'%arg.replace('"', '\\"')
else:
returnarg
defcheck_call(cmd, cwd=None, env=os.environ, verbose=False, output=None):
ifverbose:
print(' '.join([escapeCmdArg(arg) forargincmd]))
try:
subprocess.check_call(cmd, cwd=cwd, env=env,
stderr=None, stdout=output)
return0
exceptExceptionaserror:
printerr(error)
return1
defcheck_output(cmd, verbose=False):
ifverbose:
print(' '.join([escapeCmdArg(arg) forargincmd]))
returnsubprocess.check_output(cmd).strip()
defget_sdk_path(platform):
ifplatform.startswith('iosmac'):
platform='macosx'
returncheck_output(['xcrun', '-sdk', platform, '-show-sdk-path'])
defwrite_fixed_module(file, platform, infix, verbose):
common_modules_path=os.path.join(INFER_IMPORT_DIR, 'fixed-'+infix+
'-modules-common.txt')
platform_modules_path=os.path.join(INFER_IMPORT_DIR, 'fixed-'+infix+
'-modules-'+platform+'.txt')
withopen(common_modules_path, 'r') asextra:
ifverbose:
print('Including modules in: '+common_modules_path)
file.write(extra.read())
withopen(platform_modules_path, 'r') asextra:
ifverbose:
print('Including modules in: '+platform_modules_path)
file.write(extra.read())
defprepare_module_list(platform, file, verbose, module_filter_flags,
include_fixed_clang_modules):
cmd= [INFER_IMPORT_PATH, '-s', get_sdk_path(platform)]
cmd.extend(module_filter_flags)
ifplatform.startswith('iosmac'):
cmd.extend(['--catalyst'])
ifverbose:
cmd.extend(['--v'])
check_call(cmd, verbose=verbose, output=file)
# Always include fixed swift modules
write_fixed_module(file, platform, 'swift', verbose)
# Check if we need fixed clang modules
ifinclude_fixed_clang_modules:
write_fixed_module(file, platform, 'clang', verbose)
defget_api_digester_path(tool_path):
iftool_path:
returntool_path
returncheck_output(['xcrun', '--find', 'swift-api-digester'])
defcreate_directory(path):
ifnotos.path.isdir(path):
os.makedirs(path)
classDumpConfig:
def__init__(self, tool_path, platform, platform_alias, abi, verbose):
target_map= {
'iphoneos': 'arm64-apple-ios13.0',
'macosx': 'x86_64-apple-macosx10.15',
'appletvos': 'arm64-apple-tvos13.0',
'watchos': 'armv7k-apple-watchos6.0',
'iosmac': 'x86_64-apple-ios13.1-macabi',
}
self.tool_path=get_api_digester_path(tool_path)
self.platform=platform
self.target=target_map[platform]
self.sdk=get_sdk_path(platform)
self.inputs= []
self.platform_alias=platform_alias
self.abi=abi
ifself.platform=='macosx':
# We need this input search path for CreateML
self.inputs.extend([self.sdk+'/usr/lib/swift/'])
# This is where XCTest is
self.frameworks= [self.sdk+'/../../Library/Frameworks/']
ifself.platform.startswith('iosmac'):
# Catalyst modules need this extra framework dir
iOSSupport=self.sdk+ \
'/System/iOSSupport/System/Library/Frameworks'
self.frameworks.extend([iOSSupport])
self._environ=dict(os.environ)
self._environ['SWIFT_FORCE_MODULE_LOADING'] ='prefer-interface'
self.verbose=verbose
defdumpZipperedContent(self, cmd, output, module):
dir_path=os.path.realpath(output+'/'+module)
ifself.abi:
dir_path=os.path.join(dir_path, 'ABI')
else:
dir_path=os.path.join(dir_path, 'API')
file_path=os.path.realpath(dir_path+'/'+self.platform_alias+
'.json')
create_directory(dir_path)
current_cmd=list(cmd)
current_cmd.extend(['-module', module])
current_cmd.extend(['-o', file_path])
check_call(current_cmd, env=self._environ, verbose=self.verbose)
defrun(self, output, module, swift_ver, opts,
module_filter_flags, include_fixed_clang_modules,
separate_by_module, zippered):
cmd= [self.tool_path, '-sdk', self.sdk, '-target',
self.target, '-dump-sdk', '-module-cache-path',
'/tmp/ModuleCache', '-swift-version',
swift_ver, '-abort-on-module-fail']
forpathinself.frameworks:
cmd.extend(['-iframework', path])
forpathinself.inputs:
cmd.extend(['-I', path])
ifself.abi:
cmd.extend(['-abi', '-swift-only'])
cmd.extend(['-'+oforoinopts])
ifself.verbose:
cmd.extend(['-v'])
ifmodule:
ifzippered:
create_directory(output)
self.dumpZipperedContent(cmd, output, module)
else:
cmd.extend(['-module', module])
cmd.extend(['-o', output])
check_call(cmd, env=self._environ, verbose=self.verbose)
else:
withtempfile.NamedTemporaryFile() astmp:
prepare_module_list(self.platform, tmp, self.verbose,
module_filter_flags,
include_fixed_clang_modules)
ifseparate_by_module:
tmp.seek(0)
create_directory(output)
formodulein [name.strip() fornameintmp.readlines()]:
# Skip comments
ifmodule.startswith('//'):
continue
self.dumpZipperedContent(cmd, output, module)
else:
cmd.extend(['-o', output])
cmd.extend(['-module-list-file', tmp.name])
check_call(cmd, env=self._environ, verbose=self.verbose)
classDiagnoseConfig:
def__init__(self, tool_path, abi):
self.tool_path=get_api_digester_path(tool_path)
self.abi=abi
defrun(self, opts, before, after, output, verbose):
cmd= [self.tool_path, '-diagnose-sdk', '-input-paths', before,
'-input-paths', after, '-print-module']
ifoutput:
cmd.extend(['-o', output])
ifself.abi:
cmd.extend(['-abi'])
cmd.extend(['-'+oforoinopts])
ifverbose:
cmd.extend(['-v'])
check_call(cmd, verbose=verbose)
defmain():
parser=argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''
A convenient wrapper for swift-api-digester.
''')
basic_group=parser.add_argument_group('Basic')
basic_group.add_argument('--tool-path', default=None, help='''
the path to a swift-api-digester; if not specified, the script will
use the one from the toolchain
''')
basic_group.add_argument('--action', default='', help='''
the action to perform for swift-api-digester
''')
basic_group.add_argument('--target', default=None, help='''
one of macosx, iphoneos, appletvos, and watchos
''')
basic_group.add_argument('--output', default=None, help='''
the output file of the module baseline should end with .json
''')
basic_group.add_argument('--swift-version', default='5', help='''
Swift version to use; default is 5
''')
basic_group.add_argument('--module', default=None, help='''
name of the module/framework to generate baseline, e.g. Foundation
''')
basic_group.add_argument('--module-filter', default='', help='''
the action to perform for swift-api-digester
''')
basic_group.add_argument('--opts', nargs='+', default=[], help='''
additional flags to pass to swift-api-digester
''')
basic_group.add_argument('--v',
action='store_true',
help='Process verbosely')
basic_group.add_argument('--dump-before',
action=None,
help='''
Path to the json file generated before change'
''')
basic_group.add_argument('--dump-after',
action=None,
help='''
Path to the json file generated after change
''')
basic_group.add_argument('--separate-by-module',
action='store_true',
help='When importing entire SDK, dump content '
'separately by module names')
basic_group.add_argument('--zippered',
action='store_true',
help='dump module content to a dir with files for'
'separately targets')
basic_group.add_argument('--abi',
action='store_true',
help='Process verbosely')
basic_group.add_argument('--platform-alias', default='', help='''
Specify a file name to use if using a platform name in json file isn't
optimal
''')
args=parser.parse_args(sys.argv[1:])
ifargs.action=='dump':
ifnotargs.target:
fatal_error("Need to specify --target")
ifnotargs.output:
fatal_error("Need to specify --output")
ifargs.module_filter=='':
module_filter_flags= []
include_fixed_clang_modules=True
elifargs.module_filter=='swift-frameworks-only':
module_filter_flags= ['--swift-frameworks-only']
include_fixed_clang_modules=False
elifargs.module_filter=='swift-overlay-only':
module_filter_flags= ['--swift-overlay-only']
include_fixed_clang_modules=False
else:
fatal_error("cannot recognize --module-filter")
ifargs.platform_alias=='':
args.platform_alias=args.target
runner=DumpConfig(tool_path=args.tool_path, platform=args.target,
platform_alias=args.platform_alias,
abi=args.abi,
verbose=args.v)
runner.run(output=args.output, module=args.module,
swift_ver=args.swift_version, opts=args.opts,
module_filter_flags=module_filter_flags,
include_fixed_clang_modules=include_fixed_clang_modules,
separate_by_module=args.separate_by_module,
zippered=args.zippered)
elifargs.action=='diagnose':
ifnotargs.dump_before:
fatal_error("Need to specify --dump-before")
ifnotargs.dump_after:
fatal_error("Need to specify --dump-after")
runner=DiagnoseConfig(tool_path=args.tool_path, abi=args.abi)
runner.run(opts=args.opts, before=args.dump_before,
after=args.dump_after, output=args.output, verbose=args.v)
else:
fatal_error('Cannot recognize action: '+args.action)
if__name__=='__main__':
main()