From 08902cf05eddf53e0ba8ccd8a4b12d615d3445e3 Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Thu, 19 Nov 2020 19:03:52 +0100 Subject: Upgrade to 0.7.1 Test: build, TH Change-Id: I3cb2c39b931e2b5c5b1a0bf775c6185c0e521da8 --- build.rs | 126 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 61 insertions(+), 65 deletions(-) (limited to 'build.rs') diff --git a/build.rs b/build.rs index b5f34eab..34732393 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,6 @@ // Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. +use std::collections::HashSet; use std::env::VarError; use std::io::prelude::*; use std::io::BufReader; @@ -10,7 +11,7 @@ use cmake::Config as CmakeConfig; use pkg_config::{Config as PkgConfig, Library}; use walkdir::WalkDir; -const GRPC_VERSION: &str = "1.29.1"; +const GRPC_VERSION: &str = "1.33.1"; fn probe_library(library: &str, cargo_metadata: bool) -> Library { match PkgConfig::new() @@ -24,17 +25,14 @@ fn probe_library(library: &str, cargo_metadata: bool) -> Library { } fn prepare_grpc() { - let mut modules = vec![ + let modules = vec![ "grpc", "grpc/third_party/cares/cares", "grpc/third_party/address_sorting", "grpc/third_party/abseil-cpp", + "grpc/third_party/re2", ]; - if cfg!(feature = "secure") && !cfg!(feature = "openssl") { - modules.push("grpc/third_party/boringssl-with-bazel"); - } - for module in modules { if is_directory_empty(module).unwrap_or(true) { panic!( @@ -62,15 +60,7 @@ fn trim_start<'a>(s: &'a str, prefix: &str) -> Option<&'a str> { fn build_grpc(cc: &mut cc::Build, library: &str) { prepare_grpc(); - let mut third_party = vec![ - "cares/cares/lib", - "abseil-cpp/absl/strings", - "abseil-cpp/absl/time", - "abseil-cpp/absl/base", - "abseil-cpp/absl/types", - "abseil-cpp/absl/numeric", - ]; - + let target = env::var("TARGET").unwrap(); let dst = { let mut config = CmakeConfig::new("grpc"); @@ -90,7 +80,7 @@ fn build_grpc(cc: &mut cc::Build, library: &str) { } // Cross-compile support for iOS - match env::var("TARGET").unwrap().as_str() { + match target.as_str() { "aarch64-apple-ios" => { config .define("CMAKE_OSX_SYSROOT", "iphoneos") @@ -133,13 +123,13 @@ fn build_grpc(cc: &mut cc::Build, library: &str) { config.define("gRPC_BUILD_CODEGEN", "false"); // We don't need to build benchmarks. config.define("gRPC_BENCHMARK_PROVIDER", "none"); + config.define("gRPC_SSL_PROVIDER", "package"); if cfg!(feature = "openssl") { - config.define("gRPC_SSL_PROVIDER", "package"); if cfg!(feature = "openssl-vendored") { config.register_dep("openssl"); } - } else if cfg!(feature = "secure") { - third_party.extend_from_slice(&["boringssl-with-bazel"]); + } else { + build_boringssl(&mut config); } if cfg!(feature = "no-omit-frame-pointer") { config @@ -151,58 +141,41 @@ fn build_grpc(cc: &mut cc::Build, library: &str) { config.build_target(library).uses_cxx11().build() }; - let build_dir = format!("{}/build", dst.display()); - if get_env("CARGO_CFG_TARGET_OS").map_or(false, |s| s == "windows") { - let profile = match &*env::var("PROFILE").unwrap() { - "bench" | "release" => "Release", - _ => "Debug", - }; - println!("cargo:rustc-link-search=native={}/{}", build_dir, profile); - for path in third_party { - println!( - "cargo:rustc-link-search=native={}/third_party/{}/{}", - build_dir, path, profile - ); - } + let lib_suffix = if target.contains("msvc") { + ".lib" } else { - println!("cargo:rustc-link-search=native={}", build_dir); - for path in third_party { + ".a" + }; + let build_dir = format!("{}/build", dst.display()); + for e in WalkDir::new(&build_dir) { + let e = e.unwrap(); + if e.file_name().to_string_lossy().ends_with(lib_suffix) { println!( - "cargo:rustc-link-search=native={}/third_party/{}", - build_dir, path, + "cargo:rustc-link-search=native={}", + e.path().parent().unwrap().display() ); } } - // link libz - println!("cargo:rustc-link-lib=static=z"); - // link cares - println!("cargo:rustc-link-lib=static=cares"); - // link address_sorting - println!("cargo:rustc-link-lib=static=address_sorting"); - // link absl/base - println!("cargo:rustc-link-lib=static=absl_base"); - println!("cargo:rustc-link-lib=static=absl_raw_logging_internal"); - println!("cargo:rustc-link-lib=static=absl_dynamic_annotations"); - println!("cargo:rustc-link-lib=static=absl_throw_delegate"); - println!("cargo:rustc-link-lib=static=absl_log_severity"); - println!("cargo:rustc-link-lib=static=absl_spinlock_wait"); - // link absl/strings - println!("cargo:rustc-link-lib=static=absl_strings"); - println!("cargo:rustc-link-lib=static=absl_strings_internal"); - println!("cargo:rustc-link-lib=static=absl_str_format_internal"); - // link absl/time - println!("cargo:rustc-link-lib=static=absl_civil_time"); - println!("cargo:rustc-link-lib=static=absl_time_zone"); - println!("cargo:rustc-link-lib=static=absl_time"); - // link absl/types - println!("cargo:rustc-link-lib=static=absl_bad_optional_access"); - // link absl/numeric - println!("cargo:rustc-link-lib=static=absl_int128"); - // link grpc related lib - println!("cargo:rustc-link-lib=static=gpr"); - println!("cargo:rustc-link-lib=static=upb"); - println!("cargo:rustc-link-lib=static={}", library); + let collect = |path, to: &mut HashSet<_>| { + let f = fs::File::open(format!("{}/libs/opt/pkgconfig/{}.pc", build_dir, path)).unwrap(); + for l in io::BufReader::new(f).lines() { + let l = l.unwrap(); + if l.starts_with("Libs: ") { + for lib in l.split_whitespace() { + if let Some(s) = lib.strip_prefix("-l") { + to.insert(s.to_string()); + } + } + } + } + }; + let mut libs = HashSet::new(); + collect("gpr", &mut libs); + collect(library, &mut libs); + for l in libs { + println!("cargo:rustc-link-lib=static={}", l); + } if cfg!(feature = "secure") { if cfg!(feature = "openssl") && !cfg!(feature = "openssl-vendored") { @@ -211,6 +184,12 @@ fn build_grpc(cc: &mut cc::Build, library: &str) { println!("cargo:rustc-link-lib=static=ssl"); println!("cargo:rustc-link-lib=static=crypto"); } + } else { + // grpc_unsecure.pc is not accurate, see also grpc/grpc#24512. + println!("cargo:rustc-link-lib=static=upb"); + println!("cargo:rustc-link-lib=static=cares"); + println!("cargo:rustc-link-lib=static=z"); + println!("cargo:rustc-link-lib=static=address_sorting"); } cc.include("grpc/include"); @@ -243,6 +222,19 @@ fn figure_ssl_path(build_dir: &str) { println!("cargo:rustc-link-lib=crypto"); } +fn build_boringssl(config: &mut CmakeConfig) { + let boringssl_artifact = boringssl_src::Build::new().build(); + config.define( + "OPENSSL_ROOT_DIR", + format!("{}", boringssl_artifact.root_dir().display()), + ); + // To avoid linking system library, set lib path explicitly. + println!( + "cargo:rustc-link-search=native={}", + boringssl_artifact.lib_dir().display() + ); +} + fn setup_libz(config: &mut CmakeConfig) { config.define("gRPC_ZLIB_PROVIDER", "package"); config.register_dep("Z"); @@ -296,6 +288,9 @@ fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) { config = config.header(path); } + println!("cargo:rerun-if-env-changed=TEST_BIND"); + let gen_tests = env::var("TEST_BIND").map_or(false, |s| s == "1"); + let cfg = config .header("grpc_wrap.cc") .clang_arg("-xc++") @@ -320,6 +315,7 @@ fn bindgen_grpc(mut config: bindgen::Builder, file_path: &PathBuf) { .blacklist_type(r"gpr_cv") .blacklist_type(r"gpr_once") .constified_enum_module(r"grpc_status_code") + .layout_tests(gen_tests) .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: false, }); -- cgit v1.2.3