6
6
// spell-checker:ignore (ToDO) tempdir dyld dylib optgrps libstdbuf
7
7
8
8
use clap:: { Arg , ArgAction , ArgMatches , Command } ;
9
- use std:: fs:: File ;
10
- use std:: io:: Write ;
11
9
use std:: os:: unix:: process:: ExitStatusExt ;
12
10
use std:: path:: PathBuf ;
13
11
use std:: process;
@@ -29,16 +27,19 @@ mod options {
29
27
pub const COMMAND : & str = "command" ;
30
28
}
31
29
32
- #[ cfg( any(
33
- target_os = "linux" ,
34
- target_os = "android" ,
35
- target_os = "freebsd" ,
36
- target_os = "netbsd" ,
37
- target_os = "dragonfly"
30
+ #[ cfg( all(
31
+ not( feature = "feat_external_libstdbuf" ) ,
32
+ any(
33
+ target_os = "linux" ,
34
+ target_os = "android" ,
35
+ target_os = "freebsd" ,
36
+ target_os = "netbsd" ,
37
+ target_os = "dragonfly"
38
+ )
38
39
) ) ]
39
40
const STDBUF_INJECT : & [ u8 ] = include_bytes ! ( concat!( env!( "OUT_DIR" ) , "/libstdbuf.so" ) ) ;
40
41
41
- #[ cfg( target_vendor = "apple" ) ]
42
+ #[ cfg( all ( not ( feature = "feat_external_libstdbuf" ) , target_vendor = "apple" ) ) ]
42
43
const STDBUF_INJECT : & [ u8 ] = include_bytes ! ( concat!( env!( "OUT_DIR" ) , "/libstdbuf.dylib" ) ) ;
43
44
44
45
enum BufferType {
@@ -137,7 +138,11 @@ fn set_command_env(command: &mut process::Command, buffer_name: &str, buffer_typ
137
138
}
138
139
}
139
140
141
+ #[ cfg( not( feature = "feat_external_libstdbuf" ) ) ]
140
142
fn get_preload_env ( tmp_dir : & TempDir ) -> UResult < ( String , PathBuf ) > {
143
+ use std:: fs:: File ;
144
+ use std:: io:: Write ;
145
+
141
146
let ( preload, extension) = preload_strings ( ) ?;
142
147
let inject_path = tmp_dir. path ( ) . join ( "libstdbuf" ) . with_extension ( extension) ;
143
148
@@ -147,6 +152,29 @@ fn get_preload_env(tmp_dir: &TempDir) -> UResult<(String, PathBuf)> {
147
152
Ok ( ( preload. to_owned ( ) , inject_path) )
148
153
}
149
154
155
+ #[ cfg( feature = "feat_external_libstdbuf" ) ]
156
+ fn get_preload_env ( _tmp_dir : & TempDir ) -> UResult < ( String , PathBuf ) > {
157
+ let ( preload, extension) = preload_strings ( ) ?;
158
+
159
+ // Use the directory provided at compile time via LIBSTDBUF_DIR environment variable
160
+ // This will fail to compile if LIBSTDBUF_DIR is not set, which is the desired behavior
161
+ const LIBSTDBUF_DIR : & str = env ! ( "LIBSTDBUF_DIR" ) ;
162
+ let path_buf = PathBuf :: from ( LIBSTDBUF_DIR )
163
+ . join ( "libstdbuf" )
164
+ . with_extension ( extension) ;
165
+ if path_buf. exists ( ) {
166
+ return Ok ( ( preload. to_owned ( ) , path_buf) ) ;
167
+ }
168
+
169
+ Err ( USimpleError :: new (
170
+ 1 ,
171
+ format ! (
172
+ "External libstdbuf not found at configured path: {}" ,
173
+ path_buf. display( )
174
+ ) ,
175
+ ) )
176
+ }
177
+
150
178
#[ uucore:: main]
151
179
pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
152
180
let matches = uu_app ( ) . try_get_matches_from ( args) . with_exit_code ( 125 ) ?;
0 commit comments