DemesneGH commented on code in PR #156:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/156#discussion_r1897353918


##########
docs/migrating-to-new-building-env.md:
##########
@@ -2,6 +2,8 @@
 permalink: /trustzone-sdk-docs/migrating-to-new-building-env.md
 ---
 
+> After optee-utee-build release, this doc is keeping for developers who 
intend to know the detail of building process, we suggest use optee-utee-build 
for building instead.

Review Comment:
   Please rewrap the new part to match the line wrapping style of the rest of 
the document



##########
examples/hello_world-rs/ta/Makefile:
##########
@@ -20,7 +20,7 @@ UUID ?= $(shell cat "../uuid.txt")
 TARGET ?= aarch64-unknown-linux-gnu
 CROSS_COMPILE ?= aarch64-linux-gnu-
 OBJCOPY := $(CROSS_COMPILE)objcopy
-LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)ld.bfd\"
+LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"

Review Comment:
   Using the `ld.bfd` works as well, right?



##########
docs/writing-rust-tas-using-optee-utee-build.md:
##########
@@ -0,0 +1,183 @@
+---
+permalink: /trustzone-sdk-docs/writing-rust-tas-using-optee-utee-build.md
+---
+

Review Comment:
   When finalizing this documentation, please rewrap all text in this document 
at 80 characters, this helps to further review and see changes.
   Reference: 
https://thoughtbot.com/blog/wrap-existing-text-at-80-characters-in-vim 



##########
optee-utee-build/Cargo.toml:
##########
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+[package]
+name = "optee-utee-build"

Review Comment:
   Do we need a publish of this crate on `crates.io`? After this PR merged I 
can do that.



##########
docs/writing-rust-tas-using-optee-utee-build.md:
##########
@@ -0,0 +1,183 @@
+---
+permalink: /trustzone-sdk-docs/writing-rust-tas-using-optee-utee-build.md
+---
+
+Currently we provide a `optee-utee-build` crate to simplify the compilcated 
building process of TA, and we recommend everyone use it in future developement.
+

Review Comment:
   Recommend adding index for this document, something like this:
   
   - For legacy app structures migrating to use this crate, refer to [link to 
Migration Guide]
   - If you're new to development, start with [Minimal Example]
   - To customize the build process, see [Customization]



##########
.licenserc.yaml:
##########
@@ -31,3 +31,4 @@ header:
     - 'DISCLAIMER-WIP'
     - '*.json'
     - 'examples/tls_server-rs/ta/test-ca/**'
+    - 'optee-utee-build/test_files/**'

Review Comment:
   Could the license header be added to the `test_files`?



