Blob Blame History Raw
From 603f7e1d6ca4b0dffdb0275550a75e67d7a04623 Mon Sep 17 00:00:00 2001
From: Jonathan Lebon <jonathan@jlebon.com>
Date: Thu, 11 Oct 2018 14:16:02 -0400
Subject: [PATCH] Backport f50f9e8d to v2018.8 for RHEL8

We don't want to use cbindgen there, we want to use the vendored
rpmostree-rust.h.
---
 Makefile-rpm-ostree.am               | 22 +++++++++++++++++++---
 bindgen/.gitignore                   |  2 ++
 bindgen/Cargo.toml                   | 12 ++++++++++++
 rust/build.rs => bindgen/src/main.rs | 10 ++++++----
 configure.ac                         | 12 ++++++++++++
 packaging/make-git-snapshot.sh       |  7 +++++++
 rust/Cargo.toml                      |  4 ----
 7 files changed, 58 insertions(+), 11 deletions(-)
 create mode 100644 bindgen/.gitignore
 create mode 100644 bindgen/Cargo.toml
 rename rust/build.rs => bindgen/src/main.rs (61%)

diff --git a/Makefile-rpm-ostree.am b/Makefile-rpm-ostree.am
index 50a57e8e..9198961b 100644
--- a/Makefile-rpm-ostree.am
+++ b/Makefile-rpm-ostree.am
@@ -82,6 +82,16 @@ if BUILDOPT_NEW_NAME
 INSTALL_DATA_HOOKS += install-bin-hook
 endif
 
+if !HAVE_PREBUILT_CBINDGEN
+if !HAVE_EXTERNAL_CBINDGEN
+rpmostree-bindgen: bindgen/Cargo.toml bindgen/src/main.rs
+	cd $(top_srcdir)/bindgen && \
+	export CARGO_TARGET_DIR=@abs_top_builddir@/bindgen-target && \
+	$(cargo) build --verbose && \
+	ln -sf $${CARGO_TARGET_DIR}/debug/rpmostree-bindgen $(abs_top_builddir)/rpmostree-bindgen
+endif
+endif
+
 librpmostree_rust_path = @abs_top_builddir@/target/@RUST_TARGET_SUBDIR@/librpmostree_rust.a
 # If the target directory exists, use --frozen; we don't
 # want to (by default) touch the Internet during builds here.
@@ -95,11 +105,17 @@ $(librpmostree_rust_path): Makefile $(LIBRPMOSTREE_RUST_SRCS)
 	  $(cargo) build --verbose $${frozen} $(CARGO_RELEASE_ARGS)
 EXTRA_DIST += $(LIBRPMOSTREE_RUST_SRCS) rust/Cargo.lock
 
-# See rust/build.rs - it uses cbindgen as part of the Rust build
+# Generate bindings from Rust to C
+if !HAVE_PREBUILT_CBINDGEN
+if HAVE_EXTERNAL_CBINDGEN
 rpmostree-rust.h: $(librpmostree_rust_path)
-	$(AM_V_GEN) src=$$(find @abs_top_builddir@/target/@RUST_TARGET_SUBDIR@/ -name 'rpmostree-rust.h') && \
-	  test -n "$${src}" && ln -sfr "$${src}" rpmostree-rust.h
+	$(AM_V_GEN) cbindgen -c rust/cbindgen.toml -o $@ $(top_srcdir)/rust
+else
+rpmostree-rust.h: $(librpmostree_rust_path) rpmostree-bindgen
+	$(AM_V_GEN) ./rpmostree-bindgen $(top_srcdir)/rust
+endif
 BUILT_SOURCES += rpmostree-rust.h
+endif
 
 rpm_ostree_CFLAGS += $(PKGDEP_RPMOSTREE_RS_CFLAGS)
 rpm_ostree_LDADD += $(librpmostree_rust_path) $(PKGDEP_RPMOSTREE_RS_LIBS)
