@@ -25,7 +25,16 @@ pub(crate) fn compile_cdylib() {
2525/// Compiles the test C program with source at `src_path` into
2626/// an executable at `bin_path`.
2727pub ( 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) ;
2938 // We disable the usage of builtin functions, e.g., from libm.
3039 // This should ideally produce a link failure if libm is not dynamically
3140 // linked.
@@ -51,27 +60,23 @@ where
5160 let mut cmd = process:: Command :: new ( path) ;
5261
5362 // 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+ } ) ;
6268
6369 // Replace libm at runtime
6470 if cfg ! ( target_os = "macos" ) {
71+ let lib_path = libm_path. join ( "liblibm.dylib" ) ;
6572 // for debugging:
6673 // cmd.env("DYLD_PRINT_LIBRARIES", "1");
6774 // cmd.env("X", "1");
6875 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 ( ) ) ;
7377 } 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 ( ) ) ;
7580 }
7681 // Run the binary:
7782 let output = cmd. output ( ) . unwrap ( ) ;
@@ -137,3 +142,11 @@ pub(crate) fn ctype_and_printf_format_specifier(x: &str) -> (&str, &str) {
137142 _ => panic ! ( "unknown type: {}" , x) ,
138143 }
139144}
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