##########
optee-utee-build/src/linker.rs:
##########
@@ -0,0 +1,175 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::env;
+use std::fs::File;
+use std::io::Write;
+use std::io::{BufRead, BufReader};
+use std::path::PathBuf;
+
+use crate::Error;
+
+/// The type of the linker, there are difference when using gcc/cc or ld/lld 
as linker,
+/// For example, `--sort-section=alignment` parameter changes to 
`-Wl,--sort-section=alignment`
+/// when using gcc as linker.
+///
+/// CC: gcc, cc, etc.
+/// LD: ld, lld, ld.bfd, ld.gold, etc.
+#[derive(Debug, Clone)]
+pub enum LinkerType {
+    CC,
+    LD,
+}
+
+/// Linker of ta, use it to handle all linking stuff
+///
+/// Use only if you just want to handle the linking stuff, and use a hand 
written user_ta_header.rs,
+/// or you should use Builder instead
+/// Usage:
+///
+/// ```no_run
+/// use optee_utee_build::Linker;
+/// use std::env;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// let out_dir = env::var("OUT_DIR")?;
+/// Linker::auto().link_all(out_dir)?;
+/// # Ok(())
+/// # }
+/// ```
+///
+/// We detect the type of the linker automatically, you can set it manually if 
you met some
+/// problems with it.
+/// ```no_run
+/// use optee_utee_build::{Linker, LinkerType};
+/// use std::env;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// let out_dir = env::var("OUT_DIR")?;
+/// Linker::new(LinkerType::CC).link_all(out_dir)?;
+/// # Ok(())
+/// # }
+/// ```
+///
+pub struct Linker {
+    linker_type: LinkerType,
+}
+
+impl Linker {
+    pub fn new(linker_type: LinkerType) -> Self {
+        Self { linker_type }
+    }
+    pub fn auto() -> Self {
+        Self { linker_type: Self::auto_detect_linker_type() }
+    }
+    pub fn link_all<P: Into<PathBuf>>(self, out_dir: P) -> Result<(), Error> {
+        const ENV_TA_DEV_KIT_DIR: &str = "TA_DEV_KIT_DIR";
+        println!("cargo:rerun-if-env-changed={}", ENV_TA_DEV_KIT_DIR);
+        let ta_dev_kit_dir = PathBuf::from(std::env::var(ENV_TA_DEV_KIT_DIR)?);
+        let out_dir: PathBuf = out_dir.into();
+
+        self.write_and_set_linker_script(out_dir.clone(), 
ta_dev_kit_dir.clone())?;
+
+        let search_path = ta_dev_kit_dir.join("lib");
+        println!("cargo:rustc-link-search={}", search_path.display());
+        println!("cargo:rustc-link-lib=static=utee");
+        println!("cargo:rustc-link-lib=static=utils");
+        println!("cargo:rustc-link-arg=-e__ta_entry");
+        println!("cargo:rustc-link-arg=-pie");
+        println!("cargo:rustc-link-arg=-Os");
+        match self.linker_type {
+            LinkerType::CC => 
println!("cargo:rustc-link-arg=-Wl,--sort-section=alignment"),
+            LinkerType::LD => 
println!("cargo:rustc-link-arg=--sort-section=alignment"),
+        };
+        let mut dyn_list = File::create(out_dir.join("dyn_list"))?;
+        write!(
+            dyn_list,
+            "{{ __elf_phdr_info; trace_ext_prefix; trace_level; ta_head; }};\n"
+        )?;
+        match self.linker_type {
+            LinkerType::CC => 
println!("cargo:rustc-link-arg=-Wl,--dynamic-list=dyn_list"),
+            LinkerType::LD => 
println!("cargo:rustc-link-arg=--dynamic-list=dyn_list"),
+        }
+
+        Ok(())
+    }
+}
+
+impl Linker {
+    // generate a link script file for cc/ld, and link to it
+    fn write_and_set_linker_script(&self, out_dir: PathBuf, ta_dev_kit_dir: 
PathBuf) -> Result<(), Error> {
+        const ENV_TARGET_TA: &str = "TARGET_TA";
+        println!("cargo:rerun-if-env-changed={}", ENV_TARGET_TA);
+        let mut aarch64_flag = true;
+        match env::var(ENV_TARGET_TA) {
+            Ok(ref v) if v == "arm-unknown-linux-gnueabihf" || v == 
"arm-unknown-optee" => {
+                match self.linker_type {
+                    LinkerType::CC => 
println!("cargo:rustc-link-arg=-Wl,--no-warn-mismatch"),
+                    LinkerType::LD => 
println!("cargo:rustc-link-arg=--no-warn-mismatch"),
+                };
+                aarch64_flag = false;
+            }
+            _ => {}
+        };
+
+        let f = 
BufReader::new(File::open(ta_dev_kit_dir.join("src/ta.ld.S"))?);
+        let ta_lds_file_path = out_dir.join("ta.lds");
+        let mut ta_lds = File::create(ta_lds_file_path.clone())?;
+        for line in f.lines() {
+            let l = line?;
+
+            if aarch64_flag {
+                if l.starts_with('#')
+                    || l == "OUTPUT_FORMAT(\"elf32-littlearm\")"
+                    || l == "OUTPUT_ARCH(arm)"
+                {
+                    continue;
+                }
+            } else {
+                if l.starts_with('#')
+                    || l == "OUTPUT_FORMAT(\"elf64-littleaarch64\")"
+                    || l == "OUTPUT_ARCH(aarch64)"
+                {
+                    continue;
+                }
+            }
+
+            if l == "\t. = ALIGN(4096);" {
+                write!(ta_lds, "\t. = ALIGN(65536);\n")?;
+            } else {
+                write!(ta_lds, "{}\n", l)?;
+            }
+        }
+
+        println!("cargo:rustc-link-search={}", out_dir.display());
+        println!("cargo:rerun-if-changed={}", ta_lds_file_path.display());
+        println!("cargo:rustc-link-arg=-T{}", ta_lds_file_path.display());
+        Ok(())
+    }
+
+    fn auto_detect_linker_type() -> LinkerType {
+        const ENV_RUSTC_LINKER: &str = "RUSTC_LINKER";
+        println!("cargo:rerun-if-env-changed={}", ENV_RUSTC_LINKER);
+        match env::var(ENV_RUSTC_LINKER) {
+            Ok(ref linker_name) if linker_name.ends_with("ld") || 
linker_name.ends_with("ld.bfd") => {
+                return LinkerType::LD;
+            },
+            _ => {},
+        };
+        return LinkerType::CC;

Review Comment:
   simplify to:
   Ok(xxxx) => LinkerType::LD,
   _ => LinkerType::CC



##########
optee-utee-build/src/ta_config.rs:
##########
@@ -0,0 +1,187 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+use crate::Error;
+use std::convert::TryInto;
+
+/// Configuration options for TA
+///
+/// Examples
+///
+/// # use a standard configuration
+/// ```rust
+/// use optee_utee_build::TAConfig;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// const UUID: &str = "d93c2970-b1a6-4b86-90ac-b42830e78d9b";
+/// let ta_config = TAConfig::new_standard(
+///     UUID,
+///     "0.1.0",
+///     "hello world",
+/// )?;
+/// # Ok(())
+/// # }
+/// ```
+///
+/// and since we already have `version` and `description` in `cargo.toml`,
+/// we can make it simpler by using them as parameters:
+///
+/// ```rust
+/// use optee_utee_build::TAConfig;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// const UUID: &str = "d93c2970-b1a6-4b86-90ac-b42830e78d9b";
+/// let ta_config = TAConfig::new_standard_with_cargo_env(UUID)?;
+/// # Ok(())
+/// # }
+/// ```
+///
+/// # make some modifications
+/// ```rust
+/// use optee_utee_build::TAConfig;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// const UUID: &str = "d93c2970-b1a6-4b86-90ac-b42830e78d9b";
+/// let ta_config = TAConfig::new_standard(
+///     UUID,
+///     "0.1.0",
+///     "hello world",
+/// )?.ta_stack_size(10 * 1024).ta_data_size(32 * 1024);
+/// # Ok(())
+/// # }
+/// ```
+#[derive(Debug, Clone)]
+pub struct TAConfig {
+    pub uuid: uuid::Uuid,
+    pub ta_flags: u32,
+    pub ta_data_size: u32,
+    pub ta_stack_size: u32,
+    pub ta_version: String,
+    pub ta_description: String,
+    pub trace_level: i32,
+    pub trace_ext_prefix: String,
+    pub ta_framework_stack_size: u32,
+    pub ext_properties: Vec<Property>,
+}
+
+impl TAConfig {
+    /// generate a standard config by uuid, retrieve version and description 
from cargo.toml
+    pub fn new_standard_with_cargo_env(uuid_str: &str) -> Result<Self, Error> {

Review Comment:
   1. How about naming this to "default config"? 
   `new_default_with_cargo_env`
   `new_default`
   2. Could we have some description of when we should use 
`new_standard_with_cargo_env`? Why we read these values `CARGO_PKG_VERSION` 
`CARGO_PKG_DESCRIPTION`?



##########
optee-utee-build/Cargo.toml:
##########
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+[package]
+name = "optee-utee-build"
+version = "0.2.0"
+authors = ["Teaclave Contributors <dev@teaclave.apache.org>"]
+license = "Apache-2.0"
+repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git";
+edition = "2018"
+description = "Build tool for TA"
+

Review Comment:
   When finalizing, please run `cargo fmt` for this crate



##########
optee-utee-build/src/ta_config.rs:
##########
@@ -0,0 +1,187 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+use crate::Error;
+use std::convert::TryInto;
+
+/// Configuration options for TA
+///
+/// Examples
+///
+/// # use a standard configuration
+/// ```rust
+/// use optee_utee_build::TAConfig;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// const UUID: &str = "d93c2970-b1a6-4b86-90ac-b42830e78d9b";
+/// let ta_config = TAConfig::new_standard(
+///     UUID,
+///     "0.1.0",
+///     "hello world",
+/// )?;
+/// # Ok(())
+/// # }
+/// ```
+///
+/// and since we already have `version` and `description` in `cargo.toml`,
+/// we can make it simpler by using them as parameters:
+///
+/// ```rust
+/// use optee_utee_build::TAConfig;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// const UUID: &str = "d93c2970-b1a6-4b86-90ac-b42830e78d9b";
+/// let ta_config = TAConfig::new_standard_with_cargo_env(UUID)?;
+/// # Ok(())
+/// # }
+/// ```
+///
+/// # make some modifications
+/// ```rust
+/// use optee_utee_build::TAConfig;
+/// # use optee_utee_build::Error;
+/// # fn main() -> Result<(), Error> {
+/// const UUID: &str = "d93c2970-b1a6-4b86-90ac-b42830e78d9b";
+/// let ta_config = TAConfig::new_standard(
+///     UUID,
+///     "0.1.0",
+///     "hello world",
+/// )?.ta_stack_size(10 * 1024).ta_data_size(32 * 1024);
+/// # Ok(())
+/// # }
+/// ```
+#[derive(Debug, Clone)]
+pub struct TAConfig {
+    pub uuid: uuid::Uuid,
+    pub ta_flags: u32,
+    pub ta_data_size: u32,
+    pub ta_stack_size: u32,
+    pub ta_version: String,
+    pub ta_description: String,
+    pub trace_level: i32,
+    pub trace_ext_prefix: String,
+    pub ta_framework_stack_size: u32,
+    pub ext_properties: Vec<Property>,
+}
+
+impl TAConfig {
+    /// generate a standard config by uuid, retrieve version and description 
from cargo.toml
+    pub fn new_standard_with_cargo_env(uuid_str: &str) -> Result<Self, Error> {
+        Self::new_standard(
+            uuid_str,
+            std::env::var("CARGO_PKG_VERSION")?.as_str(),
+            std::env::var("CARGO_PKG_DESCRIPTION")?.as_str(),
+        )
+    }
+    /// generate a standard config
+    pub fn new_standard(
+        uuid_str: &str,
+        ta_version: &str,
+        ta_description: &str,
+    ) -> Result<Self, Error> {
+        Ok(Self {
+            uuid: uuid_str.try_into()?,
+            ta_flags: 0,
+            ta_data_size: 1 * 1024 * 1024,
+            ta_stack_size: 2 * 1024,

Review Comment:
   Keep the default values same as the which in optee_examples: 
https://github.com/linaro-swg/optee_examples/blob/master/hello_world/ta/user_ta_header_defines.h#L47-L50



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to