From 35c3664f0d4325083d9530a1ce440e30fffbbdd7 Mon Sep 17 00:00:00 2001 From: Bryan Lai Date: Mon, 4 Mar 2024 22:22:49 +0800 Subject: [PATCH] Read from env variables for the default web bundle This commit makes it easy to override the default web bundle through the environment variables: - `TECTONIC_WEB_BUNDLE_PREFIX` - `TECTONIC_WEB_BUNDLE_LOCKED` The PREFIX variable makes it easy for people to track a mirrored bundle, while the LOCKED variable ensures reproducible builds across all CLIs. --- crates/bundles/build.rs | 35 +++++++++++++++++++++++++++++++++++ crates/bundles/src/lib.rs | 15 +++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 crates/bundles/build.rs diff --git a/crates/bundles/build.rs b/crates/bundles/build.rs new file mode 100644 index 000000000..af469b68c --- /dev/null +++ b/crates/bundles/build.rs @@ -0,0 +1,35 @@ +// Copyright 2016-2021 the Tectonic Project +// Licensed under the MIT License. + +use std::env; + +/// The current hardcoded default prefix for tectonic's web bundle. +const TECTONIC_WEB_BUNDLE_PREFIX_DEFAULT: &str = "https://relay.fullyjustified.net"; + +/// Environment variable names to look for the bundle URLs. +const LOCKED_VAR: &str = "TECTONIC_WEB_BUNDLE_LOCKED"; +const PREFIX_VAR: &str = "TECTONIC_WEB_BUNDLE_PREFIX"; + +/// Sets the environment variables for the default web bundle. +/// +/// `${TECTONIC_WEB_BUNDLE_PREFIX}` would lead to a url in the form of +/// `${TECTONIC_WEB_BUNDLE_PREFIX}/default_bundle.tar`, while the optional +/// "locked" url, `${TECTONIC_WEB_BUNDLE_LOCKED}`, can be used to pin the +/// default bundle to a specific version if specified. This would be useful +/// for reproducible builds. +fn web_bundle_presets() { + // load from env + let web_bundle_locked = env::var(LOCKED_VAR).unwrap_or("".into()); + let web_bundle_prefix = match env::var(PREFIX_VAR) { + Ok(x) if !x.is_empty() => x, + _ => TECTONIC_WEB_BUNDLE_PREFIX_DEFAULT.into(), + }; + + // export to rustc + println!("cargo:rustc-env={LOCKED_VAR}={web_bundle_locked}"); + println!("cargo:rustc-env={PREFIX_VAR}={web_bundle_prefix}"); +} + +fn main() { + web_bundle_presets(); +} diff --git a/crates/bundles/src/lib.rs b/crates/bundles/src/lib.rs index 503dbcac2..06f0b78fc 100644 --- a/crates/bundles/src/lib.rs +++ b/crates/bundles/src/lib.rs @@ -112,12 +112,23 @@ impl Bundle for Box { /// low-level reliability problems and was blocked in China. We now use a custom /// webservice. pub fn get_fallback_bundle_url(format_version: u32) -> String { + // Build time environment variables declared in `bundles/build.rs`: + let web_bundle_locked = option_env!("TECTONIC_WEB_BUNDLE_LOCKED").unwrap_or(""); + let web_bundle_prefix = option_env!("TECTONIC_WEB_BUNDLE_PREFIX") + .filter(|x| !x.is_empty()) + .expect("TECTONIC_WEB_BUNDLE_PREFIX must be defined at compile time"); + + // Simply return the locked url when it is specified: + if !web_bundle_locked.is_empty() { + return web_bundle_locked.to_owned(); + } + // Format version 32 (TeXLive 2021) was when we introduced versioning to the // URL. if format_version < 32 { - "https://relay.fullyjustified.net/default_bundle.tar".to_owned() + format!("{web_bundle_prefix}/default_bundle.tar") } else { - format!("https://relay.fullyjustified.net/default_bundle_v{format_version}.tar") + format!("{web_bundle_prefix}/default_bundle_v{format_version}.tar") } }