11use std:: env:: var;
22use std:: fs:: { read_dir, rename} ;
33use std:: path:: PathBuf ;
4+ use tracing:: { info, Level } ;
5+ use tracing_appender:: rolling:: { RollingFileAppender , Rotation } ;
46
57fn main ( ) {
8+ // init log
9+ let out_dir = PathBuf :: from ( var ( "OUT_DIR" ) . expect ( "env not found" ) ) ;
10+ let target_dir = out_dir
11+ . parent ( )
12+ . expect ( "can not find deps dir" )
13+ . parent ( )
14+ . expect ( "can not find deps dir" )
15+ . parent ( )
16+ . expect ( "can not find deps dir" )
17+ . parent ( )
18+ . expect ( "can not find deps dir" ) ;
19+ _ = tracing_subscriber:: fmt ( )
20+ . with_writer ( RollingFileAppender :: new (
21+ Rotation :: NEVER ,
22+ target_dir,
23+ "open-coroutine-build.log" ,
24+ ) )
25+ . with_thread_names ( true )
26+ . with_line_number ( true )
27+ . with_max_level ( Level :: INFO )
28+ . with_timer ( tracing_subscriber:: fmt:: time:: OffsetTime :: new (
29+ time:: UtcOffset :: from_hms ( 8 , 0 , 0 ) . expect ( "create UtcOffset failed !" ) ,
30+ time:: format_description:: well_known:: Rfc2822 ,
31+ ) )
32+ . try_init ( ) ;
633 // build dylib
734 let target = var ( "TARGET" ) . expect ( "env not found" ) ;
8- let out_dir = PathBuf :: from ( var ( "OUT_DIR" ) . expect ( "env not found" ) ) ;
9- let cargo_manifest_dir = PathBuf :: from ( var ( "CARGO_MANIFEST_DIR" ) . expect ( "env not found" ) ) ;
1035 let mut cargo = std:: process:: Command :: new ( "cargo" ) ;
1136 let mut cmd = cargo. arg ( "build" ) . arg ( "--target" ) . arg ( target. clone ( ) ) ;
1237 if cfg ! ( not( debug_assertions) ) {
@@ -15,7 +40,7 @@ fn main() {
1540 if let Err ( e) = cmd
1641 . arg ( "--manifest-path" )
1742 . arg (
18- cargo_manifest_dir
43+ PathBuf :: from ( var ( "CARGO_MANIFEST_DIR" ) . expect ( "env not found" ) )
1944 . parent ( )
2045 . expect ( "parent not found" )
2146 . join ( "hook" )
@@ -44,15 +69,17 @@ fn main() {
4469 . parent ( )
4570 . expect ( "can not find deps dir" )
4671 . join ( "deps" ) ;
72+ let dir = read_dir ( hook_deps. clone ( ) )
73+ . unwrap_or_else ( |_| read_dir ( deps. clone ( ) ) . expect ( "can not find deps dir" ) ) ;
74+ info ! ( "deps: {deps:?}" ) ;
75+ info ! ( "hook_deps:{hook_deps:?}" ) ;
76+ info ! ( "dir:{dir:?}" ) ;
4777 let lib_names = [
4878 String :: from ( "libopen_coroutine_hook.so" ) ,
4979 String :: from ( "libopen_coroutine_hook.dylib" ) ,
5080 String :: from ( "open_coroutine_hook.lib" ) ,
5181 ] ;
52- for entry in read_dir ( hook_deps. clone ( ) )
53- . expect ( "Failed to read deps" )
54- . flatten ( )
55- {
82+ for entry in dir. flatten ( ) {
5683 let file_name = entry. file_name ( ) . to_string_lossy ( ) . to_string ( ) ;
5784 if !file_name. contains ( "open_coroutine_hook" ) {
5885 continue ;
@@ -63,31 +90,47 @@ fn main() {
6390 if file_name. eq ( "open_coroutine_hook.dll" ) {
6491 continue ;
6592 }
93+ info ! ( "{file_name:?}" ) ;
6694 if cfg ! ( target_os = "linux" ) && file_name. ends_with ( ".so" ) {
6795 rename (
68- hook_deps. join ( file_name) ,
96+ hook_deps. join ( file_name. clone ( ) ) ,
6997 deps. join ( "libopen_coroutine_hook.so" ) ,
7098 )
71- . expect ( "rename to libopen_coroutine_hook.so failed!" ) ;
99+ . unwrap_or_else ( |_| {
100+ rename ( deps. join ( file_name) , deps. join ( "libopen_coroutine_hook.so" ) )
101+ . expect ( "rename to libopen_coroutine_hook.so failed!" )
102+ } ) ;
72103 } else if cfg ! ( target_os = "macos" ) && file_name. ends_with ( ".dylib" ) {
73104 rename (
74- hook_deps. join ( file_name) ,
105+ hook_deps. join ( file_name. clone ( ) ) ,
75106 deps. join ( "libopen_coroutine_hook.dylib" ) ,
76107 )
77- . expect ( "rename to libopen_coroutine_hook.dylib failed!" ) ;
108+ . unwrap_or_else ( |_| {
109+ rename (
110+ deps. join ( file_name) ,
111+ deps. join ( "libopen_coroutine_hook.dylib" ) ,
112+ )
113+ . expect ( "rename to libopen_coroutine_hook.dylib failed!" )
114+ } ) ;
78115 } else if cfg ! ( windows) {
79116 if file_name. ends_with ( ".dll" ) {
80117 rename (
81- hook_deps. join ( file_name) ,
118+ hook_deps. join ( file_name. clone ( ) ) ,
82119 deps. join ( "open_coroutine_hook.dll" ) ,
83120 )
84- . expect ( "rename to open_coroutine_hook.dll failed!" ) ;
121+ . unwrap_or_else ( |_| {
122+ rename ( deps. join ( file_name) , deps. join ( "open_coroutine_hook.dll" ) )
123+ . expect ( "rename to open_coroutine_hook.dll failed!" )
124+ } ) ;
85125 } else if file_name. ends_with ( ".lib" ) {
86126 rename (
87- hook_deps. join ( file_name) ,
127+ hook_deps. join ( file_name. clone ( ) ) ,
88128 deps. join ( "open_coroutine_hook.lib" ) ,
89129 )
90- . expect ( "rename to open_coroutine_hook.lib failed!" ) ;
130+ . unwrap_or_else ( |_| {
131+ rename ( deps. join ( file_name) , deps. join ( "open_coroutine_hook.lib" ) )
132+ . expect ( "rename to open_coroutine_hook.lib failed!" )
133+ } ) ;
91134 }
92135 }
93136 }
0 commit comments