diff --git a/Cargo.toml b/Cargo.toml index f0413919..90355ae7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,12 +31,13 @@ rust-version = "1.63" [badges] maintenance = { status = "experimental" } -travis-ci = { repository = "openSUSE/libpathrs" } [lib] -crate-type = ["rlib", "cdylib", "staticlib"] +# When building the CAPI, our Makefile adds --crate-type={cdylib,staticlib}. +crate-type = ["rlib"] [features] +capi = ["dep:rand"] # Only used for tests. _test_as_root = [] @@ -55,7 +56,7 @@ itertools = "^0.13" lazy_static = "^1" libc = "^0.2" memchr = "^2" -rand = "^0.8" +rand = { version = "^0.8", optional = true } # MSRV(1.65): Use regex >= 1.10. regex = "1.9.*" rustix = { version = "^0.38", features = ["fs"] } @@ -72,5 +73,9 @@ pretty_assertions = "^1" rustix = { version = "^0.38", features = ["process"] } [lints.rust] -# We have special handling for coverage runs (which set cfg(coverage)). -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage)'] } +unexpected_cfgs = { level = "warn", check-cfg = [ + # We have special handling for coverage runs (which set cfg(coverage)). + 'cfg(coverage)', + # We set these cfgs when building with --features=capi. + 'cfg(cdylib)', 'cfg(staticlib)' +] } diff --git a/Makefile b/Makefile index 558c73bb..7add5b32 100644 --- a/Makefile +++ b/Makefile @@ -19,17 +19,26 @@ CARGO_NIGHTLY ?= cargo +nightly SRC_FILES = $(shell find . -name '*.rs') +.DEFAULT: debug .PHONY: debug debug: target/debug target/debug: $(SRC_FILES) - $(CARGO) build + # For some reason, --crate-types needs separate invocations. We can't use + # #![crate_type] unfortunately, as using it with #![cfg_attr] has been + # deprecated. + $(CARGO) rustc --features=capi --crate-type=cdylib + $(CARGO) rustc --features=capi --crate-type=staticlib .PHONY: release release: target/release target/release: $(SRC_FILES) - $(CARGO) build --release + # For some reason, --crate-types needs separate invocations. We can't use + # #![crate_type] unfortunately, as using it with #![cfg_attr] has been + # deprecated. + $(CARGO) rustc --features=capi --crate-type=cdylib --release + $(CARGO) rustc --features=capi --crate-type=staticlib --release .PHONY: smoke-test smoke-test: @@ -78,7 +87,7 @@ test: test-rust .PHONY: docs docs: - $(CARGO) doc --document-private-items --open + $(CARGO) doc --all-features --document-private-items --open .PHONY: install install: release diff --git a/build.rs b/build.rs index e3f0fe94..ddd0e6fa 100644 --- a/build.rs +++ b/build.rs @@ -20,11 +20,15 @@ use std::env; fn main() { - // Add DT_SONAME to our cdylibs. - let name = "pathrs"; - let major = env::var("CARGO_PKG_VERSION_MAJOR").unwrap(); - println!( - "cargo:rustc-cdylib-link-arg=-Wl,-soname,lib{}.so.{}", - name, major - ); + // Add DT_SONAME to our cdylibs. We can't check the crate-type here + // directly, but we can at least avoid needless warnings for "cargo build" + // by only emitting this when the capi feature is enabled. + if cfg!(feature = "capi") { + let name = "pathrs"; + let major = env::var("CARGO_PKG_VERSION_MAJOR").unwrap(); + println!( + "cargo:rustc-cdylib-link-arg=-Wl,-soname,lib{}.so.{}", + name, major + ); + } } diff --git a/install.sh b/install.sh index 37081c46..6b36e355 100755 --- a/install.sh +++ b/install.sh @@ -136,6 +136,12 @@ includedir="${includedir:-$prefix/include}" libdir="${libdir:-$(find_libdir "$exec_prefix")}" pkgconfigdir="${pkgconfigdir:-$libdir/pkgconfig}" +# TODO: These flags come from RUSTFLAGS="--print=native-static-libs". +# Unfortunately, getting this information from cargo is incredibly unergonomic +# and will hopefully be fixed at some point. +# +native_static_libs="-lgcc_s -lutil -lrt -lpthread -lm -ldl -lc" + echo "[pkg-config] generating pathrs pkg-config" cat >"pathrs.pc" < }, diff --git a/src/lib.rs b/src/lib.rs index e7981ef9..6bbed228 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,6 +156,7 @@ pub use resolvers::{Resolver, ResolverBackend}; pub mod procfs; // C API. +#[cfg(feature = "capi")] mod capi; // Internally used helpers.