anki/qt/bundle/build.rs
Damien Elmes 95dbf30fb9 updates to the build process and binary bundles
All platforms:

- rename scripts/ to tools/: Bazelisk expects to find its wrapper script
(used by the Mac changes below) in tools/. Rather than have a separate
scripts/ and tools/, it's simpler to just move everything into tools/.
- wheel outputs and binary bundles now go into .bazel/out/dist. While
not technically Bazel build products, doing it this way ensures they get
cleaned up when 'bazel clean' is run, and it keeps them out of the source
folder.
- update to the latest Bazel

Windows changes:

- bazel.bat has been removed, and tools\setup-env.bat has been added.
Other scripts like .\run.bat will automatically call it to set up the
environment.
- because Bazel is now on the path, you can 'bazel test ...' from any
folder, instead of having to do \anki\bazel.
- the bat files can handle being called from any working directory,
so things like running "\anki\tools\python" from c:\ will work.
- build installer as part of bundling process

Mac changes:

- `arch -arch x86_64 bazel ...` will now automatically use a different
build root, so that it is cheap to switch back and forth between archs
on a new Mac.
- tools/run-qt* will now automatically use Rosetta
- disable jemalloc in Mac x86 build for now, as it won't build under
Rosetta (perhaps due to its build scripts using $host_cpu instead of
$target_cpu)
- create app bundle as part of bundling process

Linux changes:

- remove arm64 orjson workaround in Linux bundle, as without a
readily-available, relatively distro-agonstic PyQt/Qt build
we can use, the arm64 Linux bundle is of very limited usefulness.
- update Docker files for release build
- include fcitx5 in both the qt5 and qt6 bundles
- create tarballs as part of the bundling process
2022-02-10 19:23:07 +10:00

110 lines
3.6 KiB
Rust

// Based off PyOxidizer's 'init-rust-project'.
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.
use {
embed_resource,
std::path::{Path, PathBuf},
};
const DEFAULT_PYTHON_CONFIG_FILENAME: &str = "default_python_config.rs";
const DEFAULT_PYTHON_CONFIG: &str = "\
pub fn default_python_config<'a>() -> pyembed::OxidizedPythonInterpreterConfig<'a> {
pyembed::OxidizedPythonInterpreterConfig::default()
}
";
/// Build with PyOxidizer artifacts in a directory.
fn build_with_artifacts_in_dir(path: &Path) {
println!("using pre-built artifacts from {}", path.display());
let config_path = path.join(DEFAULT_PYTHON_CONFIG_FILENAME);
if !config_path.exists() {
panic!(
"{} does not exist; is {} a valid artifacts directory?",
config_path.display(),
path.display()
);
}
println!(
"cargo:rustc-env=DEFAULT_PYTHON_CONFIG_RS={}",
config_path.display()
);
}
/// Build by calling a `pyoxidizer` executable to generate build artifacts.
fn build_with_pyoxidizer_exe(exe: Option<String>, resolve_target: Option<&str>) {
let pyoxidizer_exe = if let Some(path) = exe {
path
} else {
"pyoxidizer".to_string()
};
let mut args = vec!["run-build-script", "build.rs"];
if let Some(target) = resolve_target {
args.push("--target");
args.push(target);
}
match std::process::Command::new(pyoxidizer_exe)
.args(args)
.status()
{
Ok(status) => {
if !status.success() {
panic!("`pyoxidizer run-build-script` failed");
}
}
Err(e) => panic!("`pyoxidizer run-build-script` failed: {}", e.to_string()),
}
}
#[allow(clippy::if_same_then_else)]
fn main() {
if std::env::var("CARGO_FEATURE_BUILD_MODE_STANDALONE").is_ok() {
let path = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not defined"));
let path = path.join(DEFAULT_PYTHON_CONFIG_FILENAME);
std::fs::write(&path, DEFAULT_PYTHON_CONFIG.as_bytes())
.expect("failed to write default python config");
println!(
"cargo:rustc-env=DEFAULT_PYTHON_CONFIG_RS={}",
path.display()
);
} else if std::env::var("CARGO_FEATURE_BUILD_MODE_PYOXIDIZER_EXE").is_ok() {
let target = if let Ok(target) = std::env::var("PYOXIDIZER_BUILD_TARGET") {
Some(target)
} else {
None
};
build_with_pyoxidizer_exe(
std::env::var("PYOXIDIZER_EXE").ok(),
target.as_ref().map(|target| target.as_ref()),
);
} else if std::env::var("CARGO_FEATURE_BUILD_MODE_PREBUILT_ARTIFACTS").is_ok() {
let artifact_dir_env = std::env::var("PYOXIDIZER_ARTIFACT_DIR");
let artifact_dir_path = match artifact_dir_env {
Ok(ref v) => PathBuf::from(v),
Err(_) => {
let out_dir = std::env::var("OUT_DIR").unwrap();
PathBuf::from(&out_dir)
}
};
println!("cargo:rerun-if-env-changed=PYOXIDIZER_ARTIFACT_DIR");
build_with_artifacts_in_dir(&artifact_dir_path);
} else {
panic!("build-mode-* feature not set");
}
let target_family =
std::env::var("CARGO_CFG_TARGET_FAMILY").expect("CARGO_CFG_TARGET_FAMILY not defined");
// embed manifest and icon
if target_family == "windows" {
embed_resource::compile("win/anki-manifest.rc");
}
}