Skip to content

Commit 00037cb

Browse files
committed
sys: Add raw bindings including source distribution
1 parent 5dfd59d commit 00037cb

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "tinydtls-sys/src/tinydtls"]
2+
path = tinydtls-sys/src/tinydtls
3+
url = https://github.com/eclipse/tinydtls.git
4+
branch = develop

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
3+
members = [
4+
"tinydtls-sys",
5+
]

tinydtls-sys/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "tinydtls-sys"
3+
version = "0.1.0-alpha.1+tinydtls-c8d2def"
4+
edition = "2021"
5+
links = "tinydtls"
6+
# For tinydtls, both licenses can be applied, see https://www.eclipse.org/legal/eplfaq.php#DUALLIC
7+
# BSD-3-CLAUSE is equivalent to the EDL v1.0, see https://www.eclipse.org/org/documents/edl-v10.php
8+
# First bracket is the license for TinyDTLS, the remainder is for code included with tinydtls.
9+
license = "(EPL-1.0 OR BSD-3-CLAUSE) AND BSD-1-Clause AND BSD-3-CLAUSE AND MIT"
10+
exclude = ['src/tinydtls/share/', 'src/tinydtls/include', 'src/tinydtls/configure.prev']
11+
12+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13+
[features]
14+
default = ["vendored"]
15+
# Use a vendored version of tinydtls
16+
vendored = ["static"]
17+
# Attempt static linking to tinydtls
18+
static = []
19+
20+
# Enable ECC functionality in vendored library
21+
ecc = []
22+
# Enable PSK functionality in vendored library
23+
psk = []
24+
25+
[dependencies]
26+
libc = "^0.2"
27+
28+
[build-dependencies]
29+
bindgen = "0.59.2"
30+
autotools = "0.2.3"

tinydtls-sys/build.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

tinydtls-sys/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Bindgen translates the C headers, clippy's and rustfmt's recommendations are not applicable here.
2+
#![allow(clippy::all)]
3+
#![allow(non_camel_case_types)]
4+
5+
use libc::{sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t};
6+
7+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

tinydtls-sys/src/tinydtls

Submodule tinydtls added at c8d2def

tinydtls-sys/src/wrapper.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: EPL-1.0 OR BSD-3-CLAUSE
2+
/*
3+
* wrapper.h - Wrapper header for TinyDTLS headers, used for bindgen binding generation
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+
8+
#include <tinydtls/tinydtls.h>
9+
#include <tinydtls/dtls.h>
10+
#include <tinydtls/dtls_config.h>

0 commit comments

Comments
 (0)