rust: Link to our C/C++ dependencies and internal library

This allows us to fully use cxx-rs with `extern "C++"`.  Now
we do call back into the C/C++ today, but it only works outside
of cargo/Rust's knowledge.  Most notably, it means we can't
use our C code in `cargo test`.  And that's a problem
for moving some C/C++ code to Rust, because we want to port
the unit tests too.

For now, re-declare our dependencies and part of the build
system inside the Cargo build.  However, this is also
an important step towards using Cargo as our *sole* build
system.

We don't add build dependencies too often, so the short
term duplication should be OK.

However, a major unfortunate side effect of this is that
we now need to serialize the build process; almost all the
C/C++ comes first (`librpmostreeinternals.la`) and then
the Rust build, then we finally generate the executable
with both.

The only way out of this really is to move more of the
C/C++ build into Cargo, and we probably want to refactor
into internal crates.
This commit is contained in:
Colin Walters 2021-01-23 15:32:05 +00:00 committed by OpenShift Merge Robot
parent 632cecdca6
commit 4ce5f42d12
4 changed files with 74 additions and 10 deletions

54
Cargo.lock generated
View File

@ -483,7 +483,7 @@ dependencies = [
"glib-sys", "glib-sys",
"gobject-sys", "gobject-sys",
"libc", "libc",
"system-deps", "system-deps 1.3.2",
"winapi", "winapi",
] ]
@ -529,7 +529,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7e9b997a66e9a23d073f2b1abb4dbfc3925e0b8952f67efd8d9b6e168e4cdc1" checksum = "c7e9b997a66e9a23d073f2b1abb4dbfc3925e0b8952f67efd8d9b6e168e4cdc1"
dependencies = [ dependencies = [
"libc", "libc",
"system-deps", "system-deps 1.3.2",
] ]
[[package]] [[package]]
@ -540,7 +540,7 @@ checksum = "952133b60c318a62bf82ee75b93acc7e84028a093e06b9e27981c2b6fe68218c"
dependencies = [ dependencies = [
"glib-sys", "glib-sys",
"libc", "libc",
"system-deps", "system-deps 1.3.2",
] ]
[[package]] [[package]]
@ -860,7 +860,7 @@ dependencies = [
"glib-sys", "glib-sys",
"gobject-sys", "gobject-sys",
"libc", "libc",
"system-deps", "system-deps 1.3.2",
] ]
[[package]] [[package]]
@ -1197,6 +1197,7 @@ dependencies = [
"serde_yaml", "serde_yaml",
"structopt", "structopt",
"subprocess", "subprocess",
"system-deps 2.0.3",
"systemd", "systemd",
"tempfile", "tempfile",
] ]
@ -1335,6 +1336,12 @@ version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b" checksum = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b"
[[package]]
name = "strum"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c"
[[package]] [[package]]
name = "strum_macros" name = "strum_macros"
version = "0.18.0" version = "0.18.0"
@ -1347,6 +1354,18 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "strum_macros"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "subprocess" name = "subprocess"
version = "0.2.6" version = "0.2.6"
@ -1376,11 +1395,26 @@ checksum = "0f3ecc17269a19353b3558b313bba738b25d82993e30d62a18406a24aba4649b"
dependencies = [ dependencies = [
"heck", "heck",
"pkg-config", "pkg-config",
"strum", "strum 0.18.0",
"strum_macros", "strum_macros 0.18.0",
"thiserror", "thiserror",
"toml", "toml",
"version-compare", "version-compare 0.0.10",
]
[[package]]
name = "system-deps"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b59b8aafd652f3c1469f16e6c223121e8a8dbe40c71475209c1401cff3a67ef"
dependencies = [
"heck",
"pkg-config",
"strum 0.20.0",
"strum_macros 0.20.1",
"thiserror",
"toml",
"version-compare 0.0.11",
] ]
[[package]] [[package]]
@ -1513,6 +1547,12 @@ version = "0.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1"
[[package]]
name = "version-compare"
version = "0.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c18c859eead79d8b95d09e4678566e8d70105c4e7b251f707a03df32442661b"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.1.5" version = "0.1.5"

View File

@ -3,6 +3,17 @@ name = "rpmostree-rust"
version = "0.1.0" version = "0.1.0"
authors = ["Colin Walters <walters@verbum.org>", "Jonathan Lebon <jonathan@jlebon.com>"] authors = ["Colin Walters <walters@verbum.org>", "Jonathan Lebon <jonathan@jlebon.com>"]
edition = "2018" edition = "2018"
links = "rpmostreeinternals"
# This currently needs to duplicate the libraries in configure.ac
# until we unify on Cargo as our build system
[package.metadata.system-deps]
libarchive = "3.0"
jsonglib = { name = "json-glib-1.0", version = "1" }
polkitgobject = { name = "polkit-gobject-1", version = "0" }
rpm = "4"
librepo = "1"
libsolv = "0.7"
[dependencies] [dependencies]
anyhow = "1.0.38" anyhow = "1.0.38"
@ -43,6 +54,7 @@ os-release = "0.1.0"
[build-dependencies] [build-dependencies]
cbindgen = "0.16.0" cbindgen = "0.16.0"
system-deps = "2.0"
anyhow = "1.0" anyhow = "1.0"
[lib] [lib]

View File

