Skip to content

Commit

Permalink
build: replace independent script with build.rs
Browse files Browse the repository at this point in the history
Replace independent build script with build.rs
by building frontend in build.rs.

Disable build timestamp and git info emission
to avoid triggering build.rs too often.

Fix Myriad-Dreamin#100
  • Loading branch information
kxxt committed Jan 19, 2025
1 parent 8402d83 commit bffb05b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 86 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/Myriad-Dreamin/shiroa"

[workspace]
resolver = "2"
members = ["cli", "tools/build-from-source"]
members = ["cli"]

[profile.release]
codegen-units = 1 # Reduce number of codegen units to increase optimizations
Expand Down
118 changes: 107 additions & 11 deletions cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,95 @@
use anyhow::Result;
use vergen::{BuildBuilder, CargoBuilder, Emitter, RustcBuilder};
use vergen_gitcl::GitclBuilder;
use vergen::{
AddCustomEntries, BuildBuilder, CargoBuilder, CargoRerunIfChanged, CargoWarning, DefaultConfig,
Emitter, RustcBuilder,
};

use std::{
collections::BTreeMap,
path::Path,
process::{Command, Stdio},
};

#[derive(Default)]
struct FrontendEmitter;

impl AddCustomEntries<&str, &str> for FrontendEmitter {
fn add_calculated_entries(
&self,
_idempotent: bool,
_cargo_rustc_env_map: &mut BTreeMap<&str, &str>,
cargo_rerun_if_changed: &mut CargoRerunIfChanged,
_cargo_warning: &mut CargoWarning,
) -> Result<()> {
cargo_rerun_if_changed.extend(
["src", "package.json", "tsconfig.json", "vite.config.mjs"]
.into_iter()
.map(|s| "../frontend/".to_string() + s),
);
cargo_rerun_if_changed.push("../package.json".to_string());
Ok(())
}

fn add_default_entries(
&self,
_config: &DefaultConfig,
_cargo_rustc_env_map: &mut BTreeMap<&str, &str>,
_cargo_rerun_if_changed: &mut CargoRerunIfChanged,
_cargo_warning: &mut CargoWarning,
) -> Result<()> {
Ok(())
}
}

fn run(mut cmd: Command) -> anyhow::Result<()> {
Ok(cmd
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.status()
.map(|_| ())?)
}

const fn yarn_cmd() -> &'static str {
if cfg!(windows) {
"yarn.cmd"
} else {
"yarn"
}
}

fn build_frontend() -> anyhow::Result<()> {
let m = Path::new(env!("CARGO_MANIFEST_DIR"));
let project = m.parent().unwrap();
println!("Running yarn install...");
let mut cmd = Command::new(yarn_cmd());
cmd.args(["install"]);
cmd.current_dir(project.join("frontend"));
run(cmd)?;

println!("Running yarn build...");
let mut cmd = Command::new(yarn_cmd());
cmd.args(["build"]);
cmd.current_dir(project.join("frontend"));
run(cmd)?;

// copy to assets\artifacts\book.mjs
let src = project.join("frontend/dist/book.mjs");
let dst = project.join("assets/artifacts/book.mjs");
std::fs::copy(src, dst)?;

// copy typst ts renderer wasm module
let src =
project.join("node_modules/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm");
let dst = project.join("assets/artifacts/typst_ts_renderer_bg.wasm");
std::fs::copy(src, dst)?;

Ok(())
}

fn main() -> Result<()> {
let build = BuildBuilder::default().build_timestamp(true).build()?;
let build = BuildBuilder::default()
.build_timestamp(!cfg!(debug_assertions))
.build()?;
let cargo = CargoBuilder::all_cargo()?;
let rustc = RustcBuilder::default()
.commit_hash(true)
Expand All @@ -12,17 +98,27 @@ fn main() -> Result<()> {
.channel(true)
.llvm_version(true)
.build()?;
let gitcl = GitclBuilder::default()
.sha(false)
.describe(true, true, None)
.build()?;

// Emit the instructions
Emitter::default()
let emitter = &mut Emitter::default();
emitter
.add_instructions(&build)?
.add_instructions(&cargo)?
.add_instructions(&rustc)?
.add_instructions(&gitcl)?
.emit()?;
.add_custom_instructions(&FrontendEmitter)?;

#[cfg(not(debug_assertions))]
{
let gitcl = vergen_gitcl::GitclBuilder::default()
.sha(false)
.describe(true, true, None)
.build()?;
emitter.add_instructions(&gitcl)?;
}

// Emit the instructions
emitter.emit()?;

// Build frontend
build_frontend()?;
Ok(())
}
6 changes: 3 additions & 3 deletions cli/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ impl VersionInfo {
version: VERSION,
features: env!("VERGEN_CARGO_FEATURES").split(',').collect::<Vec<_>>(),

program_semver: env!("VERGEN_GIT_DESCRIBE"),
program_commit_hash: env!("VERGEN_GIT_SHA"),
program_semver: option_env!("VERGEN_GIT_DESCRIBE").unwrap_or("N/A"),
program_commit_hash: option_env!("VERGEN_GIT_SHA").unwrap_or("N/A"),
program_target_triple: env!("VERGEN_CARGO_TARGET_TRIPLE"),
program_opt_level: env!("VERGEN_CARGO_OPT_LEVEL"),
program_build_timestamp: env!("VERGEN_BUILD_TIMESTAMP"),
program_build_timestamp: option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("N/A"),

rustc_semver: env!("VERGEN_RUSTC_SEMVER"),
rustc_commit_hash: env!("VERGEN_RUSTC_COMMIT_HASH"),
Expand Down
12 changes: 0 additions & 12 deletions tools/build-from-source/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions tools/build-from-source/dist.toml

This file was deleted.

49 changes: 0 additions & 49 deletions tools/build-from-source/src/main.rs

This file was deleted.

0 comments on commit bffb05b

Please sign in to comment.