Skip to content

Commit e29c367

Browse files
feat: clean-up the build script
1 parent dd2113d commit e29c367

1 file changed

Lines changed: 40 additions & 31 deletions

File tree

crates/exec-harness/build.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,50 +27,34 @@ struct PreloadConstants {
2727
/// Integration version reported to CodSpeed.
2828
/// This should match the version of the `codspeed` crate dependency.
2929
integration_version: &'static str,
30-
}
31-
32-
// TODO(COD-1736): Stop impersonating codspeed-rust 🥸
33-
const PRELOAD_CONSTANTS: PreloadConstants = PreloadConstants {
34-
uri_env: "CODSPEED_BENCH_URI",
35-
integration_name: "codspeed-rust",
36-
integration_version: "4.2.0",
37-
};
38-
39-
/// Filename for the preload shared library.
40-
const PRELOAD_LIB_FILENAME: &str = "libcodspeed_preload.so";
41-
42-
/// Paths required to build the preload shared library.
43-
struct PreloadBuildPaths {
44-
/// Path to the preload C source file (codspeed_preload.c).
45-
preload_c: PathBuf,
46-
/// Path to the core C source file from instrument-hooks.
47-
core_c: PathBuf,
48-
/// Path to the includes directory from instrument-hooks.
49-
includes_dir: PathBuf,
50-
/// Path where the output shared library will be written.
51-
output_lib: PathBuf,
30+
/// Filename for the preload shared library.
31+
preload_lib_filename: &'static str,
5232
}
5333

5434
fn main() {
5535
println!("cargo:rerun-if-changed=preload/codspeed_preload.c");
5636
println!("cargo:rerun-if-env-changed=CODSPEED_INSTRUMENT_HOOKS_DIR");
5737

38+
let preload_constants: PreloadConstants = PreloadConstants::default();
39+
5840
// Export constants as environment variables for the Rust code
5941
println!(
6042
"cargo:rustc-env=CODSPEED_URI_ENV={}",
61-
PRELOAD_CONSTANTS.uri_env
43+
preload_constants.uri_env
6244
);
6345
println!(
6446
"cargo:rustc-env=CODSPEED_INTEGRATION_NAME={}",
65-
PRELOAD_CONSTANTS.integration_name
47+
preload_constants.integration_name
6648
);
6749
println!(
6850
"cargo:rustc-env=CODSPEED_INTEGRATION_VERSION={}",
69-
PRELOAD_CONSTANTS.integration_version
51+
preload_constants.integration_version
52+
);
53+
println!(
54+
"cargo:rustc-env=CODSPEED_PRELOAD_LIB_FILENAME={}",
55+
preload_constants.preload_lib_filename
7056
);
71-
println!("cargo:rustc-env=CODSPEED_PRELOAD_LIB_FILENAME={PRELOAD_LIB_FILENAME}");
7257

73-
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
7458
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
7559

7660
// Try to get the instrument-hooks directory from the environment variable first,
@@ -85,25 +69,28 @@ fn main() {
8569
preload_c: manifest_dir.join("preload/codspeed_preload.c"),
8670
core_c: instrument_hooks_dir.join("dist/core.c"),
8771
includes_dir: instrument_hooks_dir.join("includes"),
88-
output_lib: out_dir.join(PRELOAD_LIB_FILENAME),
8972
};
9073
paths.check_sources_exist();
91-
build_shared_library(&paths, &PRELOAD_CONSTANTS);
74+
build_shared_library(&paths, &preload_constants);
9275
}
9376

9477
/// Build the shared library using the cc crate
9578
fn build_shared_library(paths: &PreloadBuildPaths, constants: &PreloadConstants) {
9679
let uri_env_val = format!("\"{}\"", constants.uri_env);
9780
let integration_name_val = format!("\"{}\"", constants.integration_name);
9881
let integration_version_val = format!("\"{}\"", constants.integration_version);
82+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
83+
let out_file = out_dir.join(constants.preload_lib_filename);
9984

10085
let mut build = cc::Build::new();
10186
build
10287
.file(&paths.preload_c)
10388
.file(&paths.core_c)
10489
.include(&paths.includes_dir)
10590
.pic(true)
106-
.opt_level(2)
91+
.opt_level(3)
92+
// There's no need to output cargo metadata as we are just building a shared library
93+
// that will be copied to disk and loaded through LD_PRELOAD at runtime
10794
.cargo_metadata(false)
10895
// Pass constants as C defines
10996
.define("CODSPEED_URI_ENV", uri_env_val.as_str())
@@ -131,7 +118,7 @@ fn build_shared_library(paths: &PreloadBuildPaths, constants: &PreloadConstants)
131118
link_cmd
132119
.arg("-shared")
133120
.arg("-o")
134-
.arg(&paths.output_lib)
121+
.arg(&out_file)
135122
.args(&objects)
136123
.arg("-lpthread");
137124

@@ -168,6 +155,28 @@ fn find_codspeed_instrument_hooks_dir() -> PathBuf {
168155
instrument_hooks_dir.into_std_path_buf()
169156
}
170157

158+
impl Default for PreloadConstants {
159+
// TODO(COD-1736): Stop impersonating codspeed-rust 🥸
160+
fn default() -> Self {
161+
Self {
162+
uri_env: "CODSPEED_BENCH_URI",
163+
integration_name: "codspeed-rust",
164+
integration_version: "4.2.0",
165+
preload_lib_filename: "libcodspeed_preload.so",
166+
}
167+
}
168+
}
169+
170+
/// Paths required to build the preload shared library.
171+
struct PreloadBuildPaths {
172+
/// Path to the preload C source file (codspeed_preload.c).
173+
preload_c: PathBuf,
174+
/// Path to the core C source file from instrument-hooks.
175+
core_c: PathBuf,
176+
/// Path to the includes directory from instrument-hooks.
177+
includes_dir: PathBuf,
178+
}
179+
171180
impl PreloadBuildPaths {
172181
/// Verify that all required source files and directories exist.
173182
/// Panics with a descriptive message if any path is missing.

0 commit comments

Comments
 (0)