Skip to content

Commit e1c4b9f

Browse files
committed
improve build-rerun condition
Signed-off-by: Jun Kimura <junkxdev@gmail.com>
1 parent 25ff4a7 commit e1c4b9f

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

sgx-build/src/lib.rs

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -94,39 +94,6 @@ pub struct SgxBuilder {
9494
}
9595

9696
impl SgxBuilder {
97-
/// Get the target directory for EDL artifacts
98-
fn get_edl_target_dir() -> PathBuf {
99-
// First, try CARGO_TARGET_DIR which is explicitly set
100-
if let Ok(target_dir) = env::var("CARGO_TARGET_DIR") {
101-
return PathBuf::from(target_dir).join("edl");
102-
}
103-
104-
// Otherwise, require OUT_DIR to be set (should always be set in build.rs context)
105-
let out_dir = env::var("OUT_DIR")
106-
.expect("OUT_DIR not set. This function should only be called from build.rs");
107-
108-
let out_path = PathBuf::from(out_dir);
109-
110-
// Find the target directory by looking for a directory named "target"
111-
// while traversing up the directory tree
112-
let mut current_dir = out_path.as_path();
113-
loop {
114-
if let Some(file_name) = current_dir.file_name() {
115-
if file_name == "target" {
116-
return current_dir.join("edl");
117-
}
118-
}
119-
120-
match current_dir.parent() {
121-
Some(parent) => current_dir = parent,
122-
None => panic!(
123-
"Could not find 'target' directory in OUT_DIR path: {}",
124-
out_path.display()
125-
),
126-
}
127-
}
128-
}
129-
13097
/// Create a new EnclaveBuilder with default settings from environment
13198
pub fn new() -> Self {
13299
let sgx_sdk = env::var("SGX_SDK").unwrap_or_else(|_| "/opt/intel/sgxsdk".to_string());
@@ -141,8 +108,7 @@ impl SgxBuilder {
141108
"x64".to_string()
142109
}
143110
});
144-
let debug =
145-
env::var("SGX_DEBUG").is_ok() || env::var("DEBUG").is_ok() || cfg!(debug_assertions);
111+
let debug = env::var("SGX_DEBUG").unwrap_or_default() == "1" || cfg!(debug_assertions);
146112
let mitigation_cve_2020_0551 = match env::var("MITIGATION_CVE_2020_0551")
147113
.or_else(|_| env::var("MITIGATION-CVE-2020-0551"))
148114
{
@@ -164,6 +130,51 @@ impl SgxBuilder {
164130
}
165131
}
166132

133+
/// Print cargo rerun-if-env-changed for common environment variables
134+
fn print_common_env_rerun() {
135+
println!("cargo:rerun-if-env-changed=SGX_SDK");
136+
println!("cargo:rerun-if-env-changed=SGX_MODE");
137+
println!("cargo:rerun-if-env-changed=SGX_ARCH");
138+
println!("cargo:rerun-if-env-changed=SGX_DEBUG");
139+
println!("cargo:rerun-if-env-changed=MITIGATION_CVE_2020_0551");
140+
println!("cargo:rerun-if-env-changed=MITIGATION-CVE-2020-0551");
141+
}
142+
143+
/// Get the target directory for EDL artifacts
144+
fn get_edl_target_dir() -> PathBuf {
145+
// First, try CARGO_TARGET_DIR which is explicitly set
146+
if let Ok(target_dir) = env::var("CARGO_TARGET_DIR") {
147+
println!("cargo:rerun-if-env-changed=CARGO_TARGET_DIR");
148+
return PathBuf::from(target_dir).join("edl");
149+
}
150+
151+
// Otherwise, require OUT_DIR to be set (should always be set in build.rs context)
152+
println!("cargo:rerun-if-env-changed=OUT_DIR");
153+
let out_dir = env::var("OUT_DIR")
154+
.expect("OUT_DIR not set. This function should only be called from build.rs");
155+
156+
let out_path = PathBuf::from(out_dir);
157+
158+
// Find the target directory by looking for a directory named "target"
159+
// while traversing up the directory tree
160+
let mut current_dir = out_path.as_path();
161+
loop {
162+
if let Some(file_name) = current_dir.file_name() {
163+
if file_name == "target" {
164+
return current_dir.join("edl");
165+
}
166+
}
167+
168+
match current_dir.parent() {
169+
Some(parent) => current_dir = parent,
170+
None => panic!(
171+
"Could not find 'target' directory in OUT_DIR path: {}",
172+
out_path.display()
173+
),
174+
}
175+
}
176+
}
177+
167178
/// Detect GCC version
168179
fn detect_gcc_version() -> Option<(u32, u32, u32)> {
169180
let output = Command::new("gcc").arg("--version").output().ok()?;
@@ -233,6 +244,7 @@ impl SgxBuilder {
233244

234245
// Add additional search paths if needed
235246
if let Ok(sgx_edl_search_paths) = env::var("SGX_EDL_SEARCH_PATHS") {
247+
println!("cargo:rerun-if-env-changed=SGX_EDL_SEARCH_PATHS");
236248
for path in sgx_edl_search_paths.split(':') {
237249
cmd.args(["--search-path", path]);
238250
}
@@ -507,6 +519,9 @@ impl SgxBuilder {
507519
// Tell cargo to rerun if EDL changes
508520
println!("cargo:rerun-if-changed={}", edl_path.display());
509521

522+
// Tell cargo to rerun if environment variables change
523+
Self::print_common_env_rerun();
524+
510525
Ok(())
511526
}
512527

@@ -556,6 +571,9 @@ impl SgxBuilder {
556571
// Tell cargo to rerun if EDL changes
557572
println!("cargo:rerun-if-changed={}", edl_path.display());
558573

574+
// Tell cargo to rerun if environment variables change
575+
Self::print_common_env_rerun();
576+
559577
Ok(())
560578
}
561579

0 commit comments

Comments
 (0)