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/.gitignore b/libmstpm/.gitignore deleted file mode 100644 index 5fbcfbb2f..000000000 --- a/libmstpm/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -libmstpm.a -src/bindings.rs diff --git a/libmstpm/Makefile b/libmstpm/Makefile index e78ff0870..5597b78cb 100644 --- a/libmstpm/Makefile +++ b/libmstpm/Makefile @@ -26,9 +26,11 @@ MSTPM_MAKEFILE = $(MSTPM_DIR)/Makefile LIBS = $(LIBCRT) $(LIBCRYPTO) $(LIBTPM) $(LIBPLATFORM) -all: libmstpm.a src/bindings.rs +OUT_DIR ?= $(CWD) -libmstpm.a: $(LIBS) +all: $(OUT_DIR)/libmstpm.a $(OUT_DIR)/bindings.rs + +$(OUT_DIR)/libmstpm.a: $(LIBS) rm -f $@ ar rcsTPD $@ $^ @@ -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/build.rs b/libmstpm/build.rs index b096b0428..539b6a691 100644 --- a/libmstpm/build.rs +++ b/libmstpm/build.rs @@ -4,17 +4,27 @@ // // Authors: Claudio Carvalho +use std::env::current_dir; 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(); + assert!(status.success()); - if !output.status.success() { - panic!(); - } + // Tell cargo to link libmstpm and where to find it. + 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}"); } 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")); +}