Skip to content

Commit

Permalink
merge: pull request #1 from namib-project/libcoap_431
Browse files Browse the repository at this point in the history
Update libcoap to 4.3.1 and fix some issues with the build
  • Loading branch information
pulsastrix authored Nov 20, 2022
2 parents f99176c + de6cdca commit 99db927
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "libcoap"]
path = libcoap-sys/src/libcoap
url = https://github.com/obgm/libcoap.git
branch = develop
branch = release-4.3.1
25 changes: 14 additions & 11 deletions libcoap-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[package]
name = "libcoap-sys"
description = "Raw bindings to the libcoap CoAP library."
version = "0.2.0+libcoap-develop-8b9377e"
version = "0.2.1+libcoap-4.3.1"
edition = "2021"
license = "BSD-2-Clause AND BSD-1-Clause"
links = "coap-3"
Expand All @@ -33,14 +33,14 @@ default = ["server", "client", "tcp", "async", "epoll", "vendored", "static"]
# different backends), we select one based on the auto-detection order specified in
# https://github.com/obgm/libcoap/blob/develop/configure.ac#L494 (gnutls > openssl > mbedtls > tinydtls).
dtls = []
dtls_backend_openssl = ["openssl-sys"]
dtls_backend_gnutls = ["gnutls-sys"]
dtls_backend_mbedtls = ["mbedtls-sys-auto"]
dtls_backend_tinydtls = ["tinydtls-sys"]
dtls_backend_openssl = ["dtls", "openssl-sys"]
dtls_backend_gnutls = ["dtls", "gnutls-sys"]
dtls_backend_mbedtls = ["dtls", "mbedtls-sys-auto"]
dtls_backend_tinydtls = ["dtls", "tinydtls-sys"]
# Enabling this feature will force libcoap-sys to be built with and statically linked to a vendored version of libcoap,
# which will be built by the build-script before building libcoap-sys.
# This way, it is no longer required to have libcoap installed to use this crate.
vendored = ["static", "openssl-sys?/vendored"]
vendored = ["static"]
# Enable this feature to use static linking to libcoap instead of dynamic linking.
static = []
# --- FEATURE FLAGS ---
Expand All @@ -60,14 +60,17 @@ client = []
epoll = []

[dependencies]
gnutls-sys = {version = "^0.1", optional = true}
openssl-sys = {version = "^0.9", optional = true}
gnutls-sys = {version = "^0.1.2", optional = true}
openssl-sys = {version = "^0.9.74", optional = true}
mbedtls-sys-auto = {version = "^2.26", optional = true}
libc = "^0.2"
tinydtls-sys = {version = "^0.1.1", optional = true}
libc = "^0.2.126"
tinydtls-sys = {version = "^0.1.2", optional = true}

[build-dependencies]
bindgen = "^0.59.2"
bindgen = "^0.62.0"
autotools = "^0.2.3"
fs_extra = "^1.2"
pkg-config = "^0.3.24"

