// 版权所有 (c) ling 保留所有权利。 // 除非另行说明,否则仅允许在DNF中使用此文件中的代码。 // // 由 ling 创建于 2026/1/17. #![allow(non_snake_case)] use cmake::Config; use std::env; fn main() { let target = env::var("TARGET").unwrap(); if !target.contains("i686") || !target.contains("windows") { panic!("此项目只能以32位Windows平台为构建目标!"); } // 构建类型 let profile = env::var("PROFILE").unwrap(); let mut cmake_config = Config::new("cpp"); cmake_config.build_target("cpp"); //设置构建类型 if profile == "release" { cmake_config.profile("Release"); } else { cmake_config.profile("Debug"); } // 配置交叉编译 let host = env::var("HOST").unwrap(); if host.contains("linux") && target.contains("windows") { println!("cargo:warning=正在Linux环境中为32位Windows平台交叉编译"); // 设置交叉编译工具链 // 设置交叉编译工具链 cmake_config .define("CMAKE_SYSTEM_NAME", "Windows") .define("CMAKE_SYSTEM_PROCESSOR", "i686") .define("CMAKE_C_COMPILER", "i686-w64-mingw32-gcc") .define("CMAKE_CXX_COMPILER", "i686-w64-mingw32-g++") .define("CMAKE_RC_COMPILER", "i686-w64-mingw32-windres") .define("CMAKE_FIND_ROOT_PATH", "/usr/i686-w64-mingw32") .define("CMAKE_FIND_ROOT_PATH_MODE_PROGRAM", "NEVER") .define("CMAKE_FIND_ROOT_PATH_MODE_LIBRARY", "ONLY") .define("CMAKE_FIND_ROOT_PATH_MODE_INCLUDE", "ONLY"); } cmake_config.generator("Ninja"); let dst = cmake_config.build(); let dst = dst.join("build"); println!( "cargo:warning=输出目录:{}", dst.as_path().to_str().unwrap() ); // 将编译得到的c++库链接进来 println!("cargo:rustc-link-search=native={}", dst.display()); println!("cargo:rustc-link-lib=static=cpp"); println!("cargo:rustc-link-lib=dylib=ws2_32"); println!("cargo:rustc-link-lib=dylib=user32"); println!("cargo:rustc-link-lib=dylib=kernel32"); // 静态链接 C++ 运行时 println!("cargo:rustc-link-lib=static=stdc++"); println!("cargo:rustc-link-lib=static=gcc"); println!("cargo:rustc-link-lib=static=gcc_eh"); println!("cargo:rustc-link-lib=static=pthread"); }