1818use anyhow:: { bail, Result } ;
1919use std:: env;
2020use std:: fs;
21- use std:: path:: { Path , PathBuf } ;
22- use std:: process:: { Command , Output } ;
21+ use std:: path:: Path ;
22+ use std:: path:: PathBuf ;
23+ use std:: process:: Command ;
2324use tempfile:: TempDir ;
2425
26+ use crate :: common:: { print_output_and_bail, Arch , ChangeDirectoryGuard } ;
27+
2528// Embed the target JSON files at compile time
2629const AARCH64_TARGET_JSON : & str = include_str ! ( "../aarch64-unknown-optee.json" ) ;
2730const ARM_TARGET_JSON : & str = include_str ! ( "../arm-unknown-optee.json" ) ;
2831
29- // Helper function to print command output and return error
30- fn print_output_and_bail ( cmd_name : & str , output : & Output ) -> Result < ( ) > {
31- eprintln ! (
32- "{} stdout: {}" ,
33- cmd_name,
34- String :: from_utf8_lossy( & output. stdout)
35- ) ;
36- eprintln ! (
37- "{} stderr: {}" ,
38- cmd_name,
39- String :: from_utf8_lossy( & output. stderr)
40- ) ;
41- bail ! (
42- "{} failed with exit code: {:?}" ,
43- cmd_name,
44- output. status. code( )
45- )
46- }
47-
4832pub struct TaBuildConfig {
49- pub arch : String , // "aarch64" or "arm"
33+ pub arch : Arch , // Architecture
5034 pub std : bool , // Enable std feature
5135 pub ta_dev_kit_dir : PathBuf , // Path to TA dev kit
5236 pub signing_key : PathBuf , // Path to signing key
@@ -56,9 +40,9 @@ pub struct TaBuildConfig {
5640}
5741
5842// Helper function to derive target and cross-compile from arch and std
59- fn get_target_and_cross_compile ( arch : & str , std : bool ) -> ( String , String ) {
43+ fn get_target_and_cross_compile ( arch : Arch , std : bool ) -> ( String , String ) {
6044 match arch {
61- "arm" => {
45+ Arch :: Arm => {
6246 if std {
6347 (
6448 "arm-unknown-optee" . to_string ( ) ,
@@ -71,7 +55,7 @@ fn get_target_and_cross_compile(arch: &str, std: bool) -> (String, String) {
7155 )
7256 }
7357 }
74- _ => {
58+ Arch :: Aarch64 => {
7559 if std {
7660 (
7761 "aarch64-unknown-optee" . to_string ( ) ,
@@ -108,7 +92,7 @@ fn setup_build_command(
10892 command : & str ,
10993) -> Result < ( Command , Option < TempDir > ) > {
11094 // Determine target and cross-compile based on arch
111- let ( target, _cross_compile) = get_target_and_cross_compile ( & config. arch , config. std ) ;
95+ let ( target, _cross_compile) = get_target_and_cross_compile ( config. arch , config. std ) ;
11296
11397 // Determine builder (cargo or xargo)
11498 let builder = if config. std { "xargo" } else { "cargo" } ;
@@ -151,14 +135,7 @@ fn setup_build_command(
151135// Main function to build the TA
152136pub fn build_ta ( config : TaBuildConfig ) -> Result < ( ) > {
153137 // Change to the TA directory
154- let original_dir = env:: current_dir ( ) ?;
155-
156- env:: set_current_dir ( & config. path ) ?;
157-
158- // Ensure we return to the original directory when done
159- let _guard = ChangeDirectoryGuard {
160- original : original_dir. clone ( ) ,
161- } ;
138+ let _guard = ChangeDirectoryGuard :: new ( & config. path ) ?;
162139
163140 println ! ( "Building TA in directory: {:?}" , config. path) ;
164141
@@ -211,7 +188,7 @@ fn build_binary(config: &TaBuildConfig) -> Result<()> {
211188 println ! ( "Building TA binary..." ) ;
212189
213190 // Determine target and cross-compile based on arch
214- let ( target, cross_compile) = get_target_and_cross_compile ( & config. arch , config. std ) ;
191+ let ( target, cross_compile) = get_target_and_cross_compile ( config. arch , config. std ) ;
215192
216193 // Setup build command with common environment
217194 let ( mut build_cmd, _temp_dir) = setup_build_command ( config, "build" ) ?;
@@ -238,7 +215,7 @@ fn strip_binary(config: &TaBuildConfig) -> Result<PathBuf> {
238215 println ! ( "Stripping binary..." ) ;
239216
240217 // Determine target based on arch
241- let ( target, cross_compile) = get_target_and_cross_compile ( & config. arch , config. std ) ;
218+ let ( target, cross_compile) = get_target_and_cross_compile ( config. arch , config. std ) ;
242219
243220 let profile = if config. debug { "debug" } else { "release" } ;
244221 let target_dir = PathBuf :: from ( "target" ) . join ( target) . join ( profile) ;
@@ -286,7 +263,7 @@ fn sign_ta(config: &TaBuildConfig, stripped_path: &Path) -> Result<()> {
286263 }
287264
288265 // Determine target based on arch
289- let ( target, _) = get_target_and_cross_compile ( & config. arch , config. std ) ;
266+ let ( target, _) = get_target_and_cross_compile ( config. arch , config. std ) ;
290267
291268 // Output path
292269 let profile = if config. debug { "debug" } else { "release" } ;
@@ -316,14 +293,3 @@ fn sign_ta(config: &TaBuildConfig, stripped_path: &Path) -> Result<()> {
316293
317294 Ok ( ( ) )
318295}
319-
320- // Guard to ensure we return to the original directory
321- struct ChangeDirectoryGuard {
322- original : PathBuf ,
323- }
324-
325- impl Drop for ChangeDirectoryGuard {
326- fn drop ( & mut self ) {
327- let _ = env:: set_current_dir ( & self . original ) ;
328- }
329- }
0 commit comments