|
| 1 | +// SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE |
| 2 | +/* |
| 3 | + * build.rs - build script for TinyDTLS Rust bindings. |
| 4 | + * Copyright (c) 2021 The NAMIB Project Developers, all rights reserved. |
| 5 | + * See the README as well as the LICENSE file for more information. |
| 6 | + */ |
| 7 | +use std::{ |
| 8 | + env, |
| 9 | + path::{Path, PathBuf}, |
| 10 | + process::Command, |
| 11 | +}; |
| 12 | + |
| 13 | +use bindgen::EnumVariation; |
| 14 | + |
| 15 | +fn main() { |
| 16 | + println!("cargo:rerun-if-changed=src/tinydtls/"); |
| 17 | + println!("cargo:rerun-if-changed=tinydtls_wrapper.h"); |
| 18 | + println!("cargo:rerun-if-changed=build.rs"); |
| 19 | + let mut bindgen_builder = bindgen::Builder::default(); |
| 20 | + |
| 21 | + // Build vendored library if feature was set. |
| 22 | + if cfg!(feature = "vendored") { |
| 23 | + // Read required environment variables. |
| 24 | + let out_dir = std::env::var_os("OUT_DIR").unwrap(); |
| 25 | + |
| 26 | + // TinyDTLS does not like being built out of source, but we get verification errors if files |
| 27 | + // in the source package are modified. |
| 28 | + // Therefore, we copy tinydtls over to the output directory and build from there. |
| 29 | + let mut copy_options = fs_extra::dir::CopyOptions::default(); |
| 30 | + copy_options.overwrite = true; |
| 31 | + fs_extra::dir::copy( |
| 32 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("src").join("tinydtls"), |
| 33 | + &out_dir, |
| 34 | + ©_options, |
| 35 | + ) |
| 36 | + .unwrap(); |
| 37 | + let tinydtls_src_dir = Path::new(&out_dir).join("tinydtls"); |
| 38 | + |
| 39 | + // Read Makeflags into vector of strings |
| 40 | + let make_flags = std::env::var_os("CARGO_MAKEFLAGS") |
| 41 | + .unwrap() |
| 42 | + .into_string() |
| 43 | + .unwrap() |
| 44 | + .split(" ") |
| 45 | + .map(String::from) |
| 46 | + .collect(); |
| 47 | + |
| 48 | + // Run autogen to generate necessary build files. |
| 49 | + Command::new(tinydtls_src_dir.join("autogen.sh")) |
| 50 | + .current_dir(&tinydtls_src_dir) |
| 51 | + .status() |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + // Run make clean |
| 55 | + autotools::Config::new(&tinydtls_src_dir) |
| 56 | + .insource(true) |
| 57 | + .out_dir(&out_dir) |
| 58 | + .make_target("clean") |
| 59 | + .build(); |
| 60 | + |
| 61 | + // Create build configuration instance and enable in-source builds. |
| 62 | + let mut build_config = autotools::Config::new(&tinydtls_src_dir); |
| 63 | + build_config.insource(true).out_dir(&out_dir); |
| 64 | + |
| 65 | + // Set Makeflags |
| 66 | + build_config.make_args(make_flags); |
| 67 | + |
| 68 | + // Enable debug symbols if enabled in Rust. |
| 69 | + match std::env::var_os("DEBUG").unwrap().to_str().unwrap() { |
| 70 | + "0" | "false" => {}, |
| 71 | + _ => { |
| 72 | + build_config.with("debug", None); |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + // Enable dependency features based on selected cargo features. |
| 77 | + build_config |
| 78 | + .enable("ecc", Some(if cfg!(feature = "ecc") { "yes" } else { "no" })) |
| 79 | + .enable("psk", Some(if cfg!(feature = "psk") { "yes" } else { "no" })); |
| 80 | + |
| 81 | + // Run build |
| 82 | + let dst = build_config.build(); |
| 83 | + |
| 84 | + // Add the built library to the search path |
| 85 | + println!("cargo:rustc-link-search=native={}", dst.join("lib").to_str().unwrap()); |
| 86 | + // Set some values that can be used by other crates that have to interact with the C library |
| 87 | + // directly, see https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key |
| 88 | + // for more info. |
| 89 | + println!("cargo:include={}", dst.join("include").to_str().unwrap()); |
| 90 | + println!("cargo:libs={}", dst.to_str().unwrap()); |
| 91 | + |
| 92 | + // Tell bindgen to look for the right header files. |
| 93 | + bindgen_builder = bindgen_builder |
| 94 | + .clang_arg(format!("-I{}", dst.join("include").join("tinydtls").to_str().unwrap())) |
| 95 | + .clang_arg(format!("-I{}", dst.join("include").to_str().unwrap())); |
| 96 | + } |
| 97 | + |
| 98 | + // Instruct cargo to link to the TinyDTLS C library, either statically or dynamically. |
| 99 | + println!( |
| 100 | + "cargo:rustc-link-lib={}tinydtls", |
| 101 | + cfg!(feature = "static").then(|| "static=").unwrap_or("") |
| 102 | + ); |
| 103 | + |
| 104 | + // Customize and configure generated bindings. |
| 105 | + bindgen_builder = bindgen_builder |
| 106 | + .header("src/wrapper.h") |
| 107 | + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) |
| 108 | + .default_enum_style(EnumVariation::Rust { non_exhaustive: true }) |
| 109 | + .rustfmt_bindings(false) |
| 110 | + // Declarations that should be part of the bindings. |
| 111 | + .allowlist_function("dtls_.*") |
| 112 | + .allowlist_type("dtls_.*") |
| 113 | + .allowlist_var("dtls_.*") |
| 114 | + .allowlist_function("DTLS_.*") |
| 115 | + .allowlist_type("DTLS_.*") |
| 116 | + .allowlist_var("DTLS_.*") |
| 117 | + .allowlist_type("seqnum_t") |
| 118 | + .allowlist_type("__attribute__") |
| 119 | + .allowlist_type("clock_time_t") |
| 120 | + .allowlist_var("CLOCK_SECOND") |
| 121 | + .allowlist_var("TLS_.*") |
| 122 | + .allowlist_var("DTLSv12") |
| 123 | + .allowlist_function("memxor") |
| 124 | + .allowlist_function("equals") |
| 125 | + .allowlist_var("WITH_.*") |
| 126 | + .allowlist_type("WITH_.*") |
| 127 | + .allowlist_function("WITH_.*") |
| 128 | + .allowlist_var("PACKAGE_.*") |
| 129 | + .allowlist_type("PACKAGE_.*") |
| 130 | + .allowlist_function("PACKAGE_.*") |
| 131 | + .allowlist_function("netq_.*") |
| 132 | + .allowlist_type("netq_.*") |
| 133 | + .allowlist_var("netq_.*") |
| 134 | + .allowlist_function("NETQ_.*") |
| 135 | + .allowlist_type("NETQ_.*") |
| 136 | + .allowlist_var("NETQ_.*") |
| 137 | + .allowlist_type("session_t") |
| 138 | + // We use the definitions made by the libc crate instead |
| 139 | + .blocklist_type("sockaddr(_in|_in6|_storage)?") |
| 140 | + .blocklist_type("in6?_(addr|port)(_t)?") |
| 141 | + .blocklist_type("in6_addr__bindgen_ty_1") |
| 142 | + .blocklist_type("(__)?socklen_t") |
| 143 | + .blocklist_type("sa_family_t") |
| 144 | + .blocklist_type("__fd_mask") |
| 145 | + // size_t matches usize in our case here. |
| 146 | + .size_t_is_usize(true); |
| 147 | + |
| 148 | + // Run binding generation and write the output to a file. |
| 149 | + let bindings = bindgen_builder.generate().expect("Could not generate bindings!"); |
| 150 | + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 151 | + bindings.write_to_file(out_path.join("bindings.rs")).unwrap(); |
| 152 | +} |
0 commit comments