From 9bd36dfdd40996947e913c520d1d08a53e562cbe Mon Sep 17 00:00:00 2001 From: Stefano Garzarella Date: Thu, 30 Jan 2025 11:52:43 +0100 Subject: [PATCH] libtcgtpm: use bindgen in build.rs instead of bindgen-cli in Makefile We had several issues in our CI because we forgot to install `bindgen-cli` used in the libtcgtpm/Makefile to generate the TPM bindings. Instead of using `bindgen-cli` (and then having as a requirement to have it installed), let's use `bindgen` in build.rs, this way the dependency is handled directly by cargo. Let's also clean up the references to `libtcgtpm/src/bindings.rs` since bindings were already generated in the OUT_DIR. Remove the reference to install `bindgen-cli` in the CI workflows, Documentation, and container image. Signed-off-by: Stefano Garzarella --- .github/workflows/rust.yml | 16 +- Cargo.lock | 176 +++++++++++++++++++-- Documentation/docs/installation/INSTALL.md | 1 - libtcgtpm/Cargo.toml | 3 + libtcgtpm/Makefile | 10 +- libtcgtpm/build.rs | 18 ++- scripts/container/opensuse-rust.docker | 3 +- 7 files changed, 188 insertions(+), 39 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5ebc37611..91f88d50e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -28,12 +28,6 @@ jobs: override: true components: rustfmt, rust-src, clippy - - name: Install bindgen-cli - uses: actions-rs/cargo@v1 - with: - command: install - args: bindgen-cli - - name: Install TPM 2.0 Reference Implementation build dependencies run: sudo apt install -y autoconf autoconf-archive pkg-config build-essential automake @@ -112,13 +106,6 @@ jobs: override: true components: rustfmt - # The bindings.rs is auto-generated during make, but we are not - # building the code with nightly. So we initialize bindings.rs here - # for cargo-fmt in the next workflow, otherwise it will fail reporting - # that bindings.rs does not exist. - - name: Touch libtcgtpm bindings - run: echo "" > libtcgtpm/src/bindings.rs - # release/src/git_version.rs is auto-generated via a build.rs file. Touch # it here to avoid CI failures. - name: Touch release/src/git_version.rs @@ -154,6 +141,9 @@ jobs: override: true components: rustfmt, rust-src, clippy + # TODO: remove this step after merging https://github.com/coconut-svsm/svsm/pull/606 + # This is still required because here we checkout the `base` branch that + # still requires `bindgen-cli` to build libtcgtpm - name: Install bindgen-cli uses: actions-rs/cargo@v1 with: diff --git a/Cargo.lock b/Cargo.lock index eb5a353fb..665e2fdb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,15 @@ dependencies = [ "subtle", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.18" @@ -113,6 +122,26 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" +[[package]] +name = "bindgen" +version = "0.71.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f58bf3d7db68cfbac37cfc485a8d711e87e064c3d0fe0435b92f7a407f9d6b3" +dependencies = [ + "bitflags 2.6.0", + "cexpr", + "clang-sys", + "itertools", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.96", +] + [[package]] name = "bitfield-struct" version = "0.6.2" @@ -121,7 +150,7 @@ checksum = "adc0846593a56638b74e136a45610f9934c052e14761bebca6b092d5522599e3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -132,7 +161,7 @@ checksum = "6c2ce686adbebce0ee484a502c440b4657739adbad65eadf06d64f5816ee9765" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -198,6 +227,15 @@ dependencies = [ "shlex", ] +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -214,6 +252,17 @@ dependencies = [ "inout", ] +[[package]] +name = "clang-sys" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "clap" version = "4.5.21" @@ -245,7 +294,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -342,7 +391,7 @@ checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -371,6 +420,12 @@ dependencies = [ "spki", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "elf" version = "0.1.0" @@ -465,6 +520,12 @@ dependencies = [ "polyval", ] +[[package]] +name = "glob" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" + [[package]] name = "group" version = "0.13.0" @@ -601,6 +662,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "jobserver" version = "0.1.32" @@ -626,9 +696,22 @@ dependencies = [ "cc", ] +[[package]] +name = "libloading" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" +dependencies = [ + "cfg-if", + "windows-targets", +] + [[package]] name = "libtcgtpm" version = "0.1.0" +dependencies = [ + "bindgen", +] [[package]] name = "log" @@ -642,6 +725,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ca88d725a0a943b096803bd34e73a4437208b6077654cc4ecb2947a5f91618d" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "memmap2" version = "0.7.1" @@ -660,6 +749,22 @@ dependencies = [ "autocfg", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -698,7 +803,7 @@ checksum = "8d1296fab5231654a5aec8bf9e87ba4e3938c502fc4c3c0425a00084c78944be" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -765,6 +870,16 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "prettyplease" +version = "0.2.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac" +dependencies = [ + "proc-macro2", + "syn 2.0.96", +] + [[package]] name = "prettyplease_verus" version = "0.1.15" @@ -785,9 +900,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" dependencies = [ "unicode-ident", ] @@ -816,6 +931,35 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cc2191ec1fd850e3ede4cf09ccfd40a33df561111f73e96e1b7c3f9eee31328" +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "release" version = "0.1.0" @@ -830,6 +974,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "rustc-hash" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" + [[package]] name = "rustc_version" version = "0.4.1" @@ -989,9 +1139,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.87" +version = "2.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" dependencies = [ "proc-macro2", "quote", @@ -1048,7 +1198,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -1070,7 +1220,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -1278,7 +1428,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] @@ -1289,7 +1439,7 @@ checksum = "593e7c96176495043fcb9e87cf7659f4d18679b5bab6b92bdef359c76a7795dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.96", ] [[package]] diff --git a/Documentation/docs/installation/INSTALL.md b/Documentation/docs/installation/INSTALL.md index 9b99c285b..cc7577312 100644 --- a/Documentation/docs/installation/INSTALL.md +++ b/Documentation/docs/installation/INSTALL.md @@ -208,7 +208,6 @@ Then checkout the SVSM repository and build the SVSM binary: $ git clone https://github.com/coconut-svsm/svsm $ cd svsm $ git submodule update --init -$ cargo install bindgen-cli ``` That checks out the SVSM which can be built by diff --git a/libtcgtpm/Cargo.toml b/libtcgtpm/Cargo.toml index 2b513531b..5f633d582 100644 --- a/libtcgtpm/Cargo.toml +++ b/libtcgtpm/Cargo.toml @@ -5,5 +5,8 @@ edition = "2021" [dependencies] +[build-dependencies] +bindgen = "0.71.0" + [lints] workspace = true diff --git a/libtcgtpm/Makefile b/libtcgtpm/Makefile index 2c25da918..26e003448 100644 --- a/libtcgtpm/Makefile +++ b/libtcgtpm/Makefile @@ -28,7 +28,7 @@ LIBS = $(LIBCRT) $(LIBCRYPTO) $(LIBTPM) $(LIBPLATFORM) OUT_DIR ?= $(CWD) -all: $(OUT_DIR)/libtcgtpm.a $(OUT_DIR)/bindings.rs +all: $(OUT_DIR)/libtcgtpm.a $(OUT_DIR)/libtcgtpm.a: $(LIBS) rm -f $@ @@ -134,19 +134,11 @@ $(TCGTPM_MAKEFILE): LIBCRYPTO_LIBS="$(LIBCRT) $(LIBCRYPTO)" \ LIBCRYPTO_CFLAGS="${TCGTPM_CFLAGS}") -# bindings.rs -BINDGEN_FLAGS = --use-core -CLANG_FLAGS = -Wno-incompatible-library-redeclaration - -$(OUT_DIR)/bindings.rs: deps/libtcgtpm.h $(LIBTPM) - bindgen $(BINDGEN_FLAGS) --output $@ deps/libtcgtpm.h -- $(CLANG_FLAGS) - clean: $(OPENSSL_MAKEFILE) $(TCGTPM_MAKEFILE) make -C $(LIBCRT_DIR) clean make -C $(OPENSSL_DIR) clean make -C $(TCGTPM_DIR) clean rm -f libtcgtpm.a - rm -f src/bindings.rs distclean: clean rm -f $(OPENSSL_MAKEFILE) diff --git a/libtcgtpm/build.rs b/libtcgtpm/build.rs index 31fe6a941..7424b9c2c 100644 --- a/libtcgtpm/build.rs +++ b/libtcgtpm/build.rs @@ -5,6 +5,7 @@ // Authors: Claudio Carvalho use std::env::current_dir; +use std::path::PathBuf; use std::process::Command; use std::process::Stdio; @@ -17,8 +18,23 @@ fn main() { .unwrap(); assert!(status.success()); - // Tell cargo to link libtcgtpm and where to find it. let out_dir = std::env::var("OUT_DIR").unwrap(); + let out_path = PathBuf::from(out_dir.clone()); + + let bindings = bindgen::Builder::default() + .header("deps/libtcgtpm.h".to_string()) + .allowlist_file("deps/libtcgtpm.h") + .use_core() + .clang_arg("-Wno-incompatible-library-redeclaration") + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + .generate() + .unwrap_or_else(|_| panic!("Unable to generate bindings for deps/libtcgtpm.h")); + + bindings + .write_to_file(out_path.join("bindings.rs")) + .unwrap_or_else(|_| panic!("Unable to write bindings.rs")); + + // Tell cargo to link libtcgtpm and where to find it. println!("cargo:rustc-link-search={out_dir}"); println!("cargo:rustc-link-lib=tcgtpm"); diff --git a/scripts/container/opensuse-rust.docker b/scripts/container/opensuse-rust.docker index 90154c0e1..04e43e360 100644 --- a/scripts/container/opensuse-rust.docker +++ b/scripts/container/opensuse-rust.docker @@ -36,5 +36,4 @@ ENV PATH="${PATH}:${CARGO_HOME}/bin" RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > /tmp/rustup-init.sh && \ sh /tmp/rustup-init.sh -y \ --default-toolchain stable-x86_64-unknown-linux-gnu \ - --target x86_64-unknown-none && \ - cargo install bindgen-cli + --target x86_64-unknown-none