Skip to content

Commit

Permalink
Enable ZMQ_HAVE_STRLCPY for GLIBC >= 2.38 (#29)
Browse files Browse the repository at this point in the history
* Determine at compile time whether a GLIBC target's std library contains strlcpy. If it does then set ZMQ_HAVE_STRLCPY=1.
Co-authored-by: Yiannis Marangos <psyberbits@gmail.com>
  • Loading branch information
jean-airoldie committed Aug 11, 2023
1 parent 5059cd7 commit 2cc6e19
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
env, fs,
fs::File,
env,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -56,6 +56,35 @@ where
Err(())
}

#[cfg(target_env = "gnu")]
mod glibc {
use std::{
env,
path::{Path, PathBuf},
};

// Attempt to compile a c program that links to strlcpy from the std
// library to determine whether glibc packages it.
pub(crate) fn has_strlcpy() -> bool {
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/strlcpy.c");
println!("cargo:rerun-if-changed={}", src.display());

let dest =
PathBuf::from(env::var("OUT_DIR").unwrap()).join("has_strlcpy");

cc::Build::new()
.warnings(false)
.get_compiler()
.to_command()
.arg(src)
.arg("-o")
.arg(dest)
.status()
.expect("failed to execute gcc")
.success()
}
}

/// The location of a library.
#[derive(Debug, Clone)]
pub struct LibLocation {
Expand Down Expand Up @@ -303,7 +332,7 @@ impl Build {
println!("cargo:rustc-link-search={:?}", libsodium.lib_dir());

if target.contains("msvc") {
std::fs::copy(
fs::copy(
libsodium
.include_dir()
.join("../../../builds/msvc/version.h"),
Expand All @@ -323,7 +352,7 @@ impl Build {
// https://cmake.org/cmake/help/latest/command/configure_file.html
// TODO: Replace `#cmakedefine` with the appropriate `#define`
// let _platform_file =
// std::fs::read_to_string(path.join("builds/cmake/platform.hpp.in"))
// fs::read_to_string(path.join("builds/cmake/platform.hpp.in"))
// .unwrap();

let out_includes = PathBuf::from(std::env::var("OUT_DIR").unwrap());
Expand All @@ -338,6 +367,7 @@ impl Build {
build.include(out_includes);
};

let mut has_strlcpy = false;
if target.contains("windows") {
// on windows vista and up we can use `epoll` through the `wepoll` lib
add_c_sources(
Expand Down Expand Up @@ -379,22 +409,31 @@ impl Build {
build.define("ZMQ_HAVE_UIO", "1");

if target.contains("android") {
build.define("ZMQ_HAVE_STRLCPY", "1");
has_strlcpy = true;
}

if target.contains("musl") {
build.define("ZMQ_HAVE_STRLCPY", "1");
has_strlcpy = true;
}
} else if target.contains("apple") {
create_platform_hpp_shim(&mut build);
build.define("ZMQ_IOTHREAD_POLLER_USE_KQUEUE", "1");
build.define("ZMQ_POLL_BASED_ON_POLL", "1");
build.define("HAVE_STRNLEN", "1");
build.define("ZMQ_HAVE_STRLCPY", "1");
build.define("ZMQ_HAVE_UIO", "1");
build.define("ZMQ_HAVE_IPC", "1");
has_strlcpy = true;
}

// https://github.com/jean-airoldie/zeromq-src-rs/issues/28
#[cfg(target_env = "gnu")]
if !has_strlcpy && glibc::has_strlcpy() {
has_strlcpy = true;
}

if has_strlcpy {
build.define("ZMQ_HAVE_STRLCPY", "1");
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let lib_dir = out_dir.join("lib");

Expand Down
7 changes: 7 additions & 0 deletions src/strlcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <string.h>

int main() {
char buf[1];
(void)strlcpy(buf, "a", 1);
return 0;
}

0 comments on commit 2cc6e19

Please sign in to comment.