diff --git a/Cargo.lock b/Cargo.lock index e98072f..8ed2b40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,8 @@ version = 4 name = "DNF" version = "0.1.0" dependencies = [ + "cmake", + "cxx", "libloading", "log_collection", "once_cell", @@ -22,6 +24,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -68,6 +76,52 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "clap" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00" +dependencies = [ + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + +[[package]] +name = "cmake" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75443c44cd6b379beb8c5b45d85d0773baf31cce901fe7bb252f4eff3008ef7d" +dependencies = [ + "cc", +] + +[[package]] +name = "codespan-reporting" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" +dependencies = [ + "serde", + "termcolor", + "unicode-width", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -140,6 +194,68 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "cxx" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbda285ba6e5866529faf76352bdf73801d9b44a6308d7cd58ca2379f378e994" +dependencies = [ + "cc", + "cxx-build", + "cxxbridge-cmd", + "cxxbridge-flags", + "cxxbridge-macro", + "foldhash", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af9efde466c5d532d57efd92f861da3bdb7f61e369128ce8b4c3fe0c9de4fa4d" +dependencies = [ + "cc", + "codespan-reporting", + "indexmap", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-cmd" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3efb93799095bccd4f763ca07997dc39a69e5e61ab52d2c407d4988d21ce144d" +dependencies = [ + "clap", + "codespan-reporting", + "indexmap", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3092010228026e143b32a4463ed9fa8f86dca266af4bf5f3b2a26e113dbe4e45" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.192" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31d72ebfcd351ae404fb00ff378dfc9571827a00722c9e735c9181aec320ba0a" +dependencies = [ + "indexmap", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "deranged" version = "0.5.5" @@ -203,6 +319,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + [[package]] name = "foreign-types" version = "0.3.2" @@ -600,6 +722,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "link-cplusplus" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f78c730aaa7d0b9336a299029ea49f9ee53b0ed06e9202e8cb7db9bae7b8c82" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.11.0" @@ -1018,6 +1149,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "scratch" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d68f2ec51b097e4c1a75b681a8bec621909b5e91f15bb7b840c4f2f7b01148b2" + [[package]] name = "security-framework" version = "2.11.1" @@ -1158,6 +1295,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -1229,6 +1372,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "2.0.17" @@ -1514,6 +1666,12 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + [[package]] name = "untrusted" version = "0.9.0" @@ -1643,6 +1801,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "windows-link" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index eb6b03e..dfb19e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,7 @@ tracing = "0.1.44" log_collection = { git = "https://gitea.lingapi.top/Rust/log_service.git", rev = "7086053f" } tokio = "1.49.0" once_cell = "1.21.3" +cxx = "1.0.192" + +[build-dependencies] +cmake = "0.1.57" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..557d52e --- /dev/null +++ b/build.rs @@ -0,0 +1,63 @@ +// 版权所有 (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"); +} diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt new file mode 100644 index 0000000..0af8fd1 --- /dev/null +++ b/cpp/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 4.1) +project(cpp) + +set(CMAKE_CXX_STANDARD 20) + +add_library(cpp STATIC library.cpp) diff --git a/cpp/library.cpp b/cpp/library.cpp new file mode 100644 index 0000000..7468f63 --- /dev/null +++ b/cpp/library.cpp @@ -0,0 +1,9 @@ +#include "library.h" + +#include + +extern "C" { +void hello() { + std::cout << "Hello, World!" << std::endl; +} +} diff --git a/cpp/library.h b/cpp/library.h new file mode 100644 index 0000000..b2e3c39 --- /dev/null +++ b/cpp/library.h @@ -0,0 +1,7 @@ +#ifndef CPP_LIBRARY_H +#define CPP_LIBRARY_H + +extern "C" { +void hello(); +} +#endif // CPP_LIBRARY_H diff --git a/cpp/toolchain-mingw32.cmake b/cpp/toolchain-mingw32.cmake new file mode 100644 index 0000000..de5f493 --- /dev/null +++ b/cpp/toolchain-mingw32.cmake @@ -0,0 +1,23 @@ +# 目标系统 - 32位Windows +set(CMAKE_SYSTEM_NAME Windows) +set(CMAKE_SYSTEM_PROCESSOR i686) + +# 使用32位MinGW编译器 +set(CMAKE_C_COMPILER i686-w64-mingw32-gcc) +set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) +set(CMAKE_RC_COMPILER i686-w64-mingw32-windres) +set(CMAKE_AR i686-w64-mingw32-ar) +set(CMAKE_RANLIB i686-w64-mingw32-ranlib) + +# 调整搜索路径 +set(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +# 静态链接运行时库,避免需要额外DLL +set(CMAKE_EXE_LINKER_FLAGS_INIT "-static-libgcc -static-libstdc++ -static") + +# 确保是32位 +set(CMAKE_C_FLAGS_INIT "-m32") +set(CMAKE_CXX_FLAGS_INIT "-m32") \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 60077c1..c66198b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use tracing::debug; use crate::log::get_log_path; mod log; @@ -9,4 +10,12 @@ async fn main() { // 先初始化dll库 memory::init(); + debug!("调用C++"); + unsafe { hello() } + debug!("调用成功") } + +unsafe extern "C" { + fn hello(); +} +