55// spell-checker:ignore (ToDO) dylib libstdbuf deps liblibstdbuf
66
77use std:: env;
8+ use std:: env:: current_exe;
89use std:: fs;
910use std:: path:: Path ;
1011
@@ -24,53 +25,25 @@ mod platform {
2425}
2526
2627fn main ( ) {
27- let out_dir = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
28- let mut target_dir = Path :: new ( & out_dir) ;
28+ let current_exe = current_exe ( ) . unwrap ( ) ;
2929
30- // Depending on how this is util is built, the directory structure changes.
31- // This seems to work for now. Here are three cases to test when changing
32- // this:
33- //
34- // - cargo run
35- // - cross run
36- // - cargo install --git
37- // - cargo publish --dry-run
38- //
39- // The goal is to find the directory in which we are installing, but that
40- // depends on the build method, which is annoying. Additionally the env
41- // var for the profile can only be "debug" or "release", not a custom
42- // profile name, so we have to use the name of the directory within target
43- // as the profile name.
44- //
45- // Adapted from https://stackoverflow.com/questions/73595435/how-to-get-profile-from-cargo-toml-in-build-rs-or-at-runtime
46- let profile_name = out_dir
47- . split ( std:: path:: MAIN_SEPARATOR )
48- . nth_back ( 3 )
49- . unwrap ( ) ;
30+ let out_dir_string = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
31+ let out_dir = Path :: new ( & out_dir_string) ;
5032
51- let mut name = target_dir. file_name ( ) . unwrap ( ) . to_string_lossy ( ) ;
52- while name != "target" && !name. starts_with ( "cargo-install" ) {
53- target_dir = target_dir. parent ( ) . unwrap ( ) ;
54- name = target_dir. file_name ( ) . unwrap ( ) . to_string_lossy ( ) ;
55- }
56- let mut dir = target_dir. to_path_buf ( ) ;
57- dir. push ( profile_name) ;
58- dir. push ( "deps" ) ;
59- let mut path = None ;
33+ let deps_dir = current_exe. ancestors ( ) . nth ( 3 ) . unwrap ( ) . join ( "deps" ) ;
34+ dbg ! ( & deps_dir) ;
6035
61- // When running cargo publish, cargo appends hashes to the filenames of the compiled artifacts.
62- // Therefore, it won't work to just get liblibstdbuf.so. Instead, we look for files with the
63- // glob pattern "liblibstdbuf*.so" (i.e. starts with liblibstdbuf and ends with the extension).
64- for entry in fs:: read_dir ( dir) . unwrap ( ) . flatten ( ) {
65- let name = entry. file_name ( ) ;
66- let name = name. to_string_lossy ( ) ;
67- if name. starts_with ( "liblibstdbuf" ) && name. ends_with ( platform:: DYLIB_EXT ) {
68- path = Some ( entry. path ( ) ) ;
69- }
70- }
71- fs:: copy (
72- path. expect ( "liblibstdbuf was not found" ) ,
73- Path :: new ( & out_dir) . join ( "libstdbuf.so" ) ,
74- )
75- . unwrap ( ) ;
36+ let libstdbuf = deps_dir
37+ . read_dir ( )
38+ . unwrap ( )
39+ . flatten ( )
40+ . find ( |entry| {
41+ let n = entry. file_name ( ) ;
42+ let name = n. to_string_lossy ( ) ;
43+
44+ name. starts_with ( "liblibstdbuf" ) && name. ends_with ( platform:: DYLIB_EXT )
45+ } )
46+ . expect ( "unable to find libstdbuf" ) ;
47+
48+ fs:: copy ( libstdbuf. path ( ) , out_dir. join ( "libstdbuf.so" ) ) . unwrap ( ) ;
7649}
0 commit comments