Skip to main content

multivers_runner/
lib.rs

1#![doc = "README.md"]
2#![cfg_attr(not(test), no_main)]
3#![cfg_attr(test, allow(dead_code))]
4
5mod build;
6
7use build::{Build, Executable};
8
9/// Function called at program startup.
10///
11/// When [main] is executed, it uncompresses and executes the version that matches the CPU features
12/// of the host.
13///
14/// # Example
15///
16/// ```no_run
17/// #![no_main]
18///
19/// pub use multivers_runner::main;
20/// ```
21///
22/// # Safety
23///
24/// - `argc` must never be negative.
25/// - `argv` and `envp` must be null-terminated arrays of valid pointers to null-terminated strings.
26/// - Each element of `argv` and `envp` must be valid for reads of bytes up to and including the null terminator.
27#[unsafe(no_mangle)]
28#[cfg(not(test))]
29pub unsafe extern "C" fn main(argc: i32, argv: *const *const i8, envp: *const *const i8) {
30    let result = unsafe { run(argc, argv, envp) };
31
32    proc_exit::exit(result);
33}
34
35unsafe fn run(argc: i32, argv: *const *const i8, envp: *const *const i8) -> proc_exit::ExitResult {
36    #[cfg(feature = "debug")]
37    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();
38
39    let build = Build::find().unwrap_or_default();
40
41    #[cfg(feature = "debug")]
42    log::debug!(
43        "Executing build with the following CPU features: {}",
44        build.features()
45    );
46
47    unsafe { build.exec(argc, argv, envp) }?;
48
49    Ok(())
50}