forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsplit-cmdline
executable file
·350 lines (341 loc) · 12.2 KB
/
split-cmdline
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python3
# split-cmdline - Split swift compiler command lines ------------*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 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
#
# ----------------------------------------------------------------------------
#
# Split swift compiler command lines into multiple lines.
#
# Reads the command line from stdin and outputs the split line to stdout.
# Example:
#
# $ swiftc -c hello.swift -### | split-cmdline
# /path-to-swift/bin/swift \
# -frontend \
# -c \
# -primary-file hello.swift \
# -target x86_64-apple-macosx10.9 \
# -enable-objc-interop \
# -color-diagnostics \
# -module-name hello \
# -o hello.o
#
# Example usage in vim:
# *) make sure that split-cmdline is in the $PATH
# *) copy-paste the swift command line the text buffer
# *) select the command line
# *) go to the command prompt (= press ':')
# :'<,'>!split-cmdline
#
# ----------------------------------------------------------------------------
import os
import re
import shlex
import sys
def main():
for line in sys.stdin:
first = True
is_arg_param = False
# Handle escaped spaces
args = shlex.split(line)
for arg in args:
if arg == "":
continue
if not first:
# Print option arguments in the same line
print(" " if is_arg_param else " \\\n ", end="")
first = False
# Expand @ option files
m = re.match(r"^@(\S+\.txt)$", arg)
if m:
cmd_file = m.group(1)
if os.path.isfile(cmd_file):
with open(cmd_file) as f:
for ln in f.readlines():
for name in ln.rstrip().split(";"):
if name != "":
print(name + " \\")
first = True
continue
if " " in arg or ";" in arg:
print('"' + arg + '"', end="")
else:
print(arg, end="")
# A hard-coded list of options which expect a parameter
is_arg_param = arg in [
# This list is generated by scraping --help menus from running:
#
# $ ( \
# swift --help; \
# swift build --help; \
# swift run --help; \
# swift repl --help; \
# swift test --help; \
# swift package --help; \
# swiftc --help; \
# xcrun swift-frontend --help; \
# clang --help; \
# ) | rg -o -r '$3 $2' -- ' ((-[\w-]+),)? (-[\w-]+) <' \
# | sed 's/ *$//' | tr ' ' '\n' \
# | sort | uniq \
# | grep -v -e '-c$' \
# | sed -E 's/(.*)/"\1",/'
"--analyzer-output",
"--build-system",
"--cache-path",
"--config-path",
"--configuration",
"--default-registry-url",
"--experimental-traits",
"--explicit-target-dependency-import-check",
"--filter",
"--jobs",
"--manifest-cache",
"--netrc-file",
"--num-workers",
"--package-path",
"--pkg-config-path",
"--product",
"--resolver-fingerprint-checking",
"--resolver-signing-entity-checking",
"--sanitize",
"--scratch-path",
"--sdk",
"--security-path",
"--skip",
"--specifier",
"--swift-sdk",
"--swift-sdks-path",
"--target",
"--toolchain",
"--triple",
"--xunit-output",
"-B",
"-D",
"-F",
"-Fsystem",
"-G",
"-I",
"-L",
"-MF",
"-MJ",
"-MQ",
"-MT",
"-T",
"-U",
"-Xanalyzer",
"-Xarch_device",
"-Xarch_host",
"-Xassembler",
"-Xcc",
"-Xclang",
"-Xcuda-fatbinary",
"-Xcuda-ptxas",
"-Xcxx",
"-Xlinker",
"-Xopenmp-target",
"-Xpreprocessor",
"-Xswiftc",
"-access-notes-path",
"-allowable-client",
"-api-diff-data-dir",
"-api-diff-data-file",
"-arcmt-migrate-report-output",
"-assert-config",
"-autolink-library",
"-b",
"-backup-module-interface-path",
"-blocklist-file",
"-build-id",
"-cache-replay-prefix-map",
"-cas-path",
"-cas-plugin-option",
"-cas-plugin-path",
"-cc1-args",
"-clang-build-session-file",
"-clang-scanner-module-cache-path",
"-clang-target",
"-compare-to-baseline-path",
"-const-gather-protocols-file",
"-coverage-prefix-map",
"-cxx-isystem",
"-darwin-target-variant",
"-darwin-target-variant-triple",
"-debug-info-format",
"-debug-prefix-map",
"-define-availability",
"-dependency-dot",
"-dependency-file",
"-dependency-scan-cache-path",
"-diagnostic-documentation-path",
"-diagnostic-style",
"-digester-breakage-allowlist-path",
"-digester-mode",
"-dsym-dir",
"-dump-api-path",
"-dump-migration-states-dir",
"-dump-scope-maps",
"-dumpdir",
"-e",
"-embed-tbd-for-module",
"-emit-abi-descriptor-path",
"-emit-api-descriptor-path",
"-emit-clang-header-path",
"-emit-const-values-path",
"-emit-dependencies-path",
"-emit-digester-baseline-path",
"-emit-fixits-path",
"-emit-loaded-module-trace-path",
"-emit-migrated-file-path",
"-emit-module-dependencies-path",
"-emit-module-doc-path",
"-emit-module-interface-path",
"-emit-module-path",
"-emit-module-semantic-info-path",
"-emit-module-serialize-diagnostics-path",
"-emit-module-source-info-path",
"-emit-module-summary-path",
"-emit-objc-header-path",
"-emit-reference-dependencies-path",
"-emit-remap-file-path",
"-emit-tbd-path",
"-enable-experimental-feature",
"-enable-upcoming-feature",
"-explain-module-dependency",
"-explain-module-dependency-detailed",
"-explicit-swift-module-map-file",
"-export-as",
"-external-plugin-path",
"-fdepscan-share-identifier",
"-file-compilation-dir",
"-file-prefix-map",
"-filelist",
"-fmodules-user-build-path",
"-framework",
"-group-info-path",
"-iapinotes-modules",
"-iapinotes-path",
"-idirafter",
"-iframework",
"-iframeworkwithsysroot",
"-imacros",
"-in-process-plugin-server-path",
"-include",
"-include-pch",
"-index-file-path",
"-index-store-path",
"-index-unit-output-path",
"-index-unit-output-path-filelist",
"-iprefix",
"-iquote",
"-isysroot",
"-isystem",
"-isystem-after",
"-ivfsoverlay",
"-ivfsstatcache",
"-iwithprefix",
"-iwithprefixbefore",
"-iwithsysroot",
"-j",
"-l",
"-libc",
"-load-plugin-executable",
"-load-plugin-library",
"-locale",
"-localization-path",
"-lto-library",
"-meabi",
"-mllvm",
"-mmlir",
"-module-abi-name",
"-module-alias",
"-module-cache-path",
"-module-can-import",
"-module-can-import-version",
"-module-dependency-dir",
"-module-link-name",
"-module-name",
"-mthread-model",
"-num-threads",
"-o",
"-output-file-map",
"-output-filelist",
"-package-name",
"-placeholder-dependency-module-map-file",
"-plugin-path",
"-prebuilt-module-cache-path",
"-primary-file",
"-primary-filelist",
"-project-name",
"-require-explicit-availability-target",
"-runtime-compatibility-version",
"-s",
"-save-optimization-record-passes",
"-save-optimization-record-path",
"-scanner-prefix-map",
"-scanner-prefix-map-sdk",
"-scanner-prefix-map-toolchain",
"-sdk",
"-serialize-breaking-changes-path",
"-serialize-diagnostics",
"-serialize-diagnostics-path",
"-serialized-path-obfuscate",
"-supplementary-output-file-map",
"-swift-isa-ptrauth-mode",
"-swift-module-cross-import",
"-swift-ptrauth-mode",
"-swift-version",
"-sysroot",
"-target",
"-target-cpu",
"-target-min-inlining-version",
"-target-variant",
"-tbd-compatibility-version",
"-tbd-current-version",
"-tbd-install_name",
"-tools-directory",
"-user-module-version",
"-verify-additional-file",
"-verify-additional-prefix",
"-verify-generic-signatures",
"-vfsoverlay",
"-visualc-tools-root",
"-visualc-tools-version",
"-windows-sdk-root",
"-windows-sdk-version",
"-working-directory",
"-x",
"-z",
] + [
# This list contains options that do not appear in --help menus:
"-Xfrontend",
"-Xllvm",
"-add_ast_path",
"-arch",
"-emit-ir",
"-emit-sil",
"-fileno",
"-import-objc-header",
"-macosx_version_min",
"-resource-dir",
"-rpath",
"-syslibroot",
"-target-sdk-version",
"-target-sdk-name",
]
# Heuristic: options ending in -path expect an argument
if arg.startswith("-") and arg.endswith("-path"):
is_arg_param = True
if arg == "-c" and args[0] == "swift":
is_arg_param = True
# Print 2 new lines after each command line
print("\n")
if __name__ == "__main__":
main()