mirror of
https://github.com/ankitects/anki.git
synced 2026-01-15 06:49:03 -05:00
(for upgrading users, please see the notes at the bottom) Bazel brought a lot of nice things to the table, such as rebuilds based on content changes instead of modification times, caching of build products, detection of incorrect build rules via a sandbox, and so on. Rewriting the build in Bazel was also an opportunity to improve on the Makefile-based build we had prior, which was pretty poor: most dependencies were external or not pinned, and the build graph was poorly defined and mostly serialized. It was not uncommon for fresh checkouts to fail due to floating dependencies, or for things to break when trying to switch to an older commit. For day-to-day development, I think Bazel served us reasonably well - we could generally switch between branches while being confident that builds would be correct and reasonably fast, and not require full rebuilds (except on Windows, where the lack of a sandbox and the TS rules would cause build breakages when TS files were renamed/removed). Bazel achieves that reliability by defining rules for each programming language that define how source files should be turned into outputs. For the rules to work with Bazel's sandboxing approach, they often have to reimplement or partially bypass the standard tools that each programming language provides. The Rust rules call Rust's compiler directly for example, instead of using Cargo, and the Python rules extract each PyPi package into a separate folder that gets added to sys.path. These separate language rules allow proper declaration of inputs and outputs, and offer some advantages such as caching of build products and fine-grained dependency installation. But they also bring some downsides: - The rules don't always support use-cases/platforms that the standard language tools do, meaning they need to be patched to be used. I've had to contribute a number of patches to the Rust, Python and JS rules to unblock various issues. - The dependencies we use with each language sometimes make assumptions that do not hold in Bazel, meaning they either need to be pinned or patched, or the language rules need to be adjusted to accommodate them. I was hopeful that after the initial setup work, things would be relatively smooth-sailing. Unfortunately, that has not proved to be the case. Things frequently broke when dependencies or the language rules were updated, and I began to get frustrated at the amount of Anki development time I was instead spending on build system upkeep. It's now about 2 years since switching to Bazel, and I think it's time to cut losses, and switch to something else that's a better fit. The new build system is based on a small build tool called Ninja, and some custom Rust code in build/. This means that to build Anki, Bazel is no longer required, but Ninja and Rust need to be installed on your system. Python and Node toolchains are automatically downloaded like in Bazel. This new build system should result in faster builds in some cases: - Because we're using cargo to build now, Rust builds are able to take advantage of pipelining and incremental debug builds, which we didn't have with Bazel. It's also easier to override the default linker on Linux/macOS, which can further improve speeds. - External Rust crates are now built with opt=1, which improves performance of debug builds. - Esbuild is now used to transpile TypeScript, instead of invoking the TypeScript compiler. This results in faster builds, by deferring typechecking to test/check time, and by allowing more work to happen in parallel. As an example of the differences, when testing with the mold linker on Linux, adding a new message to tags.proto (which triggers a recompile of the bulk of the Rust and TypeScript code) results in a compile that goes from about 22s on Bazel to about 7s in the new system. With the standard linker, it's about 9s. Some other changes of note: - Our Rust workspace now uses cargo-hakari to ensure all packages agree on available features, preventing unnecessary rebuilds. - pylib/anki is now a PEP420 implicit namespace, avoiding the need to merge source files and generated files into a single folder for running. By telling VSCode about the extra search path, code completion now works with generated files without needing to symlink them into the source folder. - qt/aqt can't use PEP420 as it's difficult to get rid of aqt/__init__.py. Instead, the generated files are now placed in a separate _aqt package that's added to the path. - ts/lib is now exposed as @tslib, so the source code and generated code can be provided under the same namespace without a merging step. - MyPy and PyLint are now invoked once for the entire codebase. - dprint will be used to format TypeScript/json files in the future instead of the slower prettier (currently turned off to avoid causing conflicts). It can automatically defer to prettier when formatting Svelte files. - svelte-check is now used for typechecking our Svelte code, which revealed a few typing issues that went undetected with the old system. - The Jest unit tests now work on Windows as well. If you're upgrading from Bazel, updated usage instructions are in docs/development.md and docs/build.md. A summary of the changes: - please remove node_modules and .bazel - install rustup (https://rustup.rs/) - install rsync if not already installed (on windows, use pacman - see docs/windows.md) - install Ninja (unzip from https://github.com/ninja-build/ninja/releases/tag/v1.11.1 and place on your path, or from your distro/homebrew if it's 1.10+) - update .vscode/settings.json from .vscode.dist
337 lines
10 KiB
Rust
337 lines
10 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use std::{borrow::Cow, collections::HashMap};
|
|
|
|
use super::*;
|
|
use crate::{
|
|
action::BuildAction,
|
|
archives::{download_and_extract, OnlineArchive, Platform},
|
|
hash::simple_hash,
|
|
input::{space_separated, BuildInput},
|
|
};
|
|
|
|
pub fn node_archive(platform: Platform) -> OnlineArchive {
|
|
match platform {
|
|
Platform::LinuxX64 => OnlineArchive {
|
|
url: "https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-x64.tar.xz",
|
|
sha256: "4481a34bf32ddb9a9ff9540338539401320e8c3628af39929b4211ea3552a19e",
|
|
},
|
|
Platform::LinuxArm => OnlineArchive {
|
|
url: "https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-arm64.tar.xz",
|
|
sha256: "3904869935b7ecc51130b4b86486d2356539a174d11c9181180cab649f32cd2a",
|
|
},
|
|
Platform::MacX64 => OnlineArchive {
|
|
url: "https://nodejs.org/dist/v18.12.1/node-v18.12.1-darwin-x64.tar.xz",
|
|
sha256: "6c88d462550a024661e74e9377371d7e023321a652eafb3d14d58a866e6ac002",
|
|
},
|
|
Platform::MacArm => OnlineArchive {
|
|
url: "https://nodejs.org/dist/v18.12.1/node-v18.12.1-darwin-arm64.tar.xz",
|
|
sha256: "17f2e25d207d36d6b0964845062160d9ed16207c08d09af33b9a2fd046c5896f",
|
|
},
|
|
Platform::WindowsX64 => OnlineArchive {
|
|
url: "https://nodejs.org/dist/v18.12.1/node-v18.12.1-win-x64.zip",
|
|
sha256: "5478a5a2dce2803ae22327a9f8ae8494c1dec4a4beca5bbf897027380aecf4c7",
|
|
},
|
|
}
|
|
}
|
|
|
|
pub struct YarnSetup {}
|
|
|
|
impl BuildAction for YarnSetup {
|
|
fn command(&self) -> &str {
|
|
if cfg!(windows) {
|
|
"corepack.cmd enable yarn"
|
|
} else {
|
|
"corepack enable yarn"
|
|
}
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("", inputs![":extract:node"]);
|
|
build.add_outputs_ext(
|
|
"bin",
|
|
vec![if cfg!(windows) {
|
|
"extracted/node/yarn.cmd"
|
|
} else {
|
|
"extracted/node/bin/yarn"
|
|
}],
|
|
true,
|
|
);
|
|
}
|
|
|
|
fn check_output_timestamps(&self) -> bool {
|
|
true
|
|
}
|
|
}
|
|
pub struct YarnInstall<'a> {
|
|
pub package_json_and_lock: BuildInput,
|
|
pub exports: HashMap<&'a str, Vec<Cow<'a, str>>>,
|
|
}
|
|
|
|
impl BuildAction for YarnInstall<'_> {
|
|
fn command(&self) -> &str {
|
|
"$runner yarn $yarn $out"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("", &self.package_json_and_lock);
|
|
build.add_inputs("yarn", inputs![":yarn:bin"]);
|
|
build.add_outputs("out", vec!["node_modules/.marker"]);
|
|
for (key, value) in &self.exports {
|
|
let outputs: Vec<_> = value.iter().map(|o| format!("node_modules/{o}")).collect();
|
|
build.add_outputs_ext(*key, outputs, true);
|
|
}
|
|
}
|
|
|
|
fn check_output_timestamps(&self) -> bool {
|
|
true
|
|
}
|
|
}
|
|
|
|
fn with_cmd_ext(bin: &str) -> Cow<str> {
|
|
if cfg!(windows) {
|
|
format!("{bin}.cmd").into()
|
|
} else {
|
|
bin.into()
|
|
}
|
|
}
|
|
|
|
pub fn setup_node(
|
|
build: &mut Build,
|
|
archive: OnlineArchive,
|
|
binary_exports: &[&'static str],
|
|
mut data_exports: HashMap<&str, Vec<Cow<str>>>,
|
|
) -> Result<()> {
|
|
download_and_extract(
|
|
build,
|
|
"node",
|
|
archive,
|
|
hashmap! {
|
|
"bin" => vec![if cfg!(windows) { "node.exe" } else { "bin/node" }],
|
|
"npm" => vec![if cfg!(windows) { "npm.cmd " } else { "bin/npm" }]
|
|
},
|
|
)?;
|
|
build.add("yarn", YarnSetup {})?;
|
|
|
|
for binary in binary_exports {
|
|
data_exports.insert(
|
|
*binary,
|
|
vec![format!(".bin/{}", with_cmd_ext(binary)).into()],
|
|
);
|
|
}
|
|
build.add(
|
|
"node_modules",
|
|
YarnInstall {
|
|
package_json_and_lock: inputs!["yarn.lock", "package.json"],
|
|
exports: data_exports,
|
|
},
|
|
)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub struct EsbuildScript<'a> {
|
|
pub script: BuildInput,
|
|
pub entrypoint: BuildInput,
|
|
pub deps: BuildInput,
|
|
/// .js will be appended, and any extra extensions
|
|
pub output_stem: &'a str,
|
|
/// eg ['.css', '.html']
|
|
pub extra_exts: &'a [&'a str],
|
|
}
|
|
|
|
impl BuildAction for EsbuildScript<'_> {
|
|
fn command(&self) -> &str {
|
|
"$node_bin $script $entrypoint $out"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("node_bin", inputs![":extract:node:bin"]);
|
|
build.add_inputs("script", &self.script);
|
|
build.add_inputs("entrypoint", &self.entrypoint);
|
|
build.add_inputs("", inputs!["yarn.lock", ":node_modules", &self.deps]);
|
|
let stem = self.output_stem;
|
|
let mut outs = vec![format!("{stem}.js")];
|
|
outs.extend(self.extra_exts.iter().map(|ext| format!("{stem}.{ext}")));
|
|
build.add_outputs("out", outs);
|
|
}
|
|
}
|
|
|
|
pub struct DPrint {
|
|
pub inputs: BuildInput,
|
|
pub check_only: bool,
|
|
}
|
|
|
|
impl BuildAction for DPrint {
|
|
fn command(&self) -> &str {
|
|
"$dprint $mode"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("dprint", inputs![":node_modules:dprint"]);
|
|
build.add_inputs("", &self.inputs);
|
|
let mode = if self.check_only { "check" } else { "fmt" };
|
|
build.add_variable("mode", mode);
|
|
build.add_output_stamp(format!("tests/dprint.{mode}"));
|
|
}
|
|
}
|
|
|
|
pub struct SvelteCheck {
|
|
pub tsconfig: BuildInput,
|
|
pub inputs: BuildInput,
|
|
}
|
|
|
|
impl BuildAction for SvelteCheck {
|
|
fn command(&self) -> &str {
|
|
"$svelte-check --tsconfig $tsconfig $
|
|
--fail-on-warnings --threshold warning --use-new-transformation"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("svelte-check", inputs![":node_modules:svelte-check"]);
|
|
build.add_inputs("tsconfig", &self.tsconfig);
|
|
build.add_inputs("", &self.inputs);
|
|
build.add_inputs("", inputs!["yarn.lock"]);
|
|
let hash = simple_hash(&self.tsconfig);
|
|
build.add_output_stamp(format!("tests/svelte-check.{hash}"));
|
|
}
|
|
}
|
|
|
|
pub struct TypescriptCheck {
|
|
pub tsconfig: BuildInput,
|
|
pub inputs: BuildInput,
|
|
}
|
|
|
|
impl BuildAction for TypescriptCheck {
|
|
fn command(&self) -> &str {
|
|
"$tsc --noEmit -p $tsconfig"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("tsc", inputs![":node_modules:tsc"]);
|
|
build.add_inputs("tsconfig", &self.tsconfig);
|
|
build.add_inputs("", &self.inputs);
|
|
build.add_inputs("", inputs!["yarn.lock"]);
|
|
let hash = simple_hash(&self.tsconfig);
|
|
build.add_output_stamp(format!("tests/typescript.{hash}"));
|
|
}
|
|
}
|
|
|
|
pub struct Eslint<'a> {
|
|
pub folder: &'a str,
|
|
pub inputs: BuildInput,
|
|
pub eslint_rc: BuildInput,
|
|
pub fix: bool,
|
|
}
|
|
|
|
impl BuildAction for Eslint<'_> {
|
|
fn command(&self) -> &str {
|
|
"$eslint --max-warnings=0 -c $eslint_rc $fix $folder"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("eslint", inputs![":node_modules:eslint"]);
|
|
build.add_inputs("eslint_rc", &self.eslint_rc);
|
|
build.add_inputs("in", &self.inputs);
|
|
build.add_inputs("", inputs!["yarn.lock"]);
|
|
build.add_variable("fix", if self.fix { "--fix" } else { "" });
|
|
build.add_variable("folder", self.folder);
|
|
let hash = simple_hash(self.folder);
|
|
let kind = if self.fix { "fix" } else { "check" };
|
|
build.add_output_stamp(format!("tests/eslint.{kind}.{hash}"));
|
|
}
|
|
}
|
|
|
|
pub struct JestTest<'a> {
|
|
pub folder: &'a str,
|
|
pub deps: BuildInput,
|
|
pub jest_rc: BuildInput,
|
|
pub jsdom: bool,
|
|
}
|
|
|
|
impl BuildAction for JestTest<'_> {
|
|
fn command(&self) -> &str {
|
|
"$jest --config $config $env $folder"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("jest", inputs![":node_modules:jest"]);
|
|
build.add_inputs("", &self.deps);
|
|
build.add_inputs("config", &self.jest_rc);
|
|
build.add_variable("env", if self.jsdom { "--env=jsdom" } else { "" });
|
|
build.add_variable("folder", self.folder);
|
|
let hash = simple_hash(self.folder);
|
|
build.add_output_stamp(format!("tests/jest.{hash}"));
|
|
}
|
|
}
|
|
|
|
pub struct SqlFormat {
|
|
pub inputs: BuildInput,
|
|
pub check_only: bool,
|
|
}
|
|
|
|
impl BuildAction for SqlFormat {
|
|
fn command(&self) -> &str {
|
|
"$tsx $sql_format $mode $in"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("tsx", inputs![":node_modules:tsx"]);
|
|
build.add_inputs("sql_format", inputs!["ts/sql_format/sql_format.ts"]);
|
|
build.add_inputs("in", &self.inputs);
|
|
let mode = if self.check_only { "check" } else { "fix" };
|
|
build.add_variable("mode", mode);
|
|
build.add_output_stamp(format!("tests/sql_format.{mode}"));
|
|
}
|
|
}
|
|
|
|
pub struct GenTypescriptProto {
|
|
pub protos: BuildInput,
|
|
/// .js and .d.ts will be added to it
|
|
pub output_stem: &'static str,
|
|
}
|
|
|
|
impl BuildAction for GenTypescriptProto {
|
|
fn command(&self) -> &str {
|
|
"$pbjs --target=static-module --wrap=default --force-number --force-message --out=$static $in && $
|
|
$pbjs --target=json-module --wrap=default --force-number --force-message --out=$js $in && $
|
|
$pbts --out=$dts $static && $
|
|
rm $static"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("pbjs", inputs![":node_modules:pbjs"]);
|
|
build.add_inputs("pbts", inputs![":node_modules:pbts"]);
|
|
build.add_inputs("in", &self.protos);
|
|
build.add_inputs("", inputs!["yarn.lock"]);
|
|
|
|
let stem = self.output_stem;
|
|
build.add_variable("static", format!("$builddir/{stem}_static.js"));
|
|
build.add_outputs("js", vec![format!("{stem}.js")]);
|
|
build.add_outputs("dts", vec![format!("{stem}.d.ts")]);
|
|
}
|
|
}
|
|
|
|
pub struct CompileSass<'a> {
|
|
pub input: BuildInput,
|
|
pub output: &'a str,
|
|
pub deps: BuildInput,
|
|
pub load_paths: Vec<&'a str>,
|
|
}
|
|
|
|
impl BuildAction for CompileSass<'_> {
|
|
fn command(&self) -> &str {
|
|
"$sass -s compressed $args $in -- $out"
|
|
}
|
|
|
|
fn files(&mut self, build: &mut impl build::FilesHandle) {
|
|
build.add_inputs("sass", inputs![":node_modules:sass"]);
|
|
build.add_inputs("in", &self.input);
|
|
build.add_inputs("", &self.deps);
|
|
|
|
let args = space_separated(self.load_paths.iter().map(|path| format!("-I {path}")));
|
|
build.add_variable("args", args);
|
|
|
|
build.add_outputs("out", vec![self.output]);
|
|
}
|
|
}
|