blob: ede641102541eecfe0254045d9c02b89f75290de [file] [log] [blame]
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -04001/* Copyright (c) 2021, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15
Kenichi Ishibashie5abf582022-03-02 17:41:50 +090016use std::env;
17use std::path::Path;
David Benjamin26669ff2023-04-21 15:12:24 -040018use std::path::PathBuf;
19
Alex Gaynor8d71d242023-10-11 17:01:38 -040020// Keep in sync with the list in include/openssl/opensslconf.h
21const OSSL_CONF_DEFINES: &[&str] = &[
22 "OPENSSL_NO_ASYNC",
23 "OPENSSL_NO_BF",
24 "OPENSSL_NO_BLAKE2",
25 "OPENSSL_NO_BUF_FREELISTS",
26 "OPENSSL_NO_CAMELLIA",
27 "OPENSSL_NO_CAPIENG",
28 "OPENSSL_NO_CAST",
29 "OPENSSL_NO_CMS",
30 "OPENSSL_NO_COMP",
31 "OPENSSL_NO_CT",
32 "OPENSSL_NO_DANE",
33 "OPENSSL_NO_DEPRECATED",
34 "OPENSSL_NO_DGRAM",
35 "OPENSSL_NO_DYNAMIC_ENGINE",
36 "OPENSSL_NO_EC_NISTP_64_GCC_128",
37 "OPENSSL_NO_EC2M",
38 "OPENSSL_NO_EGD",
39 "OPENSSL_NO_ENGINE",
40 "OPENSSL_NO_GMP",
41 "OPENSSL_NO_GOST",
42 "OPENSSL_NO_HEARTBEATS",
43 "OPENSSL_NO_HW",
44 "OPENSSL_NO_IDEA",
45 "OPENSSL_NO_JPAKE",
46 "OPENSSL_NO_KRB5",
47 "OPENSSL_NO_MD2",
48 "OPENSSL_NO_MDC2",
49 "OPENSSL_NO_OCB",
50 "OPENSSL_NO_OCSP",
51 "OPENSSL_NO_RC2",
52 "OPENSSL_NO_RC5",
53 "OPENSSL_NO_RFC3779",
54 "OPENSSL_NO_RIPEMD",
55 "OPENSSL_NO_RMD160",
56 "OPENSSL_NO_SCTP",
57 "OPENSSL_NO_SEED",
58 "OPENSSL_NO_SM2",
59 "OPENSSL_NO_SM3",
60 "OPENSSL_NO_SM4",
61 "OPENSSL_NO_SRP",
62 "OPENSSL_NO_SSL_TRACE",
63 "OPENSSL_NO_SSL2",
64 "OPENSSL_NO_SSL3",
65 "OPENSSL_NO_SSL3_METHOD",
66 "OPENSSL_NO_STATIC_ENGINE",
67 "OPENSSL_NO_STORE",
68 "OPENSSL_NO_WHIRLPOOL",
69];
70
David Benjamin26669ff2023-04-21 15:12:24 -040071fn get_bssl_build_dir() -> PathBuf {
72 println!("cargo:rerun-if-env-changed=BORINGSSL_BUILD_DIR");
73 if let Some(build_dir) = env::var_os("BORINGSSL_BUILD_DIR") {
74 return PathBuf::from(build_dir);
75 }
76
77 let crate_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
78 return Path::new(&crate_dir).join("../../build");
79}
Kenichi Ishibashie5abf582022-03-02 17:41:50 +090080
Kenichi Ishibashi54c956b2024-03-17 16:02:03 +100081fn get_cpp_runtime_lib() -> Option<String> {
82 println!("cargo:rerun-if-env-changed=BORINGSSL_RUST_CPPLIB");
83
84 if let Ok(cpp_lib) = env::var("BORINGSSL_RUST_CPPLIB") {
85 return Some(cpp_lib);
86 }
87
88 if env::var_os("CARGO_CFG_UNIX").is_some() {
89 match env::var("CARGO_CFG_TARGET_OS").unwrap().as_ref() {
90 "macos" => Some("c++".into()),
91 _ => Some("stdc++".into()),
92 }
93 } else {
94 None
95 }
96}
97
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -040098fn main() {
David Benjamin26669ff2023-04-21 15:12:24 -040099 let bssl_build_dir = get_bssl_build_dir();
100 let bssl_sys_build_dir = bssl_build_dir.join("rust/bssl-sys");
101 let target = env::var("TARGET").unwrap();
Nabil Wadihbe792832023-02-14 11:46:37 -0800102
Nabil Wadih404d98b2023-04-14 13:05:02 -0700103 // Find the bindgen generated target platform bindings file and set BINDGEN_RS_FILE
David Benjamin26669ff2023-04-21 15:12:24 -0400104 let bindgen_file = bssl_sys_build_dir.join(format!("wrapper_{}.rs", target));
105 println!("cargo:rustc-env=BINDGEN_RS_FILE={}", bindgen_file.display());
Kenichi Ishibashie5abf582022-03-02 17:41:50 +0900106
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -0400107 // Statically link libraries.
Kenichi Ishibashie5abf582022-03-02 17:41:50 +0900108 println!(
109 "cargo:rustc-link-search=native={}",
David Benjamin26669ff2023-04-21 15:12:24 -0400110 bssl_build_dir.join("crypto").display()
Kenichi Ishibashie5abf582022-03-02 17:41:50 +0900111 );
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -0400112 println!("cargo:rustc-link-lib=static=crypto");
113
Kenichi Ishibashie5abf582022-03-02 17:41:50 +0900114 println!(
115 "cargo:rustc-link-search=native={}",
David Benjamin26669ff2023-04-21 15:12:24 -0400116 bssl_build_dir.join("ssl").display()
Kenichi Ishibashie5abf582022-03-02 17:41:50 +0900117 );
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -0400118 println!("cargo:rustc-link-lib=static=ssl");
119
David Benjamin26669ff2023-04-21 15:12:24 -0400120 println!(
121 "cargo:rustc-link-search=native={}",
122 bssl_sys_build_dir.display()
123 );
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -0400124 println!("cargo:rustc-link-lib=static=rust_wrapper");
Alex Gaynor8d71d242023-10-11 17:01:38 -0400125
Kenichi Ishibashi54c956b2024-03-17 16:02:03 +1000126 if let Some(cpp_lib) = get_cpp_runtime_lib() {
127 println!("cargo:rustc-link-lib={}", cpp_lib);
128 }
129
Alex Gaynor8d71d242023-10-11 17:01:38 -0400130 println!("cargo:conf={}", OSSL_CONF_DEFINES.join(","));
Benjamin Brittain8d8d8f32021-09-28 10:00:50 -0400131}