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 proto;
19 use std::env;
20 use std::fs::File;
21 use std::io::Write;
22 use std::path::{Path, PathBuf};
23 use uuid::Uuid;
24 
main() -> std::io::Result<()>25 fn main() -> std::io::Result<()> {
26     let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
27 
28     let mut buffer = File::create(out.join("user_ta_header.rs"))?;
29     buffer.write_all(include_bytes!("ta_static.rs"))?;
30 
31     let tee_uuid = Uuid::parse_str(proto::UUID).unwrap();
32     let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = tee_uuid.as_fields();
33 
34     write!(buffer, "\n")?;
35     write!(
36         buffer,
37         "const TA_UUID: optee_utee_sys::TEE_UUID = optee_utee_sys::TEE_UUID {{
38     timeLow: {:#x},
39     timeMid: {:#x},
40     timeHiAndVersion: {:#x},
41     clockSeqAndNode: {:#x?},
42 }};",
43         time_low, time_mid, time_hi_and_version, clock_seq_and_node
44     )?;
45     let optee_os_dir = env::var("OPTEE_OS_DIR").unwrap_or("../../../optee/optee_os".to_string());
46     let search_path = match env::var("ARCH") {
47         Ok(ref v) if v == "arm" => {
48             File::create(out.join("ta.lds"))?.write_all(include_bytes!("ta_arm.lds"))?;
49             Path::new(&optee_os_dir).join("out/arm/export-ta_arm32/lib")
50         },
51         _ => {
52             File::create(out.join("ta.lds"))?.write_all(include_bytes!("ta_aarch64.lds"))?;
53             Path::new(&optee_os_dir).join("out/arm/export-ta_arm64/lib")
54         }
55     };
56     println!("cargo:rustc-link-search={}", out.display());
57     println!("cargo:rerun-if-changed=ta.lds");
58 
59     println!("cargo:rustc-link-search={}", search_path.display());
60     println!("cargo:rustc-link-lib=static=utee");
61     Ok(())
62 }
63