From cc8bfec02d2ae6e80f48ce8a505e312471225f00 Mon Sep 17 00:00:00 2001 From: William Brown Date: Nov 05 2019 05:00:37 +0000 Subject: Ticket 50633 - Add cargo vendor support for offline builds Bug Description: At suse/rh we need to be able to build offline. To achieve this we need offline builds. This adds support for these in 389-ds with cargo and rust. Fix Description: This adds cargo vendor support for offline builds, and shows that they work. We add a stub library for librslapd/libslapd so that we can begin to develop features in rust. To build normally: work as usual. To build offline: make -f rpm.mk download-cargo-dependencies ./configure --enable-rust --enable-rust-offline Continue to build as usual. A note to keep in mind is cargo test does not work offline as dev-dependencies are not vendored. The download-cargo-dependencies has been added to dist-bz2 for distributions. https://pagure.io/389-ds-base/pull-request/50633 Author: William Brown Review by: mhonek (Thanks) --- diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..af24cf1 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,8 @@ + + +[source.crates-io] +replace-with = "vendored-sources" + +[source.vendored-sources] +directory = "./vendor" + diff --git a/.gitignore b/.gitignore index e6b941a..45436c4 100644 --- a/.gitignore +++ b/.gitignore @@ -225,8 +225,10 @@ html/ src/lib389/dist/ src/lib389/man/ src/libsds/target/ +src/librslapd/target/ dist venv .idea src/cockpit/389-console/cockpit_dist/ src/cockpit/389-console/node_modules/ +ldap/servers/slapd/rust-slapi-private.h diff --git a/Makefile.am b/Makefile.am index 5462845..63f7120 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,12 +33,19 @@ NSPR_INCLUDES = $(NSPR_CFLAGS) # Rust inclusions. if RUST_ENABLE +# Rust enabled RUST_ON = 1 CARGO_FLAGS = @cargo_defs@ RUSTC_FLAGS = @asan_rust_defs@ @msan_rust_defs@ @tsan_rust_defs@ @debug_rust_defs@ RUST_LDFLAGS = -ldl -lpthread -lgcc_s -lc -lm -lrt -lutil RUST_DEFINES = -DRUST_ENABLE +if RUST_ENABLE_OFFLINE +RUST_OFFLINE = --locked --offline else +RUST_OFFLINE = +endif +else +# Rust disabled RUST_ON = 0 CARGO_FLAGS = RUSTC_FLAGS = @@ -211,6 +218,10 @@ SLAPD_LDFLAGS = -version-info 1:0:1 BUILT_SOURCES = dberrstrs.h \ $(POLICY_FC) +if RUST_ENABLE +BUILT_SOURCES += rust-slapi-private.h +endif + if enable_posix_winsync LIBPOSIX_WINSYNC_PLUGIN = libposix-winsync-plugin.la endif @@ -269,6 +280,10 @@ CLEANFILES = dberrstrs.h ns-slapd.properties \ doxyfile.stamp ldap/admin/src/scripts/dbmon.sh \ $(NULL) +if RUST_ENABLE +CLEANFILES += rust-slapi-private.h +endif + clean-local: -rm -rf dist -rm -rf $(abs_top_builddir)/html @@ -1172,7 +1187,7 @@ libsds_la_LDFLAGS = $(AM_LDFLAGS) $(SDS_LDFLAGS) if RUST_ENABLE -noinst_LTLIBRARIES = librsds.la +noinst_LTLIBRARIES = librsds.la librslapd.la ### Why does this exist? # @@ -1181,6 +1196,8 @@ noinst_LTLIBRARIES = librsds.la # https://people.gnome.org/~federico/blog/librsvg-build-infrastructure.html # https://gitlab.gnome.org/GNOME/librsvg/blob/master/Makefile.am +### Rust datastructures + RSDS_LIB = @abs_top_builddir@/rs/@rust_target_dir@/librsds.a libsds_la_LIBADD = $(RSDS_LIB) @@ -1193,15 +1210,47 @@ librsds_la_SOURCES = \ librsds_la_EXTRA = src/libsds/Cargo.lock @abs_top_builddir@/rs/@rust_target_dir@/librsds.a: $(librsds_la_SOURCES) - CARGO_TARGET_DIR=$(abs_top_builddir)/rs RUSTC_BOOTSTRAP=1 \ - cargo rustc --manifest-path=$(srcdir)/src/libsds/Cargo.toml \ + RUST_BACKTRACE=1 RUSTC_BOOTSTRAP=1 \ + CARGO_TARGET_DIR=$(abs_top_builddir)/rs \ + cargo rustc $(RUST_OFFLINE) --manifest-path=$(srcdir)/src/libsds/Cargo.toml \ $(CARGO_FLAGS) --verbose -- $(RUSTC_FLAGS) -EXTRA_DIST = $(librsds_la_SOURCES) $(librsds_la_EXTRA) +### Rust lib slapd components +RSLAPD_LIB = @abs_top_builddir@/rs/@rust_target_dir@/librslapd.a + +librslapd_la_SOURCES = \ + src/librslapd/Cargo.toml \ + src/librslapd/build.rs \ + src/librslapd/src/lib.rs + +librslapd_la_EXTRA = src/librslapd/Cargo.lock +@abs_top_builddir@/rs/@rust_target_dir@/librslapd.a: $(librslapd_la_SOURCES) + RUST_BACKTRACE=1 RUSTC_BOOTSTRAP=1 \ + CARGO_TARGET_DIR=$(abs_top_builddir)/rs \ + SLAPD_HEADER_DIR=$(abs_top_builddir)/ \ + cargo rustc $(RUST_OFFLINE) --manifest-path=$(srcdir)/src/librslapd/Cargo.toml \ + $(CARGO_FLAGS) --verbose -- $(RUSTC_FLAGS) + +# The header needs the lib build first. +rust-slapi-private.h: @abs_top_builddir@/rs/@rust_target_dir@/librslapd.a + +EXTRA_DIST = $(librsds_la_SOURCES) $(librsds_la_EXTRA) \ + $(librslapd_la_SOURCES) $(librslapd_la_EXTRA) + +## Run rust tests +# cargo does not support offline tests :( +if RUST_ENABLE_OFFLINE +else check-local: - CARGO_TARGET_DIR=$(abs_top_builddir)/rs RUSTC_BOOTSTRAP=1 \ - cargo test --manifest-path=$(srcdir)/src/libsds/Cargo.toml + RUST_BACKTRACE=1 RUSTC_BOOTSTRAP=1 \ + CARGO_TARGET_DIR=$(abs_top_builddir)/rs \ + cargo test $(RUST_OFFLINE) --manifest-path=$(srcdir)/src/libsds/Cargo.toml + RUST_BACKTRACE=1 RUSTC_BOOTSTRAP=1 \ + CARGO_TARGET_DIR=$(abs_top_builddir)/rs \ + SLAPD_HEADER_DIR=$(abs_top_builddir)/ \ + cargo test $(RUST_OFFLINE) --manifest-path=$(srcdir)/src/librslapd/Cargo.toml +endif else # Just build the tqueue in C. @@ -1363,6 +1412,11 @@ libslapd_la_SOURCES = ldap/servers/slapd/add.c \ libslapd_la_CPPFLAGS = $(AM_CPPFLAGS) $(DSPLUGIN_CPPFLAGS) $(SASL_CFLAGS) @db_inc@ $(KERBEROS_CFLAGS) $(PCRE_CFLAGS) $(SDS_CPPFLAGS) $(SVRCORE_INCLUDES) libslapd_la_LIBADD = $(LDAPSDK_LINK) $(SASL_LINK) $(NSS_LINK) $(NSPR_LINK) $(KERBEROS_LIBS) $(PCRE_LIBS) $(THREADLIB) $(SYSTEMD_LIBS) libsds.la libsvrcore.la + +if RUST_ENABLE +libslapd_la_LIBADD += $(RSLAPD_LIB) +endif + libslapd_la_LDFLAGS = $(AM_LDFLAGS) $(SLAPD_LDFLAGS) diff --git a/configure.ac b/configure.ac index 233bc5b..64fc2ff 100644 --- a/configure.ac +++ b/configure.ac @@ -85,19 +85,27 @@ AC_CHECK_FUNCS([clock_gettime], [], AC_MSG_ERROR([unable to locate required symb LT_LIB_DLLOAD # Optional rust component support. +AC_MSG_CHECKING(for --enable-rust-offline) +AC_ARG_ENABLE(rust_offline, AS_HELP_STRING([--enable-rust-offline], [Enable rust building offline. you MUST have run vendor! (default: no)]), + [], [ enable_rust_offline=no ]) +AC_MSG_RESULT($enable_rust_offline) +AM_CONDITIONAL([RUST_ENABLE_OFFLINE],[test "$enable_rust_offline" = yes]) + AC_MSG_CHECKING(for --enable-rust) AC_ARG_ENABLE(rust, AS_HELP_STRING([--enable-rust], [Enable rust language features (default: no)]), [], [ enable_rust=no ]) AC_MSG_RESULT($enable_rust) -if test "$enable_rust" = yes ; then +if test "$enable_rust" = yes -o "$enable_rust_offline" = yes; then AC_CHECK_PROG(CARGO, [cargo], [yes], [no]) AC_CHECK_PROG(RUSTC, [rustc], [yes], [no]) AS_IF([test "$CARGO" != "yes" -o "$RUSTC" != "yes"], [ AC_MSG_FAILURE("Rust based plugins cannot be built cargo=$CARGO rustc=$RUSTC") ]) + + fi -AM_CONDITIONAL([RUST_ENABLE],[test "$enable_rust" = yes]) +AM_CONDITIONAL([RUST_ENABLE],[test "$enable_rust" = yes -o "$enable_rust_offline" = yes]) AC_MSG_CHECKING(for --enable-debug) AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], [Enable debug features (default: no)]), diff --git a/ldap/servers/slapd/libglobs.c b/ldap/servers/slapd/libglobs.c index 167a3fe..db61ee0 100644 --- a/ldap/servers/slapd/libglobs.c +++ b/ldap/servers/slapd/libglobs.c @@ -133,6 +133,11 @@ #endif #include +#ifdef RUST_ENABLE +#include +#endif + + #define REMOVE_CHANGELOG_CMD "remove" int slapd_ldap_debug = SLAPD_DEFAULT_ERRORLOG_LEVEL; @@ -1533,6 +1538,11 @@ FrontendConfig_init(void) struct rlimit rlp; int64_t maxdescriptors = SLAPD_DEFAULT_MAXDESCRIPTORS; +#ifdef RUST_ENABLE + /* prove rust is working */ + PR_ASSERT(do_nothing_rust() == 0); +#endif + #if SLAPI_CFG_USE_RWLOCK == 1 /* initialize the read/write configuration lock */ if ((cfg->cfg_rwlock = slapi_new_rwlock()) == NULL) { diff --git a/rpm.mk b/rpm.mk index 9eb4500..d81e399 100644 --- a/rpm.mk +++ b/rpm.mk @@ -32,13 +32,23 @@ clean: rm -rf dist rm -rf rpmbuild +update-cargo-dependencies: + cargo update --manifest-path=./src/libsds/Cargo.toml + cargo update --manifest-path=./src/librslapd/Cargo.toml + +download-cargo-dependencies: + cargo vendor --manifest-path=./src/libsds/Cargo.toml + cargo fetch --manifest-path=./src/libsds/Cargo.toml + cargo vendor --manifest-path=./src/librslapd/Cargo.toml + cargo fetch --manifest-path=./src/librslapd/Cargo.toml + install-node-modules: cd src/cockpit/389-console; make -f node_modules.mk install build-cockpit: install-node-modules cd src/cockpit/389-console; make -f node_modules.mk build-cockpit-plugin -dist-bz2: install-node-modules +dist-bz2: install-node-modules download-cargo-dependencies cd src/cockpit/389-console; \ rm -rf cockpit_dist; \ make -f node_modules.mk build-cockpit-plugin; \ diff --git a/src/librslapd/Cargo.toml b/src/librslapd/Cargo.toml new file mode 100644 index 0000000..3111e59 --- /dev/null +++ b/src/librslapd/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "librslapd" +version = "0.1.0" +authors = ["William Brown "] +edition = "2018" +build = "build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +path = "src/lib.rs" +name = "rslapd" +crate-type = ["staticlib", "lib"] + +[profile.release] +panic = "abort" +lto = true + + +[dependencies] + +[build-dependencies] +cbindgen = "0.9" + diff --git a/src/librslapd/build.rs b/src/librslapd/build.rs new file mode 100644 index 0000000..4d4c1ce --- /dev/null +++ b/src/librslapd/build.rs @@ -0,0 +1,15 @@ +extern crate cbindgen; + +use std::env; + +fn main() { + let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let out_dir = env::var("SLAPD_HEADER_DIR").unwrap(); + + cbindgen::Builder::new() + .with_language(cbindgen::Language::C) + .with_crate(crate_dir) + .generate() + .expect("Unable to generate bindings") + .write_to_file(format!("{}/rust-slapi-private.h", out_dir)); +} diff --git a/src/librslapd/src/lib.rs b/src/librslapd/src/lib.rs new file mode 100644 index 0000000..e485f92 --- /dev/null +++ b/src/librslapd/src/lib.rs @@ -0,0 +1,12 @@ +#[no_mangle] +pub extern "C" fn do_nothing_rust() -> usize { + 0 +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/src/libsds/Cargo.toml b/src/libsds/Cargo.toml index 6c035ae..518cc6e 100644 --- a/src/libsds/Cargo.toml +++ b/src/libsds/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "rsds" version = "0.1.0" -authors = ["William Brown "] +authors = ["William Brown "] +edition = "2018" [dependencies] @@ -14,3 +15,4 @@ crate-type = ["staticlib", "lib"] panic = "abort" lto = true +