@@ -6,7 +6,7 @@ import org.gradle.api.tasks.TaskProvider
66import 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
2121internal 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 */
4041internal 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:
5155internal 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+
5466internal 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 }
0 commit comments