Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURES_LOWERCASE contains features that do not exists #74

Open
barakugav opened this issue Nov 27, 2024 · 0 comments
Open

FEATURES_LOWERCASE contains features that do not exists #74

barakugav opened this issue Nov 27, 2024 · 0 comments

Comments

@barakugav
Copy link

The FEATURES_LOWERCASE static variable is created by searching for env variables with prefix CARGO_FEATURE_, and parsing the suffixes to extract features names.
Its correct that every feature has such env variable, but not each such env variable is a real enabled feature.
For example, when I compile my executable I get these additional features that are not part of my crate:

"libc", "numpy", "pyo3"

I can't say why these environment variables were on, but its not due to exports in bashrc or something like that, probably env variables manipulations by my dependencies build scripts.
Anyway, I think a better approach is to parse Cargo.toml and extract all of the possible features of the current built crate, and than check if there is an env variable called format!(CARGO_FEATURE_{}", feature.to_uppercase().replace("-", "_")).

This is how I do it in my build.rs:

fn enabled_features() -> Vec<String> {
    let cargo_toml = Path::new(&env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
    fs::read_to_string(cargo_toml)
        .expect("Failed to read Cargo.toml")
        .parse::<toml::Table>()
        .expect("Failed to parse Cargo.toml")
        .into_iter()
        .flat_map(|features| {
            features
                .as_table()
                .expect("features must be a table")
                .keys()
                .filter(|k| *k != "default")
                .cloned()
        })
        .filter(|f| {
            env::var(format!(
                "CARGO_FEATURE_{}",
                f.to_uppercase().replace("-", "_")
            ))
            .is_ok()
        })
        .collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant