|
| 1 | +use std::{ |
| 2 | + env, |
| 3 | + io::ErrorKind, |
| 4 | + path::{Path, PathBuf}, |
| 5 | + process::Command, |
| 6 | +}; |
| 7 | + |
| 8 | +use bindgen::EnumVariation; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + println!("cargo:rerun-if-changed=src/tinydtls"); |
| 12 | + println!("cargo:rerun-if-changed=tinydtls_wrapper.h"); |
| 13 | + println!("cargo:rerun-if-changed=build.rs"); |
| 14 | + let mut bindgen_builder = bindgen::Builder::default(); |
| 15 | + |
| 16 | + // Build vendored library if feature was set. |
| 17 | + if cfg!(feature = "vendored") { |
| 18 | + // Read required environment variables. |
| 19 | + let out_dir = std::env::var_os("OUT_DIR").unwrap(); |
| 20 | + let tinydtls_src_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src").join("tinydtls"); |
| 21 | + // Read Makeflags into vector of strings |
| 22 | + let make_flags = std::env::var_os("CARGO_MAKEFLAGS") |
| 23 | + .unwrap() |
| 24 | + .into_string() |
| 25 | + .unwrap() |
| 26 | + .split(" ") |
| 27 | + .map(String::from) |
| 28 | + .collect(); |
| 29 | + |
| 30 | + Command::new("autoconf") |
| 31 | + .current_dir(&tinydtls_src_dir) |
| 32 | + .status() |
| 33 | + .unwrap(); |
| 34 | + |
| 35 | + autotools::Config::new(&tinydtls_src_dir) |
| 36 | + .insource(true) |
| 37 | + .out_dir(&out_dir) |
| 38 | + .make_target("clean") |
| 39 | + .build(); |
| 40 | + let mut build_config = autotools::Config::new(&tinydtls_src_dir); |
| 41 | + build_config.insource(true).out_dir(out_dir); |
| 42 | + |
| 43 | + // Is not deleted by default for some reason |
| 44 | + if let Err(e) = std::fs::remove_dir_all(&tinydtls_src_dir.join("include").join("tinydtls")) { |
| 45 | + match e.kind() { |
| 46 | + ErrorKind::NotFound => {}, |
| 47 | + e => panic!("Error deleting old tinydtls include directory: {:?}", e), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Set Makeflags |
| 52 | + build_config.make_args(make_flags); |
| 53 | + |
| 54 | + // Enable debug symbols if enabled in Rust |
| 55 | + match std::env::var_os("DEBUG").unwrap().to_str().unwrap() { |
| 56 | + "0" | "false" => {}, |
| 57 | + _ => { |
| 58 | + build_config.with("debug", None); |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + // Enable dependency features based on selected cargo features. |
| 63 | + build_config |
| 64 | + .enable("ecc", Some(if cfg!(feature = "ecc") { "yes" } else { "no" })) |
| 65 | + .enable("psk", Some(if cfg!(feature = "psk") { "yes" } else { "no" })); |
| 66 | + |
| 67 | + // Run build |
| 68 | + let dst = build_config.build(); |
| 69 | + |
| 70 | + // Add the built library to the search path |
| 71 | + println!("cargo:rustc-link-search=native={}", dst.join("lib").to_str().unwrap()); |
| 72 | + println!("cargo:include={}", dst.join("include").to_str().unwrap()); |
| 73 | + println!("cargo:libs={}", dst.to_str().unwrap()); |
| 74 | + bindgen_builder = bindgen_builder |
| 75 | + .clang_arg(format!("-I{}", dst.join("include").join("tinydtls").to_str().unwrap())) |
| 76 | + .clang_arg(format!("-I{}", dst.join("include").to_str().unwrap())); |
| 77 | + } |
| 78 | + |
| 79 | + println!( |
| 80 | + "cargo:rustc-link-lib={}tinydtls", |
| 81 | + cfg!(feature = "static").then(|| "static=").unwrap_or("") |
| 82 | + ); |
| 83 | + |
| 84 | + bindgen_builder = bindgen_builder |
| 85 | + .header("src/wrapper.h") |
| 86 | + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) |
| 87 | + .default_enum_style(EnumVariation::Rust { non_exhaustive: true }) |
| 88 | + .rustfmt_bindings(false) |
| 89 | + .allowlist_function("dtls_.*") |
| 90 | + .allowlist_type("dtls_.*") |
| 91 | + .allowlist_var("dtls_.*") |
| 92 | + .allowlist_function("DTLS_.*") |
| 93 | + .allowlist_type("DTLS_.*") |
| 94 | + .allowlist_var("DTLS_.*") |
| 95 | + .allowlist_type("seqnum_t") |
| 96 | + .allowlist_type("__attribute__") |
| 97 | + .allowlist_type("clock_time_t") |
| 98 | + .allowlist_var("CLOCK_SECOND") |
| 99 | + .allowlist_var("TLS_.*") |
| 100 | + .allowlist_var("DTLSv12") |
| 101 | + .allowlist_function("memxor") |
| 102 | + .allowlist_function("equals") |
| 103 | + .allowlist_var("WITH_.*") |
| 104 | + .allowlist_type("WITH_.*") |
| 105 | + .allowlist_function("WITH_.*") |
| 106 | + .allowlist_var("PACKAGE_.*") |
| 107 | + .allowlist_type("PACKAGE_.*") |
| 108 | + .allowlist_function("PACKAGE_.*") |
| 109 | + .allowlist_function("netq_.*") |
| 110 | + .allowlist_type("netq_.*") |
| 111 | + .allowlist_var("netq_.*") |
| 112 | + .allowlist_function("NETQ_.*") |
| 113 | + .allowlist_type("NETQ_.*") |
| 114 | + .allowlist_var("NETQ_.*") |
| 115 | + .allowlist_type("session_t") |
| 116 | + // We use the definitions made by the libc crate instead |
| 117 | + .blocklist_type("sockaddr(_in|_in6|_storage)?") |
| 118 | + .blocklist_type("in6?_(addr|port)(_t)?") |
| 119 | + .blocklist_type("in6_addr__bindgen_ty_1") |
| 120 | + .blocklist_type("(__)?socklen_t") |
| 121 | + .blocklist_type("sa_family_t") |
| 122 | + .blocklist_type("__fd_mask") |
| 123 | + // Are generated because they are typedef-ed inside of the C headers, blocklisting them |
| 124 | + // will instead replace them with the appropriate rust types. |
| 125 | + // See https://github.com/rust-lang/rust-bindgen/issues/1215 for an open issue concerning |
| 126 | + // this problem. |
| 127 | + .size_t_is_usize(true); |
| 128 | + let bindings = bindgen_builder.generate().unwrap(); |
| 129 | + |
| 130 | + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 131 | + bindings.write_to_file(out_path.join("bindings.rs")).unwrap(); |
| 132 | +} |
0 commit comments