|
4 | 4 | # heuristics that omit 'needless' words from APIs imported from Clang |
5 | 5 | # into Swift. |
6 | 6 |
|
7 | | -import getopt |
8 | | -import sys |
| 7 | +import argparse |
9 | 8 | import subprocess |
10 | 9 |
|
11 | | -# Print help |
12 | | -def help(): |
13 | | - print('omit-needless-words.py [options] -m <modulename>') |
14 | | - print('') |
15 | | - print('Summary:') |
16 | | - print("\tDetermines the effects of omitting 'needless' words from imported APIs") |
17 | | - print('') |
18 | | - print('Options:') |
19 | | - print('\t-s <sdkname>\t\t\tThe SDK to use (e.g., macosx)') |
20 | | - print("\t--sdk=<sdkname>'\t\tDefaults to 'macosx'") |
21 | | - print('') |
22 | | - print('\t-t <triple>\t\t\tThe target triple to use (e.g., x86_64-apple-macosx10.10)') |
23 | | - print("\t--target=<triple>") |
24 | | - print('') |
25 | | - print('\t-i <executable>\t\t\tThe swift-ide-test executable') |
26 | | - print("\t--swift-ide-test=<executable>\tDefaults to 'swift-ide-test'") |
27 | | - print('') |
28 | | - print('\t-d <executable>\t\t\tThe tool to use to diff the results') |
29 | | - print("\t--diff_tool=<executable>\tDefaults to 'opendiff'") |
30 | | - print('') |
31 | | - print('\t-b\t\t\t\tOnly emit the "before" result') |
32 | | - print('\t--only-before') |
33 | | - print('') |
34 | | - print('\t--before-file=<filename>\tEmit "before" results to the given file') |
35 | | - print('\t\t\t\t\tDefaults to <modulename>.before.txt') |
36 | | - print('') |
37 | | - print('\t-a\t\t\t\tOnly emit the "after" result') |
38 | | - print('\t--only-after') |
39 | | - print('') |
40 | | - print('\t--after-file=<filename>\t\tEmit "after" results to the given file') |
41 | | - print('\t\t\t\t\tDefaults to <modulename>.after.txt') |
42 | | - print('') |
43 | | - print('\t-q\t\t\t\tSuppress printing of status messages') |
44 | | - print('') |
45 | | - print('Examples:') |
46 | | - print('\tpython omit-needless-words.py -m AppKit') |
47 | | - |
48 | | -# Configuration information |
49 | | -sdk = 'macosx' |
50 | | -target = '' |
51 | | -module = '' |
52 | | -source_filename = 'omit-needless-words.swift' |
53 | | -swift_ide_test = 'swift-ide-test' |
54 | | -diff_tool = 'opendiff' |
55 | | -only_before=0 |
56 | | -only_after=0 |
57 | | - |
58 | | -# Parse command-line arguments. |
59 | | -try: |
60 | | - opts, args = getopt.getopt(sys.argv[1:], 'hs:t:m:i:d:baq', |
61 | | - ['help', 'sdk=', 'target=', 'module=', |
62 | | - 'swift-ide-test=','diff_tool=','only-before', |
63 | | - 'only-after', 'before-file=', 'after-file=']) |
64 | | -except getopt.GetoptError: |
65 | | - help() |
66 | | - sys.exit(2) |
67 | | - |
68 | | -before_filename = "" |
69 | | -after_filename = "" |
70 | | -verbose = 1 |
71 | | -for opt, arg in opts: |
72 | | - if opt in ('-h', '--help'): |
73 | | - help() |
74 | | - sys.exit() |
75 | | - |
76 | | - if opt in ('-s', '--sdk'): |
77 | | - sdk = arg |
78 | | - continue |
79 | | - |
80 | | - if opt in ('-t', '--target'): |
81 | | - target = arg |
82 | | - continue |
83 | | - |
84 | | - if opt in ('-m', '--module'): |
85 | | - module = arg |
86 | | - continue |
87 | | - |
88 | | - if opt in ('-i', '--swift-ide-test'): |
89 | | - swift_ide_test = arg |
90 | | - continue |
91 | | - |
92 | | - if opt in ('-d', '--diff_tool'): |
93 | | - diff_tool = arg |
94 | | - continue |
95 | | - |
96 | | - if opt in ('-b', '--only-before'): |
97 | | - only_before=1 |
98 | | - continue |
99 | | - |
100 | | - if opt in ('-a', '--only-after'): |
101 | | - only_after=1 |
102 | | - continue |
103 | | - |
104 | | - if opt == '--before-file': |
105 | | - before_filename = arg |
106 | | - continue |
107 | | - |
108 | | - if opt == '--after-file': |
109 | | - after_filename = arg |
110 | | - continue |
111 | | - |
112 | | - if opt == '-q': |
113 | | - verbose = 0 |
114 | | - continue |
| 10 | +DEFAULT_TARGET_BASED_ON_SDK = { |
| 11 | + 'macosx' : 'x86_64-apple-macosx10.11', |
| 12 | + 'iphoneos' : 'arm64-apple-ios9.0', |
| 13 | + 'iphonesimulator' : 'x86_64-apple-ios9.0', |
| 14 | + 'watchos' : 'armv7k-apple-watchos2.0', |
| 15 | + 'watchos.simulator' : 'i386-apple-watchos2.0', |
| 16 | + 'appletvos' : 'arm64-apple-tvos9', |
| 17 | + 'appletvos.simulator' : 'x86_64-apple-tvos9', |
| 18 | +} |
| 19 | + |
| 20 | +def create_parser(): |
| 21 | + parser = argparse.ArgumentParser( |
| 22 | + description="Determines the effects of omitting 'needless' words from imported APIs", |
| 23 | + prog='omit-needless-words.py', |
| 24 | + usage='python omit-needless-words.py -m AppKit') |
| 25 | + parser.add_argument('-m', '--module', required=True, help='The module name.') |
| 26 | + parser.add_argument('-s', '--sdk', default='macosx', help="The SDK to use.") |
| 27 | + parser.add_argument('-t', '--target', help="The target triple to use.") |
| 28 | + parser.add_argument('-i', '--swift-ide-test', default='swift-ide-test', help="The swift-ide-test executable.") |
| 29 | + parser.add_argument('-d', '--diff_tool', default='opendiff', help="The tool to use to diff the results.") |
| 30 | + parser.add_argument('-b', '--only-before', action='store_true', help='Only emit the "before" result.') |
| 31 | + parser.add_argument('--before-file', help='Emit "before" results to the given file [defaults to <modulename>.before.txt].') |
| 32 | + parser.add_argument('-a', '--only-after', action='store_true', help='Only emit the "after" result.') |
| 33 | + parser.add_argument('--after-file', help='Emit "after" results to the given file [defaults to <modulename>.after.txt].') |
| 34 | + parser.add_argument('-q', '--quiet', action='store_true', help='Suppress printing of status messages.') |
| 35 | + return parser |
| 36 | + |
| 37 | +def output_command_result_to_file(command_args, filename): |
| 38 | + with open(filename, 'w') as output_file: |
| 39 | + subprocess.call(command_args, stdout=output_file) |
| 40 | + |
| 41 | +def main(): |
| 42 | + source_filename = 'omit-needless-words.swift' |
| 43 | + parser = create_parser() |
| 44 | + args = parser.parse_args() |
| 45 | + if not args.target: |
| 46 | + args.target = DEFAULT_TARGET_BASED_ON_SDK[args.sdk] |
| 47 | + |
| 48 | + # Figure out the SDK root for the requested SDK |
| 49 | + sdkroot = subprocess.check_output(['xcrun', '--show-sdk-path', '--sdk', args.sdk]).rstrip() |
| 50 | + if not args.quiet: |
| 51 | + print('SDK Root = %s' % (sdkroot)) |
| 52 | + |
| 53 | + swift_ide_test_cmd = [args.swift_ide_test, '-print-module', '-source-filename', source_filename, '-sdk', sdkroot, '-target', args.target, '-module-print-skip-overlay', '-skip-unavailable', '-module-print-submodules', '-skip-imports', '-module-to-print=%s' % (args.module)] |
| 54 | + omit_needless_words_args = ['-enable-omit-needless-words', '-enable-infer-default-arguments'] |
115 | 55 |
|
116 | | - help() |
117 | | - sys.exit(2) |
118 | | - |
119 | | -if module == '': |
120 | | - help() |
121 | | - sys.exit(2) |
122 | | - |
123 | | -if target == '': |
124 | | - if sdk == 'macosx': |
125 | | - target = 'x86_64-apple-macosx10.11' |
126 | | - if sdk == 'iphoneos': |
127 | | - target = 'arm64-apple-ios9.0' |
128 | | - if sdk == 'iphonesimulator': |
129 | | - target = 'x86_64-apple-ios9.0' |
130 | | - if sdk == 'watchos': |
131 | | - target = 'armv7k-apple-watchos2.0' |
132 | | - if sdk == 'watchos.simulator': |
133 | | - target = 'i386-apple-watchos2.0' |
134 | | - if sdk == 'appletvos': |
135 | | - target = 'arm64-apple-tvos9' |
136 | | - if sdk == 'appletvos.simulator': |
137 | | - target = 'x86_64-apple-tvos9' |
138 | | - |
139 | | -# Figure out the SDK root for the requested SDK |
140 | | -sdkroot = subprocess.check_output(['xcrun', '--show-sdk-path', '--sdk', sdk]).rstrip() |
141 | | -if verbose != 0: |
142 | | - print('SDK Root = %s' % (sdkroot)) |
143 | | - |
144 | | -swift_ide_test_cmd = [swift_ide_test, '-print-module', '-source-filename', source_filename, '-sdk', sdkroot, '-target', target, '-module-print-skip-overlay', '-skip-unavailable', '-module-print-submodules', '-skip-imports', '-module-to-print=%s' % (module)] |
145 | | -omit_needless_words_args = ['-enable-omit-needless-words', '-enable-infer-default-arguments'] |
146 | | - |
147 | | -# Determine the output files. |
148 | | -if before_filename == "": |
149 | | - before_filename = '%s.before.txt' % (module) |
150 | | -if after_filename == "": |
151 | | - after_filename = '%s.after.txt' % (module) |
152 | | - |
153 | | -# Create a .swift file we can feed into swift-ide-test |
154 | | -subprocess.call(['touch', source_filename]) |
155 | | - |
156 | | -if only_after == 0: |
157 | | - # Print the interface without omitting needless words |
158 | | - if verbose != 0: |
159 | | - print('Writing %s...' % before_filename) |
160 | | - before_file = open(before_filename, 'w') |
161 | | - subprocess.call(swift_ide_test_cmd, stdout=before_file) |
162 | | - before_file.close() |
163 | | - |
164 | | -if only_before == 0: |
165 | | - # Print the interface omitting needless words |
166 | | - if verbose != 0: |
167 | | - print('Writing %s...' % after_filename) |
168 | | - after_file = open(after_filename, 'w') |
169 | | - subprocess.call(swift_ide_test_cmd + omit_needless_words_args, stdout=after_file) |
170 | | - after_file.close() |
| 56 | + # Determine the output files. |
| 57 | + # No good way with argparse to set default value based on depenency of other arg. |
| 58 | + if not args.before_file: |
| 59 | + args.before_file = '%s.before.txt' % (args.module) |
| 60 | + if not args.after_file: |
| 61 | + args.after_file = '%s.after.txt' % (args.module) |
| 62 | + |
| 63 | + # Create a .swift file we can feed into swift-ide-test |
| 64 | + subprocess.call(['touch', source_filename]) |
| 65 | + |
| 66 | + if not args.only_after: |
| 67 | + # Print the interface without omitting needless words |
| 68 | + if not args.quiet: |
| 69 | + print('Writing %s...' % args.before_file) |
| 70 | + output_command_result_to_file(swift_ide_test_cmd, args.before_file) |
| 71 | + |
| 72 | + if not args.only_before: |
| 73 | + # Print the interface omitting needless words |
| 74 | + if not args.quiet: |
| 75 | + print('Writing %s...' % args.after_file) |
| 76 | + output_command_result_to_file(swift_ide_test_cmd + omit_needless_words_args, args.after_file) |
| 77 | + |
| 78 | + # Remove the .swift file we fed into swift-ide-test |
| 79 | + subprocess.call(['rm', '-f', source_filename]) |
| 80 | + |
| 81 | + # Diff them |
| 82 | + subprocess.call([args.diff_tool, args.before_file, args.after_file]) |
171 | 83 |
|
172 | | -# Remove the .swift file we fed into swift-ide-test |
173 | | -subprocess.call(['rm', '-f', source_filename]) |
| 84 | +if __name__ == '__main__': |
| 85 | + main() |
174 | 86 |
|
175 | | -# Diff them. |
176 | | -if diff_tool != '': |
177 | | - subprocess.call([diff_tool, before_filename, after_filename]) |
0 commit comments