Skip to content

Commit 99db927

Browse files
authored
merge: pull request #1 from namib-project/libcoap_431
Update libcoap to 4.3.1 and fix some issues with the build
2 parents f99176c + de6cdca commit 99db927

File tree

7 files changed

+128
-80
lines changed

7 files changed

+128
-80
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "libcoap"]
22
path = libcoap-sys/src/libcoap
33
url = https://github.com/obgm/libcoap.git
4-
branch = develop
4+
branch = release-4.3.1

libcoap-sys/Cargo.toml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[package]
77
name = "libcoap-sys"
88
description = "Raw bindings to the libcoap CoAP library."
9-
version = "0.2.0+libcoap-develop-8b9377e"
9+
version = "0.2.1+libcoap-4.3.1"
1010
edition = "2021"
1111
license = "BSD-2-Clause AND BSD-1-Clause"
1212
links = "coap-3"
@@ -33,14 +33,14 @@ default = ["server", "client", "tcp", "async", "epoll", "vendored", "static"]
3333
# different backends), we select one based on the auto-detection order specified in
3434
# https://github.com/obgm/libcoap/blob/develop/configure.ac#L494 (gnutls > openssl > mbedtls > tinydtls).
3535
dtls = []
36-
dtls_backend_openssl = ["openssl-sys"]
37-
dtls_backend_gnutls = ["gnutls-sys"]
38-
dtls_backend_mbedtls = ["mbedtls-sys-auto"]
39-
dtls_backend_tinydtls = ["tinydtls-sys"]
36+
dtls_backend_openssl = ["dtls", "openssl-sys"]
37+
dtls_backend_gnutls = ["dtls", "gnutls-sys"]
38+
dtls_backend_mbedtls = ["dtls", "mbedtls-sys-auto"]
39+
dtls_backend_tinydtls = ["dtls", "tinydtls-sys"]
4040
# Enabling this feature will force libcoap-sys to be built with and statically linked to a vendored version of libcoap,
4141
# which will be built by the build-script before building libcoap-sys.
4242
# This way, it is no longer required to have libcoap installed to use this crate.
43-
vendored = ["static", "openssl-sys?/vendored"]
43+
vendored = ["static"]
4444
# Enable this feature to use static linking to libcoap instead of dynamic linking.
4545
static = []
4646
# --- FEATURE FLAGS ---
@@ -60,14 +60,17 @@ client = []
6060
epoll = []
6161

6262
[dependencies]
63-
gnutls-sys = {version = "^0.1", optional = true}
64-
openssl-sys = {version = "^0.9", optional = true}
63+
gnutls-sys = {version = "^0.1.2", optional = true}
64+
openssl-sys = {version = "^0.9.74", optional = true}
6565
mbedtls-sys-auto = {version = "^2.26", optional = true}
66-
libc = "^0.2"
67-
tinydtls-sys = {version = "^0.1.1", optional = true}
66+
libc = "^0.2.126"
67+
tinydtls-sys = {version = "^0.1.2", optional = true}
6868

6969
[build-dependencies]
70-
bindgen = "^0.59.2"
70+
bindgen = "^0.62.0"
7171
autotools = "^0.2.3"
7272
fs_extra = "^1.2"
7373
pkg-config = "^0.3.24"
74+
75+
[package.metadata.docs.rs]
76+
features = ["dtls", "dtls_backend_openssl", "vendored"]

libcoap-sys/ar-lib

Whitespace-only changes.

libcoap-sys/build.rs

