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 5fac13aa31d8253c41133a242b182fe1ccae2c96
Author: ivila <[email protected]>
AuthorDate: Tue May 6 17:08:47 2025 +0800

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

diff --git a/optee-teec/Cargo.toml b/optee-teec/Cargo.toml
index e77c1d2..0d10e89 100644
--- a/optee-teec/Cargo.toml
+++ b/optee-teec/Cargo.toml
@@ -31,5 +31,10 @@ uuid = "0.7"
 hex = "0.3"
 num_enum = "0.7.3"
 
+[dev-dependencies]
+# disable linking when running unit tests
+optee-teec-sys = { version = "0.4.0", path = "optee-teec-sys", features = 
["no_link"] }
+
 [workspace]
+resolver = "2"
 members = ['systest']
diff --git a/optee-teec/optee-teec-sys/Cargo.toml 
b/optee-teec/optee-teec-sys/Cargo.toml
index 8ba1706..485ab5f 100644
--- a/optee-teec/optee-teec-sys/Cargo.toml
+++ b/optee-teec/optee-teec-sys/Cargo.toml
@@ -27,3 +27,7 @@ links = "teec"
 
 [dependencies]
 libc = "0.2.48" 
+
+[features]
+default=[]
+no_link=[]
diff --git a/optee-teec/optee-teec-sys/build.rs 
b/optee-teec/optee-teec-sys/build.rs
index c2e406e..afd9956 100644
--- a/optee-teec/optee-teec-sys/build.rs
+++ b/optee-teec/optee-teec-sys/build.rs
@@ -15,26 +15,34 @@
 // 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(even x86)
-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() {
-    let optee_client_dir = 
env::var("OPTEE_CLIENT_EXPORT").expect("OPTEE_CLIENT_EXPORT is not set");
+fn link() {
+    const ENV_OPTEE_CLIENT_EXPORT: &str = "OPTEE_CLIENT_EXPORT";
+    println!("cargo:rerun-if-env-changed={}", ENV_OPTEE_CLIENT_EXPORT);
+
+    let optee_client_dir =
+        env::var(ENV_OPTEE_CLIENT_EXPORT).expect("OPTEE_CLIENT_EXPORT is not 
set");
     let search_path = Path::new(&optee_client_dir).join("usr/lib");
     println!("cargo:rustc-link-search={}", search_path.display());
     println!("cargo:rustc-link-lib=dylib=teec");


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

Reply via email to