|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | + |
| 11 | +INFER_IMPORT_DIR = \ |
| 12 | + os.path.dirname(os.path.realpath(__file__)) + '/sdk-module-lists' |
| 13 | + |
| 14 | +INFER_IMPORT_PATH = INFER_IMPORT_DIR + '/infer-imports.py' |
| 15 | + |
| 16 | + |
| 17 | +def printerr(message): |
| 18 | + print(message, file=sys.stderr) |
| 19 | + |
| 20 | + |
| 21 | +def fatal_error(message): |
| 22 | + printerr(message) |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + |
| 26 | +def escapeCmdArg(arg): |
| 27 | + if '"' in arg or ' ' in arg: |
| 28 | + return '"%s"' % arg.replace('"', '\\"') |
| 29 | + else: |
| 30 | + return arg |
| 31 | + |
| 32 | + |
| 33 | +def check_call(cmd, cwd=None, env=os.environ, verbose=True, output=None): |
| 34 | + if verbose: |
| 35 | + print(' '.join([escapeCmdArg(arg) for arg in cmd])) |
| 36 | + return subprocess.check_call(cmd, cwd=cwd, env=env, |
| 37 | + stderr=subprocess.STDOUT, stdout=output) |
| 38 | + |
| 39 | + |
| 40 | +def check_output(cmd, verbose=False): |
| 41 | + if verbose: |
| 42 | + print(' '.join([escapeCmdArg(arg) for arg in cmd])) |
| 43 | + return subprocess.check_output(cmd).strip() |
| 44 | + |
| 45 | + |
| 46 | +def get_sdk_path(platform): |
| 47 | + return check_output(['xcrun', '-sdk', platform, '-show-sdk-path']) |
| 48 | + |
| 49 | + |
| 50 | +def prepare_module_list(platform, file): |
| 51 | + check_call([INFER_IMPORT_PATH, '-s', get_sdk_path(platform)], output=file) |
| 52 | + with open(INFER_IMPORT_DIR + '/fixed-modules-common.txt', 'r') as extra: |
| 53 | + file.write(extra.read()) |
| 54 | + with open(INFER_IMPORT_DIR + '/fixed-modules-' + platform + '.txt', |
| 55 | + 'r') as extra: |
| 56 | + file.write(extra.read()) |
| 57 | + |
| 58 | + |
| 59 | +class DumpConfig: |
| 60 | + def __init__(self, platform): |
| 61 | + target_map = { |
| 62 | + 'iphoneos': 'arm64-apple-ios10.0', |
| 63 | + 'macosx': 'x86_64-apple-macosx10.11', |
| 64 | + 'appletvos': 'arm64-apple-tvos10.0', |
| 65 | + 'watchos': 'armv7k-apple-watchos3.0', |
| 66 | + } |
| 67 | + self.platform = platform |
| 68 | + self.target = target_map[platform] |
| 69 | + self.sdk = get_sdk_path(platform) |
| 70 | + self.frameworks = [ |
| 71 | + self.sdk + '/System/Library/Frameworks/', |
| 72 | + os.path.realpath(self.sdk + '/../../Library/Frameworks/')] |
| 73 | + self.tool_path = check_output(['xcrun', '--find', |
| 74 | + 'swift-api-digester']) |
| 75 | + |
| 76 | + def run(self, output, module, swift_ver, abi): |
| 77 | + cmd = [self.tool_path, '-o', output, '-sdk', self.sdk, '-target', |
| 78 | + self.target, '-dump-sdk', '-module-cache-path', |
| 79 | + '/tmp/ModuleCache', '-swift-version', |
| 80 | + swift_ver, '-abort-on-module-fail'] |
| 81 | + for path in self.frameworks: |
| 82 | + cmd.extend(['-iframework', path]) |
| 83 | + if abi: |
| 84 | + cmd.extend(['-abi']) |
| 85 | + if module: |
| 86 | + cmd.extend(['-module', module]) |
| 87 | + check_call(cmd) |
| 88 | + else: |
| 89 | + with tempfile.NamedTemporaryFile() as tmp: |
| 90 | + prepare_module_list(self.platform, tmp) |
| 91 | + cmd.extend(['-module-list-file', tmp.name]) |
| 92 | + check_call(cmd) |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + parser = argparse.ArgumentParser( |
| 97 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 98 | + description=''' |
| 99 | +A convenient wrapper for swift-api-digester. |
| 100 | +''') |
| 101 | + |
| 102 | + basic_group = parser.add_argument_group('Basic') |
| 103 | + basic_group.add_argument('--action', default='', help=''' |
| 104 | + the action to perform for swift-api-digester |
| 105 | + ''') |
| 106 | + |
| 107 | + basic_group.add_argument('--target', default=None, help=''' |
| 108 | + one of macosx, iphoneos, appletvos, and watchos |
| 109 | + ''') |
| 110 | + |
| 111 | + basic_group.add_argument('--output', default=None, help=''' |
| 112 | + the output file of the module baseline should end with .json |
| 113 | + ''') |
| 114 | + |
| 115 | + basic_group.add_argument('--swift-version', default='4', help=''' |
| 116 | + Swift version to use; default is 4 |
| 117 | + ''') |
| 118 | + |
| 119 | + basic_group.add_argument('--module', default=None, help=''' |
| 120 | + name of the module/framework to generate baseline, e.g. Foundation |
| 121 | + ''') |
| 122 | + |
| 123 | + basic_group.add_argument('--abi', |
| 124 | + action='store_true', |
| 125 | + help='Whether we are jsonizing for abi') |
| 126 | + |
| 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") |
| 132 | + |
| 133 | + if args.action == 'dump': |
| 134 | + runner = DumpConfig(platform=args.target) |
| 135 | + runner.run(output=args.output, module=args.module, |
| 136 | + swift_ver=args.swift_version, abi=args.abi) |
| 137 | + elif args.action == 'diagnose': |
| 138 | + fatal_error('Not implemented') |
| 139 | + else: |
| 140 | + fatal_error('Cannot recognize action: ' + args.action) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + main() |
0 commit comments