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
|
#!/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:
print('"' + arg + '"', end="")
else:
print(arg, end="")
# A hard-coded list of options which expect a parameter
is_arg_param = arg in [
"--sdk",
"--toolchain",
"-D",
"-F",
"-I",
"-L",
"-Xcc",
"-Xclang",
"-Xfrontend",
"-Xlinker",
"-Xllvm",
"-add_ast_path",
"-arch",
"-emit-ir",
"-emit-sil",
"-filelist",
"-fileno",
"-framework",
"-import-objc-header",
"-iquote",
"-isysroot",
"-macosx_version_min",
"-module-link-name",
"-module-name",
"-num-threads",
"-o",
"-output-file-map",
"-primary-file",
"-resource-dir",
"-rpath",
"-sdk",
"-swift-version",
"-syslibroot",
"-target",
"-target-sdk-version",
"-working-directory",
"-x",
]
# Heuristic: options ending in -path expect an argument
if arg.startswith("-") and arg.endswith("-path"):
is_arg_param = True
# Print 2 new lines after each command line
print("\n")
if __name__ == "__main__":
main()
|