-
Notifications
You must be signed in to change notification settings - Fork 155
devices: build init binary as part of build script #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,54 @@ | ||
| use std::ffi::OsStr; | ||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
|
|
||
| fn build_default_init() -> PathBuf { | ||
| let manifest_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); | ||
| let libkrun_root = manifest_dir.join("../.."); | ||
| let init_src = libkrun_root.join("init/init.c"); | ||
| let init_bin = libkrun_root.join("init/init"); | ||
|
|
||
| println!("cargo:rerun-if-env-changed=CC_LINUX"); | ||
| println!("cargo:rerun-if-env-changed=CC"); | ||
| println!("cargo:rerun-if-env-changed=TIMESYNC"); | ||
| println!("cargo:rerun-if-changed={}", init_src.display()); | ||
| println!( | ||
| "cargo:rerun-if-changed={}", | ||
| libkrun_root.join("init/jsmn.h").display() | ||
| ); | ||
|
|
||
| let mut init_cc_flags = vec!["-O2", "-static", "-Wall"]; | ||
| if std::env::var_os("TIMESYNC").as_deref() == Some(OsStr::new("1")) { | ||
| init_cc_flags.push("-D__TIMESYNC__"); | ||
| } | ||
|
Comment on lines
+20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This build script is missing the conditional compilation logic for SEV and TDX builds that was present in the Makefile. The Without this logic, the For example, for SEV, you need to:
A similar logic is needed for TDX.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems wrong. As per my understanding this was dead code, see reasoning in a68a626, but let me know if I missed something. |
||
|
|
||
| let cc_value = std::env::var("CC_LINUX") | ||
| .or_else(|_| std::env::var("CC")) | ||
| .unwrap_or_else(|_| "cc".to_string()); | ||
| let mut cc_parts = cc_value.split_ascii_whitespace(); | ||
| let cc = cc_parts.next().expect("CC_LINUX/CC must not be empty"); | ||
| let status = Command::new(cc) | ||
| .args(cc_parts) | ||
| .args(&init_cc_flags) | ||
| .arg("-o") | ||
| .arg(&init_bin) | ||
| .arg(&init_src) | ||
| .status() | ||
| .unwrap_or_else(|e| panic!("failed to execute {cc}: {e}")); | ||
|
|
||
| if !status.success() { | ||
| panic!("failed to compile init/init.c: {status}"); | ||
| } | ||
| init_bin | ||
| } | ||
|
|
||
| fn main() { | ||
| let init_binary_path = std::env::var("KRUN_INIT_BINARY_PATH").unwrap_or_else(|_| { | ||
| format!( | ||
| "{}/../../init/init", | ||
| std::env::var("CARGO_MANIFEST_DIR").unwrap() | ||
| ) | ||
| }); | ||
| println!("cargo:rustc-env=KRUN_INIT_BINARY_PATH={init_binary_path}"); | ||
| let init_binary_path = std::env::var_os("KRUN_INIT_BINARY_PATH") | ||
| .map(PathBuf::from) | ||
| .unwrap_or_else(build_default_init); | ||
| println!( | ||
| "cargo:rustc-env=KRUN_INIT_BINARY_PATH={}", | ||
| init_binary_path.display() | ||
| ); | ||
| println!("cargo:rerun-if-env-changed=KRUN_INIT_BINARY_PATH"); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The downside of building in the build script and us storing stuff in the source tree is that
cargo cleanwon't remove the binary. I think we could:init/inithere from theMakefile(it's a bit odd to have two different components create/remove the file)initbinary intoOUT_DIR, which I think is probably the best approach. It would just mean adjustingKRUN_INIT_BINARY_PATHfrom the build script.Any preferences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer doing it the right way, so in my opinion we should do option 3.