• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 The BoringSSL Authors
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 
16 use std::env;
17 use std::path::Path;
18 use std::path::PathBuf;
19 
20 // Keep in sync with the list in include/openssl/opensslconf.h
21 const 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 
get_bssl_build_dir() -> PathBuf71 fn 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 }
80 
get_cpp_runtime_lib() -> Option<String>81 fn 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 
main()98 fn main() {
99     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();
102     let out_dir = env::var("OUT_DIR").unwrap();
103     let bindgen_out_file = Path::new(&out_dir).join("bindgen.rs");
104 
105     // Find the bindgen generated target platform bindings file and put it into
106     // OUT_DIR/bindgen.rs.
107     let bindgen_source_file = bssl_sys_build_dir.join(format!("wrapper_{}.rs", target));
108     std::fs::copy(&bindgen_source_file, &bindgen_out_file).expect(&format!(
109         "Could not copy bindings from '{}' to '{}'",
110         bindgen_source_file.display(),
111         bindgen_out_file.display()
112     ));
113     println!("cargo:rerun-if-changed={}", bindgen_source_file.display());
114 
115     // Statically link libraries.
116     println!(
117         "cargo:rustc-link-search=native={}",
118         bssl_build_dir.join("crypto").display()
119     );
120     println!("cargo:rustc-link-lib=static=crypto");
121 
122     println!(
123         "cargo:rustc-link-search=native={}",
124         bssl_build_dir.join("ssl").display()
125     );
126     println!("cargo:rustc-link-lib=static=ssl");
127 
128     println!(
129         "cargo:rustc-link-search=native={}",
130         bssl_sys_build_dir.display()
131     );
132     println!("cargo:rustc-link-lib=static=rust_wrapper");
133 
134     if let Some(cpp_lib) = get_cpp_runtime_lib() {
135         println!("cargo:rustc-link-lib={}", cpp_lib);
136     }
137 
138     println!("cargo:conf={}", OSSL_CONF_DEFINES.join(","));
139 }
140