|
| 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 | + |
1 | 6 | /* |
2 | 7 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 | 8 | * SPDX-License-Identifier: Apache-2.0 |
@@ -57,19 +62,59 @@ kotlin { |
57 | 62 |
|
58 | 63 |
|
59 | 64 | 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 | + |
60 | 95 | compilations["main"].cinterops { |
61 | 96 | 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) |
64 | 103 | } |
65 | 104 | } |
66 | 105 |
|
67 | 106 | // TODO clean up |
68 | 107 | 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 |
70 | 109 | "-linker-option", |
71 | 110 | "-v", |
72 | 111 | ) |
73 | 112 | compilerOptions.freeCompilerArgs.addAll(compilerArgs) |
74 | 113 | } |
75 | 114 | } |
| 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.") |
0 commit comments