11// 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+ */
27use std:: {
38 env,
4- io:: ErrorKind ,
59 path:: { Path , PathBuf } ,
610 process:: Command ,
711} ;
812
913use bindgen:: EnumVariation ;
1014
1115fn main ( ) {
12- println ! ( "cargo:rerun-if-changed=src/tinydtls" ) ;
16+ println ! ( "cargo:rerun-if-changed=src/tinydtls/ " ) ;
1317 println ! ( "cargo:rerun-if-changed=tinydtls_wrapper.h" ) ;
1418 println ! ( "cargo:rerun-if-changed=build.rs" ) ;
1519 let mut bindgen_builder = bindgen:: Builder :: default ( ) ;
@@ -18,7 +22,20 @@ fn main() {
1822 if cfg ! ( feature = "vendored" ) {
1923 // Read required environment variables.
2024 let out_dir = std:: env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
21- let tinydtls_src_dir = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) . join ( "src" ) . join ( "tinydtls" ) ;
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+ & copy_options,
35+ )
36+ . unwrap ( ) ;
37+ let tinydtls_src_dir = Path :: new ( & out_dir) . join ( "tinydtls" ) ;
38+
2239 // Read Makeflags into vector of strings
2340 let make_flags = std:: env:: var_os ( "CARGO_MAKEFLAGS" )
2441 . unwrap ( )
@@ -28,31 +45,27 @@ fn main() {
2845 . map ( String :: from)
2946 . collect ( ) ;
3047
31- Command :: new ( "autoconf" )
48+ // Run autogen to generate necessary build files.
49+ Command :: new ( tinydtls_src_dir. join ( "autogen.sh" ) )
3250 . current_dir ( & tinydtls_src_dir)
3351 . status ( )
3452 . unwrap ( ) ;
3553
54+ // Run make clean
3655 autotools:: Config :: new ( & tinydtls_src_dir)
3756 . insource ( true )
3857 . out_dir ( & out_dir)
3958 . make_target ( "clean" )
4059 . build ( ) ;
60+
61+ // Create build configuration instance and enable in-source builds.
4162 let mut build_config = autotools:: Config :: new ( & tinydtls_src_dir) ;
42- build_config. insource ( true ) . out_dir ( out_dir) ;
43-
44- // Is not deleted by default for some reason
45- if let Err ( e) = std:: fs:: remove_dir_all ( & tinydtls_src_dir. join ( "include" ) . join ( "tinydtls" ) ) {
46- match e. kind ( ) {
47- ErrorKind :: NotFound => { } ,
48- e => panic ! ( "Error deleting old tinydtls include directory: {:?}" , e) ,
49- }
50- }
63+ build_config. insource ( true ) . out_dir ( & out_dir) ;
5164
5265 // Set Makeflags
5366 build_config. make_args ( make_flags) ;
5467
55- // Enable debug symbols if enabled in Rust
68+ // Enable debug symbols if enabled in Rust.
5669 match std:: env:: var_os ( "DEBUG" ) . unwrap ( ) . to_str ( ) . unwrap ( ) {
5770 "0" | "false" => { } ,
5871 _ => {
@@ -70,23 +83,31 @@ fn main() {
7083
7184 // Add the built library to the search path
7285 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.
7389 println ! ( "cargo:include={}" , dst. join( "include" ) . to_str( ) . unwrap( ) ) ;
7490 println ! ( "cargo:libs={}" , dst. to_str( ) . unwrap( ) ) ;
91+
92+ // Tell bindgen to look for the right header files.
7593 bindgen_builder = bindgen_builder
7694 . clang_arg ( format ! ( "-I{}" , dst. join( "include" ) . join( "tinydtls" ) . to_str( ) . unwrap( ) ) )
7795 . clang_arg ( format ! ( "-I{}" , dst. join( "include" ) . to_str( ) . unwrap( ) ) ) ;
7896 }
7997
98+ // Instruct cargo to link to the TinyDTLS C library, either statically or dynamically.
8099 println ! (
81100 "cargo:rustc-link-lib={}tinydtls" ,
82101 cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "" )
83102 ) ;
84103
104+ // Customize and configure generated bindings.
85105 bindgen_builder = bindgen_builder
86106 . header ( "src/wrapper.h" )
87107 . parse_callbacks ( Box :: new ( bindgen:: CargoCallbacks ) )
88108 . default_enum_style ( EnumVariation :: Rust { non_exhaustive : true } )
89109 . rustfmt_bindings ( false )
110+ // Declarations that should be part of the bindings.
90111 . allowlist_function ( "dtls_.*" )
91112 . allowlist_type ( "dtls_.*" )
92113 . allowlist_var ( "dtls_.*" )
@@ -121,13 +142,11 @@ fn main() {
121142 . blocklist_type ( "(__)?socklen_t" )
122143 . blocklist_type ( "sa_family_t" )
123144 . blocklist_type ( "__fd_mask" )
124- // Are generated because they are typedef-ed inside of the C headers, blocklisting them
125- // will instead replace them with the appropriate rust types.
126- // See https://github.com/rust-lang/rust-bindgen/issues/1215 for an open issue concerning
127- // this problem.
145+ // size_t matches usize in our case here.
128146 . size_t_is_usize ( true ) ;
129- let bindings = bindgen_builder. generate ( ) . unwrap ( ) ;
130147
148+ // Run binding generation and write the output to a file.
149+ let bindings = bindgen_builder. generate ( ) . expect ( "Could not generate bindings!" ) ;
131150 let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
132151 bindings. write_to_file ( out_path. join ( "bindings.rs" ) ) . unwrap ( ) ;
133152}
0 commit comments