Last active
December 4, 2023 13:57
-
-
Save langyo/926c643a67e0361da8ccf5befe52298d to your computer and use it in GitHub Desktop.
Rust demo - Select custom mod by env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
if std::env::var("TARGET_2").is_ok() { | |
println!("cargo:rustc-cfg=target_2_enabled"); | |
} | |
let manifest_dir = | |
std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get manifest directory"); | |
std::fs::write( | |
std::path::Path::new(&manifest_dir) | |
.join("src") | |
.join("manifest_dir.rs"), | |
format!("pub const MANIFEST_DIR: &str = {:?};", manifest_dir), | |
) | |
.unwrap(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let all the components join the syntax check. | |
mod mod_target_1; | |
mod mod_target_2; | |
#[cfg(target_2_enabled)] | |
#[path = "mod_target_2.rs"] | |
mod mod_target; | |
#[cfg(not(target_2_enabled))] | |
#[path = "mod_target_1.rs"] | |
mod mod_target; | |
mod manifest_dir; | |
fn main() { | |
mod_target::log(); | |
println!("MANIFEST_DIR: {}", manifest_dir::MANIFEST_DIR); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[config] | |
default_to_workspace = false | |
skip_core_tasks = true | |
[tasks.test-target-1] | |
script = ''' | |
#!@duckscript | |
exec cargo run | |
''' | |
[tasks.test-target-2] | |
script = ''' | |
#!@duckscript | |
set_env TARGET_2 true | |
exec cargo run | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code)] | |
pub fn log() { | |
println!("Hello from {}", file!()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code)] | |
pub fn log() { | |
println!("Hello from {}", file!()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment