Skip to content

Commit 0e7b538

Browse files
committed
discover mingw paths automatically
1 parent 78db455 commit 0e7b538

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

runtime/runtime-core/build.gradle.kts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import aws.sdk.kotlin.gradle.util.prop
2+
import aws.sdk.kotlin.gradle.util.typedProp
3+
import java.nio.file.Files
4+
import java.nio.file.Paths
5+
16
/*
27
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
38
* SPDX-License-Identifier: Apache-2.0
@@ -57,19 +62,59 @@ kotlin {
5762

5863

5964
mingwX64 {
65+
val mingwHome = findMingwHome()
66+
val defPath = layout.buildDirectory.file("cinterop/winver.def")
67+
68+
// Dynamically construct def file because of dynamic mingw paths
69+
val defFileTask by tasks.registering {
70+
outputs.file(defPath)
71+
72+
val mingwLibs = Paths.get(mingwHome, "lib").toString().replace("\\", "\\\\") // Windows path shenanigans
73+
74+
doLast {
75+
Files.writeString(
76+
defPath.get().asFile.toPath(),
77+
"""
78+
package = aws.smithy.kotlin.native.winver
79+
headers = windows.h
80+
compilerOpts = \
81+
-DUNICODE \
82+
-DWINVER=0x0601 \
83+
-D_WIN32_WINNT=0x0601 \
84+
-DWINAPI_FAMILY=3 \
85+
-DOEMRESOURCE \
86+
-Wno-incompatible-pointer-types \
87+
-Wno-deprecated-declarations
88+
libraryPaths = $mingwLibs
89+
staticLibraries = libversion.a
90+
""".trimIndent(),
91+
)
92+
}
93+
}
94+
6095
compilations["main"].cinterops {
6196
create("winver") {
62-
definitionFile.set(file("native/interop/winver.def"))
63-
includeDirs("C:\\msys64\\mingw64\\include")
97+
val mingwIncludes = Paths.get(mingwHome, "include").toString()
98+
includeDirs(mingwIncludes)
99+
definitionFile.set(defPath)
100+
101+
// Ensure that the def file is written first
102+
tasks[interopProcessingTaskName].dependsOn(defFileTask)
64103
}
65104
}
66105

67106
// TODO clean up
68107
val compilerArgs = listOf(
69-
"-Xverbose-phases=Linker", // Enable verbose linking phase from the compiler
108+
"-Xverbose-phases=linker", // Enable verbose linking phase from the compiler
70109
"-linker-option",
71110
"-v",
72111
)
73112
compilerOptions.freeCompilerArgs.addAll(compilerArgs)
74113
}
75114
}
115+
116+
private fun findMingwHome(): String =
117+
System.getenv("MINGW_PREFIX")?.takeUnless { it.isBlank() } ?:
118+
typedProp("mingw.prefix") ?:
119+
throw IllegalStateException("Cannot determine MinGW prefix location. Please verify MinGW is installed correctly " +
120+
"and that either the `MINGW_PREFIX` environment variable or the `mingw.prefix` Gradle property is set.")

runtime/runtime-core/native/interop/winver.def

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)