From f291aa29dab4d6c2a8803af9fe8d6227c981ebfb Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Wed, 2 Jan 2019 14:25:02 -0800 Subject: [PATCH] WIP on using a build.rs to avoid having Gradle set environment variables. This DOES NOT WORK. What's needed is a solution for https://github.com/rust-lang/cargo/issues/4121. --- build.rs | 91 +++++++++++++++++++++++++++++++ components/logins/Cargo.toml | 1 + components/logins/ffi/Cargo.toml | 1 + components/places/Cargo.toml | 1 + components/places/ffi/Cargo.toml | 1 + components/support/sql/Cargo.toml | 1 + 6 files changed, 96 insertions(+) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000000..1c3074c376 --- /dev/null +++ b/build.rs @@ -0,0 +1,91 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use std::env; +use std::fs::canonicalize; +use std::path::Path; +use std::process::Command; + +fn main() { + // uncomment to print all env vars + for (k, v) in std::env::vars() { + println!("{} -> {}", k, v); + } + + let out_dir = env::var("OUT_DIR").unwrap(); + + // /Users/nalexander/Mozilla/application-services/target/debug/build/logins_ffi-d0290628a891f004/out + let target = Path::new(&out_dir).ancestors().find(|p| p.ends_with("target")).unwrap(); + + // path.ends_with("passwd")); + + // run the build scripts from the lib dir. + let libs_dir = + canonicalize(target.join("..").join("build").join("libs")).unwrap(); + + // canonicalize(Path::new(".").join("build").join("libs")).unwrap(); + // canonicalize(Path::new(".").join("..").join("..").join("..").join("build").join("libs")).unwrap(); + + let target_os = env::var("CARGO_CFG_TARGET_OS"); + let build_info = match target_os.as_ref().map(|x| &**x) { + Ok("linux") => Some(("desktop", "linux-x86-64")), + Ok("windows") => Some(("desktop", "win32-x86-64")), + Ok("macos") => Some(("desktop", "darwin")), + // Ok("android") => Some("android"), - TODO - need to do better at x-compile support. + _ => None, + }; + match build_info { + None => println!( + "Unknown target OS '{:?}' - not executing external build script", + target_os + ), + Some((build_platform, target_dir)) => { + // let status = Command::new("sh") + // .args(&["-c", &format!("./build-all.sh {}", build_platform)]) + // .current_dir(libs_dir.as_os_str()) + // .status() + // .unwrap(); + // if !status.success() { + // panic!("external build script failed: {:?}", status); + // } + // println!("external build script succeeded"); + + println!("cargo:rustc-env=OPENSSL_STATIC=1"); + + let openssl = libs_dir + .join(build_platform) + .join(target_dir) + .join("openssl"); + println!("cargo:rustc-env=OPENSSL_DIR={}", openssl.to_str().unwrap()); + + let sqlcipher = libs_dir + .join(build_platform) + .join(target_dir) + .join("sqlcipher"); + println!("cargo:rustc-env=SQLCIPHER_LIB_DIR={}", sqlcipher.join("lib").to_str().unwrap()); + println!("cargo:rustc-env=SQLCIPHER_INCLUDE_DIR={}", sqlcipher.join("include").to_str().unwrap()); + + // // point at the libraries + // let openssl = libs_dir + // .join(build_platform) + // .join(target_dir) + // .join("openssl") + // .join("lib"); + // println!( + // "cargo:rustc-link-search=native={}", + // openssl.to_str().unwrap() + // ); + + // let sqlcipher = libs_dir + // .join(build_platform) + // .join(target_dir) + // .join("sqlcipher") + // .join("lib"); + // println!( + // "cargo:rustc-link-search=native={}", + // sqlcipher.to_str().unwrap() + // ); + } + }; +} diff --git a/components/logins/Cargo.toml b/components/logins/Cargo.toml index f1138f6362..3b41c30999 100644 --- a/components/logins/Cargo.toml +++ b/components/logins/Cargo.toml @@ -3,6 +3,7 @@ name = "logins" edition = "2018" version = "0.1.0" authors = ["Thom Chiovoloni "] +build = "../../build.rs" [features] ffi = ["ffi-support"] diff --git a/components/logins/ffi/Cargo.toml b/components/logins/ffi/Cargo.toml index e67e21fe45..07bb96ebdd 100644 --- a/components/logins/ffi/Cargo.toml +++ b/components/logins/ffi/Cargo.toml @@ -3,6 +3,7 @@ name = "logins_ffi" edition = "2018" version = "0.1.0" authors = ["Thom Chiovoloni "] +build = "../../../build.rs" [lib] name = "logins_ffi" diff --git a/components/places/Cargo.toml b/components/places/Cargo.toml index 9917efe659..8786964ff9 100644 --- a/components/places/Cargo.toml +++ b/components/places/Cargo.toml @@ -3,6 +3,7 @@ name = "places" edition = "2018" version = "0.1.0" authors = [] +build = "../../build.rs" [features] ffi = ["ffi-support"] diff --git a/components/places/ffi/Cargo.toml b/components/places/ffi/Cargo.toml index bd095d5d0d..acef265e48 100644 --- a/components/places/ffi/Cargo.toml +++ b/components/places/ffi/Cargo.toml @@ -3,6 +3,7 @@ name = "places-ffi" edition = "2018" version = "0.1.0" authors = ["Thom Chiovoloni "] +build = "../../../build.rs" [lib] name = "places_ffi" diff --git a/components/support/sql/Cargo.toml b/components/support/sql/Cargo.toml index ffb748cce0..767b80fc9f 100644 --- a/components/support/sql/Cargo.toml +++ b/components/support/sql/Cargo.toml @@ -3,6 +3,7 @@ name = "sql-support" edition = "2018" version = "0.1.0" authors = ["Thom Chiovoloni "] +build = "../../../build.rs" [features] default = ["sqlcipher"]