This is an automated email from the ASF dual-hosted git repository.

ivila pushed a commit to branch main
in repository 
https://gitbox.apache.org/repos/asf/incubator-teaclave-trustzone-sdk.git

commit 1f59a349fa4543133fbcf3594bc804053a754fc8
Author: ivila <[email protected]>
AuthorDate: Tue May 6 17:01:31 2025 +0800

    optee-utee: sys: use standard feature flag for conditional linking.
    
    Signed-off-by: Zehui Chen <[email protected]>
    Acked-by: Yuan Zhuang <[email protected]>
---
 optee-utee/Cargo.toml                |  3 +++
 optee-utee/optee-utee-sys/Cargo.toml |  4 ++++
 optee-utee/optee-utee-sys/build.rs   | 29 ++++++++++++++++-------------
 3 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/optee-utee/Cargo.toml b/optee-utee/Cargo.toml
index 7e09e39..0bc7a9d 100644
--- a/optee-utee/Cargo.toml
+++ b/optee-utee/Cargo.toml
@@ -37,9 +37,12 @@ rand = "0.8.5"
 once_cell = "1.20.2"
 serde = { version = "1.0.215" }
 serde_json = { version = "1.0.133" }
+# disable linking when running unit tests
+optee-utee-sys = { version = "0.4.0", path = "optee-utee-sys", features = 
["no_link"] }
 
 [features]
 no_panic_handler = []
 
 [workspace]
+resolver = "2"
 members = ['systest']
diff --git a/optee-utee/optee-utee-sys/Cargo.toml 
b/optee-utee/optee-utee-sys/Cargo.toml
index a650074..1dbb7b9 100644
--- a/optee-utee/optee-utee-sys/Cargo.toml
+++ b/optee-utee/optee-utee-sys/Cargo.toml
@@ -27,3 +27,7 @@ links = "utee"
 
 [dependencies]
 libc = "0.2"
+
+[features]
+default=[]
+no_link=[]
diff --git a/optee-utee/optee-utee-sys/build.rs 
b/optee-utee/optee-utee-sys/build.rs
index 5a7233d..6295d55 100644
--- a/optee-utee/optee-utee-sys/build.rs
+++ b/optee-utee/optee-utee-sys/build.rs
@@ -15,26 +15,29 @@
 // specific language governing permissions and limitations
 // under the License.
 
-use std::env;
+use std::env::{self, VarError};
 use std::path::Path;
 
-fn main() {
-    const ENV_SYS_BUILD_TYPE: &str = "SYS_BUILD_TYPE";
-    println!("cargo:rerun-if-env-changed={}", ENV_SYS_BUILD_TYPE);
-
-    let build_type = 
env::var(ENV_SYS_BUILD_TYPE).unwrap_or(String::from("")).to_lowercase();
-    match build_type.as_str() {
-        "unit_test" => unit_test_build(),
-        _ => production_build(),
+fn main() -> Result<(), VarError> {
+    if !is_feature_enable("no_link")? {
+        link();
     }
+    Ok(())
 }
 
-// this allow developers to run unit tests in host machine by
-// SYS_BUILD_TYPE=unit_test cargo test --lib --features no_panic_handler
-fn unit_test_build() {
+// Check if feature enabled.
+// Refer to: 
https://doc.rust-lang.org/cargo/reference/features.html#build-scripts
+fn is_feature_enable(feature: &str) -> Result<bool, VarError> {
+    let feature_env = format!("CARGO_FEATURE_{}", 
feature.to_uppercase().replace("-", "_"));
+
+    match env::var(feature_env) {
+        Err(VarError::NotPresent) => Ok(false),
+        Ok(_) => Ok(true),
+        Err(err) => Err(err),
+    }
 }
 
-fn production_build() {
+fn link() {
     let optee_os_dir = env::var("TA_DEV_KIT_DIR").unwrap();
     let search_path = Path::new(&optee_os_dir).join("lib");
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to