@@ -24,53 +24,22 @@ mod platform {
2424}
2525
2626fn main ( ) {
27- let out_dir = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
28- let mut target_dir = Path :: new ( & out_dir ) ;
27+ let out_dir_string = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
28+ let out_dir = Path :: new ( & out_dir_string ) ;
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 deps_dir = out_dir. ancestors ( ) . nth ( 3 ) . unwrap ( ) . join ( "deps" ) ;
5031
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 ;
32+ let libstdbuf = deps_dir
33+ . read_dir ( )
34+ . unwrap ( )
35+ . flatten ( )
36+ . find ( |entry| {
37+ let n = entry. file_name ( ) ;
38+ let name = n. to_string_lossy ( ) ;
6039
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 ( ) ;
40+ name. starts_with ( "liblibstdbuf" ) && name. ends_with ( platform:: DYLIB_EXT )
41+ } )
42+ . expect ( "libstdbuf not found" ) ;
43+
44+ fs:: copy ( libstdbuf. path ( ) , out_dir. join ( "libstdbuf.so" ) ) . unwrap ( ) ;
7645}
0 commit comments