Lines changed: 97 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ impl ToString for DtlsBackend {
3737
fn main() {
3838
println!("cargo:rerun-if-changed=src/libcoap/");
3939
println!("cargo:rerun-if-changed=src/wrapper.h");
40-
let mut pkgconf = pkg_config::Config::new();
4140
let mut bindgen_builder = bindgen::Builder::default();
42-
let mut orig_pkg_config = None;
41+
// Read required environment variables.
42+
let orig_pkg_config = std::env::var_os("PKG_CONFIG_PATH").map(|v| String::from(v.to_str().unwrap()));
43+
let out_dir = env::var_os("OUT_DIR").unwrap();
4344

4445
let mut dtls_backend = Option::None;
4546
if cfg!(feature = "dtls") {
@@ -82,8 +83,7 @@ fn main() {
8283

8384
// Build vendored library if feature was set.
8485
if cfg!(feature = "vendored") {
85-
// Read required environment variables.
86-
let out_dir = std::env::var_os("OUT_DIR").unwrap();
86+
let libcoap_src_dir = Path::new(&out_dir).join("libcoap");
8787
// Read Makeflags into vector of strings
8888
//let make_flags: Vec<String> = std::env::var_os("CARGO_MAKEFLAGS")
8989
// .unwrap()
@@ -101,7 +101,7 @@ fn main() {
101101
overwrite: true,
102102
..Default::default()
103103
};
104-
match std::fs::remove_dir_all(Path::new(&out_dir).join("libcoap")) {
104+
match std::fs::remove_dir_all(&libcoap_src_dir) {
105105
Ok(_) => {},
106106
Err(e) if e.kind() == ErrorKind::NotFound => {},
107107
e => e.unwrap(),
@@ -112,42 +112,89 @@ fn main() {
112112
&copy_options,
113113
)
114114
.unwrap();
115-
let libcoap_src_dir = Path::new(&out_dir).join("libcoap");
115+
let current_dir_backup = env::current_dir().unwrap();
116+
env::set_current_dir(&libcoap_src_dir).expect("unable to change to libcoap build dir");
116117
Command::new(libcoap_src_dir.join("autogen.sh")).status().unwrap();
117-
118118
let mut build_config = autotools::Config::new(libcoap_src_dir);
119-
build_config.out_dir(out_dir);
119+
build_config.out_dir(&out_dir);
120120
if let Some(dtls_backend) = dtls_backend {
121121
build_config
122122
.enable("dtls", None)
123123
.with(dtls_backend.to_string().as_str(), None);
124124

125-
if dtls_backend == DtlsBackend::TinyDtls {
126-
// We do not ship tinydtls with our source distribution. Instead, we use tinydtls-sys.
127-
build_config.with("system-tinydtls", None);
128-
build_config.without("vendored-tinydtls", None);
129-
// If tinydtls-sys is built with the vendored feature, the library is built alongside
130-
// the Rust crate. To use the version built by the tinydtls-sys build script, we use the
131-
// environment variables set by the build script.
132-
if let Some(tinydtls_libs) = env::var_os("DEP_TINYDTLS_LIBS") {
133-
build_config.env(
134-
"TinyDTLS_LIBS",
135-
format!("-L{} -l:libtinydtls.a", tinydtls_libs.to_str().unwrap(),),
136-
);
137-
}
138-
if let Some(tinydtls_include) = env::var_os("DEP_TINYDTLS_INCLUDE") {
139-
build_config.env(
140-
"TinyDTLS_CFLAGS",
141-
format!(
142-
"-I{} -I{}",
143-
tinydtls_include.to_str().unwrap(),
144-
Path::new(tinydtls_include.to_str().unwrap())
145-
.join("tinydtls")
146-
.to_str()
125+
match dtls_backend {
126+
DtlsBackend::TinyDtls => {
127+
// We do not ship tinydtls with our source distribution. Instead, we use tinydtls-sys.
128+
build_config.without("submodule-tinydtls", None);
129+
130+
// If tinydtls-sys is built with the vendored feature, the library is built alongside
131+
// the Rust crate. To use the version built by the tinydtls-sys build script, we use the
132+
// environment variables set by the build script.
133+
if let Some(tinydtls_include) = env::var_os("DEP_TINYDTLS_INCLUDE") {
134+
build_config.env(
135+
"TinyDTLS_CFLAGS",
136+
format!(
137+
"-I{} -I{}",
138+
tinydtls_include.to_str().unwrap(),
139+
Path::new(tinydtls_include.to_str().unwrap())
140+
.join("tinydtls")
141+
.to_str()
142+
.unwrap()
143+
),
144+
);
145+
};
146+
147+
if let Some(tinydtls_libs) = env::var_os("DEP_TINYDTLS_LIBS") {
148+
build_config.env("TinyDTLS_LIBS", format!("-L{}", tinydtls_libs.to_str().unwrap()));
149+
150+
build_config.env(
151+
"PKG_CONFIG_PATH",
152+
Path::new(tinydtls_libs.as_os_str())
153+
.join("lib")
154+
.join("pkgconfig")
155+
.into_os_string(),
156+
);
157+
}
158+
},
159+
DtlsBackend::OpenSsl => {
160+
// Set include path according to the path provided by openssl-sys (relevant if
161+
// openssl-sys is vendored)
162+
if let Some(openssl_include) = env::var_os("DEP_OPENSSL_INCLUDE") {
163+
build_config.env("OpenSSL_CFLAGS", format!("-I{}", openssl_include.to_str().unwrap()));
164+
build_config.env(
165+
"PKG_CONFIG_PATH",
166+
Path::new(openssl_include.as_os_str())
167+
.parent()
168+
.unwrap()
169+
.join("lib")
170+
.join("pkgconfig")
171+
.into_os_string(),
172+
);
173+
}
174+
},
175+
DtlsBackend::MbedTls => {
176+
// Set include path according to the path provided by mbedtls-sys (relevant if
177+
// mbedtls-sys is vendored).
178+
// libcoap doesn't support overriding the MbedTLS CFLAGS, but doesn't set those
179+
// either, so we just set CFLAGS and hope they propagate.
180+
if let Some(mbedtls_include) = env::var_os("DEP_MBEDTLS_INCLUDE") {
181+
build_config.env("CFLAGS", format!("-I{}", mbedtls_include.to_str().unwrap()));
182+
build_config.env(
183+
"PKG_CONFIG_PATH",
184+
Path::new(mbedtls_include.as_os_str())
185+
.parent()
147186
.unwrap()
148-
),
149-
);
150-
}
187+
.join("lib")
188+
.join("pkgconfig")
189+
.into_os_string(),
190+
);
191+
}
192+
},
193+
DtlsBackend::GnuTls => {
194+
// gnutls-sys doesn't provide include directory metadata, but doesn't support
195+
// vendoring the library either. Instead, it just uses the GnuTLS found via
196+
// pkg-config, which is already found by libcoap by default.
197+
},
151198
}
152199
} else {
153200
build_config.disable("dtls", None);
@@ -195,43 +242,28 @@ fn main() {
195242
bindgen_builder = bindgen_builder
196243
.clang_arg(format!("-I{}", dst.join("include").to_str().unwrap()))
197244
.clang_arg(format!("-L{}", dst.join("lib").to_str().unwrap()));
198-
orig_pkg_config = std::env::var_os("PKG_CONFIG_PATH").map(|v| String::from(v.to_str().unwrap()));
199-
std::env::set_var(
245+
env::set_var(
200246
"PKG_CONFIG_PATH",
201247
format!(
202248
"{}:{}",
203249
dst.join("lib").join("pkgconfig").to_str().unwrap(),
204250
orig_pkg_config.as_ref().map(String::clone).unwrap_or_else(String::new)
205251
),
206-
)
207-
}
208-
209-
pkgconf.statik(cfg!(feature = "static"));
210-
pkgconf.cargo_metadata(false);
211-
for link_lib in pkgconf
212-
.probe(
213-
format!(
214-
"libcoap-3-{}",
215-
&dtls_backend
216-
.as_ref()
217-
.map(|v| v.to_string())
218-
.unwrap_or_else(|| "notls".to_string())
219-
)
220-
.as_str(),
221-
)
222-
.unwrap()
223-
.libs
224-
{
225-
let link_lib = match link_lib.as_str() {
226-
":libtinydtls.a" => String::from("tinydtls"),
227-
v => String::from(v),
228-
};
229-
println!(
230-
"cargo:rustc-link-lib={}{}",
231-
cfg!(feature = "static").then(|| "static=").unwrap_or(""),
232-
&link_lib
233252
);
253+
env::set_current_dir(current_dir_backup).expect("unable to switch back to source dir");
234254
}
255+
println!(
256+
"cargo:rustc-link-lib={}{}",
257+
cfg!(feature = "static").then(|| "static=").unwrap_or(""),
258+
format!(
259+
"coap-3-{}",
260+
&dtls_backend
261+
.as_ref()
262+
.map(|v| v.to_string())
263+
.unwrap_or_else(|| "notls".to_string())
264+
)
265+
.as_str()
266+
);
235267

236268
bindgen_builder = bindgen_builder
237269
.header("src/wrapper.h")
@@ -271,11 +303,11 @@ fn main() {
271303
}
272304
let bindings = bindgen_builder.generate().unwrap();
273305

274-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
306+
let out_path = PathBuf::from(out_dir);
275307
bindings.write_to_file(out_path.join("bindings.rs")).unwrap();
276308

277309
match orig_pkg_config.as_ref() {
278-
Some(value) => std::env::set_var("PKG_CONFIG_PATH", value),
279-
None => std::env::remove_var("PKG_CONFIG_PATH"),
310+
Some(value) => env::set_var("PKG_CONFIG_PATH", value),
311+
None => env::remove_var("PKG_CONFIG_PATH"),
280312
}
281313
}

libcoap-sys/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ use libc::{epoll_event, fd_set, sockaddr, sockaddr_in, sockaddr_in6, socklen_t,
8787

8888
use crate::coap_pdu_type_t::COAP_MESSAGE_RST;
8989

90+
#[cfg(feature = "dtls_backend_gnutls")]
91+
use gnutls_sys as _;
92+
#[cfg(feature = "dtls_backend_mbedtls")]
93+
use mbedtls_sys as _;
94+
#[cfg(feature = "dtls_backend_openssl")]
95+
use openssl_sys as _;
96+
#[cfg(feature = "dtls_backend_tinydtls")]
97+
use tinydtls_sys as _;
98+
9099
#[cfg(target_family = "windows")]
91100
include!(concat!(env!("OUT_DIR"), "\\bindings.rs"));
92101
#[cfg(not(target_family = "windows"))]

libcoap-sys/src/libcoap

Submodule libcoap updated 79 files

libcoap/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[package]
77
name = "libcoap-rs"
88
description = "An idiomatic wrapper around the libcoap CoAP library for Rust."
9-
version = "0.2.0"
9+
version = "0.2.1"
1010
edition = "2021"
1111
license = "BSD-2-Clause"
1212
readme = "README.md"
@@ -24,12 +24,16 @@ dtls_gnutls = ["libcoap-sys/dtls_backend_gnutls"]
2424
dtls_mbedtls = ["libcoap-sys/dtls_backend_mbedtls"]
2525
tcp = []
2626
nightly = []
27+
vendored = ["libcoap-sys/vendored"]
2728

2829
[dependencies]
29-
libcoap-sys = { version = "^0.2.0", path = "../libcoap-sys" }
30+
libcoap-sys = { version = "^0.2.1", path = "../libcoap-sys" }
3031
libc = { version = "^0.2.95" }
3132
num-derive = { version = "^0.3.3" }
3233
num-traits = { version = "^0.2.14" }
3334
url = { version = "^2.2" }
3435
rand = { version = "^0.8.4" }
3536
thiserror = "^1.0"
37+
38+
[package.metadata.docs.rs]
39+
features = ["dtls", "dtls_openssl", "vendored"]

0 commit comments

Comments
 (0)