1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 use std::env;
19 use std::fs::File;
20 use std::io::Write;
21 use std::path::{Path, PathBuf};
22 use std::process::Command;
23 
main()24 fn main() {
25     let mut cfg = ctest::TestGenerator::new();
26     cfg.target("aarch64-unknown-linux-gnu")
27         .header("tee_api_types.h")
28         .header("tee_api_defines.h")
29         .header("utee_types.h")
30         .header("user_ta_header.h")
31         .header("tee_api.h")
32         .header("utee_syscalls.h")
33         .include(env::var("OPTEE_OS_INCLUDE").unwrap())
34         .type_name(|s, _is_struct, _is_union| {
35             if s == "utee_params"
36                 || s == "ta_head"
37                 || s == "utee_attribute"
38                 || s == "user_ta_property"
39             {
40                 return format!("struct {}", s);
41             }
42             s.to_string()
43         });
44     cfg.skip_struct(|s| {
45         s == "Memref"
46             || s == "Value"
47             || s == "content"
48             || s.ends_with("Handle")
49             || s == "ta_prop"
50             || s == "user_ta_property"
51     });
52     cfg.skip_field(|s, field| {
53         (s == "ta_head" && field == "entry")
54             || field == "content"
55             || field == "value"
56             || field == "memref"
57             || field == "keyInformation"
58     });
59     cfg.skip_type(|s| s == "Memref" || s == "Value");
60     cfg.skip_fn(|s| s == "TEE_BigIntFMMConvertToBigInt");
61     cfg.skip_const(|s| s.starts_with("TA_PROP_STR") || s == "TEE_HANDLE_NULL");
62     cfg.skip_roundtrip(|s| s.starts_with("TEE_") || s.starts_with("utee_") || s == "ta_head");
63     cfg.generate("../optee-utee-sys/src/lib.rs", "all.rs");
64     println!("cargo:rustc-link-lib=static=mbedtls");
65     println!("cargo:rustc-link-lib=static=utee");
66     println!("cargo:rustc-link-lib=static=utils");
67 
68     let out_dir = env::var("OUT_DIR").unwrap();
69     let undefined_path = PathBuf::from(&out_dir).join("undefined.c");
70 
71     let mut buffer = File::create(&undefined_path).unwrap();
72     write!(
73         buffer,
74         "
75         void* ta_props = 0;
76         void* ta_num_props = 0;
77         void* trace_level = 0;
78         void* trace_ext_prefix = 0;
79     "
80     )
81     .unwrap();
82     Command::new("aarch64-linux-gnu-gcc")
83         .args(&[undefined_path.to_str().unwrap(), "-c", "-fPIC", "-o"])
84         .arg(&format!("{}/undefined.o", out_dir))
85         .status()
86         .unwrap();
87     Command::new("aarch64-linux-gnu-ar")
88         .args(&["crus", "libundefined.a", "undefined.o"])
89         .current_dir(&Path::new(&out_dir))
90         .status()
91         .unwrap();
92 
93     println!("cargo:rustc-link-search=native={}", out_dir);
94     println!("cargo:rustc-link-lib=static=undefined");
95 }
96