Build the shared library using the cc crate
(paths: &PreloadBuildPaths, constants: &PreloadConstants)
| 69 | |
| 70 | /// Build the shared library using the cc crate |
| 71 | fn build_shared_library(paths: &PreloadBuildPaths, constants: &PreloadConstants) { |
| 72 | let uri_env_val = format!("\"{}\"", constants.uri_env); |
| 73 | let integration_name_val = format!("\"{}\"", constants.integration_name); |
| 74 | let integration_version_val = format!("\"{}\"", constants.integration_version); |
| 75 | let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 76 | let out_file = out_dir.join(constants.preload_lib_filename); |
| 77 | |
| 78 | let mut build = cc::Build::new(); |
| 79 | build |
| 80 | .file(&paths.preload_c) |
| 81 | .file(&paths.core_c) |
| 82 | .include(&paths.includes_dir) |
| 83 | .pic(true) |
| 84 | .opt_level(3) |
| 85 | // There's no need to output cargo metadata as we are just building a shared library |
| 86 | // that will be copied to disk and loaded through LD_PRELOAD at runtime |
| 87 | .cargo_metadata(false) |
| 88 | // Pass constants as C defines |
| 89 | .define("CODSPEED_URI_ENV", uri_env_val.as_str()) |
| 90 | .define("CODSPEED_INTEGRATION_NAME", integration_name_val.as_str()) |
| 91 | .define( |
| 92 | "CODSPEED_INTEGRATION_VERSION", |
| 93 | integration_version_val.as_str(), |
| 94 | ) |
| 95 | .std("gnu11") // need gnu11 instead of just c11 for setenv |
| 96 | // Suppress warnings from generated Zig code |
| 97 | .flag("-Wno-format") |
| 98 | .flag("-Wno-format-security") |
| 99 | .flag("-Wno-unused-but-set-variable") |
| 100 | .flag("-Wno-unused-const-variable") |
| 101 | .flag("-Wno-type-limits") |
| 102 | .flag("-Wno-uninitialized") |
| 103 | .flag("-Wno-overflow") |
| 104 | .flag("-Wno-unused-function") |
| 105 | .flag("-Wno-unterminated-string-initialization"); |
| 106 | |
| 107 | // Compile source files to object files |
| 108 | let objects = build.compile_intermediates(); |
| 109 | |
| 110 | // Link object files into shared library |
| 111 | let compiler = build.get_compiler(); |
| 112 | let mut link_cmd = compiler.to_command(); |
| 113 | link_cmd |
| 114 | .arg("-shared") |
| 115 | .arg("-o") |
| 116 | .arg(&out_file) |
| 117 | .args(&objects) |
| 118 | .arg("-lpthread"); |
| 119 | |
| 120 | let status = link_cmd.status().expect("Failed to run linker"); |
| 121 | if !status.success() { |
| 122 | panic!("Failed to link libcodspeed_preload.so"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl Default for PreloadConstants { |
| 127 | fn default() -> Self { |