Skip to content

Commit c280dab

Browse files
authored
Merge pull request #23447 from nkcsgexi/diagnose-api-checker
swift-api-checker: teach the script to also diagnose the breakages between two previous dumps
2 parents c2f7729 + 6a502aa commit c280dab

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

utils/api_checker/swift-api-checker.py

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def escapeCmdArg(arg):
3030
return arg
3131

3232

33-
def check_call(cmd, cwd=None, env=os.environ, verbose=True, output=None):
33+
def check_call(cmd, cwd=None, env=os.environ, verbose=False, output=None):
3434
if verbose:
3535
print(' '.join([escapeCmdArg(arg) for arg in cmd]))
3636
return subprocess.check_call(cmd, cwd=cwd, env=env,
@@ -56,24 +56,29 @@ def prepare_module_list(platform, file):
5656
file.write(extra.read())
5757

5858

59+
def get_api_digester_path(tool_path):
60+
if tool_path:
61+
return tool_path
62+
return check_output(['xcrun', '--find', 'swift-api-digester'])
63+
64+
5965
class DumpConfig:
60-
def __init__(self, platform):
66+
def __init__(self, tool_path, platform):
6167
target_map = {
6268
'iphoneos': 'arm64-apple-ios10.0',
6369
'macosx': 'x86_64-apple-macosx10.11',
6470
'appletvos': 'arm64-apple-tvos10.0',
6571
'watchos': 'armv7k-apple-watchos3.0',
6672
}
73+
self.tool_path = get_api_digester_path(tool_path)
6774
self.platform = platform
6875
self.target = target_map[platform]
6976
self.sdk = get_sdk_path(platform)
7077
self.frameworks = [
7178
self.sdk + '/System/Library/Frameworks/',
7279
os.path.realpath(self.sdk + '/../../Library/Frameworks/')]
73-
self.tool_path = check_output(['xcrun', '--find',
74-
'swift-api-digester'])
7580

76-
def run(self, output, module, swift_ver, abi):
81+
def run(self, output, module, swift_ver, abi, verbose):
7782
cmd = [self.tool_path, '-o', output, '-sdk', self.sdk, '-target',
7883
self.target, '-dump-sdk', '-module-cache-path',
7984
'/tmp/ModuleCache', '-swift-version',
@@ -82,14 +87,32 @@ def run(self, output, module, swift_ver, abi):
8287
cmd.extend(['-iframework', path])
8388
if abi:
8489
cmd.extend(['-abi'])
90+
if verbose:
91+
cmd.extend(['-v'])
8592
if module:
8693
cmd.extend(['-module', module])
87-
check_call(cmd)
94+
check_call(cmd, verbose=verbose)
8895
else:
8996
with tempfile.NamedTemporaryFile() as tmp:
9097
prepare_module_list(self.platform, tmp)
9198
cmd.extend(['-module-list-file', tmp.name])
92-
check_call(cmd)
99+
check_call(cmd, verbose=verbose)
100+
101+
102+
class DiagnoseConfig:
103+
def __init__(self, tool_path):
104+
self.tool_path = get_api_digester_path(tool_path)
105+
106+
def run(self, abi, before, after, output, verbose):
107+
cmd = [self.tool_path, '-diagnose-sdk', '-input-paths', before,
108+
'-input-paths', after]
109+
if output:
110+
cmd.extend(['-o', output])
111+
if abi:
112+
cmd.extend(['-abi'])
113+
if verbose:
114+
cmd.extend(['-v'])
115+
check_call(cmd, verbose=verbose)
93116

94117

95118
def main():
@@ -100,6 +123,12 @@ def main():
100123
''')
101124

102125
basic_group = parser.add_argument_group('Basic')
126+
127+
basic_group.add_argument('--tool-path', default=None, help='''
128+
the path to a swift-api-digester; if not specified, the script will
129+
use the one from the toolchain
130+
''')
131+
103132
basic_group.add_argument('--action', default='', help='''
104133
the action to perform for swift-api-digester
105134
''')
@@ -124,18 +153,39 @@ def main():
124153
action='store_true',
125154
help='Whether we are jsonizing for abi')
126155

127-
args = parser.parse_args(sys.argv[1:])
128-
if not args.target:
129-
fatal_error("Need to specify --target")
130-
if not args.output:
131-
fatal_error("Need to specify --output")
156+
basic_group.add_argument('--v',
157+
action='store_true',
158+
help='Process verbosely')
159+
160+
basic_group.add_argument('--dump-before',
161+
action=None,
162+
help='''
163+
Path to the json file generated before change'
164+
''')
165+
166+
basic_group.add_argument('--dump-after',
167+
action=None,
168+
help='''
169+
Path to the json file generated after change
170+
''')
132171

172+
args = parser.parse_args(sys.argv[1:])
133173
if args.action == 'dump':
134-
runner = DumpConfig(platform=args.target)
174+
if not args.target:
175+
fatal_error("Need to specify --target")
176+
if not args.output:
177+
fatal_error("Need to specify --output")
178+
runner = DumpConfig(tool_path=args.tool_path, platform=args.target)
135179
runner.run(output=args.output, module=args.module,
136-
swift_ver=args.swift_version, abi=args.abi)
180+
swift_ver=args.swift_version, abi=args.abi, verbose=args.v)
137181
elif args.action == 'diagnose':
138-
fatal_error('Not implemented')
182+
if not args.dump_before:
183+
fatal_error("Need to specify --dump-before")
184+
if not args.dump_after:
185+
fatal_error("Need to specify --dump-after")
186+
runner = DiagnoseConfig(tool_path=args.tool_path)
187+
runner.run(abi=args.abi, before=args.dump_before,
188+
after=args.dump_after, output=args.output, verbose=args.v)
139189
else:
140190
fatal_error('Cannot recognize action: ' + args.action)
141191

0 commit comments

Comments
 (0)