Skip to content

Commit 4d62ad7

Browse files
authored
Merge pull request #2940 from compnerd/c-flags
Workspace: handle `/MT`, `/MTd`, `/MD`, `/MDd` for C/C++
2 parents f1f6134 + ccde30c commit 4d62ad7

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

Sources/Workspace/UserToolchain.swift

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ public final class UserToolchain: Toolchain {
3030
/// Path of the `swiftc` compiler.
3131
public let swiftCompiler: AbsolutePath
3232

33-
public let extraCCFlags: [String]
33+
public var extraCCFlags: [String]
3434

3535
public let extraSwiftCFlags: [String]
3636

37-
public var extraCPPFlags: [String] {
38-
return destination.extraCPPFlags
39-
}
37+
public var extraCPPFlags: [String]
4038

4139
/// Path of the `swift` interpreter.
4240
public var swiftInterpreter: AbsolutePath {
@@ -213,13 +211,21 @@ public final class UserToolchain: Toolchain {
213211
// the SDK. This is not the same value as the SDKROOT parameter
214212
// in Xcode, however, the value represents a similar concept.
215213
if let SDKROOT = ProcessEnv.vars["SDKROOT"], let root = try? AbsolutePath(validating: SDKROOT) {
216-
217214
var runtime: [String] = []
218215
var xctest: [String] = []
219216

220217
if let settings = WindowsSDKSettings(reading: root.appending(component: "SDKSettings.plist"),
221218
diagnostics: nil, filesystem: localFileSystem) {
222-
runtime = [ "-libc", settings.defaults.runtime.rawValue ]
219+
switch settings.defaults.runtime {
220+
case .multithreadedDebugDLL:
221+
runtime = [ "-libc", "MDd" ]
222+
case .multithreadedDLL:
223+
runtime = [ "-libc", "MD" ]
224+
case .multithreadedDebug:
225+
runtime = [ "-libc", "MTd" ]
226+
case .multithreaded:
227+
runtime = [ "-libc", "MT" ]
228+
}
223229
}
224230

225231
if let DEVELOPER_DIR = ProcessEnv.vars["DEVELOPER_DIR"],
@@ -308,8 +314,40 @@ public final class UserToolchain: Toolchain {
308314
self.extraCCFlags = [
309315
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
310316
] + destination.extraCCFlags
317+
318+
self.extraCPPFlags = destination.extraCPPFlags
311319
} else {
312320
self.extraCCFlags = destination.extraCCFlags
321+
self.extraCPPFlags = destination.extraCPPFlags
322+
}
323+
324+
if triple.isWindows() {
325+
if let SDKROOT = ProcessEnv.vars["SDKROOT"], let root = try? AbsolutePath(validating: SDKROOT) {
326+
if let settings = WindowsSDKSettings(reading: root.appending(component: "SDKSettings.plist"),
327+
diagnostics: nil, filesystem: localFileSystem) {
328+
switch settings.defaults.runtime {
329+
case .multithreadedDebugDLL:
330+
// Defines _DEBUG, _MT, and _DLL
331+
// Linker uses MSVCRTD.lib
332+
self.extraCCFlags += ["-D_DEBUG", "-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrtd"]
333+
334+
case .multithreadedDLL:
335+
// Defines _MT, and _DLL
336+
// Linker uses MSVCRT.lib
337+
self.extraCCFlags += ["-D_MT", "-D_DLL", "-Xclang", "--dependent-lib=msvcrt"]
338+
339+
case .multithreadedDebug:
340+
// Defines _DEBUG, and _MT
341+
// Linker uses LIBCMTD.lib
342+
self.extraCCFlags += ["-D_DEBUG", "-D_MT", "-Xclang", "--dependent-lib=libcmtd"]
343+
344+
case .multithreaded:
345+
// Defines _MT
346+
// Linker uses LIBCMT.lib
347+
self.extraCCFlags += ["-D_MT", "-Xclang", "--dependent-lib=libcmt"]
348+
}
349+
}
350+
}
313351
}
314352

315353
// Compute the path of directory containing the PackageDescription libraries.

0 commit comments

Comments
 (0)