From f1c6bc6210332ae922042b3286d247e7fe71ce52 Mon Sep 17 00:00:00 2001 From: Light Ning Date: Sat, 10 Nov 2018 00:55:45 +0800 Subject: [PATCH] cross compile grpc-rs to windows under *nix (#169) * update for cross compile * add cross_compile * update cross_compile * add cross compile link * update * update * fix typo --- README.md | 4 +++ cross_compile.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++ grpc-sys/build.rs | 6 ++-- 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 cross_compile.md diff --git a/README.md b/README.md index 8f5910d2a..b2b8d5430 100644 --- a/README.md +++ b/README.md @@ -97,3 +97,7 @@ grpcio = { version = "0.4", default-features = false, features = ["protobuf-code ## Performance See [benchmark](https://github.com/pingcap/grpc-rs/tree/master/benchmark) to find out how to run a benchmark by yourself. + +Cross Compile +------------- +See [cross_compile](cross_compile.md) diff --git a/cross_compile.md b/cross_compile.md new file mode 100644 index 000000000..a3146227e --- /dev/null +++ b/cross_compile.md @@ -0,0 +1,82 @@ +# Cross Compile gRPC-rs(0.2.1) to Windows under *nix + +## First you need to install mingw + +```bash +# macOS +brew install mingw-w64 + +# CentOS +yum install mingw64-openssl-static mingw64-zlib-static mingw64-winpthreads-static +``` + +## Fix CMake + +``` +# modify grpc-rs/grpc-sys/build.rs +# fix SYSTEM_PROCESSOR +"CMAKE_SYSTEM_PROCESSOR", get_env("CARGO_CFG_TARGET_ARCH").unwrap() +# fix try_run +"CMAKE_CROSSCOMPILING", "true" +``` + +### All diff in `fn build_grpc` + +```rust + let dst = { + let mut config = Config::new("grpc"); + if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "macos") { + config.cxxflag("-stdlib=libc++"); + } + config + .define("CMAKE_SYSTEM_PROCESSOR", get_env("CARGO_CFG_TARGET_ARCH").unwrap()) + .define("CMAKE_CROSSCOMPILING", "true") + .build_target(library) + .uses_cxx11() + .build() + // config.build_target(library).uses_cxx11().build() + }; +``` + +### Fix find zlib + +```rust + // try these values + let mut zlib = "z"; + let mut zlib = "zlibstatic"; + let mut zlib = "zlibstaticd"; +``` + +## Fix try_run + +``` +# grpc-rs/grpc-sys/grpc/third_party/benchmark/cmake/CXXFeatureCheck.cmake +# add these code to fix try_run +SET( RUN_HAVE_STD_REGEX + 0 + CACHE STRING "Result from TRY_RUN" FORCE) + +SET( RUN_HAVE_STEADY_CLOCK + 0 + CACHE STRING "Result from TRY_RUN" FORCE) +``` + +## Fix WIN32 API + +``` +# grpc-rs/grpc-sys/grpc/CMakeLists.txt +# add these code after about line number 295 +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_WIN32_WINNT=0x600") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_WIN32_WINNT=0x600") +set(C_CXX_FLAGS "${C_CXX_FLAGS} -D_WIN32_WINNT=0x600") +``` + +## Fix boringssl + +Just update third_party/boringssl + +```bash +cd third_party/boringssl +git checkout master +git pull +``` diff --git a/grpc-sys/build.rs b/grpc-sys/build.rs index f674c9ae9..80d17f4be 100644 --- a/grpc-sys/build.rs +++ b/grpc-sys/build.rs @@ -75,7 +75,7 @@ fn build_grpc(cc: &mut Build, library: &str) { // the unnecessary dependency. config.define("GO_EXECUTABLE", "fake-go-nonexist"); } - if cfg!(target_os = "macos") { + if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "macos") { config.cxxflag("-stdlib=libc++"); } if env::var("CARGO_CFG_TARGET_ENV").unwrap_or("".to_owned()) == "musl" { @@ -106,7 +106,7 @@ fn build_grpc(cc: &mut Build, library: &str) { "boringssl/ssl", "boringssl/crypto", ]; - if cfg!(target_os = "windows") { + if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") { let profile = match &*env::var("PROFILE").unwrap_or("debug".to_owned()) { "bench" | "release" => { zlib = "zlibstatic"; @@ -193,7 +193,7 @@ fn main() { } cc.file("grpc_wrap.cc"); - if cfg!(target_os = "windows") { + if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") { // At lease win7 cc.define("_WIN32_WINNT", Some("0x0700")); }