Skip to content

Commit

Permalink
use simpler archive format
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Dec 30, 2023
1 parent a8f25d5 commit fc7eed0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
16 changes: 7 additions & 9 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@ url = "2"
serde_repr = "0.1"
webpki-roots = "0.25"
thousands = "0.2"
tar = { version = "0.4", git = "https://github.com/chris-morgan/tar-rs", rev = "b031dee050b6efb242b14f93b27a9734d0c334fb", default-features = false }
ruzstd = "0.5"
xz2 = { version = "0.1", features = ["static"] }
ar = "0.9"

[target.'cfg(target_arch = "x86_64")'.dependencies]
raw-cpuid = "11"

[build-dependencies]
glob = "0.3"
tar = { version = "0.4", git = "https://github.com/chris-morgan/tar-rs", rev = "b031dee050b6efb242b14f93b27a9734d0c334fb", features = ["builder"], default-features = false }
xz2 = "0.1"
ar = "0.9"

[target.'cfg(windows)'.build-dependencies]
winres = "0.1"
23 changes: 10 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ const EVAL_FILE: &str = "nn-5af11540bbfe.nnue";
fn main() {
hooks();

let mut archive = tar::Builder::new(XzEncoder::new(
File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("assets.tar.xz")).unwrap(),
let mut archive = ar::Builder::new(XzEncoder::new(
File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("assets.ar.xz")).unwrap(),
6,
));
archive.mode(tar::HeaderMode::Deterministic);
stockfish_build(&mut archive);
stockfish_eval_file(EVAL_FILE, &mut archive);
archive.into_inner().unwrap().finish().unwrap();
Expand Down Expand Up @@ -90,7 +89,7 @@ macro_rules! has_aarch64_builder_feature {
}};
}

fn stockfish_build<W: Write>(archive: &mut tar::Builder<W>) {
fn stockfish_build<W: Write>(archive: &mut ar::Builder<W>) {
// Note: The target arch of the build script is the architecture of the
// builder and decides if pgo is possible. It is not necessarily the same
// as CARGO_CFG_TARGET_ARCH, the target arch of the fishnet binary.
Expand Down Expand Up @@ -232,7 +231,7 @@ impl Target {
flavor: Flavor,
src_dir: &'static str,
name: &'static str,
archive: &mut tar::Builder<W>,
archive: &mut ar::Builder<W>,
) {
let release = env::var("PROFILE").unwrap() == "release";
let windows = env::var("CARGO_CFG_TARGET_FAMILY").unwrap() == "windows";
Expand Down Expand Up @@ -340,9 +339,7 @@ impl Target {
"$(MAKE) strip"
);

archive
.append_path_with_name(Path::new(src_dir).join(&exe), exe)
.unwrap();
archive.append_path(Path::new(src_dir).join(exe)).unwrap();

assert!(
Command::new(make)
Expand All @@ -356,11 +353,11 @@ impl Target {
);
}

fn build_official<W: Write>(&self, archive: &mut tar::Builder<W>) {
fn build_official<W: Write>(&self, archive: &mut ar::Builder<W>) {
self.build(Flavor::Official, "Stockfish/src", "stockfish", archive);
}

fn build_multi_variant<W: Write>(&self, archive: &mut tar::Builder<W>) {
fn build_multi_variant<W: Write>(&self, archive: &mut ar::Builder<W>) {
self.build(
Flavor::MultiVariant,
"Fairy-Stockfish/src",
Expand All @@ -369,14 +366,14 @@ impl Target {
);
}

fn build_both<W: Write>(&self, archive: &mut tar::Builder<W>) {
fn build_both<W: Write>(&self, archive: &mut ar::Builder<W>) {
self.build_official(archive);
self.build_multi_variant(archive);
}
}

fn stockfish_eval_file<W: Write>(name: &str, archive: &mut tar::Builder<W>) {
fn stockfish_eval_file<W: Write>(name: &str, archive: &mut ar::Builder<W>) {
archive
.append_path_with_name(Path::new("Stockfish").join("src").join(name), name)
.append_path(Path::new("Stockfish").join("src").join(name))
.unwrap();
}
40 changes: 31 additions & 9 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::{fmt, io, path::PathBuf};

use std::{
fmt,
fs::File,
io,
path::{Path, PathBuf},
str,
};

use ar::Archive;
use bitflags::bitflags;
use serde::Serialize;
use tempfile::TempDir;
use xz2::read::XzDecoder;

static ASSETS_TAR_XZ: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/assets.tar.xz"));
static ASSETS_AR_XZ: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/assets.ar.xz"));

bitflags! {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -182,12 +189,11 @@ impl Assets {
let mut stockfish = ByEngineFlavor::<Option<PathBuf>>::default();
let dir = tempfile::Builder::new().prefix("fishnet-").tempdir()?;

let mut archive = tar::Archive::new(XzDecoder::new(ASSETS_TAR_XZ));
for entry in archive.entries()? {
let mut archive = Archive::new(XzDecoder::new(ASSETS_AR_XZ));
while let Some(entry) = archive.next_entry() {
let mut entry = entry?;
let path = entry.path()?;
let target_path = dir.path().join(&path); // Trusted
let filename = path.to_str().expect("path printable");
let filename = str::from_utf8(entry.header().identifier()).expect("utf-8 filename");
let target_path = dir.path().join(filename); // Trusted
if filename.starts_with("stockfish-") {
if stockfish.official.is_none() && cpu.contains(Cpu::requirements(filename)) {
sf_name = Some(filename.to_owned());
Expand All @@ -203,7 +209,8 @@ impl Assets {
continue;
}
}
entry.unpack(target_path)?;
let mode = entry.header().mode();
io::copy(&mut entry, &mut create_file(&target_path, mode)?)?;
}

Ok(Assets {
Expand All @@ -218,3 +225,18 @@ impl Assets {
})
}
}

#[cfg(unix)]
fn create_file(path: &Path, mode: u32) -> io::Result<File> {
use std::os::unix::fs::OpenOptionsExt as _;
File::options()
.create_new(true)
.write(true)
.mode(mode)
.open(path)
}

#[cfg(not(unix))]
fn create_file(path: &Path, _mode: u32) -> io::Result<File> {
File::options().create_new(true).write(true).open(path)
}

0 comments on commit fc7eed0

Please sign in to comment.