1 // Copyright 2021 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use std::env;
16 use std::path::Path;
17 use std::path::PathBuf;
18 
19 // Keep in sync with the list in include/openssl/opensslconf.h
20 const OSSL_CONF_DEFINES: &[&str] = &[
21     "OPENSSL_NO_ASYNC",
22     "OPENSSL_NO_BF",
23     "OPENSSL_NO_BLAKE2",
24     "OPENSSL_NO_BUF_FREELISTS",
25     "OPENSSL_NO_CAMELLIA",
26     "OPENSSL_NO_CAPIENG",
27     "OPENSSL_NO_CAST",
28     "OPENSSL_NO_CMS",
29     "OPENSSL_NO_COMP",
30     "OPENSSL_NO_CT",
31     "OPENSSL_NO_DANE",
32     "OPENSSL_NO_DEPRECATED",
33     "OPENSSL_NO_DGRAM",
34     "OPENSSL_NO_DYNAMIC_ENGINE",
35     "OPENSSL_NO_EC_NISTP_64_GCC_128",
36     "OPENSSL_NO_EC2M",
37     "OPENSSL_NO_EGD",
38     "OPENSSL_NO_ENGINE",
39     "OPENSSL_NO_GMP",
40     "OPENSSL_NO_GOST",
41     "OPENSSL_NO_HEARTBEATS",
42     "OPENSSL_NO_HW",
43     "OPENSSL_NO_IDEA",
44     "OPENSSL_NO_JPAKE",
45     "OPENSSL_NO_KRB5",
46     "OPENSSL_NO_MD2",
47     "OPENSSL_NO_MDC2",
48     "OPENSSL_NO_OCB",
49     "OPENSSL_NO_OCSP",
50     "OPENSSL_NO_RC2",
51     "OPENSSL_NO_RC5",
52     "OPENSSL_NO_RFC3779",
53     "OPENSSL_NO_RIPEMD",
54     "OPENSSL_NO_RMD160",
55     "OPENSSL_NO_SCTP",
56     "OPENSSL_NO_SEED",
57     "OPENSSL_NO_SM2",
58     "OPENSSL_NO_SM3",
59     "OPENSSL_NO_SM4",
60     "OPENSSL_NO_SRP",
61     "OPENSSL_NO_SSL_TRACE",
62     "OPENSSL_NO_SSL2",
63     "OPENSSL_NO_SSL3",
64     "OPENSSL_NO_SSL3_METHOD",
65     "OPENSSL_NO_STATIC_ENGINE",
66     "OPENSSL_NO_STORE",
67     "OPENSSL_NO_WHIRLPOOL",
68 ];
69 
get_bssl_build_dir() -> PathBuf70 fn get_bssl_build_dir() -> PathBuf {
71     println!("cargo:rerun-if-env-changed=BORINGSSL_BUILD_DIR");
72     if let Some(build_dir) = env::var_os("BORINGSSL_BUILD_DIR") {
73         return PathBuf::from(build_dir);
74     }
75 
76     let crate_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
77     return Path::new(&crate_dir).join("../../build");
78 }
79 
get_cpp_runtime_lib() -> Option<String>80 fn get_cpp_runtime_lib() -> Option<String> {
81     println!("cargo:rerun-if-env-changed=BORINGSSL_RUST_CPPLIB");
82 
83     if let Ok(cpp_lib) = env::var("BORINGSSL_RUST_CPPLIB") {
84         return Some(cpp_lib);
85     }
86 
87     if env::var_os("CARGO_CFG_UNIX").is_some() {
88         match env::var("CARGO_CFG_TARGET_OS").unwrap().as_ref() {
89             "macos" => Some("c++".into()),
90             _ => Some("stdc++".into()),
91         }
92     } else {
93         None
94     }
95 }
96 
main()97 fn main() {
98     let bssl_build_dir = get_bssl_build_dir();
99     let bssl_sys_build_dir = bssl_build_dir.join("rust/bssl-sys");
100     let target = env::var("TARGET").unwrap();
101     let out_dir = env::var("OUT_DIR").unwrap();
102     let bindgen_out_file = Path::new(&out_dir).join("bindgen.rs");
103 
104     // Find the bindgen generated target platform bindings file and put it into
105     // OUT_DIR/bindgen.rs.
106     let bindgen_source_file = bssl_sys_build_dir.join(format!("wrapper_{}.rs", target));
107     std::fs::copy(&bindgen_source_file, &bindgen_out_file).expect(&format!(
108         "Could not copy bindings from '{}' to '{}'",
109         bindgen_source_file.display(),
110         bindgen_out_file.display()
111     ));
112     println!("cargo:rerun-if-changed={}", bindgen_source_file.display());
113 
114     // Statically link libraries.
115     println!(
116         "cargo:rustc-link-search=native={}",
117         bssl_build_dir.display()
118     );
119     println!("cargo:rustc-link-lib=static=crypto");
120     println!("cargo:rustc-link-lib=static=ssl");
121 
122     println!(
123         "cargo:rustc-link-search=native={}",
124         bssl_sys_build_dir.display()
125     );
126     println!("cargo:rustc-link-lib=static=rust_wrapper");
127 
128     if let Some(cpp_lib) = get_cpp_runtime_lib() {
129         println!("cargo:rustc-link-lib={}", cpp_lib);
130     }
131 
132     println!("cargo:conf={}", OSSL_CONF_DEFINES.join(","));
133 }
134