Skip to content

Commit 4802fe6

Browse files
committed
sys: Clarify documentation, add license headers and fix packaging issues
1 parent edbb48c commit 4802fe6

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

LICENSE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ which accompany this distribution.
66
The Eclipse Public License is also available at http://www.eclipse.org/legal/epl-v10.html
77
and the 3-Clause BSD License is also available at https://opensource.org/licenses/BSD-3-Clause.
88

9+
The underlying TinyDTLS library also includes third-party code for which different licensing terms might apply.
10+
See tinydtls-sys/src/tinydtls/ABOUT.md and tinydtls-sys/src/tinydtls/LICENSE for more information.
11+
912
3-Clause BSD License
1013

1114
Copyright 2021 The NAMIB project contributors

tinydtls-sys/Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
# Copyright (c) 2021 The NAMIB Project Developers, all rights reserved.
2+
# See the README as well as the LICENSE file for more information.
13
# SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE
24
[package]
35
name = "tinydtls-sys"
6+
description = "Raw bindings to the TinyDTLS library."
47
version = "0.1.0-alpha.1+tinydtls-c8d2def"
58
edition = "2021"
69
links = "tinydtls"
710
# For tinydtls, both licenses can be applied, see https://www.eclipse.org/legal/eplfaq.php#DUALLIC
811
# BSD-3-CLAUSE is equivalent to the EDL v1.0, see https://www.eclipse.org/org/documents/edl-v10.php
912
# First bracket is the license for TinyDTLS, the remainder is for code included with tinydtls.
1013
license = "(EPL-1.0 OR BSD-3-CLAUSE) AND BSD-1-Clause AND BSD-3-CLAUSE AND MIT"
11-
exclude = ['src/tinydtls/share/', 'src/tinydtls/include', 'src/tinydtls/configure.prev']
14+
readme = "README.md"
15+
homepage = "https://namib.me/"
16+
authors = ["Hugo Hakim Damer <[email protected]>"]
17+
categories = ["external-ffi-bindings", "network-programming", "cryptography", "embedded"]
18+
keywords = ["tinydtls", "sys", "dtls", "crypto"]
19+
exclude = ['/src/tinydtls/share/', '/src/tinydtls/include/', '/src/tinydtls/configure.prev']
1220

1321
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1422
[features]
@@ -28,4 +36,5 @@ libc = "^0.2"
2836

2937
[build-dependencies]
3038
bindgen = "0.59.2"
31-
autotools = "0.2.3"
39+
autotools = "0.2.3"
40+
fs_extra = "1.2.0"

tinydtls-sys/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE

tinydtls-sys/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md

tinydtls-sys/build.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
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+
*/
27
use std::{
38
env,
4-
io::ErrorKind,
59
path::{Path, PathBuf},
610
process::Command,
711
};
812

913
use bindgen::EnumVariation;
1014

1115
fn 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
}

tinydtls-sys/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
// SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE
2+
/*
3+
* lib.rs - Main library file for raw 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+
28
// Bindgen translates the C headers, clippy's and rustfmt's recommendations are not applicable here.
39
#![allow(clippy::all)]
410
#![allow(non_camel_case_types)]
11+
#![allow(non_snake_case)]
12+
#![allow(non_upper_case_globals)]
513

6-
use libc::{sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t};
14+
use libc::{sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t};
715

16+
#[cfg(target_family = "windows")]
17+
include!(concat!(env!("OUT_DIR"), "\\bindings.rs"));
18+
#[cfg(not(target_family = "windows"))]
819
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 commit comments

Comments
 (0)