From 2d918acca5d5e541f4c9a62af3957c8a793c2f2f Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Mon, 28 Oct 2024 09:08:54 +0000 Subject: [PATCH 1/5] libmstpm: store generated bindings in OUT_DIR Generated code shouldn't be stored directly in the src directory. All files generated by build-scripts should be stored in the directory pointed to in the OUT_DIR environment variable. One advantage of this is that we can tell the build-script to emit rerun-if statements that tell cargo to only rerun if any of the files in this directory have changed. If we generate files into this directory, cargo may not always realize that the generated files shouldn't trigger a re-run. Signed-off-by: Tom Dohrmann --- libmstpm/.gitignore | 1 - libmstpm/Makefile | 13 +++++-------- libmstpm/src/lib.rs | 10 +++++++++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libmstpm/.gitignore b/libmstpm/.gitignore index 5fbcfbb2f..e9a3e9fb1 100644 --- a/libmstpm/.gitignore +++ b/libmstpm/.gitignore @@ -1,2 +1 @@ libmstpm.a -src/bindings.rs diff --git a/libmstpm/Makefile b/libmstpm/Makefile index e78ff0870..162a9a501 100644 --- a/libmstpm/Makefile +++ b/libmstpm/Makefile @@ -26,7 +26,9 @@ MSTPM_MAKEFILE = $(MSTPM_DIR)/Makefile LIBS = $(LIBCRT) $(LIBCRYPTO) $(LIBTPM) $(LIBPLATFORM) -all: libmstpm.a src/bindings.rs +OUT_DIR ?= $(CWD) + +all: libmstpm.a $(OUT_DIR)/bindings.rs libmstpm.a: $(LIBS) rm -f $@ @@ -123,13 +125,8 @@ $(MSTPM_MAKEFILE): BINDGEN_FLAGS = --use-core CLANG_FLAGS = -Wno-incompatible-library-redeclaration -src/bindings.rs: deps/libmstpm.h $(LIBTPM) - echo "#![allow(non_upper_case_globals)]" > $@ - echo "#![allow(non_camel_case_types)]" >> $@ - echo "#![allow(non_snake_case)]" >> $@ - echo "#![allow(unused)]" >> $@ - echo "#![allow(improper_ctypes)]" >> $@ - bindgen $(BINDGEN_FLAGS) deps/libmstpm.h -- $(CLANG_FLAGS) >> $@ +$(OUT_DIR)/bindings.rs: deps/libmstpm.h $(LIBTPM) + bindgen $(BINDGEN_FLAGS) --output $@ deps/libmstpm.h -- $(CLANG_FLAGS) clean: $(OPENSSL_MAKEFILE) $(MSTPM_MAKEFILE) make -C $(LIBCRT_DIR) clean diff --git a/libmstpm/src/lib.rs b/libmstpm/src/lib.rs index 55cfd93cd..e8954b1e8 100644 --- a/libmstpm/src/lib.rs +++ b/libmstpm/src/lib.rs @@ -10,4 +10,12 @@ #![no_std] /// C bindings -pub mod bindings; +pub mod bindings { + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![allow(unused)] + #![allow(improper_ctypes)] + + include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +} From 922106df7ea477eba94e769cf5fb159fe125fff6 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Mon, 28 Oct 2024 09:30:59 +0000 Subject: [PATCH 2/5] kernel: move linker args in build.rs to libmstpm Linker args shouldn't be set by the crate consuming a library, but by the crate producing a library. Signed-off-by: Tom Dohrmann --- kernel/build.rs | 4 ---- libmstpm/build.rs | 7 +++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/kernel/build.rs b/kernel/build.rs index 2a43d34fb..526b10203 100644 --- a/kernel/build.rs +++ b/kernel/build.rs @@ -21,10 +21,6 @@ fn main() { println!("cargo:rustc-link-arg-bin=svsm=--no-relax"); println!("cargo:rustc-link-arg-bin=svsm=-Tkernel/src/svsm.lds"); println!("cargo:rustc-link-arg-bin=svsm=-no-pie"); - if std::env::var("CARGO_FEATURE_MSTPM").is_ok() && std::env::var("CARGO_CFG_TEST").is_err() { - println!("cargo:rustc-link-arg-bin=svsm=-Llibmstpm"); - println!("cargo:rustc-link-arg-bin=svsm=-lmstpm"); - } // Extra linker args for tests. println!("cargo:rerun-if-env-changed=LINK_TEST"); diff --git a/libmstpm/build.rs b/libmstpm/build.rs index b096b0428..b9f5e6b60 100644 --- a/libmstpm/build.rs +++ b/libmstpm/build.rs @@ -4,6 +4,7 @@ // // Authors: Claudio Carvalho +use std::env::current_dir; use std::process::Command; use std::process::Stdio; @@ -17,4 +18,10 @@ fn main() { if !output.status.success() { panic!(); } + + // Tell cargo to link libmstpm and where to find it. + let cwd = current_dir().unwrap(); + let cwd = cwd.as_os_str().to_str().unwrap(); + println!("cargo:rustc-link-search={cwd}"); + println!("cargo:rustc-link-lib=mstpm"); } From 5f4987a1967c706ea832fa208783078b827a683b Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Mon, 28 Oct 2024 09:32:21 +0000 Subject: [PATCH 3/5] libmstpm: store library in OUT_DIR Much like the generated Rust bindings, the built static library should reside in OUT_DIR. Signed-off-by: Tom Dohrmann --- libmstpm/.gitignore | 1 - libmstpm/Makefile | 4 ++-- libmstpm/build.rs | 6 ++---- 3 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 libmstpm/.gitignore diff --git a/libmstpm/.gitignore b/libmstpm/.gitignore deleted file mode 100644 index e9a3e9fb1..000000000 --- a/libmstpm/.gitignore +++ /dev/null @@ -1 +0,0 @@ -libmstpm.a diff --git a/libmstpm/Makefile b/libmstpm/Makefile index 162a9a501..5597b78cb 100644 --- a/libmstpm/Makefile +++ b/libmstpm/Makefile @@ -28,9 +28,9 @@ LIBS = $(LIBCRT) $(LIBCRYPTO) $(LIBTPM) $(LIBPLATFORM) OUT_DIR ?= $(CWD) -all: libmstpm.a $(OUT_DIR)/bindings.rs +all: $(OUT_DIR)/libmstpm.a $(OUT_DIR)/bindings.rs -libmstpm.a: $(LIBS) +$(OUT_DIR)/libmstpm.a: $(LIBS) rm -f $@ ar rcsTPD $@ $^ diff --git a/libmstpm/build.rs b/libmstpm/build.rs index b9f5e6b60..73b1e7502 100644 --- a/libmstpm/build.rs +++ b/libmstpm/build.rs @@ -4,7 +4,6 @@ // // Authors: Claudio Carvalho -use std::env::current_dir; use std::process::Command; use std::process::Stdio; @@ -20,8 +19,7 @@ fn main() { } // Tell cargo to link libmstpm and where to find it. - let cwd = current_dir().unwrap(); - let cwd = cwd.as_os_str().to_str().unwrap(); - println!("cargo:rustc-link-search={cwd}"); + let out_dir = std::env::var("OUT_DIR").unwrap(); + println!("cargo:rustc-link-search={out_dir}"); println!("cargo:rustc-link-lib=mstpm"); } From b15e4b7a8b4fa0c1bafd4fbdc4911313aa247fcf Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Mon, 28 Oct 2024 09:33:55 +0000 Subject: [PATCH 4/5] libmstpm: add rerun-if-changed output to build.rs This tell cargo to rerun the build-script if any of the files in this directory have changed. Signed-off-by: Tom Dohrmann --- libmstpm/build.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libmstpm/build.rs b/libmstpm/build.rs index 73b1e7502..feb9a686c 100644 --- a/libmstpm/build.rs +++ b/libmstpm/build.rs @@ -4,6 +4,7 @@ // // Authors: Claudio Carvalho +use std::env::current_dir; use std::process::Command; use std::process::Stdio; @@ -22,4 +23,10 @@ fn main() { let out_dir = std::env::var("OUT_DIR").unwrap(); println!("cargo:rustc-link-search={out_dir}"); println!("cargo:rustc-link-lib=mstpm"); + + // Tell cargo not to rerun the build-script unless anything in this + // directory changes. + let cwd = current_dir().unwrap(); + let cwd = cwd.as_os_str().to_str().unwrap(); + println!("cargo:rerun-if-changed={cwd}"); } From d29f6bf8790b0c822631612fcc22a37d800a8464 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Mon, 28 Oct 2024 09:35:33 +0000 Subject: [PATCH 5/5] libmstpm: simplify make invocation We don't care about the output, we only care about the status. Signed-off-by: Tom Dohrmann --- libmstpm/build.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libmstpm/build.rs b/libmstpm/build.rs index feb9a686c..539b6a691 100644 --- a/libmstpm/build.rs +++ b/libmstpm/build.rs @@ -9,15 +9,13 @@ use std::process::Command; use std::process::Stdio; fn main() { - let output = Command::new("make") + // Build libmstpm. + let status = Command::new("make") .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) - .output() + .status() .unwrap(); - - if !output.status.success() { - panic!(); - } + assert!(status.success()); // Tell cargo to link libmstpm and where to find it. let out_dir = std::env::var("OUT_DIR").unwrap();