Skip to content

Commit 2577ca4

Browse files
committed
Use #[cfg()] instead of if cfg!()
Actually conditionally compile instead of checking at runtime
1 parent 66a42fb commit 2577ca4

File tree

3 files changed

+74
-49
lines changed

3 files changed

+74
-49
lines changed

Launcher/src/config_wrapper.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ pub fn get_config_path() -> PathBuf {
2020
fn get_parent_folder() -> PathBuf {
2121
let current_exe = current_exe().unwrap();
2222

23-
if cfg!(target_os = "windows") || cfg!(target_os = "linux") {
24-
return current_exe.parent().unwrap().to_path_buf();
25-
} else if cfg!(target_os = "macos") {
23+
#[cfg(not(target_os = "macos"))]
24+
return current_exe.parent().unwrap().to_path_buf();
25+
26+
#[cfg(target_os = "macos")]
27+
{
2628
// MacOS has "application" *folders* (folders with the extension .app),
2729
// so we need to navigate up this.
2830
return current_exe // rhythm_doctor_editor_launcher
@@ -36,7 +38,6 @@ fn get_parent_folder() -> PathBuf {
3638
.unwrap()
3739
.to_path_buf();
3840
}
39-
panic!("Unsupported OS");
4041
}
4142

4243
pub fn build_config() -> Result<Config, ConfigError> {

Launcher/src/main.rs

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
use built::{BUILT_TIME_UTC, GIT_VERSION, PKG_VERSION, PROFILE, RUSTC_VERSION, TARGET};
44
use config_wrapper::{
5-
build_config, get_config_path, get_log_path, open_config_file, write_config_file,
5+
build_config, get_config_path, get_log_path, write_config_file,
66
};
7-
#[cfg(target_os = "macos")]
8-
use macos::event_loop;
97
use rhythm_doctor::launch_rhythm_doctor;
108

119
use log::{debug, error, info, warn};
@@ -15,9 +13,19 @@ use std::process::ExitCode;
1513

1614
mod built;
1715
mod config_wrapper;
18-
mod macos;
1916
mod rhythm_doctor;
2017

18+
// MacOS
19+
#[cfg(target_os = "macos")]
20+
use macos::event_loop;
21+
22+
#[cfg(target_os = "macos")]
23+
mod macos;
24+
25+
// Not MacOS
26+
#[cfg(not(target_os = "macos"))]
27+
use config_wrapper::open_config_file;
28+
2129
/// Forward the given launch option to Rhythm Doctor.
2230
fn main() -> ExitCode {
2331
init_log();
@@ -59,9 +67,12 @@ fn main() -> ExitCode {
5967

6068
let Some(level) = args.get(1) else {
6169
info!("Run with no arguments");
62-
if cfg!(target_os = "macos") {
63-
event_loop(with_steam);
64-
} else {
70+
71+
#[cfg(target_os = "macos")]
72+
event_loop(with_steam);
73+
74+
#[cfg(not(target_os = "macos"))]
75+
{
6576
info!("Opening configuration file");
6677
let _ = open_config_file();
6778
return ExitCode::from(1);
@@ -74,35 +85,35 @@ fn main() -> ExitCode {
7485

7586
/// Sets up logging
7687
fn init_log() {
77-
if cfg!(debug_assertions) {
78-
let _ = simplelog::CombinedLogger::init(vec![
79-
simplelog::TermLogger::new(
80-
simplelog::LevelFilter::Trace,
81-
simplelog::Config::default(),
82-
simplelog::TerminalMode::Stdout,
83-
simplelog::ColorChoice::Auto,
84-
),
85-
simplelog::WriteLogger::new(
86-
simplelog::LevelFilter::Trace,
87-
simplelog::Config::default(),
88-
std::fs::File::create(get_log_path()).unwrap(),
89-
),
90-
]);
91-
} else {
92-
let _ = simplelog::CombinedLogger::init(vec![
93-
simplelog::TermLogger::new(
94-
simplelog::LevelFilter::Info,
95-
simplelog::Config::default(),
96-
simplelog::TerminalMode::Stdout,
97-
simplelog::ColorChoice::Auto,
98-
),
99-
simplelog::WriteLogger::new(
100-
simplelog::LevelFilter::Info,
101-
simplelog::Config::default(),
102-
std::fs::File::create(get_log_path()).unwrap(),
103-
),
104-
]);
105-
}
88+
#[cfg(debug_assertions)]
89+
let _ = simplelog::CombinedLogger::init(vec![
90+
simplelog::TermLogger::new(
91+
simplelog::LevelFilter::Trace,
92+
simplelog::Config::default(),
93+
simplelog::TerminalMode::Stdout,
94+
simplelog::ColorChoice::Auto,
95+
),
96+
simplelog::WriteLogger::new(
97+
simplelog::LevelFilter::Trace,
98+
simplelog::Config::default(),
99+
std::fs::File::create(get_log_path()).unwrap(),
100+
),
101+
]);
102+
103+
#[cfg(not(debug_assertions))]
104+
let _ = simplelog::CombinedLogger::init(vec![
105+
simplelog::TermLogger::new(
106+
simplelog::LevelFilter::Info,
107+
simplelog::Config::default(),
108+
simplelog::TerminalMode::Stdout,
109+
simplelog::ColorChoice::Auto,
110+
),
111+
simplelog::WriteLogger::new(
112+
simplelog::LevelFilter::Info,
113+
simplelog::Config::default(),
114+
std::fs::File::create(get_log_path()).unwrap(),
115+
),
116+
]);
106117
}
107118

108119
fn open_rhythm_doctor_with_level(with_steam: bool, level: &str) -> ExitCode {
@@ -113,4 +124,4 @@ fn open_rhythm_doctor_with_level(with_steam: bool, level: &str) -> ExitCode {
113124
}
114125

115126
ExitCode::from(0)
116-
}
127+
}

Launcher/src/rhythm_doctor.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ pub fn launch_rhythm_doctor(path: &str, with_steam: bool) -> Result<(), String>
5151
// Couldn't open Steam, fallback to using Rhythm Doctor (slow!)
5252
warn!("Opening Rhythm Doctor directly");
5353
if let Some(mut game_path) = find_rhythm_doctor() {
54-
if cfg!(target_os = "windows") {
54+
#[cfg(target_os = "windows")]
55+
{
5556
game_path = game_path.join("Rhythm Doctor.exe");
56-
} else if cfg!(target_os = "macos") {
57+
}
58+
59+
#[cfg(target_os = "macos")]
60+
{
5761
game_path = game_path.join("Rhythm Doctor.app");
58-
} else if cfg!(target_os = "linux") {
62+
}
63+
64+
#[cfg(target_os = "linux")]
65+
{
5966
game_path = game_path.join("Rhythm Doctor");
60-
} else {
61-
return Err("Unsupported OS".to_owned());
6267
}
6368

6469
Command::new(game_path)
@@ -91,21 +96,29 @@ fn find_steam_executable() -> Option<PathBuf> {
9196

9297
// Try to find Steam based on default install locations
9398
trace!("Trying to find Steam based on default install locations");
94-
if cfg!(target_os = "windows") {
99+
100+
#[cfg(target_os = "windows")]
101+
{
95102
trace!(r"Checking for Steam executable at C:\Program Files (x86)\Steam\steam.exe");
96103
if Path::new(r"C:\Program Files (x86)\Steam\steam.exe").exists() {
97104
info!(r"Found Steam executable at C:\Program Files (x86)\Steam\steam.exe");
98105
return Some(PathBuf::from(r"C:\Program Files (x86)\Steam\steam.exe"));
99106
}
100-
} else if cfg!(target_os = "macos") {
107+
}
108+
109+
#[cfg(target_os = "macos")]
110+
{
101111
trace!("Checking for Steam executable at /Applications/Steam.app/Contents/MacOS/steam_osx");
102112
if Path::new("/Applications/Steam.app/Contents/MacOS/steam_osx").exists() {
103113
info!("Found Steam executable at /Applications/Steam.app/Contents/MacOS/steam_osx");
104114
return Some(PathBuf::from(
105115
"/Applications/Steam.app/Contents/MacOS/steam_osx",
106116
));
107117
}
108-
} else if cfg!(target_os = "linux") {
118+
}
119+
120+
#[cfg(target_os = "linux")]
121+
{
109122
// TODO: Add flatpak, native, etc
110123
// We should have better luck searching on PATH anyways.
111124
// Official .deb package from https://cdn.cloudflare.steamstatic.com/client/installer/steam.deb

0 commit comments

Comments
 (0)