#build-script #cfg #macro #directive #evaluate

build build_cfg

Evaluate cfg() directives in build scripts

2 releases (1 stable)

1.1.0 Jan 13, 2022
0.1.0 Sep 26, 2021

#323 in FFI

Download history 26/week @ 2023-11-20 23/week @ 2023-11-27 30/week @ 2023-12-04 27/week @ 2023-12-11 40/week @ 2023-12-18 20/week @ 2023-12-25 2/week @ 2024-01-01 34/week @ 2024-01-08 25/week @ 2024-01-15 61/week @ 2024-01-22 15/week @ 2024-01-29 33/week @ 2024-02-05 28/week @ 2024-02-12 58/week @ 2024-02-19 67/week @ 2024-02-26 86/week @ 2024-03-04

243 downloads per month
Used in 5 crates (4 directly)

MIT license

9KB
177 lines

../README.md


lib.rs:

crates.io

build_cfg

Test cfg directives at build time!

Currently, cfg directives do not work "correctly" in a build script. This is because the build script must be compiled for the host machine to execute. Therefore, it is always compiled to target the native environment.

This means that we can't conditionally compile stuff in our build scripts depending on the target platform we pass in with --target. This crate aims to solve this problem by collecting and evaluating cfg directives at runtime during the build script.

Example

#[macro_use]
extern crate build_cfg;

#[build_cfg_main]
fn main() {
    if build_cfg!(all(target_os = "windows", target_pointer_width = "64")) {
        println!("Building for Windows 64-bit");
    } else {
        println!("Not building for Windows 64-bit");
    }
}

Dependencies

~1.5MB
~34K SLoC