File: CompilerArgs.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (128 lines) | stat: -rw-r--r-- 3,697 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//

import Foundation

public struct CompilerArg: Equatable {
  /// The original argument as passed
  public let original: String

  /// If the argument is a file list (@/some/file), this is the arguments
  /// parsed from that file. Otherwise it is an array of a single value - the
  /// original argument.
  public let transformed: [String]

  public init(_ argument: String) {
    self.original = argument
    self.transformed = fileListArgs(arg: argument)
      .map({ args in
        args.map({ $0.replacingOccurrences(of: "\\ ", with: " ") })
      }) ?? [argument]
  }
}

public struct CompilerArgs {
  private static let SKIP_FLAGS: Set = [
    "-incremental",
    "-enable-batch-mode",
    "-emit-object", "-c"
  ]
  private static let SKIP_OPTIONS: Set = [
    "-num-threads",
    "-output-file-map",
    "-module-name",
    "-emit-module-path", "-o"
  ]

  /// Main file intended to be compiled
  public let forFile: URL

  /// Original arguments as passed
  public let original: [String]

  /// Arguments with any file list arguments (@file) replaced with the
  /// arguments found in those files
  public let sourcekitdArgs: [String]

  /// Original arguments but with references to `forFile` removed. This
  /// includes replacing any file list with a new file when it contains
  /// `forFile`. Arguments relating to incremental compilation or the output
  /// of the module are also removed.
  public let processArgs: [String]

  public init(for file: URL, args: [CompilerArg], tempDir: URL) {
    self.forFile = file
    self.original = args.map { $0.original }
    self.sourcekitdArgs = args.flatMap { $0.transformed }

    var processArgs = [String]()
    var skip = false
    for arg in args {
      if skip {
        skip = false
        continue;
      }

      if arg.original == file.path {
        continue
      }

      if CompilerArgs.SKIP_FLAGS.contains(arg.original) {
        continue
      }

      if CompilerArgs.SKIP_OPTIONS.contains(arg.original) {
        skip = true
        continue
      }

      if let listArgs = fileListArgs(arg: arg.original) {
        let fileRemoved = listArgs.filter { $0 != file.path }
        if fileRemoved.count != listArgs.count {
          let newFileList = tempDir.appendingPathComponent(UUID().uuidString)
            .appendingPathExtension("SwiftFileList")
          let data = Data(fileRemoved.joined(separator: "\n").utf8)
          try! data.write(to: newFileList)
          processArgs.append("@" + newFileList.path)
        }

        continue
      }

      processArgs.append(arg.original)
    }
    self.processArgs = processArgs
  }
}

public func escapeArgs(_ args: [String]) -> String {
  return args.map { arg in
    if arg.contains(" ") {
      return "\"\(arg)\""
    }
    return arg
  }.joined(separator: " ")
}

func fileListArgs(arg: String) -> [String]? {
  guard arg.starts(with: "@") else {
    return nil
  }

  let url = URL(fileURLWithPath: String(arg.dropFirst()) , isDirectory: false)
  if let content = try? String(contentsOf: url, encoding: .utf8) {
    return content.split(separator: "\n").map {
      $0.trimmingCharacters(in: .whitespacesAndNewlines)
    }
  }
  return nil
}