{"id":17604286,"url":"https://github.com/katharostech/cfg_aliases","last_synced_at":"2025-04-05T04:14:04.016Z","repository":{"id":57546491,"uuid":"253269251","full_name":"katharostech/cfg_aliases","owner":"katharostech","description":"A tiny utility to help save you a lot of effort with long winded `#[cfg()]` checks in Rust.","archived":false,"fork":false,"pushed_at":"2024-09-09T15:27:29.000Z","size":34,"stargazers_count":62,"open_issues_count":1,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-29T16:57:01.581Z","etag":null,"topics":["rust"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/katharostech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-05T15:37:44.000Z","updated_at":"2024-09-09T15:27:34.000Z","dependencies_parsed_at":"2023-12-22T18:40:20.740Z","dependency_job_id":"e0212c07-eee6-495d-89bf-13f6a00a8b47","html_url":"https://github.com/katharostech/cfg_aliases","commit_stats":{"total_commits":21,"total_committers":5,"mean_commits":4.2,"dds":"0.23809523809523814","last_synced_commit":"980578175eb5908e24199205e98553f8f5826a4a"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katharostech%2Fcfg_aliases","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katharostech%2Fcfg_aliases/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katharostech%2Fcfg_aliases/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/katharostech%2Fcfg_aliases/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/katharostech","download_url":"https://codeload.github.com/katharostech/cfg_aliases/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247134373,"owners_count":20889396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rust"],"created_at":"2024-10-22T14:08:27.193Z","updated_at":"2025-04-05T04:14:03.996Z","avatar_url":"https://github.com/katharostech.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CFG Aliases\n\nCFG Aliases is a tiny utility to help save you a lot of effort with long winded `#[cfg()]` checks. This crate provides a single [`cfg_aliases!`] macro that doesn't have any dependencies and specifically avoids pulling in `syn` or `quote` so that the impact on your compile times should be negligible.\n\nYou use the the [`cfg_aliases!`] macro in your `build.rs` script to define aliases such as `x11` that could then be used in the `cfg` attribute or macro for conditional compilation: `#[cfg(x11)]`.\n\n## Example\n\n**Cargo.toml:**\n\n```toml\n[build-dependencies]\ncfg_aliases = \"0.2.1\"\n```\n\n**build.rs:**\n\n```rust\nuse cfg_aliases::cfg_aliases;\n\nfn main() {\n    // Setup cfg aliases\n    cfg_aliases! {\n        // Platforms\n        wasm: { target_arch = \"wasm32\" },\n        android: { target_os = \"android\" },\n        macos: { target_os = \"macos\" },\n        linux: { target_os = \"linux\" },\n        // Backends\n        surfman: { all(unix, feature = \"surfman\", not(wasm)) },\n        glutin: { all(feature = \"glutin\", not(wasm)) },\n        wgl: { all(windows, feature = \"wgl\", not(wasm)) },\n        dummy: { not(any(wasm, glutin, wgl, surfman)) },\n    }\n}\n```\n\nNow that we have our aliases setup we can use them just like you would expect:\n\n```rust\n#[cfg(wasm)]\nprintln!(\"This is running in WASM\");\n\n#[cfg(surfman)]\n{\n    // Do stuff related to surfman\n}\n\n#[cfg(dummy)]\nprintln!(\"We're in dummy mode, specify another feature if you want a smarter app!\");\n```\n\nThis greatly improves what would otherwise look like this without the aliases:\n\n```rust\n#[cfg(target_arch = \"wasm32\")]\nprintln!(\"We're running in WASM\");\n\n#[cfg(all(unix, feature = \"surfman\", not(target_arch = \"wasm32\")))]\n{\n    // Do stuff related to surfman\n}\n\n#[cfg(not(any(\n    target_arch = \"wasm32\",\n    all(unix, feature = \"surfman\", not(target_arch = \"wasm32\")),\n    all(windows, feature = \"wgl\", not(target_arch = \"wasm32\")),\n    all(feature = \"glutin\", not(target_arch = \"wasm32\")),\n)))]\nprintln!(\"We're in dummy mode, specify another feature if you want a smarter app!\");\n```\n\nYou can also use the `cfg!` macro or combine your aliases with other checks using `all()`, `not()`, and `any()`. Your aliases are genuine `cfg` flags now!\n\n```rust\nif cfg!(glutin) {\n    // use glutin\n} else {\n    // Do something else\n}\n\n#[cfg(all(glutin, surfman))]\ncompile_error!(\"You cannot specify both `glutin` and `surfman` features\");\n```\n\n## Syntax and Error Messages\n\nThe aliase names are restricted to the same rules as rust identifiers which, for one, means that they cannot have dashes ( `-` ) in them. Additionally, if you get certain syntax elements wrong, such as the alias name, the macro will error saying that the recursion limit was reached instead of giving a clear indication of what actually went wrong. This is due to a nuance with the macro parser and it might be fixed in a later release of this crate. It is also possible that aliases with dashes in the name might be supported in a later release. Open an issue if that is something that you would like implemented.\n\nFinally, you can also induce an infinite recursion by having rules that both reference each-other, but this isn't a real limitation because that doesn't make logical sense anyway:\n\n```rust,ignore\n// This causes an error!\ncfg_aliases! {\n    test1: { not(test2) },\n    test2: { not(test1) },\n}\n```\n\n## Attribution and Thanks\n\n- Thanks to my God and Father who led me through figuring this out and to whome I owe everything.\n- Thanks to @Yandros on the Rust forum for [showing me][sm] some crazy macro hacks!\n- Thanks to @sfackler for [pointing out][po] the way to make cargo add the cfg flags.\n- Thanks to the authors of the [`tectonic_cfg_support::target_cfg`] macro from which most of the cfg attribute parsing logic is taken from. Also thanks to @ratmice for [bringing it up][bip] on the Rust forum.\n\n[`tectonic_cfg_support::target_cfg`]: https://docs.rs/tectonic_cfg_support/0.0.1/src/tectonic_cfg_support/lib.rs.html#166-298\n[po]: https://users.rust-lang.org/t/any-such-thing-as-cfg-aliases/40100/2\n[bip]: https://users.rust-lang.org/t/any-such-thing-as-cfg-aliases/40100/13\n[sm]: https://users.rust-lang.org/t/any-such-thing-as-cfg-aliases/40100/3\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkatharostech%2Fcfg_aliases","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkatharostech%2Fcfg_aliases","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkatharostech%2Fcfg_aliases/lists"}