Skip to content

Commit

Permalink
cross compile grpc-rs to windows under *nix (#169)
Browse files Browse the repository at this point in the history
* update for cross compile

* add cross_compile

* update cross_compile

* add cross compile link

* update

* update

* fix typo
  • Loading branch information
light4 authored and Hoverbear committed Nov 9, 2018
1 parent 9d1b98a commit f1c6bc6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
82 changes: 82 additions & 0 deletions cross_compile.md
Original file line number Diff line number Diff line change
@@ -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
```
6 changes: 3 additions & 3 deletions grpc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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"));
}
Expand Down

0 comments on commit f1c6bc6

Please sign in to comment.