@@ -25,7 +25,16 @@ pub(crate) fn compile_cdylib() {
25
25
/// Compiles the test C program with source at `src_path` into
26
26
/// an executable at `bin_path`.
27
27
pub ( crate ) fn compile_file ( src_path : & Path , bin_path : & Path ) {
28
- let mut cmd = process:: Command :: new ( "CC" ) ;
28
+ let cc = if std:: env:: var ( "CC" ) . is_ok ( ) {
29
+ std:: env:: var ( "CC" ) . unwrap ( ) . to_string ( )
30
+ } else if cfg ! ( target_os = "linux" ) {
31
+ "gcc" . to_string ( )
32
+ } else if cfg ! ( target_os = "macos" ) {
33
+ "clang" . to_string ( )
34
+ } else {
35
+ panic ! ( "unknown platform - Ccompiler not found" )
36
+ } ;
37
+ let mut cmd = process:: Command :: new ( & cc) ;
29
38
// We disable the usage of builtin functions, e.g., from libm.
30
39
// This should ideally produce a link failure if libm is not dynamically
31
40
// linked.
@@ -51,27 +60,23 @@ where
51
60
let mut cmd = process:: Command :: new ( path) ;
52
61
53
62
// Find the cdylib - we just support standard locations for now.
54
- let libm_path = format ! (
55
- "../../target/{}/liblibm" ,
56
- if cfg!( release_profile) {
57
- "release"
58
- } else {
59
- "debug"
60
- } ,
61
- ) ;
63
+ let libm_path = target_dir ( ) . join ( if cfg ! ( release_profile) {
64
+ "release"
65
+ } else {
66
+ "debug"
67
+ } ) ;
62
68
63
69
// Replace libm at runtime
64
70
if cfg ! ( target_os = "macos" ) {
71
+ let lib_path = libm_path. join ( "liblibm.dylib" ) ;
65
72
// for debugging:
66
73
// cmd.env("DYLD_PRINT_LIBRARIES", "1");
67
74
// cmd.env("X", "1");
68
75
cmd. env ( "DYLD_FORCE_FLAT_NAMESPACE" , "1" ) ;
69
- cmd. env (
70
- "DYLD_INSERT_LIBRARIES" ,
71
- format ! ( "{}.{}" , libm_path, "dylib" ) ,
72
- ) ;
76
+ cmd. env ( "DYLD_INSERT_LIBRARIES" , lib_path. display ( ) . to_string ( ) ) ;
73
77
} else if cfg ! ( target_os = "linux" ) {
74
- cmd. env ( "LD_PRELOAD" , format ! ( "{}.{}" , libm_path, "so" ) ) ;
78
+ let lib_path = libm_path. join ( "liblibm.so" ) ;
79
+ cmd. env ( "LD_PRELOAD" , lib_path. display ( ) . to_string ( ) ) ;
75
80
}
76
81
// Run the binary:
77
82
let output = cmd. output ( ) . unwrap ( ) ;
@@ -137,3 +142,11 @@ pub(crate) fn ctype_and_printf_format_specifier(x: &str) -> (&str, &str) {
137
142
_ => panic ! ( "unknown type: {}" , x) ,
138
143
}
139
144
}
145
+
146
+ pub ( crate ) fn target_dir ( ) -> std:: path:: PathBuf {
147
+ if let Ok ( dir) = std:: env:: var ( "CARGO_TARGET_DIR" ) {
148
+ std:: path:: PathBuf :: from ( & dir)
149
+ } else {
150
+ Path :: new ( "../../target" ) . into ( )
151
+ }
152
+ }
0 commit comments