@ -86,14 +86,15 @@ rpmostree_common_cflags = -I$(srcdir)/src/app -I$(srcdir)/src/daemon \
-DLIBDIR=\"$(libdir)\" -DPKGLIBDIR=\"$(pkglibdir)\" \ -DLIBDIR=\"$(libdir)\" -DPKGLIBDIR=\"$(pkglibdir)\" \
$(PKGDEP_RPMOSTREE_CFLAGS) $(PKGDEP_RPMOSTREE_RS_CFLAGS) $(PKGDEP_RPMOSTREE_CFLAGS) $(PKGDEP_RPMOSTREE_RS_CFLAGS)
rpmostree_bin_common_cflags = $(rpmostree_common_cflags) rpmostree_bin_common_cflags = $(rpmostree_common_cflags)
rpmostree_bin_common_libs = $(PKGDEP_RPMOSTREE_LIBS) $(CAP_LIBS) libglnx.la librpmostree-1.la librpmostreecxxrs.la $(librpmostree_rust_path) $(PKGDEP_RPMOSTREE_RS_LIBS) -lstdc++ -lrt rpmostree_common_libs = $(PKGDEP_RPMOSTREE_LIBS) $(CAP_LIBS) libglnx.la librpmostree-1.la librpmostreecxxrs.la $(PKGDEP_RPMOSTREE_RS_LIBS) -lstdc++ -lrt
rpmostree_bin_common_libs = librpmostreeinternals.la $(librpmostree_rust_path) $(rpmostree_common_libs)
rpm_ostree_CFLAGS = $(AM_CFLAGS) $(rpmostree_bin_common_cflags) rpm_ostree_CFLAGS = $(AM_CFLAGS) $(rpmostree_bin_common_cflags)
rpm_ostree_CXXFLAGS = $(AM_CXXFLAGS) $(rpmostree_bin_common_cflags) rpm_ostree_CXXFLAGS = $(AM_CXXFLAGS) $(rpmostree_bin_common_cflags)
rpm_ostree_LDADD = librpmostreeinternals.la $(rpmostree_bin_common_libs) rpm_ostree_LDADD = librpmostreeinternals.la $(rpmostree_bin_common_libs)
EXTRA_rpm_ostree_DEPENDENCIES = libdnf.so.2 EXTRA_rpm_ostree_DEPENDENCIES = libdnf.so.2
librpmostreeinternals_la_CFLAGS = $(AM_CFLAGS) $(rpmostree_common_cflags) librpmostreeinternals_la_CFLAGS = $(AM_CFLAGS) $(rpmostree_common_cflags)
librpmostreeinternals_la_CXXFLAGS = $(AM_CXXFLAGS) $(rpmostree_common_cflags) librpmostreeinternals_la_CXXFLAGS = $(AM_CXXFLAGS) $(rpmostree_common_cflags)
librpmostreeinternals_la_LIBADD = $(rpmostree_bin_common_libs) librpmostreeinternals_la_LIBADD = $(rpmostree_common_libs)
EXTRA_librpmostreeinternals_la_DEPENDENCIES = libdnf.so.2 EXTRA_librpmostreeinternals_la_DEPENDENCIES = libdnf.so.2
privdatadir=$(pkglibdir) privdatadir=$(pkglibdir)
@ -114,7 +115,9 @@ librpmostree_rust_path = $(top_srcdir)/target/@RUST_TARGET_SUBDIR@/librpmostree_
# we exit with a fatal error, since someone probably did `make && sudo make install`, # we exit with a fatal error, since someone probably did `make && sudo make install`,
# and in this case cargo will download into ~/.root which we don't want. # and in this case cargo will download into ~/.root which we don't want.
LIBRPMOSTREE_RUST_SRCS = $(shell find rust/src/ -name '*.rs') Cargo.toml Cargo.lock cbindgen.toml LIBRPMOSTREE_RUST_SRCS = $(shell find rust/src/ -name '*.rs') Cargo.toml Cargo.lock cbindgen.toml
$(librpmostree_rust_path): Makefile $(LIBRPMOSTREE_RUST_SRCS) # FIXME - build all this code in a rpmostree-sys crate, or just move all the C/C++ build
# to Rust. Currently this forces build system serialization
$(librpmostree_rust_path): Makefile $(LIBRPMOSTREE_RUST_SRCS) librpmostreeinternals.la
$(cargo_build) $(CARGO_RELEASE_ARGS) $(cargo_build) $(CARGO_RELEASE_ARGS)
EXTRA_DIST += $(LIBRPMOSTREE_RUST_SRCS) EXTRA_DIST += $(LIBRPMOSTREE_RUST_SRCS)

View File

@ -16,6 +16,15 @@ fn detect_fedora_feature() -> Result<()> {
} }
fn main() -> Result<()> { fn main() -> Result<()> {
let cwd = std::env::current_dir()?;
let cwd = cwd.to_str().expect("utf8 pwd");
println!("cargo:rustc-link-search={}/.libs", cwd);
println!("cargo:rustc-link-lib=static=rpmostreeinternals");
println!("cargo:rustc-link-lib=cap");
println!("cargo:rustc-link-search={}/libdnf-build/libdnf", cwd);
println!("cargo:rustc-link-lib=dnf");
println!("cargo:rustc-link-lib=rpmostree-1");
system_deps::Config::new().probe()?;
detect_fedora_feature()?; detect_fedora_feature()?;
Ok(()) Ok(())
} }