use crate::cache::{Cache, Glue, CHANNEL};
use std::fs;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
pub const ARCHES: [&str; 2] = ["x86_64", "aarch64"];
pub fn build(
cache: &Cache,
manifest_dir: &Path,
release: bool,
args: &[&str],
) -> Result<Vec<PathBuf>, String> {
let glue = cache.materialize()?;
let mut per_arch = Vec::new();
for (arch, spec) in ARCHES.iter().zip(glue.specs.iter()) {
let inject = crate::inject::build(cache, arch, spec)?;
per_arch.push(cargo(arch, spec, &glue, &inject, manifest_dir, release, args)?);
}
let outdir = manifest_dir
.join("target")
.join("cosmo")
.join(if release { "release" } else { "debug" });
fs::create_dir_all(&outdir).map_err(|e| format!("{}: {e}", outdir.display()))?;
let mut out = Vec::new();
for (name, x86) in &per_arch[0] {
let arm = match per_arch[1].iter().find(|(n, _)| n == name) {
Some((_, p)) => p,
None => continue,
};
out.push(apelink(cache, name, x86, arm, &outdir)?);
}
if out.is_empty() {
return Err("no executable was built for both architectures".into());
}
Ok(out)
}
fn cargo(
arch: &str,
spec: &Path,
glue: &Glue,
inject: &Path,
manifest_dir: &Path,
release: bool,
args: &[&str],
) -> Result<Vec<(String, PathBuf)>, String> {
let mut cmd = Command::new("rustup");
cmd.args(["run", CHANNEL.trim(), "cargo", "build"]);
cmd.args([
"-Zjson-target-spec",
"-Zbuild-std=std,panic_unwind",
"-Zbuild-std-features=compiler-builtins-mem",
]);
cmd.arg("--target").arg(spec);
cmd.arg("--message-format=json-render-diagnostics");
if release {
cmd.arg("--release");
}
cmd.args(args);
if std::env::var_os("COSMO_KEEP_PROFILE").is_none() {
cmd.args(["--config", "profile.release.codegen-units=16"]);
cmd.args(["--config", "profile.release.lto=false"]);
}
cmd.current_dir(manifest_dir);
scrub(&mut cmd);
if std::env::var_os("CARGO_BUILD_JOBS").is_none() {
if let Ok(jobs) = std::env::var("NUM_JOBS") {
cmd.env("CARGO_BUILD_JOBS", jobs);
}
}
let flags = std::env::var("COSMO_RUSTFLAGS").unwrap_or_default();
let mut flags: Vec<String> = flags.split_whitespace().map(str::to_owned).collect();
flags.extend(["--cfg".into(), "cosmo".into(), format!("-Clink-arg={}", inject.display())]);
cmd.env("CARGO_ENCODED_RUSTFLAGS", flags.join("\x1f"));
cmd.env("CARGO_TARGET_DIR", manifest_dir.join("target").join("cosmo-inner"));
for (var, tool) in [("CC", "cc"), ("CXX", "c++"), ("AR", "ar")] {
let triple = format!("{arch}-unknown-cosmo");
let keys = [format!("{var}_{triple}"), format!("{var}_{}", triple.replace('-', "_"))];
if keys.iter().all(|k| std::env::var_os(k).is_none()) {
cmd.env(&keys[0], glue.tool(&format!("{triple}-{tool}")));
}
}
cmd.env("COSMO_APE_INNER", "1");
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
let mut child = cmd.spawn().map_err(|e| format!("cargo: {e}"))?;
let err = child.stderr.take().unwrap();
let prefix = format!("[cosmo {arch}] ");
let echo = std::thread::spawn(move || {
let mut sink = progress();
let mut all = Vec::new();
for line in BufReader::new(err).split(b'\n').map_while(Result::ok) {
if let Some(f) = sink.as_mut() {
let mut l = prefix.clone().into_bytes();
l.extend_from_slice(&line);
l.push(b'\n');
let _ = f.write_all(&l);
}
all.extend_from_slice(&line);
all.push(b'\n');
}
all
});
let mut stdout = Vec::new();
let _ = child.stdout.take().unwrap().read_to_end(&mut stdout);
let status = child.wait().map_err(|e| format!("cargo: {e}"))?;
let stderr = echo.join().unwrap_or_default();
if !status.success() {
return Err(format!("{arch} build failed:\n{}", String::from_utf8_lossy(&stderr)));
}
let mut found = Vec::new();
for line in String::from_utf8_lossy(&stdout).lines() {
let Some(exe) = json_string_field(line, "\"executable\":") else {
continue;
};
let path = PathBuf::from(&exe);
let Some(name) = path.file_stem().map(|s| s.to_string_lossy().into_owned()) else {
continue;
};
found.push((name, path));
}
Ok(found)
}
fn progress() -> Option<fs::File> {
if let Some(path) = std::env::var_os("COSMO_PROGRESS") {
return fs::OpenOptions::new().create(true).append(true).open(path).ok();
}
let tty = if cfg!(windows) { "CONOUT$" } else { "/dev/tty" };
fs::OpenOptions::new().read(true).write(true).open(tty).ok()
}
fn json_string_field(line: &str, key: &str) -> Option<String> {
let rest = line.find(key).map(|i| &line[i + key.len()..])?;
let rest = rest.trim_start();
let mut chars = rest.strip_prefix('"')?.chars();
let mut out = String::new();
loop {
match chars.next()? {
'"' => return Some(out),
'\\' => out.push(match chars.next()? {
'n' => '\n',
't' => '\t',
c => c,
}),
c => out.push(c),
}
}
}
fn apelink(
cache: &Cache,
name: &str,
x86: &Path,
arm: &Path,
outdir: &Path,
) -> Result<PathBuf, String> {
let out = outdir.join(format!("{name}.com"));
let bin = |n: &str| cache.bin(n);
if let Some(mut f) = progress() {
let _ = writeln!(f, "[cosmo] apelink {}", out.display());
}
let mut cmd = ape(&bin("apelink"));
cmd.arg("-V").arg("-1"); cmd.arg("-l").arg(bin("ape-x86_64.elf"));
cmd.arg("-l").arg(bin("ape-aarch64.elf"));
cmd.arg("-M").arg(bin("ape-m1.c")); cmd.arg("-o").arg(&out);
cmd.arg(x86).arg(arm);
let r = cmd.output().map_err(|e| format!("apelink: {e}"))?;
if !r.status.success() {
return Err(format!(
"apelink failed for {name}:\n{}{}",
String::from_utf8_lossy(&r.stdout),
String::from_utf8_lossy(&r.stderr)
));
}
let chk = ape(&bin("pecheck"))
.arg(&out)
.output()
.map_err(|e| format!("pecheck: {e}"))?;
if !chk.status.success() {
return Err(format!(
"pecheck rejected {}:\n{}{}",
out.display(),
String::from_utf8_lossy(&chk.stdout),
String::from_utf8_lossy(&chk.stderr)
));
}
Ok(out)
}
pub fn ape(path: &Path) -> Command {
if cfg!(windows) {
return Command::new(path);
}
let elf = fs::read(path)
.map(|b| b.starts_with(b"\x7fELF"))
.unwrap_or(false);
if elf {
return Command::new(path);
}
let mut cmd = Command::new("/bin/sh");
cmd.arg(path);
cmd
}
pub(crate) fn scrub(cmd: &mut Command) {
for key in [
"RUSTC",
"RUSTDOC",
"RUSTC_WRAPPER",
"RUSTC_WORKSPACE_WRAPPER",
"RUSTUP_TOOLCHAIN",
"CARGO",
"RUSTFLAGS",
"CARGO_ENCODED_RUSTFLAGS",
"CARGO_MAKEFLAGS",
] {
cmd.env_remove(key);
}
}