#!/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()