Skip to content

Commit bf6cd51

Browse files
authored
Merge pull request #522 from thisisthepy/feat/191-native-consumer-windows
Plugin: SPEC L-9c for mingwX64 consumers (#191)
2 parents 68baeef + 5719060 commit bf6cd51

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎docs/SPEC.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ fail, then implement.
148148
already set (L-9's rule);
149149
4. after linking each executable, copies the packaged prefix (L-9's includes and excludes) to
150150
`<the executable's output directory>/python-multiplatform-home`, unless `packagePythonHome` is false.
151+
On Windows (`mingwX64`, SPEC P-1a) the rule differs where the loader does: binaries link the import library
152+
`<prefix>/libs/python3XYt.lib` and carry no search path; packaging puts the prefix's `*.dll` (the interpreter
153+
and the MSVC runtime) beside the executable, where Windows looks, and `Lib/` and `DLLs/` into
154+
`python-multiplatform-home`; `mingwX64Test` also gets the staged prefix on `PATH`.
151155
The library's own klib carries no libpython link option; a consumer that does not apply the plugin links it
152156
itself. `Status: draft` (#191), `GP/DesktopNativeConsumerWiringTest.kt`.
153157
- **L-10** Desktop: the library uses no JDK module beyond `java.base`, so it runs on a `jlink`ed

‎python-multiplatform-gradle-plugin/src/main/kotlin/python/multiplatform/gradle/DesktopNativeConsumerWiring.kt‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.gradle.api.tasks.TaskProvider
66
import java.io.File
77

88
/**
9-
* SPEC L-9c (#191): a consumer's Kotlin/Native desktop targets (`macosArm64`, `linuxX64`) link the
9+
* SPEC L-9c (#191): a consumer's Kotlin/Native desktop targets (`macosArm64`, `linuxX64`, `mingwX64`) link the
1010
* free-threaded `libpython` and ship it, with the standard library, beside the executable.
1111
*
1212
* Source: leader decision 2026-10-25 on #191, item 3: "K/N CPython: libpython is shipped beside the
@@ -21,6 +21,7 @@ import java.io.File
2121
internal fun desktopNativePlatformFor(konanName: String): String? = when (konanName) {
2222
"macos_arm64" -> "macos-aarch64"
2323
"linux_x64" -> "linux-x86_64"
24+
"mingw_x64" -> "windows-x86_64"
2425
else -> null
2526
}
2627

@@ -38,6 +39,9 @@ internal fun desktopNativeLibPythonName(pythonVersion: String, freeThreaded: Boo
3839
* build path.
3940
*/
4041
internal fun desktopNativeLinkerOpts(konanName: String, prefix: String, libName: String, debug: Boolean): List<String> {
42+
// Windows (SPEC P-1a): the MSVC import library, which lld links in MinGW mode. There is no
43+
// run-time search path to set: the loader looks beside the executable, where packaging puts the DLL.
44+
if (konanName.startsWith("mingw")) return listOf("$prefix/libs/${libName.replace(".", "")}.lib")
4145
val origin = if (konanName.startsWith("macos")) "@executable_path" else "\$ORIGIN"
4246
return buildList {
4347
add("-L$prefix/lib")
@@ -51,6 +55,14 @@ internal fun desktopNativeLinkerOpts(konanName: String, prefix: String, libName:
5155
internal fun isLinkedDesktopNativeBinary(binaryClassName: String): Boolean =
5256
binaryClassName in setOf("Executable", "TestExecutable", "SharedLibrary")
5357

58+
/** What of the prefix goes beside the executable itself: on Windows the DLLs (the loader's own search), elsewhere nothing. */
59+
internal fun desktopNativeExecutableDirIncludes(windows: Boolean): List<String> =
60+
if (windows) listOf("*.dll") else emptyList()
61+
62+
/** What of the prefix goes into `python-multiplatform-home`: L-9's set, without Windows' DLLs (they sit beside the executable). */
63+
internal fun desktopNativeHomeIncludes(pythonVersion: String, freeThreaded: Boolean, windows: Boolean): List<String> =
64+
if (windows) listOf("Lib/**", "DLLs/**") else packagedPrefixIncludes(pythonVersion, freeThreaded, windows = false)
65+
5466
internal fun desktopNativeStageTaskName(targetName: String): String =
5567
"stagePythonHome" + targetName.replaceFirstChar { it.uppercase() }
5668

@@ -70,6 +82,7 @@ internal fun PythonBindingsPlugin.configureDesktopNativeConsumer(project: Projec
7082
val packaging = project.provider { extension.packagePythonHome.getOrElse(true) }
7183

7284
for ((target, konan, platform) in targets) {
85+
val windows = platform.startsWith("windows")
7386
val targetName = invokeNoArg(target, "getName") as? String ?: continue
7487
val destination = File(cacheRoot, "$version+$release$flavour/$platform")
7588
val prefix = File(destination, "python")
@@ -108,11 +121,18 @@ internal fun PythonBindingsPlugin.configureDesktopNativeConsumer(project: Projec
108121
project.delete(home)
109122
project.copy {
110123
from(prefix) {
111-
include(packagedPrefixIncludes(version, freeThreaded, windows = false))
124+
include(desktopNativeHomeIncludes(version, freeThreaded, windows))
112125
exclude(PACKAGED_PREFIX_EXCLUDES)
113126
}
114127
into(home)
115128
}
129+
val besideExecutable = desktopNativeExecutableDirIncludes(windows)
130+
if (besideExecutable.isNotEmpty()) {
131+
project.copy {
132+
from(prefix) { include(besideExecutable) }
133+
into(outputDir)
134+
}
135+
}
116136
requireNoEmptyLibraries(home)
117137
}
118138
}
@@ -128,6 +148,11 @@ internal fun PythonBindingsPlugin.configureDesktopNativeConsumer(project: Projec
128148
it.name == "environment" && it.parameterCount == 2
129149
} ?: return@configureEach
130150
environment.invoke(this, "PYTHONHOME", prefix.absolutePath)
151+
// Windows finds the DLL a test executable imports on PATH (the executable sits in the build tree).
152+
if (windows) {
153+
val path = project.providers.environmentVariable("PATH").orNull.orEmpty()
154+
environment.invoke(this, "PATH", prefix.absolutePath + File.pathSeparator + path)
155+
}
131156
}
132157
}
133158
}

‎python-multiplatform-gradle-plugin/src/test/kotlin/python/multiplatform/gradle/DesktopNativeConsumerWiringTest.kt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,36 @@ class DesktopNativeConsumerWiringTest {
6868
assertEquals("stagePythonHomeMacosArm64", desktopNativeStageTaskName("macosArm64"))
6969
assertEquals("stagePythonHomeLinuxX64", desktopNativeStageTaskName("linuxX64"))
7070
}
71+
72+
// ---- SPEC L-9c, Windows (mingwX64, SPEC P-1a) ----
73+
74+
@Test
75+
fun windowsStagesTheWindowsPrefix() {
76+
assertEquals("windows-x86_64", desktopNativePlatformFor("mingw_x64"))
77+
}
78+
79+
/** No rpath on Windows: the import library is linked, and the DLL is found beside the executable. */
80+
@Test
81+
fun windowsLinksTheImportLibraryAndCarriesNoSearchPath() {
82+
assertEquals(
83+
listOf("/cache/python/libs/python315t.lib"),
84+
desktopNativeLinkerOpts("mingw_x64", "/cache/python", "python3.15t", debug = true),
85+
)
86+
assertEquals(
87+
listOf("/cache/python/libs/python315t.lib"),
88+
desktopNativeLinkerOpts("mingw_x64", "/cache/python", "python3.15t", debug = false),
89+
)
90+
}
91+
92+
/** The loader looks beside the executable, not in a subdirectory: the DLLs go there; the stdlib stays in the home. */
93+
@Test
94+
fun windowsPutsTheDllsBesideTheExecutable() {
95+
assertEquals(listOf("*.dll"), desktopNativeExecutableDirIncludes(windows = true))
96+
assertEquals(emptyList(), desktopNativeExecutableDirIncludes(windows = false))
97+
assertEquals(listOf("Lib/**", "DLLs/**"), desktopNativeHomeIncludes("3.15.0", freeThreaded = true, windows = true))
98+
assertEquals(
99+
listOf("lib/python3.15t/**", "lib/libpython3.15t.*"),
100+
desktopNativeHomeIncludes("3.15.0", freeThreaded = true, windows = false),
101+
)
102+
}
71103
}

0 commit comments

Comments
 (0)