[package.metadata.docs.rs]
features = ["dtls", "dtls_backend_openssl", "vendored"]
Empty file removed libcoap-sys/ar-lib
Empty file.
162 changes: 97 additions & 65 deletions libcoap-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ impl ToString for DtlsBackend {
fn main() {
println!("cargo:rerun-if-changed=src/libcoap/");
println!("cargo:rerun-if-changed=src/wrapper.h");
let mut pkgconf = pkg_config::Config::new();
let mut bindgen_builder = bindgen::Builder::default();
let mut orig_pkg_config = None;
// Read required environment variables.
let orig_pkg_config = std::env::var_os("PKG_CONFIG_PATH").map(|v| String::from(v.to_str().unwrap()));
let out_dir = env::var_os("OUT_DIR").unwrap();

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

// Build vendored library if feature was set.
if cfg!(feature = "vendored") {
// Read required environment variables.
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let libcoap_src_dir = Path::new(&out_dir).join("libcoap");
// Read Makeflags into vector of strings
//let make_flags: Vec<String> = std::env::var_os("CARGO_MAKEFLAGS")
// .unwrap()
Expand All @@ -101,7 +101,7 @@ fn main() {
overwrite: true,
..Default::default()
};
match std::fs::remove_dir_all(Path::new(&out_dir).join("libcoap")) {
match std::fs::remove_dir_all(&libcoap_src_dir) {
Ok(_) => {},
Err(e) if e.kind() == ErrorKind::NotFound => {},
e => e.unwrap(),
Expand All @@ -112,42 +112,89 @@ fn main() {
&copy_options,
)
.unwrap();
let libcoap_src_dir = Path::new(&out_dir).join("libcoap");
let current_dir_backup = env::current_dir().unwrap();
env::set_current_dir(&libcoap_src_dir).expect("unable to change to libcoap build dir");
Command::new(libcoap_src_dir.join("autogen.sh")).status().unwrap();

let mut build_config = autotools::Config::new(libcoap_src_dir);
build_config.out_dir(out_dir);
build_config.out_dir(&out_dir);
if let Some(dtls_backend) = dtls_backend {
build_config
.enable("dtls", None)
.with(dtls_backend.to_string().as_str(), None);

if dtls_backend == DtlsBackend::TinyDtls {
// We do not ship tinydtls with our source distribution. Instead, we use tinydtls-sys.
build_config.with("system-tinydtls", None);
build_config.without("vendored-tinydtls", None);
// If tinydtls-sys is built with the vendored feature, the library is built alongside
// the Rust crate. To use the version built by the tinydtls-sys build script, we use the
// environment variables set by the build script.
if let Some(tinydtls_libs) = env::var_os("DEP_TINYDTLS_LIBS") {
build_config.env(
"TinyDTLS_LIBS",
format!("-L{} -l:libtinydtls.a", tinydtls_libs.to_str().unwrap(),),
);
}
if let Some(tinydtls_include) = env::var_os("DEP_TINYDTLS_INCLUDE") {
build_config.env(
"TinyDTLS_CFLAGS",
format!(
"-I{} -I{}",
tinydtls_include.to_str().unwrap(),
Path::new(tinydtls_include.to_str().unwrap())
.join("tinydtls")
.to_str()
match dtls_backend {
DtlsBackend::TinyDtls => {
// We do not ship tinydtls with our source distribution. Instead, we use tinydtls-sys.
build_config.without("submodule-tinydtls", None);

// If tinydtls-sys is built with the vendored feature, the library is built alongside
// the Rust crate. To use the version built by the tinydtls-sys build script, we use the
// environment variables set by the build script.
if let Some(tinydtls_include) = env::var_os("DEP_TINYDTLS_INCLUDE") {
build_config.env(
"TinyDTLS_CFLAGS",
format!(
"-I{} -I{}",
tinydtls_include.to_str().unwrap(),
Path::new(tinydtls_include.to_str().unwrap())
.join("tinydtls")
.to_str()
.unwrap()
),
);
};

if let Some(tinydtls_libs) = env::var_os("DEP_TINYDTLS_LIBS") {
build_config.env("TinyDTLS_LIBS", format!("-L{}", tinydtls_libs.to_str().unwrap()));

build_config.env(
"PKG_CONFIG_PATH",
Path::new(tinydtls_libs.as_os_str())
.join("lib")
.join("pkgconfig")
.into_os_string(),
);
}
},
DtlsBackend::OpenSsl => {
// Set include path according to the path provided by openssl-sys (relevant if
// openssl-sys is vendored)
if let Some(openssl_include) = env::var_os("DEP_OPENSSL_INCLUDE") {
build_config.env("OpenSSL_CFLAGS", format!("-I{}", openssl_include.to_str().unwrap()));
build_config.env(
"PKG_CONFIG_PATH",
Path::new(openssl_include.as_os_str())
.parent()
.unwrap()
.join("lib")
.join("pkgconfig")
.into_os_string(),
);
}
},
DtlsBackend::MbedTls => {
// Set include path according to the path provided by mbedtls-sys (relevant if
// mbedtls-sys is vendored).
// libcoap doesn't support overriding the MbedTLS CFLAGS, but doesn't set those
// either, so we just set CFLAGS and hope they propagate.
if let Some(mbedtls_include) = env::var_os("DEP_MBEDTLS_INCLUDE") {
build_config.env("CFLAGS", format!("-I{}", mbedtls_include.to_str().unwrap()));
build_config.env(
"PKG_CONFIG_PATH",
Path::new(mbedtls_include.as_os_str())
.parent()
.unwrap()
),
);
}
.join("lib")
.join("pkgconfig")
.into_os_string(),
);
}
},
DtlsBackend::GnuTls => {
// gnutls-sys doesn't provide include directory metadata, but doesn't support
// vendoring the library either. Instead, it just uses the GnuTLS found via
// pkg-config, which is already found by libcoap by default.
},
}
} else {
build_config.disable("dtls", None);
Expand Down Expand Up @@ -195,43 +242,28 @@ fn main() {
bindgen_builder = bindgen_builder
.clang_arg(format!("-I{}", dst.join("include").to_str().unwrap()))
.clang_arg(format!("-L{}", dst.join("lib").to_str().unwrap()));
orig_pkg_config = std::env::var_os("PKG_CONFIG_PATH").map(|v| String::from(v.to_str().unwrap()));
std::env::set_var(
env::set_var(
"PKG_CONFIG_PATH",
format!(
"{}:{}",
dst.join("lib").join("pkgconfig").to_str().unwrap(),
orig_pkg_config.as_ref().map(String::clone).unwrap_or_else(String::new)
),
)
}

pkgconf.statik(cfg!(feature = "static"));
pkgconf.cargo_metadata(false);
for link_lib in pkgconf
.probe(
format!(
"libcoap-3-{}",
&dtls_backend
.as_ref()
.map(|v| v.to_string())
.unwrap_or_else(|| "notls".to_string())
)
.as_str(),
)
.unwrap()
.libs
{
let link_lib = match link_lib.as_str() {
":libtinydtls.a" => String::from("tinydtls"),
v => String::from(v),
};
println!(
"cargo:rustc-link-lib={}{}",
cfg!(feature = "static").then(|| "static=").unwrap_or(""),
&link_lib
);
env::set_current_dir(current_dir_backup).expect("unable to switch back to source dir");
}
println!(
"cargo:rustc-link-lib={}{}",
cfg!(feature = "static").then(|| "static=").unwrap_or(""),
format!(
"coap-3-{}",
&dtls_backend
.as_ref()
.map(|v| v.to_string())
.unwrap_or_else(|| "notls".to_string())
)
.as_str()
);

bindgen_builder = bindgen_builder
.header("src/wrapper.h")
Expand Down Expand Up @@ -271,11 +303,11 @@ fn main() {
}
let bindings = bindgen_builder.generate().unwrap();

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_path = PathBuf::from(out_dir);
bindings.write_to_file(out_path.join("bindings.rs")).unwrap();

match orig_pkg_config.as_ref() {
Some(value) => std::env::set_var("PKG_CONFIG_PATH", value),
None => std::env::remove_var("PKG_CONFIG_PATH"),
Some(value) => env::set_var("PKG_CONFIG_PATH", value),
None => env::remove_var("PKG_CONFIG_PATH"),
}
}
9 changes: 9 additions & 0 deletions libcoap-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ use libc::{epoll_event, fd_set, sockaddr, sockaddr_in, sockaddr_in6, socklen_t,

use crate::coap_pdu_type_t::COAP_MESSAGE_RST;

#[cfg(feature = "dtls_backend_gnutls")]
use gnutls_sys as _;
#[cfg(feature = "dtls_backend_mbedtls")]
use mbedtls_sys as _;
#[cfg(feature = "dtls_backend_openssl")]
use openssl_sys as _;
#[cfg(feature = "dtls_backend_tinydtls")]
use tinydtls_sys as _;

#[cfg(target_family = "windows")]
include!(concat!(env!("OUT_DIR"), "\\bindings.rs"));
#[cfg(not(target_family = "windows"))]
Expand Down
2 changes: 1 addition & 1 deletion libcoap-sys/src/libcoap
Submodule libcoap updated 79 files
+13 −21 CMakeLists.txt
+25 −0 ChangeLog
+1 −1 LICENSE
+8 −8 Makefile.am
+1 −1 Makefile.libcoap
+2 −0 README.md
+13 −7 coap_config.h.contiki
+3 −3 coap_config.h.lwip
+1 −1 coap_config.h.lwip.in
+3 −3 coap_config.h.riot
+1 −1 coap_config.h.riot.in
+2 −2 coap_config.h.windows
+3 −3 configure.ac
+130 −37 doc/Makefile.am
+33 −0 doc/docbook.local.css
+2 −0 doc/main.md
+1 −1 doc/module_api_wrap.h
+6 −0 doc/upgrade_4.3.0_4.3.1.txt
+1 −1 examples/Makefile.am
+1 −0 examples/coap-client.c
+4 −3 examples/coap-server.c
+1 −1 examples/lwip/Makefile
+1 −1 examples/lwip/server-coap.c
+1 −1 ext/tinydtls
+1 −1 include/coap3/block.h
+5 −5 include/coap3/coap.h.in
+8 −8 include/coap3/coap.h.windows
+5 −5 include/coap3/coap.h.windows.in
+3 −3 include/coap3/coap_address.h
+2 −2 include/coap3/coap_async.h
+1 −1 include/coap3/coap_async_internal.h
+12 −7 include/coap3/coap_block_internal.h
+21 −11 include/coap3/coap_event.h
+1 −1 include/coap3/coap_forward_decls.h
+1 −1 include/coap3/coap_internal.h
+2 −2 include/coap3/coap_io.h
+1 −1 include/coap3/coap_io_internal.h
+27 −4 include/coap3/coap_mutex.h
+2 −1 include/coap3/coap_net_internal.h
+3 −3 include/coap3/coap_option.h
+16 −6 include/coap3/coap_pdu_internal.h
+1 −1 include/coap3/coap_resource_internal.h
+7 −0 include/coap3/coap_session.h
+9 −0 include/coap3/coap_session_internal.h
+3 −3 include/coap3/coap_subscribe.h
+19 −13 include/coap3/coap_subscribe_internal.h
+1 −1 include/coap3/lwippools.h
+1 −1 include/coap3/net.h
+1 −1 include/coap3/pdu.h
+3 −3 include/coap3/resource.h
+1 −0 libcoap-3.map
+1 −0 libcoap-3.sym
+8 −1 man/Makefile.am
+2 −0 man/coap.txt.in
+2 −1 man/coap_attribute.txt.in
+2 −3 man/coap_block.txt.in
+1 −1 man/coap_cache.txt.in
+1 −1 man/coap_endpoint_server.txt.in
+2 −1 man/coap_handler.txt.in
+1 −1 man/coap_io.txt.in
+11 −3 man/coap_observe.txt.in
+2 −2 man/coap_pdu_access.txt.in
+5 −5 man/coap_pdu_setup.txt.in
+1 −1 man/coap_recovery.txt.in
+5 −4 man/coap_resource.txt.in
+1 −1 scripts/github_dist.sh
+181 −95 src/block.c
+3 −3 src/coap_address.c
+2 −2 src/coap_async.c
+25 −11 src/coap_io.c
+25 −1 src/coap_mbedtls.c
+3 −3 src/coap_option.c
+21 −5 src/coap_session.c
+4 −4 src/coap_subscribe.c
+71 −28 src/net.c
+14 −3 src/pdu.c
+5 −22 src/resource.c
+8 −8 win32/libcoap.vcxproj
+19 −19 win32/libcoap.vcxproj.filters
8 changes: 6 additions & 2 deletions libcoap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[package]
name = "libcoap-rs"
description = "An idiomatic wrapper around the libcoap CoAP library for Rust."
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "BSD-2-Clause"
readme = "README.md"
Expand All @@ -24,12 +24,16 @@ dtls_gnutls = ["libcoap-sys/dtls_backend_gnutls"]
dtls_mbedtls = ["libcoap-sys/dtls_backend_mbedtls"]
tcp = []
nightly = []
vendored = ["libcoap-sys/vendored"]

[dependencies]
libcoap-sys = { version = "^0.2.0", path = "../libcoap-sys" }
libcoap-sys = { version = "^0.2.1", path = "../libcoap-sys" }
libc = { version = "^0.2.95" }
num-derive = { version = "^0.3.3" }
num-traits = { version = "^0.2.14" }
url = { version = "^2.2" }
rand = { version = "^0.8.4" }
thiserror = "^1.0"

[package.metadata.docs.rs]
features = ["dtls", "dtls_openssl", "vendored"]

0 comments on commit 99db927

Please sign in to comment.