make console optional on Windows

This commit is contained in:
Damien Elmes 2021-10-28 18:57:21 +10:00
parent 5a8e064a7d
commit 568dbe798a
6 changed files with 65 additions and 3 deletions

13
qt/package/Cargo.lock generated
View file

@ -17,9 +17,12 @@ version = "0.1.0"
dependencies = [
"embed-resource",
"jemallocator",
"libc",
"libc-stdhandle",
"mimalloc",
"pyembed",
"snmalloc-rs",
"winapi",
]
[[package]]
@ -204,6 +207,16 @@ version = "0.2.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013"
[[package]]
name = "libc-stdhandle"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dac2473dc28934c5e0b82250dab231c9d3b94160d91fe9ff483323b05797551"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "libmimalloc-sys"
version = "0.1.22"

View file

@ -4,7 +4,10 @@ version = "0.1.0"
build = "build.rs"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[target.'cfg(windows)'.dependencies]
winapi = {version = "0.3", features = ["wincon"]}
libc = "0.2"
libc-stdhandle = "=0.1.0"
[dependencies.pyembed]
git = "https://github.com/ankitects/PyOxidizer.git"

4
qt/package/rustfmt.toml Normal file
View file

@ -0,0 +1,4 @@
# this is not supported on stable Rust, and is ignored by the Bazel rules; it is only
# useful for manual invocation with 'cargo +nightly fmt'
imports_granularity = "Crate"
group_imports = "StdExternalCrate"

35
qt/package/src/anki.rs Normal file
View file

@ -0,0 +1,35 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
pub(super) fn init() {
#[cfg(target_os = "windows")]
attach_console();
println!("Anki starting...");
}
/// If parent process has a console (eg cmd.exe), redirect our output there.
#[cfg(target_os = "windows")]
fn attach_console() {
use std::ffi::CString;
use libc_stdhandle::*;
use winapi::um::wincon;
let console_attached = unsafe { wincon::AttachConsole(wincon::ATTACH_PARENT_PROCESS) };
if console_attached == 0 {
return;
}
let conin = CString::new("CONIN$").unwrap();
let conout = CString::new("CONOUT$").unwrap();
let r = CString::new("r").unwrap();
let w = CString::new("w").unwrap();
// Python uses the CRT for I/O, and it requires the descriptors are reopened.
unsafe {
libc::freopen(conin.as_ptr(), r.as_ptr(), stdin());
libc::freopen(conout.as_ptr(), w.as_ptr(), stdout());
libc::freopen(conout.as_ptr(), w.as_ptr(), stderr());
}
}

View file

@ -3,7 +3,9 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#![windows_subsystem = "console"]
#![windows_subsystem = "windows"]
mod anki;
use pyembed::{MainPythonInterpreter, OxidizedPythonInterpreterConfig};
@ -14,7 +16,7 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
include!(env!("DEFAULT_PYTHON_CONFIG_RS"));
fn main() {
println!("Anki starting...");
anki::init();
let exit_code = {
let config: OxidizedPythonInterpreterConfig = default_python_config();

View file

@ -0,0 +1,5 @@
@echo off
anki
pause