Implement static linking for Windows executables and add CI validation workflow#28
Implement static linking for Windows executables and add CI validation workflow#28Copilot wants to merge 4 commits into
Conversation
Co-authored-by: BppleMan <16437384+BppleMan@users.noreply.github.com>
|
@copilot repaire this: 开启:推送:分支:[“main”,“dev”]路径:-'kommand-examples / ' -'kommand-core / '-'src /'-'build.gradle.kts'-'.github/workflows/windows-no-mingw-examples.yml'pull_request:分支:[“main”,“dev”]路径:-'kommand-examples / '-' kommand-core /'-'src /'-'build.gradle.kts'-'.github/workflows/windows-no-mingw-examples.yml'workflow_dispatch: 权限:内容:读取 作业:构建示例:名称:构建示例(使用 MSYS2 / MinGW)运行于:windows-latest 步骤:-名称:Checkout 用途:actions/checkout@v4 windows-no-mingw-test:名称:Windows 上的测试示例(无 MinGW)运行于:windows-latest 需求:build-examples 步骤:-名称:Checkout 用途:actions/checkout@v4 *** 修改文件: kommand-examples/example1/build.gradle.kts *** (替换该文件为下面内容:) import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.konan.target.KonanTarget val currentPlatform:Platform = when { DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MACOS_X64 DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.MACOS_ARM64 DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.LINUX_X64 DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.LINUX_ARM64 DefaultNativePlatform.getCurrentOperatingSystem().isWindows && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MINGW_X64 else -> throw GradleException("Kotlin/Native 不支持主机操作系统。") } 插件 { kotlin("多平台") } 存储库 { mavenCentral() gradlePluginPortal() } kotlin { jvm { compilerOptions { jvmTarget.set(JvmTarget.JVM_17) } withJava() testRuns["test"].executionTask.configure { useJUnitPlatform() } } 枚举类平台(val archName:String){MACOS_X64(“x86_64-apple-darwin”),MACOS_ARM64(“aarch64-apple-darwin”),LINUX_X64(“x86_64-unknown-linux-gnu”),LINUX_ARM64(“aarch64-unknown-linux-gnu”),MINGW_X64(“x86_64-pc-windows-gnu”),;} *** 修改文件: kommand-examples/example2/build.gradle.kts *** (把和 example1 中四个的应用到 example2 的 build.gradle.kts;内容同上,确保加上 Targets.withType ... linkerOpts 和 buildKommandCoreForWin 任务) *** 修改文件: kommand-examples/example3/build.gradle.kts *** (把和 example1 中四个的应用到 example3 的 build.gradle.kts;内容同上) *** 修改文件: kommand-examples/timeout/build.gradle.kts *** (把和 example1 中四个的应用到 timeout 的 build.gradle.kts;内容同上) *** 修改/新增文件: kommand-core/Cargo.toml *** (在 kommand-core 的 Cargo.toml 中添加/确保以下段落;如果已有 [lib] 则合入) [lib] name = "kommand_core" crate-type = ["staticlib"] *** 新增文件: kommand-core/.cargo/config.toml *** [target.x86_64-pc-windows-gnu] rustflags = [ "-C", "link-arg=-static-libgcc", "-C", "link-arg=-static-libstdc++" ] |
- Split workflow into two jobs: build (with MinGW) and test (without MinGW) - Add static linking flags (-static-libgcc, -static-libstdc++) to all examples - Link kommand-core static library for MinGW target - Add cargo config for Windows static linking - Add buildKommandCoreForWin task to build Rust static lib before assembly Co-authored-by: BppleMan <16437384+BppleMan@users.noreply.github.com>
... All requested changes implemented in commit c3671cb: ✅ Workflow split into two jobs (build with MinGW, test without MinGW) The workflow now builds executables with MSYS2/MinGW in the first job, then tests them in a clean Windows environment without MinGW in the second job to verify they're self-contained. |
|
@copilot |
The task was being registered unconditionally, causing failures on macOS and Linux CI runners. Now it only runs when currentPlatform == MINGW_X64. Co-authored-by: BppleMan <16437384+BppleMan@users.noreply.github.com>
Fixed in commit d81c07f. The The task is now wrapped in a platform check: if (currentPlatform == Platform.MINGW_X64) {
tasks.register("buildKommandCoreForWin") { ... }
}This ensures it only runs when building for Windows. The workflows should now pass on all platforms. |
✅ Static Linking Implementation for Windows Executables
Implements comprehensive static linking to resolve libstdc++-6.dll dependency issues on Windows.
Changes Made
Static Linking Implementation
All example build.gradle.kts files (example1, example2, example3, timeout):
-static-libgcc,-static-libstdc++for MINGW_X64 targetlibkommand_core.a)buildKommandCoreForWintask to build Rust library with static linking before assemblyRust configuration (
kommand-core/.cargo/config.toml):x86_64-pc-windows-gnutargetCI Workflow
New workflow:
.github/workflows/windows-no-mingw-examples.ymlbuild-examples): Builds with MSYS2/MinGW installed, uploads executables as artifactswindows-no-mingw-test): Tests executables in clean environment WITHOUT MinGW./gradlew :kommand-examples:assemble.exefilesTriggers: push/PR to main/dev with path filters for relevant source directories
Artifacts: uploads executables (7-day retention) for debugging
How It Works
Build phase (with MinGW): Builds kommand-core Rust library as static lib with static linking flags, then links examples against it with
-static-libgcc -static-libstdc++to produce self-contained .exe filesTest phase (without MinGW): Downloads built executables and runs them in a clean Windows environment to verify they don't require external MinGW runtime DLLs
Validation
The workflow ensures Windows executables are truly self-contained and can run on systems without MinGW, catching linking issues that would cause runtime DLL dependency failures on end-user machines.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.