1+ use cargo_metadata:: MetadataCommand ;
12use std:: env:: var;
2- use std:: fs:: { read_dir , rename } ;
3+ use std:: fs:: { copy , read_dir } ;
34use std:: path:: PathBuf ;
5+ use tracing:: { info, Level } ;
6+ use tracing_appender:: rolling:: { RollingFileAppender , Rotation } ;
47
58fn main ( ) {
9+ // init log
10+ let out_dir = PathBuf :: from ( var ( "OUT_DIR" ) . expect ( "OUT_DIR not found" ) ) ;
11+ let target_dir = out_dir
12+ . parent ( )
13+ . expect ( "can not find deps dir" )
14+ . parent ( )
15+ . expect ( "can not find deps dir" )
16+ . parent ( )
17+ . expect ( "can not find deps dir" )
18+ . parent ( )
19+ . expect ( "can not find deps dir" ) ;
20+ _ = tracing_subscriber:: fmt ( )
21+ . with_writer ( RollingFileAppender :: new (
22+ Rotation :: NEVER ,
23+ target_dir,
24+ "open-coroutine-build.log" ,
25+ ) )
26+ . with_thread_names ( true )
27+ . with_line_number ( true )
28+ . with_max_level ( Level :: INFO )
29+ . with_timer ( tracing_subscriber:: fmt:: time:: OffsetTime :: new (
30+ time:: UtcOffset :: from_hms ( 8 , 0 , 0 ) . expect ( "create UtcOffset failed !" ) ,
31+ time:: format_description:: well_known:: Rfc2822 ,
32+ ) )
33+ . try_init ( ) ;
634 // build dylib
735 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" ) ) ;
1036 let mut cargo = std:: process:: Command :: new ( "cargo" ) ;
1137 let mut cmd = cargo. arg ( "build" ) . arg ( "--target" ) . arg ( target. clone ( ) ) ;
1238 if cfg ! ( not( debug_assertions) ) {
1339 cmd = cmd. arg ( "--release" ) ;
1440 }
15- if let Err ( e) = cmd
16- . arg ( "--manifest-path" )
17- . arg (
18- cargo_manifest_dir
19- . parent ( )
20- . expect ( "parent not found" )
21- . join ( "hook" )
22- . join ( "Cargo.toml" ) ,
23- )
24- . arg ( "--target-dir" )
25- . arg ( out_dir. clone ( ) )
26- . status ( )
27- {
28- panic ! ( "failed to build build dylib {e}" ) ;
41+ let mut hook_toml = PathBuf :: from ( var ( "CARGO_MANIFEST_DIR" ) . expect ( "env not found" ) )
42+ . parent ( )
43+ . expect ( "parent not found" )
44+ . join ( "hook" )
45+ . join ( "Cargo.toml" ) ;
46+ if !hook_toml. exists ( ) {
47+ info ! ( "{hook_toml:?} not exists, find open-coroutine-hook's Cargo.toml in $CARGO_HOME" ) ;
48+ // 使用cargo_metadata读到依赖版本,结合CARGO_HOME获取open-coroutine-hook的toml
49+ let mut dep_src_dir = PathBuf :: from ( var ( "CARGO_HOME" ) . expect ( "CARGO_HOME not found" ) )
50+ . join ( "registry" )
51+ . join ( "src" ) ;
52+ let rustup_dist_server = var ( "RUSTUP_DIST_SERVER" ) . expect ( "RUSTUP_DIST_SERVER not found" ) ;
53+ let host = rustup_dist_server
54+ . split ( "://" )
55+ . last ( )
56+ . expect ( "host not found" ) ;
57+ dep_src_dir = dep_src_dir. join (
58+ read_dir ( dep_src_dir. clone ( ) )
59+ . expect ( "Failed to read deps" )
60+ . flatten ( )
61+ . find ( |entry| {
62+ let file_name = entry. file_name ( ) . to_string_lossy ( ) . to_string ( ) ;
63+ file_name. contains ( host)
64+ } )
65+ . expect ( "host dir not found" )
66+ . file_name ( )
67+ . to_string_lossy ( )
68+ . to_string ( ) ,
69+ ) ;
70+ info ! ( "dep_src_dir:{dep_src_dir:?}" ) ;
71+ let metadata = MetadataCommand :: default ( )
72+ . no_deps ( )
73+ . exec ( )
74+ . expect ( "read cargo metadata failed" ) ;
75+ let package = metadata
76+ . packages
77+ . first ( )
78+ . expect ( "read current package failed" ) ;
79+ info ! ( "read package:{:#?}" , package) ;
80+ let dependency = package
81+ . dependencies
82+ . iter ( )
83+ . find ( |dep| dep. name . eq ( "open-coroutine-hook" ) )
84+ . expect ( "open-coroutine-hook not found" ) ;
85+ let version = & dependency
86+ . req
87+ . comparators
88+ . first ( )
89+ . expect ( "version not found" ) ;
90+ hook_toml = dep_src_dir
91+ . join ( format ! (
92+ "open-coroutine-hook-{}.{}.{}" ,
93+ version. major,
94+ version. minor. unwrap_or( 0 ) ,
95+ version. patch. unwrap_or( 0 )
96+ ) )
97+ . join ( "Cargo.toml" ) ;
2998 }
30- //fix dylib name
99+ info ! ( "open-coroutine-hook's Cargo.toml is here:{hook_toml:?}" ) ;
100+ assert ! (
101+ cmd. arg( "--manifest-path" )
102+ . arg( hook_toml)
103+ . arg( "--target-dir" )
104+ . arg( out_dir. clone( ) )
105+ . status( )
106+ . expect( "failed to build dylib" )
107+ . success( ) ,
108+ "failed to build dylib"
109+ ) ;
110+ // link dylib
31111 let hook_deps = out_dir
32112 . join ( target)
33113 . join ( if cfg ! ( debug_assertions) {
@@ -44,50 +124,35 @@ fn main() {
44124 . parent ( )
45125 . expect ( "can not find deps dir" )
46126 . join ( "deps" ) ;
47- let lib_names = [
48- String :: from ( "libopen_coroutine_hook.so" ) ,
49- String :: from ( "libopen_coroutine_hook.dylib" ) ,
50- String :: from ( "open_coroutine_hook.lib" ) ,
51- ] ;
52127 for entry in read_dir ( hook_deps. clone ( ) )
53- . expect ( "Failed to read deps" )
128+ . expect ( "can not find deps dir " )
54129 . flatten ( )
55130 {
56131 let file_name = entry. file_name ( ) . to_string_lossy ( ) . to_string ( ) ;
57132 if !file_name. contains ( "open_coroutine_hook" ) {
58133 continue ;
59134 }
60- if lib_names. contains ( & file_name) {
61- break ;
62- }
63- if file_name. eq ( "open_coroutine_hook.dll" ) {
64- continue ;
65- }
66135 if cfg ! ( target_os = "linux" ) && file_name. ends_with ( ".so" ) {
67- rename (
68- hook_deps. join ( file_name) ,
69- deps. join ( "libopen_coroutine_hook.so" ) ,
70- )
71- . expect ( "rename to libopen_coroutine_hook.so failed!" ) ;
136+ let from = hook_deps. join ( file_name) ;
137+ let to = deps. join ( "libopen_coroutine_hook.so" ) ;
138+ copy ( from. clone ( ) , to. clone ( ) ) . expect ( "copy to libopen_coroutine_hook.so failed!" ) ;
139+ info ! ( "copy {:?} to {:?} success!" , from, to) ;
72140 } else if cfg ! ( target_os = "macos" ) && file_name. ends_with ( ".dylib" ) {
73- rename (
74- hook_deps. join ( file_name) ,
75- deps. join ( "libopen_coroutine_hook.dylib" ) ,
76- )
77- . expect ( "rename to libopen_coroutine_hook.dylib failed!" ) ;
141+ let from = hook_deps. join ( file_name) ;
142+ let to = deps. join ( "libopen_coroutine_hook.dylib" ) ;
143+ copy ( from. clone ( ) , to. clone ( ) ) . expect ( "copy to libopen_coroutine_hook.dylib failed!" ) ;
144+ info ! ( "copy {:?} to {:?} success!" , from, to) ;
78145 } else if cfg ! ( windows) {
79146 if file_name. ends_with ( ".dll" ) {
80- rename (
81- hook_deps. join ( file_name) ,
82- deps. join ( "open_coroutine_hook.dll" ) ,
83- )
84- . expect ( "rename to open_coroutine_hook.dll failed!" ) ;
147+ let from = hook_deps. join ( file_name) ;
148+ let to = deps. join ( "open_coroutine_hook.dll" ) ;
149+ copy ( from. clone ( ) , to. clone ( ) ) . expect ( "copy to open_coroutine_hook.dll failed!" ) ;
150+ info ! ( "copy {:?} to {:?} success!" , from, to) ;
85151 } else if file_name. ends_with ( ".lib" ) {
86- rename (
87- hook_deps. join ( file_name) ,
88- deps. join ( "open_coroutine_hook.lib" ) ,
89- )
90- . expect ( "rename to open_coroutine_hook.lib failed!" ) ;
152+ let from = hook_deps. join ( file_name) ;
153+ let to = deps. join ( "open_coroutine_hook.lib" ) ;
154+ copy ( from. clone ( ) , to. clone ( ) ) . expect ( "copy to open_coroutine_hook.lib failed!" ) ;
155+ info ! ( "copy {:?} to {:?} success!" , from, to) ;
91156 }
92157 }
93158 }
0 commit comments