diff --git a/bindgen/.gitignore b/bindgen/.gitignore
new file mode 100644
index 00000000..fa8d85ac
--- /dev/null
+++ b/bindgen/.gitignore
@@ -0,0 +1,2 @@
+Cargo.lock
+target
diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml
new file mode 100644
index 00000000..5a9e09ef
--- /dev/null
+++ b/bindgen/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "rpmostree-bindgen"
+version = "0.1.0"
+authors = ["Colin Walters <walters@verbum.org>"]
+
+[dependencies]
+cbindgen = "0.6.3"
+
+# Might as well keep these from the main Rust source
+[profile.release]
+panic = "abort"
+debug = true
diff --git a/rust/build.rs b/bindgen/src/main.rs
similarity index 61%
rename from rust/build.rs
rename to bindgen/src/main.rs
index b2e487ee..502a021d 100644
--- a/rust/build.rs
+++ b/bindgen/src/main.rs
@@ -4,12 +4,14 @@
 extern crate cbindgen;
 
 fn run() -> Result<(), String> {
-    let out_dir_v = std::env::var("OUT_DIR").expect("OUT_DIR is unset");
-    let out_dir = std::path::Path::new(&out_dir_v);
-    let bindings = cbindgen::generate(std::path::Path::new(".")).map_err(|e| e.to_string())?;
+    let args: Vec<String> = std::env::args().collect();
+    let rustdir = std::path::Path::new(&args[1]);
+    let out = std::path::Path::new("rpmostree-rust.h");
+
+    let bindings = cbindgen::generate(rustdir).map_err(|e| e.to_string())?;
     // This uses unwraps internally; it'd be good to submit a patch
     // to add a Result-based API.
-    bindings.write_to_file(out_dir.join("rpmostree-rust.h"));
+    bindings.write_to_file(out);
     Ok(())
 }
 
diff --git a/configure.ac b/configure.ac
index 89cb2add..3d768e7d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -213,6 +213,17 @@ AS_IF([test -z "$cargo"], [AC_MSG_ERROR([cargo is required for --enable-rust])])
 AC_PATH_PROG([rustc], [rustc])
 AS_IF([test -z "$rustc"], [AC_MSG_ERROR([rustc is required for --enable-rust])])
 
+dnl https://github.com/projectatomic/rpm-ostree/pull/1573
+dnl We support 3 modes for bindgen:
+dnl  - External /usr/bin/cbindgen (Fedora + CI)
+dnl  - Downloading it from crates.io (CentOS + CI builds)
+dnl  - Not running it at all, and using a pre-built rpmostree-rust.h (Koji)
+AS_IF([test -d "${srcdir}"/rust/vendor],
+      [cbindgen=dist],
+      [AC_PATH_PROG([cbindgen], [cbindgen])])
+AM_CONDITIONAL(HAVE_PREBUILT_CBINDGEN, [test "${cbindgen}" = "dist"])
+AM_CONDITIONAL(HAVE_EXTERNAL_CBINDGEN, [test -n "${cbindgen}" && test "${cbindgen}" != dist])
+
 AC_MSG_CHECKING(whether to build in debug mode)
 debug_release=release
 if $(echo $CFLAGS |grep -q -E "(-O0|-Og)"); then
@@ -279,4 +290,5 @@ echo "
     bubblewrap:                              $with_bubblewrap
     gtk-doc:                                 $enable_gtk_doc
     rust:                                    $rust_debug_release
+    cbindgen:                                ${cbindgen:-internal}
 "
diff --git a/packaging/make-git-snapshot.sh b/packaging/make-git-snapshot.sh
index bbcf0092..da285e8c 100755
--- a/packaging/make-git-snapshot.sh
+++ b/packaging/make-git-snapshot.sh
@@ -47,4 +47,11 @@ mkdir ${tmpd} && touch ${tmpd}/.tmp
  cp ${TOP}/rust/cargo-vendor-config .cargo/config
  tar --transform="s,^,${PKG_VER}/rust/," -rf ${TARFILE_TMP} * .cargo/
  )
+
+# And finally, vendor rpmostree-rust.h; it's generated by cbindgen,
+# and it's easier than vendoring all of the source for that too.
+# This is currently the *only* generated file we treat this way. See also
+# https://github.com/projectatomic/rpm-ostree/pull/1573
+(cd ${srcdir} && tar --transform "s,^,${PKG_VER}/," -rf ${TARFILE_TMP} rpmostree-rust.h)
+
 mv ${TARFILE_TMP} ${TARFILE}
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index ee95b012..258da5fa 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -2,7 +2,6 @@
 name = "rpmostree-rust"
 version = "0.1.0"
 authors = ["Colin Walters <walters@verbum.org>"]
-build = "build.rs"
 
 [dependencies]
 serde = "1.0"
@@ -17,9 +16,6 @@ tempfile = "3.0.3"
 openat = "0.1.15"
 curl = "0.4.14"
 
-[build-dependencies]
-cbindgen = "0.6.3"
-
 [lib]
 name = "rpmostree_rust"
 path = "src/lib.rs"
-- 
2.17.1