Skip to content

Instantly share code, notes, and snippets.

@langyo
Last active December 4, 2023 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langyo/926c643a67e0361da8ccf5befe52298d to your computer and use it in GitHub Desktop.
Save langyo/926c643a67e0361da8ccf5befe52298d to your computer and use it in GitHub Desktop.
Rust demo - Select custom mod by env
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();
}
// 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);
}
[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
'''
#[allow(dead_code)]
pub fn log() {
println!("Hello from {}", file!());
}
#[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