-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is necessary on Windows, where we can't have the same binary serve as both a GUI and a console program, due to the constraints of the PE binary format. Instead, we add a new 'packetry-cli' console binary which simply acts as a wrapper around the main GUI binary. The wrapper sets the PACKETRY_ATTACH_CONSOLE environment variable, which tells the main process to attach to the wrapping parent's console. The wrapper waits for the child to complete, and mirrors its exit code.
- Loading branch information
1 parent
e27176c
commit 7aa98fd
Showing
6 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::process::{Command, ExitCode}; | ||
|
||
#[cfg(windows)] | ||
const MAIN_BINARY: &str = "packetry.exe"; | ||
#[cfg(not(windows))] | ||
const MAIN_BINARY: &str = "packetry"; | ||
|
||
fn main() -> ExitCode { | ||
// Find the main Packetry executable. | ||
let packetry_binary_path = std::env::current_exe() | ||
.expect("Failed to find path to current executable") | ||
.parent() | ||
.expect("Failed to find parent directory of current executable") | ||
.join(MAIN_BINARY); | ||
|
||
// Prepare to call it, passing through all arguments we were passed. | ||
let mut command = Command::new(packetry_binary_path); | ||
command.args(std::env::args().skip(1)); | ||
|
||
// If on Windows, tell the child that it needs to attach to our console. | ||
#[cfg(windows)] | ||
command.env("PACKETRY_ATTACH_CONSOLE", "1"); | ||
|
||
// Spawn the main binary as a child process, and wait for its exit status. | ||
let exit_status = command | ||
.status() | ||
.expect("Failed to start main packetry binary"); | ||
|
||
// Try to exit with the same code the child did. | ||
match exit_status.code() { | ||
Some(code) => ExitCode::from(code as u8), | ||
None => { | ||
#[cfg(unix)] | ||
if let Some(signal) = exit_status.signal() { | ||
panic!("Packetry was terminated by signal {signal}"); | ||
} | ||
panic!("Packetry was terminated without an exit code"); | ||
} | ||
} | ||
} |
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
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