Initial Commit
This commit is contained in:
27 files changed
+5470
No files matched your search
@@ -0,0 +1,351 @@
|
||||
#! @file archives.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what packages are written as: a tar archive, a zip file holding
|
||||
#! its files stored rather than compressed, and an `ar` archive, which a
|
||||
#! Debian package is. Each is written whole into memory, every entry owned by
|
||||
#! root and dated alike, so the same stage writes the same bytes. Compressing
|
||||
#! them is the system's `gzip`, `xz`, and `zstd`.
|
||||
import std::io;
|
||||
import system;
|
||||
|
||||
module blossom::pipeline;
|
||||
|
||||
#! A file or folder of a package.
|
||||
pub struct entry {
|
||||
pub var name: string; #! Its path within the package, with `/`, without one at the end.
|
||||
pub var source: path; #! The file it's read from, or empty for a folder.
|
||||
pub var folder: bool;
|
||||
pub var mode: u32; #! Who may read, write, and run it.
|
||||
}
|
||||
|
||||
#! Lists what a stage holds, as entries under a root: the root's own folders
|
||||
#! first, then each folder before what it holds, in order by name.
|
||||
#! @param stage The stage.
|
||||
#! @param root Where its files go in the package, as `usr` or
|
||||
#! `hello-1.0.0-linux-x86_64`, or empty for its top.
|
||||
#! @returns The entries.
|
||||
pub def staged(stage: &path, root: strview): vector<entry> {
|
||||
var made := vector::<entry>::new();
|
||||
var above := string::new(nothing {});
|
||||
for part in root.split('/') {
|
||||
continue if part.length == 0;
|
||||
if above.length != 0 {
|
||||
above.push('/');
|
||||
}
|
||||
above.append(part);
|
||||
made.append(entry { name = string::copy_of(&above), folder = true, mode = 0o755 });
|
||||
}
|
||||
walked(stage, above.span, &mut made);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Adds what a folder holds, in order by name, each folder before what it holds.
|
||||
def walked(folder: &path, under: strview, into: &mut vector<entry>): void {
|
||||
var names := vector::<string>::new();
|
||||
var listing := io::directory::open(folder) else (_) { return; };
|
||||
while listing.next() is some(each) {
|
||||
names.append(string::from(each.name, nothing {}));
|
||||
}
|
||||
names.sort_by(def(a: &string, b: &string): bool { return a.span.compare(b.span) < 0; });
|
||||
for &name in names {
|
||||
val at := folder.join(name.span);
|
||||
val text := under.length != 0 ? $"{under}/{name.span}" : string::copy_of(name);
|
||||
val found := io::info(&at) else (_) { continue; };
|
||||
if found.kind == file_kind::directory {
|
||||
into.append(entry { name = string::copy_of(&text), folder = true, mode = 0o755 });
|
||||
walked(&at, text.span, into);
|
||||
} else {
|
||||
into.append(entry {
|
||||
name = string::copy_of(&text),
|
||||
source = path::copy_of(&at),
|
||||
mode = (found.permissions & 0o111) != 0 ? 0o755 : 0o644
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! A tar archive, as POSIX's ustar writes one, with GNU's long names for a path
|
||||
#! too long to split between a header's name and prefix.
|
||||
pub struct tar {
|
||||
pub var bytes: string;
|
||||
pub var mtime: i64; #! When every entry was written, in seconds since 1970.
|
||||
}
|
||||
|
||||
#! Adds a folder.
|
||||
#! @param this The archive.
|
||||
#! @param name Its path, which `/` is added to.
|
||||
pub def tar::folder(&mut this, name: strview): void {
|
||||
val named := $"{name}/";
|
||||
this.header(named.span, 0o755, 0, '5');
|
||||
}
|
||||
|
||||
#! Adds a file.
|
||||
#! @param this The archive.
|
||||
#! @param name Its path.
|
||||
#! @param data What it holds.
|
||||
#! @param mode Who may read, write, and run it.
|
||||
pub def tar::file(&mut this, name: strview, data: span<u8>, mode: u32): void {
|
||||
this.header(name, mode, data.length, '0');
|
||||
this.bytes.append(data);
|
||||
this.pad();
|
||||
}
|
||||
|
||||
#! Ends the archive with its two empty blocks.
|
||||
#! @param this The archive.
|
||||
#! @returns Its bytes.
|
||||
pub def tar::finish(&mut this): strview {
|
||||
for _ in 0..1024 {
|
||||
this.bytes.push(0);
|
||||
}
|
||||
return this.bytes.span;
|
||||
}
|
||||
|
||||
# Writes an entry's header, after a long name's own entry where it needs one.
|
||||
def tar::header(&mut this, name: strview, mode: u32, size: u64, kind: u8): void {
|
||||
var split := name.length;
|
||||
if name.length > 100 {
|
||||
# The last `/` leaving a name of 100 bytes or fewer, with 155 or fewer
|
||||
# before it.
|
||||
split = 0;
|
||||
for k in 0..name.length {
|
||||
if name[k] == '/' && name.length - k - 1 <= 100 && k <= 155 && k > 0 {
|
||||
split = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if split == 0 {
|
||||
val longer := $"{name}\0";
|
||||
this.header("././@LongLink", 0o644, longer.length, 'L');
|
||||
this.bytes.append(longer.span);
|
||||
this.pad();
|
||||
split = name.length;
|
||||
}
|
||||
}
|
||||
|
||||
var block: u8[512];
|
||||
for k in 0..512 {
|
||||
block[k] = 0;
|
||||
}
|
||||
val base := split == name.length ? name : name.substr(split + 1, name.length - split - 1);
|
||||
val prefix := split == name.length ? strview::blank() : name.substr(0, split);
|
||||
put(&mut block, 0, base.substr(0, 100));
|
||||
put_octal(&mut block, 100, 8, mode as u64);
|
||||
put_octal(&mut block, 108, 8, 0);
|
||||
put_octal(&mut block, 116, 8, 0);
|
||||
put_octal(&mut block, 124, 12, size);
|
||||
put_octal(&mut block, 136, 12, this.mtime as u64);
|
||||
for k in 148..156 {
|
||||
block[k] = ' ';
|
||||
}
|
||||
block[156] = kind;
|
||||
put(&mut block, 257, "ustar");
|
||||
put(&mut block, 263, "00");
|
||||
put(&mut block, 265, "root");
|
||||
put(&mut block, 297, "root");
|
||||
put(&mut block, 345, prefix);
|
||||
|
||||
var sum := 0u;
|
||||
for k in 0..512 {
|
||||
sum += block[k] as u64;
|
||||
}
|
||||
put_octal(&mut block, 148, 7, sum);
|
||||
block[155] = ' ';
|
||||
this.bytes.append(strview::over((&block) as *u8, 512));
|
||||
}
|
||||
|
||||
# Pads the archive to a whole block.
|
||||
def tar::pad(&mut this): void {
|
||||
while this.bytes.length % 512 != 0 {
|
||||
this.bytes.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
# Writes text into a header from a byte.
|
||||
def put(block: &mut u8[512], at: u64, text: strview): void {
|
||||
for k in 0..text.length {
|
||||
block[at + k] = text[k];
|
||||
}
|
||||
}
|
||||
|
||||
# Writes a number in octal into a header, in a field of a width: zeros before
|
||||
# it, and a zero byte after.
|
||||
def put_octal(block: &mut u8[512], at: u64, width: u64, value: u64): void {
|
||||
var left := value;
|
||||
var k := width - 1;
|
||||
block[at + k] = 0;
|
||||
while k > 0 {
|
||||
k--;
|
||||
block[at + k] = ('0' + (left & 7) as u8);
|
||||
left = left >> 3;
|
||||
}
|
||||
}
|
||||
|
||||
#! A zip file whose files are stored as they are, rather than compressed, with
|
||||
#! who may run each in the Unix bits of its attributes.
|
||||
pub struct zip {
|
||||
pub var bytes: string;
|
||||
pub var central: string; #! The central directory, written after the files.
|
||||
pub var count: u64;
|
||||
pub var mtime: i64; #! When every entry was written, in seconds since 1970.
|
||||
}
|
||||
|
||||
#! Adds a folder.
|
||||
#! @param this The zip file.
|
||||
#! @param name Its path, which `/` is added to.
|
||||
pub def zip::folder(&mut this, name: strview): void {
|
||||
val named := $"{name}/";
|
||||
this.add(named.span, strview::blank(), 0o040000 | 0o755, 0x10);
|
||||
}
|
||||
|
||||
#! Adds a file.
|
||||
#! @param this The zip file.
|
||||
#! @param name Its path.
|
||||
#! @param data What it holds.
|
||||
#! @param mode Who may read, write, and run it.
|
||||
pub def zip::file(&mut this, name: strview, data: span<u8>, mode: u32): void {
|
||||
this.add(name, data, 0o100000 | mode, 0);
|
||||
}
|
||||
|
||||
# Adds an entry: its header, its bytes, and its record in the central directory.
|
||||
def zip::add(&mut this, name: strview, data: span<u8>, unix: u32, dos: u32): void {
|
||||
val offset := this.bytes.length;
|
||||
val crc := crc32(data);
|
||||
val stamp := dos_stamp(this.mtime);
|
||||
put_u32(&mut this.bytes, 0x04034b50);
|
||||
put_u16(&mut this.bytes, 20);
|
||||
put_u16(&mut this.bytes, 0x0800); # Names are UTF-8.
|
||||
put_u16(&mut this.bytes, 0);
|
||||
put_u32(&mut this.bytes, stamp);
|
||||
put_u32(&mut this.bytes, crc);
|
||||
put_u32(&mut this.bytes, data.length as u32);
|
||||
put_u32(&mut this.bytes, data.length as u32);
|
||||
put_u16(&mut this.bytes, name.length as u16);
|
||||
put_u16(&mut this.bytes, 0);
|
||||
this.bytes.append(name);
|
||||
this.bytes.append(data);
|
||||
|
||||
put_u32(&mut this.central, 0x02014b50);
|
||||
put_u16(&mut this.central, (3 << 8) | 20); # Made on Unix.
|
||||
put_u16(&mut this.central, 20);
|
||||
put_u16(&mut this.central, 0x0800);
|
||||
put_u16(&mut this.central, 0);
|
||||
put_u32(&mut this.central, stamp);
|
||||
put_u32(&mut this.central, crc);
|
||||
put_u32(&mut this.central, data.length as u32);
|
||||
put_u32(&mut this.central, data.length as u32);
|
||||
put_u16(&mut this.central, name.length as u16);
|
||||
put_u16(&mut this.central, 0);
|
||||
put_u16(&mut this.central, 0);
|
||||
put_u16(&mut this.central, 0);
|
||||
put_u16(&mut this.central, 0);
|
||||
put_u32(&mut this.central, (unix << 16) | dos);
|
||||
put_u32(&mut this.central, offset as u32);
|
||||
this.central.append(name);
|
||||
this.count++;
|
||||
}
|
||||
|
||||
#! Ends the zip file with its central directory.
|
||||
#! @param this The zip file.
|
||||
#! @returns Its bytes.
|
||||
pub def zip::finish(&mut this): strview {
|
||||
val start := this.bytes.length;
|
||||
this.bytes.append(this.central.span);
|
||||
put_u32(&mut this.bytes, 0x06054b50);
|
||||
put_u16(&mut this.bytes, 0);
|
||||
put_u16(&mut this.bytes, 0);
|
||||
put_u16(&mut this.bytes, this.count as u16);
|
||||
put_u16(&mut this.bytes, this.count as u16);
|
||||
put_u32(&mut this.bytes, this.central.length as u32);
|
||||
put_u32(&mut this.bytes, start as u32);
|
||||
put_u16(&mut this.bytes, 0);
|
||||
return this.bytes.span;
|
||||
}
|
||||
|
||||
# A time as MS-DOS keeps it, as zip files do: the date in the high half, the
|
||||
# time in the low, in two-second steps, from 1980 on.
|
||||
def dos_stamp(seconds: i64): u32 {
|
||||
val at := seconds < 315532800 ? 315532800 : seconds;
|
||||
val days := at / 86400;
|
||||
val left := at % 86400;
|
||||
val date := civil_of(days);
|
||||
val day := ((date.year - 1980) as u32 << 9) | (date.month as u32 << 5) | date.day as u32;
|
||||
val time := ((left / 3600) as u32 << 11) | (((left % 3600) / 60) as u32 << 5) | ((left % 60) / 2) as u32;
|
||||
return (day << 16) | time;
|
||||
}
|
||||
|
||||
# A day of the Gregorian calendar.
|
||||
struct civil {
|
||||
var year: i64;
|
||||
var month: i64;
|
||||
var day: i64;
|
||||
}
|
||||
|
||||
# The date a number of days after 1970-01-01 is, as Howard Hinnant's
|
||||
# `civil_from_days` works it out.
|
||||
def civil_of(days: i64): civil {
|
||||
val z := days + 719468;
|
||||
val era := (z >= 0 ? z : z - 146096) / 146097;
|
||||
val doe := z - era * 146097;
|
||||
val yoe := (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
||||
val doy := doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
val mp := (5 * doy + 2) / 153;
|
||||
val day := doy - (153 * mp + 2) / 5 + 1;
|
||||
val month := mp < 10 ? mp + 3 : mp - 9;
|
||||
return civil { year = yoe + era * 400 + (month <= 2 ? 1 : 0), month = month, day = day };
|
||||
}
|
||||
|
||||
#! An `ar` archive, as a Debian package is: its signature, then each member
|
||||
#! with a header of 60 bytes.
|
||||
pub struct ar {
|
||||
pub var bytes: string;
|
||||
pub var mtime: i64;
|
||||
}
|
||||
|
||||
#! Starts an archive.
|
||||
#! @param mtime When every member was written, in seconds since 1970.
|
||||
#! @returns The archive, holding its signature.
|
||||
pub def ar::new(mtime: i64): ar {
|
||||
return ar { bytes = string::from("!<arch>\n", nothing {}), mtime = mtime };
|
||||
}
|
||||
|
||||
#! Adds a member.
|
||||
#! @param this The archive.
|
||||
#! @param name Its name, of 16 bytes or fewer.
|
||||
#! @param data What it holds.
|
||||
pub def ar::member(&mut this, name: strview, data: span<u8>): void {
|
||||
this.bytes.append(padded(name, 16).span);
|
||||
this.bytes.append(padded($"{this.mtime}".span, 12).span);
|
||||
this.bytes.append(padded("0", 6).span);
|
||||
this.bytes.append(padded("0", 6).span);
|
||||
this.bytes.append(padded("100644", 8).span);
|
||||
this.bytes.append(padded($"{data.length}".span, 10).span);
|
||||
this.bytes.append("`\n");
|
||||
this.bytes.append(data);
|
||||
if data.length % 2 != 0 {
|
||||
this.bytes.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
# Text with spaces after it, to a width.
|
||||
def padded(text: strview, width: u64): string {
|
||||
var made := string::from(text, nothing {});
|
||||
while made.length < width {
|
||||
made.push(' ');
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Adds a number of two bytes, the low first.
|
||||
def put_u16(into: &mut string, value: u32): void {
|
||||
into.push((value & 0xff) as u8);
|
||||
into.push(((value >> 8) & 0xff) as u8);
|
||||
}
|
||||
|
||||
# Adds a number of four bytes, the low first.
|
||||
def put_u32(into: &mut string, value: u32): void {
|
||||
put_u16(into, value & 0xffff);
|
||||
put_u16(into, value >> 16);
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
#! @file digests.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the digests packages carry: MD5, which Debian's `md5sums` lists,
|
||||
#! SHA-256, which Arch's `.MTREE` lists beside MD5, and CRC-32, which a zip
|
||||
#! file keeps of each file it holds. Both digests add words modulo 2^32, so
|
||||
#! they add through `math::wrapping_add`, which a program run or built with
|
||||
#! `--checks` doesn't stop at.
|
||||
import std::math;
|
||||
|
||||
module blossom::pipeline;
|
||||
|
||||
#! The MD5 digest of bytes, as RFC 1321 gives it.
|
||||
#! @param data The bytes.
|
||||
#! @returns The digest, as 32 hex digits.
|
||||
pub def md5(data: span<u8>): string {
|
||||
var state: u32[4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
|
||||
var block: u8[64];
|
||||
val total := data.length;
|
||||
var at := 0u;
|
||||
while at + 64 <= total {
|
||||
md5_block(&mut state, data.data + at);
|
||||
at += 64;
|
||||
}
|
||||
|
||||
# The last bytes, then `0x80`, then zeros, then the length in bits, low
|
||||
# byte first, filling one block or two.
|
||||
val left := total - at;
|
||||
for k in 0..64 {
|
||||
block[k] = 0;
|
||||
}
|
||||
for k in 0..left {
|
||||
block[k] = data[at + k];
|
||||
}
|
||||
block[left] = 0x80;
|
||||
if left >= 56 {
|
||||
md5_block(&mut state, (&block) as *u8);
|
||||
for k in 0..64 {
|
||||
block[k] = 0;
|
||||
}
|
||||
}
|
||||
val bits := total * 8;
|
||||
for k in 0..8 {
|
||||
block[56 + k] = ((bits >> (8 * k as u64)) & 0xff) as u8;
|
||||
}
|
||||
md5_block(&mut state, (&block) as *u8);
|
||||
|
||||
var made := string::new(nothing {});
|
||||
for word in state {
|
||||
for k in 0..4 {
|
||||
append_hex(&mut made, ((word >> (8 * k as u32)) & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The amounts each round of MD5 turns its words left by.
|
||||
val md5_turns: u32[16] = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
|
||||
|
||||
# Mixes a block of 64 bytes into MD5's state.
|
||||
def md5_block(state: &mut u32[4], at: *u8): void {
|
||||
var words: u32[16];
|
||||
for k in 0..16 {
|
||||
val from := at + 4 * k;
|
||||
words[k] = from[0] as u32 | (from[1] as u32 << 8) | (from[2] as u32 << 16) | (from[3] as u32 << 24);
|
||||
}
|
||||
var a := state[0];
|
||||
var b := state[1];
|
||||
var c := state[2];
|
||||
var d := state[3];
|
||||
for i in 0u..64u {
|
||||
var mixed: u32 = 0;
|
||||
var index: u64 = 0;
|
||||
if i < 16 {
|
||||
mixed = (b & c) | (~b & d);
|
||||
index = i;
|
||||
} else if i < 32 {
|
||||
mixed = (d & b) | (~d & c);
|
||||
index = (5 * i + 1) % 16;
|
||||
} else if i < 48 {
|
||||
mixed = b ^ c ^ d;
|
||||
index = (3 * i + 5) % 16;
|
||||
} else {
|
||||
mixed = c ^ (b | ~d);
|
||||
index = (7 * i) % 16;
|
||||
}
|
||||
val sum := sum_of([a, mixed, md5_constant(i), words[index]]);
|
||||
a = d;
|
||||
d = c;
|
||||
c = b;
|
||||
b = sum_of([b, turned_left(sum, md5_turns[(i / 16) * 4 + i % 4])]);
|
||||
}
|
||||
state[0] = sum_of([state[0], a]);
|
||||
state[1] = sum_of([state[1], b]);
|
||||
state[2] = sum_of([state[2], c]);
|
||||
state[3] = sum_of([state[3], d]);
|
||||
}
|
||||
|
||||
# MD5's constant of each step: the whole part of 2^32 times the sine of the
|
||||
# step's number, counted from 1.
|
||||
def md5_constant(i: u64): u32 {
|
||||
return md5_constants[i];
|
||||
}
|
||||
|
||||
val md5_constants: u32[64] = [
|
||||
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
|
||||
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
|
||||
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
|
||||
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
|
||||
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
|
||||
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
|
||||
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
|
||||
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
|
||||
];
|
||||
|
||||
#! The SHA-256 digest of bytes, as FIPS 180-4 gives it.
|
||||
#! @param data The bytes.
|
||||
#! @returns The digest, as 64 hex digits.
|
||||
pub def sha256(data: span<u8>): string {
|
||||
var state: u32[8] = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
||||
var block: u8[64];
|
||||
val total := data.length;
|
||||
var at := 0u;
|
||||
while at + 64 <= total {
|
||||
sha256_block(&mut state, data.data + at);
|
||||
at += 64;
|
||||
}
|
||||
|
||||
# As MD5 pads, but with the length's high byte first.
|
||||
val left := total - at;
|
||||
for k in 0..64 {
|
||||
block[k] = 0;
|
||||
}
|
||||
for k in 0..left {
|
||||
block[k] = data[at + k];
|
||||
}
|
||||
block[left] = 0x80;
|
||||
if left >= 56 {
|
||||
sha256_block(&mut state, (&block) as *u8);
|
||||
for k in 0..64 {
|
||||
block[k] = 0;
|
||||
}
|
||||
}
|
||||
val bits := total * 8;
|
||||
for k in 0..8 {
|
||||
block[63 - k] = ((bits >> (8 * k as u64)) & 0xff) as u8;
|
||||
}
|
||||
sha256_block(&mut state, (&block) as *u8);
|
||||
|
||||
var made := string::new(nothing {});
|
||||
for word in state {
|
||||
for k in 0..4 {
|
||||
append_hex(&mut made, ((word >> (24 - 8 * k as u32)) & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# SHA-256's constants: the first 32 bits of the fractional parts of the cube
|
||||
# roots of the first 64 primes.
|
||||
val sha256_constants: u32[64] = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
||||
];
|
||||
|
||||
# Mixes a block of 64 bytes into SHA-256's state.
|
||||
def sha256_block(state: &mut u32[8], at: *u8): void {
|
||||
var words: u32[64];
|
||||
for k in 0..16 {
|
||||
val from := at + 4 * k;
|
||||
words[k] = (from[0] as u32 << 24) | (from[1] as u32 << 16) | (from[2] as u32 << 8) | from[3] as u32;
|
||||
}
|
||||
for k in 16..64 {
|
||||
val low := words[k - 15];
|
||||
val high := words[k - 2];
|
||||
val small0 := turned_right(low, 7) ^ turned_right(low, 18) ^ (low >> 3);
|
||||
val small1 := turned_right(high, 17) ^ turned_right(high, 19) ^ (high >> 10);
|
||||
words[k] = sum_of([words[k - 16], small0, words[k - 7], small1]);
|
||||
}
|
||||
|
||||
var a := state[0];
|
||||
var b := state[1];
|
||||
var c := state[2];
|
||||
var d := state[3];
|
||||
var e := state[4];
|
||||
var f := state[5];
|
||||
var g := state[6];
|
||||
var h := state[7];
|
||||
for k in 0..64 {
|
||||
val big1 := turned_right(e, 6) ^ turned_right(e, 11) ^ turned_right(e, 25);
|
||||
val chosen := (e & f) ^ (~e & g);
|
||||
val first := sum_of([h, big1, chosen, sha256_constants[k], words[k]]);
|
||||
val big0 := turned_right(a, 2) ^ turned_right(a, 13) ^ turned_right(a, 22);
|
||||
val major := (a & b) ^ (a & c) ^ (b & c);
|
||||
val second := sum_of([big0, major]);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = sum_of([d, first]);
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = sum_of([first, second]);
|
||||
}
|
||||
state[0] = sum_of([state[0], a]);
|
||||
state[1] = sum_of([state[1], b]);
|
||||
state[2] = sum_of([state[2], c]);
|
||||
state[3] = sum_of([state[3], d]);
|
||||
state[4] = sum_of([state[4], e]);
|
||||
state[5] = sum_of([state[5], f]);
|
||||
state[6] = sum_of([state[6], g]);
|
||||
state[7] = sum_of([state[7], h]);
|
||||
}
|
||||
|
||||
#! The CRC-32 of bytes, as zip files and gzip keep it: the polynomial
|
||||
#! `0xEDB88320`, reflected, starting from and ending with every bit flipped.
|
||||
#! @param data The bytes.
|
||||
#! @returns The checksum.
|
||||
pub def crc32(data: span<u8>): u32 {
|
||||
var crc: u32 = 0xFFFFFFFF;
|
||||
for k in 0..data.length {
|
||||
crc = crc ^ data[k] as u32;
|
||||
for _ in 0..8 {
|
||||
val low := crc & 1;
|
||||
crc = crc >> 1;
|
||||
if low != 0 {
|
||||
crc = crc ^ 0xEDB88320;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
# Words added modulo 2^32, as both digests add them.
|
||||
def sum_of(words: span<u32>): u32 {
|
||||
var sum: u32 = 0;
|
||||
for word in words {
|
||||
sum = math::wrapping_add(sum, word);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
# A word turned left, its high bits coming round to the low.
|
||||
def turned_left(word: u32, by: u32): u32 {
|
||||
return (word << by) | (word >> (32 - by));
|
||||
}
|
||||
|
||||
# A word turned right, its low bits coming round to the high.
|
||||
def turned_right(word: u32, by: u32): u32 {
|
||||
return (word >> by) | (word << (32 - by));
|
||||
}
|
||||
|
||||
# Adds a byte as two hex digits.
|
||||
def append_hex(into: &mut string, byte: u8): void {
|
||||
val digits: strview = "0123456789abcdef";
|
||||
into.push(digits[(byte >> 4) as u64]);
|
||||
into.push(digits[(byte & 15) as u64]);
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
#! @file install.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `install`: the project built, then its programs put in the prefix's
|
||||
#! `bin`, its exported libraries in `share/sakura`, as source another project's
|
||||
#! `--lib` reads, and each `install` step done in turn.
|
||||
import std::io;
|
||||
import std::process;
|
||||
import system;
|
||||
import blossom::models;
|
||||
import blossom::management;
|
||||
|
||||
module blossom::pipeline;
|
||||
|
||||
#! Builds the project, then installs it.
|
||||
#! @param this The session.
|
||||
#! @param prefix Where it's installed, or empty for `~/.local`.
|
||||
#! @param destdir A folder to put what's installed under, as DESTDIR/PREFIX,
|
||||
#! or empty.
|
||||
#! @returns The status the command ends with.
|
||||
pub def session::install_command(&mut this, prefix: strview, destdir: strview): i32 {
|
||||
var into := path::new();
|
||||
if prefix.length != 0 {
|
||||
val named := path::parse(prefix) else (_) {
|
||||
fatal(2, $"`--prefix {prefix}` is no path.");
|
||||
return 2;
|
||||
};
|
||||
into = io::absolute(&named) else (why) {
|
||||
fatal(2, $"`--prefix {prefix}` can't be found: {why}.");
|
||||
return 2;
|
||||
};
|
||||
} else {
|
||||
val home := process::variable("HOME") else {
|
||||
when os == windows {
|
||||
if process::variable("USERPROFILE") is some(profile) {
|
||||
return this.install_into(prefix_under(profile), destdir);
|
||||
}
|
||||
}
|
||||
fatal(2, "`HOME` isn't set, so the prefix must be given, with `--prefix`.");
|
||||
return 2;
|
||||
};
|
||||
into = prefix_under(home);
|
||||
}
|
||||
return this.install_into(move(into), destdir);
|
||||
}
|
||||
|
||||
# Installs into a prefix, under a destination folder where one's given.
|
||||
def session::install_into(&mut this, prefix: path, destdir: strview): i32 {
|
||||
var into := path::copy_of(&prefix);
|
||||
if destdir.length != 0 {
|
||||
val named := path::parse(destdir) else (_) {
|
||||
fatal(2, $"`--destdir {destdir}` is no path.");
|
||||
return 2;
|
||||
};
|
||||
val stage := io::absolute(&named) else (why) {
|
||||
fatal(2, $"`--destdir {destdir}` can't be found: {why}.");
|
||||
return 2;
|
||||
};
|
||||
into = under_root(&stage, &prefix);
|
||||
}
|
||||
|
||||
this.build(false, "");
|
||||
if this.failed {
|
||||
return 1;
|
||||
}
|
||||
val count := this.install(&into);
|
||||
val files: strview = count == 1 ? "file" : "files";
|
||||
io::println($"Installed {this.space.projects[0].identity.span} ({count} {files}) to {into.span}".span);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Installs the project, already built, under a folder: its programs into
|
||||
#! `bin`, its exported libraries into `share/sakura`, then each step.
|
||||
#! @param this The session.
|
||||
#! @param into The folder, which stands for the prefix.
|
||||
#! @returns How many files were installed.
|
||||
pub def session::install(&mut this, into: &path): u64 {
|
||||
val holder := &this.space.projects[0];
|
||||
var count := 0u;
|
||||
for &each in holder.targets {
|
||||
if each.kind == models::target_kind::program {
|
||||
val system := this.platform_of(&each);
|
||||
val name := output_name(each.name.span, system);
|
||||
val built := holder.folder.join($"{output_text(holder).span}/bin/{name.span}".span);
|
||||
count += copied(&built, &into.join("bin").join(name.span));
|
||||
val petal := holder.folder.join($"{output_text(holder).span}/bin/{name.span}.petal".span);
|
||||
if io::exists(&petal) {
|
||||
count += copied(&petal, &into.join("bin").join($"{name.span}.petal".span));
|
||||
}
|
||||
} else if holder.exported(each.name.span) {
|
||||
val root := path::parse(each.root.span) else (_) { continue; };
|
||||
count += copied(&holder.folder.join(&root), &into.join("share").join("sakura").join(each.name.span));
|
||||
}
|
||||
}
|
||||
|
||||
for &step in holder.install {
|
||||
val source := path::parse(step.source.span) else (_) {
|
||||
fatal(2, $"{holder.file.span}:{step.line}: `{step.source.span}` is no path.");
|
||||
return count;
|
||||
};
|
||||
val from := holder.folder.join(&source);
|
||||
if !io::exists(&from) {
|
||||
fatal(2, $"{holder.file.span}:{step.line}: `{step.source.span}` isn't there to install.");
|
||||
}
|
||||
val to := step.dest.length != 0 ? path::parse(step.dest.span) : path::parse(step.source.span);
|
||||
if to is value(&place) {
|
||||
count += copied(&from, &into.join(place));
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
# Copies a file, or a folder and everything in it, making the folders above
|
||||
# it, keeping who may run each, and giving how many files were copied. A copy
|
||||
# that fails stops the command.
|
||||
def copied(from: &path, to: &path): u64 {
|
||||
val found := io::info(from) else (why) {
|
||||
fatal(1, $"`{from.span}` can't be read: {why}.");
|
||||
return 0;
|
||||
};
|
||||
if found.kind == file_kind::directory {
|
||||
_ = io::create_directories(to);
|
||||
var names := vector::<string>::new();
|
||||
var listing := io::directory::open(from) else (why) {
|
||||
fatal(1, $"`{from.span}` can't be read: {why}.");
|
||||
return 0;
|
||||
};
|
||||
while listing.next() is some(entry) {
|
||||
names.append(string::from(entry.name, nothing {}));
|
||||
}
|
||||
names.sort_by(def(a: &string, b: &string): bool { return a.span.compare(b.span) < 0; });
|
||||
var count := 0u;
|
||||
for &name in names {
|
||||
count += copied(&from.join(name.span), &to.join(name.span));
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
if to.parent() is some(&above) {
|
||||
_ = io::create_directories(above);
|
||||
}
|
||||
val bytes := io::read_file(from) else (why) {
|
||||
fatal(1, $"`{from.span}` can't be read: {why}.");
|
||||
return 0;
|
||||
};
|
||||
if io::write_file(to, bytes.span) is error(why) {
|
||||
fatal(1, $"`{to.span}` can't be written: {why}.");
|
||||
}
|
||||
_ = io::set_permissions(to, (found.permissions & 0o111) != 0 ? 0o755 : 0o644);
|
||||
return 1;
|
||||
}
|
||||
|
||||
# `.local` in a home folder, the prefix unless one's given.
|
||||
def prefix_under(home: strview): path {
|
||||
val at := path::parse(home) else (_) { return path::new(); };
|
||||
return at.join(".local");
|
||||
}
|
||||
|
||||
# A path put under a folder, as `/stage` and `/usr/local` make
|
||||
# `/stage/usr/local`.
|
||||
def under_root(stage: &path, at: &path): path {
|
||||
var made := path::copy_of(stage);
|
||||
for part in at.segments() {
|
||||
made = made.join(part);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
@@ -0,0 +1,477 @@
|
||||
#! @file package.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `package`: the project installed into its stage, then written as
|
||||
#! each package asked for into its output folder's `dist`. tar and zip files
|
||||
#! hold the stage under a folder of the project's name, version, and target;
|
||||
#! Debian and Arch packages hold it under the prefix they install to, `/usr`
|
||||
#! unless given; and an rpm package is built by `rpmbuild` from a spec written
|
||||
#! for it. Every entry is dated `SOURCE_DATE_EPOCH`, where it's set, so a
|
||||
#! package is written alike each time.
|
||||
import std::io;
|
||||
import std::process;
|
||||
import std::time;
|
||||
import system;
|
||||
import blossom::models;
|
||||
import blossom::management;
|
||||
|
||||
module blossom::pipeline;
|
||||
|
||||
#! The packages blossom writes.
|
||||
pub val formats: strview[5] = ["tar", "zip", "deb", "arch", "rpm"];
|
||||
|
||||
#! Builds the project, installs it into its stage, then writes its packages.
|
||||
#! @param this The session.
|
||||
#! @param asked The formats asked for, each a format or several with `,`
|
||||
#! between them, or none for `tar`, or `zip` for Windows.
|
||||
#! @param prefix Where a Debian, Arch, or rpm package installs, or empty for
|
||||
#! `/usr`.
|
||||
#! @returns The status the command ends with.
|
||||
pub def session::package_command(&mut this, asked: span<strview>, prefix: strview): i32 {
|
||||
var chosen := vector::<strview>::new();
|
||||
for &each in asked {
|
||||
for format in each.split(',') {
|
||||
continue if format.length == 0;
|
||||
var known := false;
|
||||
for &name in formats {
|
||||
known = known || name == format;
|
||||
}
|
||||
if !known {
|
||||
fatal(2, $"`{format}` is no format: write `tar`, `zip`, `deb`, `arch`, or `rpm`.".span);
|
||||
}
|
||||
chosen.append(format);
|
||||
}
|
||||
}
|
||||
val platform := this.package_platform();
|
||||
if chosen.length == 0 {
|
||||
chosen.append(platform == "windows-x86_64" ? "zip" : "tar");
|
||||
}
|
||||
var installed_at: strview = "/usr";
|
||||
if prefix.length != 0 {
|
||||
installed_at = prefix;
|
||||
}
|
||||
|
||||
# What a system's package can't hold is said before anything's built.
|
||||
for &format in chosen {
|
||||
if format == "deb" || format == "arch" || format == "rpm" {
|
||||
_ = distro_arch(platform, format, "");
|
||||
_ = distro_name(&this.space.projects[0]);
|
||||
_ = distro_version(&this.space.projects[0]);
|
||||
}
|
||||
}
|
||||
|
||||
this.build(false, "");
|
||||
return 1 if this.failed;
|
||||
|
||||
val holder := &this.space.projects[0];
|
||||
val output := holder.output;
|
||||
val stage := output.join("stage");
|
||||
val dist := output.join("dist");
|
||||
if management::remove_tree(&stage) is error(why) {
|
||||
fatal(1, $"`{stage.span}` can't be emptied: {why}.".span);
|
||||
}
|
||||
_ = io::create_directories(&stage);
|
||||
if this.install(&stage) == 0 {
|
||||
fatal(1, $"{holder.identity.span} installs nothing, so there's nothing to package.".span);
|
||||
}
|
||||
_ = io::create_directories(&dist);
|
||||
|
||||
val mtime := build_time();
|
||||
for &format in chosen {
|
||||
var made := path::new();
|
||||
if format == "tar" || format == "zip" {
|
||||
made = this.archive(format, &stage, &dist, platform, mtime);
|
||||
} else if format == "deb" {
|
||||
made = this.debian(&stage, &dist, trimmed(installed_at), platform, mtime);
|
||||
} else if format == "arch" {
|
||||
made = this.arch_package(&stage, &dist, trimmed(installed_at), platform, mtime);
|
||||
} else {
|
||||
made = this.rpm(&stage, &dist, trimmed(installed_at), platform);
|
||||
}
|
||||
io::println($"Packaged {made.span}".span);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
# What the packages are for: what `--target` says, else what the first program
|
||||
# that says is built for, else the system blossom runs on.
|
||||
def session::package_platform(&this): strview {
|
||||
if this.space.platform.length != 0 {
|
||||
return this.space.platform.span;
|
||||
}
|
||||
for &each in this.space.projects[0].targets {
|
||||
if each.kind == models::target_kind::program && each.system.length != 0 {
|
||||
return each.system.span;
|
||||
}
|
||||
}
|
||||
when os == windows {
|
||||
return "windows-x86_64";
|
||||
}
|
||||
return "linux-x86_64";
|
||||
}
|
||||
|
||||
# Writes a tar archive, compressed by `gzip`, or a zip file, of the stage under
|
||||
# a folder of the project's name, version, and target.
|
||||
def session::archive(&mut this, format: strview, stage: &path, dist: &path, platform: strview, mtime: i64): path {
|
||||
val holder := &this.space.projects[0];
|
||||
val stem := $"{holder.name.span}-{holder.semv.span}-{platform}";
|
||||
val items := staged(stage, stem.span);
|
||||
if format == "zip" {
|
||||
var made := zip { bytes = string::new(nothing {}), central = string::new(nothing {}), mtime = mtime };
|
||||
for &item in items {
|
||||
if item.folder {
|
||||
made.folder(item.name.span);
|
||||
} else {
|
||||
val data := read_all(&item.source);
|
||||
made.file(item.name.span, data.span, item.mode);
|
||||
}
|
||||
}
|
||||
val at := dist.join($"{stem.span}.zip".span);
|
||||
write_whole(&at, made.finish());
|
||||
return at;
|
||||
}
|
||||
|
||||
var made := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||
add_items(&mut made, items.span, strview::blank());
|
||||
val at := dist.join($"{stem.span}.tar".span);
|
||||
write_whole(&at, made.finish());
|
||||
return this.compressed(&at, "gzip", ".gz");
|
||||
}
|
||||
|
||||
# Writes a Debian package: its control file and the digest of each file in a
|
||||
# tar compressed by `gzip`, and the stage under the prefix in one compressed by
|
||||
# `xz`, as members of an `ar` archive.
|
||||
def session::debian(&mut this, stage: &path, dist: &path, prefix: strview, platform: strview, mtime: i64): path {
|
||||
val holder := &this.space.projects[0];
|
||||
val name := distro_name(holder);
|
||||
val version := distro_version(holder);
|
||||
val arch := distro_arch(platform, "deb", "amd64");
|
||||
val items := staged(stage, prefix);
|
||||
|
||||
var sums := string::new(nothing {});
|
||||
var size := 0u;
|
||||
for &item in items {
|
||||
continue if item.folder;
|
||||
val data := read_all(&item.source);
|
||||
size += data.length;
|
||||
sums.append($"{md5(data.span).span} {item.name.span}\n".span);
|
||||
}
|
||||
var control := string::new(nothing {});
|
||||
control.append($"Package: {name.span}\n".span);
|
||||
control.append($"Version: {version.span}-1\n".span);
|
||||
control.append($"Architecture: {arch}\n".span);
|
||||
control.append($"Maintainer: {or_else(holder.package.auth.span, "Unknown <unknown@localhost>")}\n".span);
|
||||
control.append($"Installed-Size: {(size + 1023) / 1024}\n".span);
|
||||
control.append("Section: misc\nPriority: optional\n");
|
||||
if holder.package.url.length != 0 {
|
||||
control.append($"Homepage: {holder.package.url.span}\n".span);
|
||||
}
|
||||
control.append($"Description: {or_else(holder.package.desc.span, holder.name.span)}\n".span);
|
||||
|
||||
val work := dist.join("deb");
|
||||
_ = management::remove_tree(&work);
|
||||
_ = io::create_directories(&work);
|
||||
|
||||
var controls := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||
controls.folder(".");
|
||||
controls.file("./control", control.span, 0o644);
|
||||
controls.file("./md5sums", sums.span, 0o644);
|
||||
val control_at := work.join("control.tar");
|
||||
write_whole(&control_at, controls.finish());
|
||||
val control_gz := this.compressed(&control_at, "gzip", ".gz");
|
||||
|
||||
var data := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||
data.folder(".");
|
||||
add_items(&mut data, items.span, "./");
|
||||
val data_at := work.join("data.tar");
|
||||
write_whole(&data_at, data.finish());
|
||||
val data_xz := this.compressed(&data_at, "xz", ".xz");
|
||||
|
||||
var made := ar::new(mtime);
|
||||
made.member("debian-binary", "2.0\n");
|
||||
made.member("control.tar.gz", read_all(&control_gz).span);
|
||||
made.member("data.tar.xz", read_all(&data_xz).span);
|
||||
val at := dist.join($"{name.span}_{version.span}-1_{arch}.deb".span);
|
||||
write_whole(&at, made.bytes.span);
|
||||
_ = management::remove_tree(&work);
|
||||
return at;
|
||||
}
|
||||
|
||||
# Writes an Arch package: its `.PKGINFO`, its `.MTREE` of each entry's
|
||||
# digests, compressed by `gzip`, and the stage under the prefix, in a tar
|
||||
# compressed by `zstd`.
|
||||
def session::arch_package(&mut this, stage: &path, dist: &path, prefix: strview, platform: strview, mtime: i64): path {
|
||||
val holder := &this.space.projects[0];
|
||||
val name := distro_name(holder);
|
||||
val version := distro_version(holder);
|
||||
val arch := distro_arch(platform, "arch", "x86_64");
|
||||
val items := staged(stage, prefix);
|
||||
|
||||
var size := 0u;
|
||||
for &item in items {
|
||||
continue if item.folder;
|
||||
val found := io::info(&item.source) else (_) { continue; };
|
||||
size += found.size;
|
||||
}
|
||||
var info := string::from("# Generated by blossom\n", nothing {});
|
||||
info.append($"pkgname = {name.span}\npkgbase = {name.span}\nxdata = pkgtype=pkg\n".span);
|
||||
info.append($"pkgver = {version.span}-1\n".span);
|
||||
info.append($"pkgdesc = {or_else(holder.package.desc.span, holder.name.span)}\n".span);
|
||||
if holder.package.url.length != 0 {
|
||||
info.append($"url = {holder.package.url.span}\n".span);
|
||||
}
|
||||
info.append($"builddate = {mtime}\n".span);
|
||||
info.append($"packager = {or_else(holder.package.auth.span, "Unknown Packager")}\n".span);
|
||||
info.append($"size = {size}\narch = {arch}\n".span);
|
||||
if holder.package.lic.length != 0 {
|
||||
info.append($"license = {holder.package.lic.span}\n".span);
|
||||
}
|
||||
|
||||
var tree := string::from("#mtree\n/set type=file uid=0 gid=0 mode=644\n", nothing {});
|
||||
tree.append($"./.PKGINFO time={mtime}.0 size={info.length} md5digest={md5(info.span).span} sha256digest={sha256(info.span).span}\n".span);
|
||||
for &item in items {
|
||||
val named := mtree_escaped(item.name.span);
|
||||
if item.folder {
|
||||
tree.append($"./{named.span} time={mtime}.0 mode=755 type=dir\n".span);
|
||||
continue;
|
||||
}
|
||||
val data := read_all(&item.source);
|
||||
var mode := string::new(nothing {});
|
||||
if item.mode != 0o644 {
|
||||
mode.append(" mode=");
|
||||
mode.append(octal_text(item.mode).span);
|
||||
}
|
||||
tree.append($"./{named.span} time={mtime}.0{mode.span} size={data.length} md5digest={md5(data.span).span} sha256digest={sha256(data.span).span}\n".span);
|
||||
}
|
||||
|
||||
val work := dist.join("arch");
|
||||
_ = management::remove_tree(&work);
|
||||
_ = io::create_directories(&work);
|
||||
val tree_at := work.join("mtree");
|
||||
write_whole(&tree_at, tree.span);
|
||||
val tree_gz := this.compressed(&tree_at, "gzip", ".gz");
|
||||
|
||||
var made := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||
made.file(".PKGINFO", info.span, 0o644);
|
||||
made.file(".MTREE", read_all(&tree_gz).span, 0o644);
|
||||
add_items(&mut made, items.span, strview::blank());
|
||||
val at := dist.join($"{name.span}-{version.span}-1-{arch}.pkg.tar".span);
|
||||
write_whole(&at, made.finish());
|
||||
_ = management::remove_tree(&work);
|
||||
return this.compressed(&at, "zstd", ".zst");
|
||||
}
|
||||
|
||||
# Builds an rpm package with `rpmbuild`, from a spec written for it that copies
|
||||
# the stage under the prefix.
|
||||
def session::rpm(&mut this, stage: &path, dist: &path, prefix: strview, platform: strview): path {
|
||||
val holder := &this.space.projects[0];
|
||||
val name := distro_name(holder);
|
||||
val version := distro_version(holder);
|
||||
val arch := distro_arch(platform, "rpm", "x86_64");
|
||||
val summary := or_else(holder.package.desc.span, holder.name.span);
|
||||
|
||||
var spec := string::from("%define debug_package %{nil}\n", nothing {});
|
||||
spec.append($"Name: {name.span}\nVersion: {version.span}\nRelease: 1\nSummary: {summary}\n".span);
|
||||
spec.append($"License: {or_else(holder.package.lic.span, "Unspecified")}\n".span);
|
||||
if holder.package.url.length != 0 {
|
||||
spec.append($"URL: {holder.package.url.span}\n".span);
|
||||
}
|
||||
spec.append($"BuildArch: {arch}\n\n%description\n{summary}\n\n%install\n".span);
|
||||
spec.append("mkdir -p \"%{buildroot}/");
|
||||
spec.append(prefix);
|
||||
spec.append("\"\ncp -a \"");
|
||||
spec.append(stage.span);
|
||||
spec.append("/.\" \"%{buildroot}/");
|
||||
spec.append(prefix);
|
||||
spec.append("/\"\n\n%files\n%defattr(-,root,root,-)\n");
|
||||
val items := staged(stage, prefix);
|
||||
for &item in items {
|
||||
continue if item.folder;
|
||||
spec.append($"\"/{item.name.span}\"\n".span);
|
||||
}
|
||||
|
||||
val top := dist.join("rpmbuild");
|
||||
val file := top.join("SPECS").join($"{name.span}.spec".span);
|
||||
if !written(&file, spec.span) {
|
||||
fatal(1, $"`{file.span}` can't be written.".span);
|
||||
}
|
||||
var words := vector::<string>::new();
|
||||
words.append(string::from("rpmbuild", nothing {}));
|
||||
words.append(string::from("-bb", nothing {}));
|
||||
words.append(string::from("--define", nothing {}));
|
||||
words.append($"_topdir {top.span}");
|
||||
words.append(string::from(file.span, nothing {}));
|
||||
var building := this.command(words.span, dist);
|
||||
val status := building.run() else (_) {
|
||||
fatal(1, $"rpm packages need `rpmbuild`, which isn't found; the spec is written to `{file.span}`.".span);
|
||||
return path::new();
|
||||
};
|
||||
if !status.succeeded {
|
||||
fatal(1, "`rpmbuild` failed.");
|
||||
}
|
||||
|
||||
# `rpmbuild` puts the package in a folder of its architecture.
|
||||
val built := top.join("RPMS").join(arch).join($"{name.span}-{version.span}-1.{arch}.rpm".span);
|
||||
val at := dist.join($"{name.span}-{version.span}-1.{arch}.rpm".span);
|
||||
if io::rename(&built, &at) is error(why) {
|
||||
fatal(1, $"`{built.span}` can't be moved to `{at.span}`: {why}.".span);
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
# Adds entries to a tar archive, each name after a prefix, as `./`.
|
||||
def add_items(made: &mut tar, items: span<entry>, before: strview): void {
|
||||
for &item in items {
|
||||
val name := $"{before}{item.name.span}";
|
||||
if item.folder {
|
||||
made.folder(name.span);
|
||||
} else {
|
||||
val data := read_all(&item.source);
|
||||
made.file(name.span, data.span, item.mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Compresses a file with a program of the system, `gzip`, `xz`, or `zstd`,
|
||||
# which replaces it with one named with a suffix, as `.gz`, giving its path.
|
||||
def session::compressed(&mut this, at: &path, program: strview, suffix: strview): path {
|
||||
var words := vector::<string>::new();
|
||||
words.append(string::from(program, nothing {}));
|
||||
if program == "gzip" {
|
||||
words.append(string::from("-n", nothing {}));
|
||||
words.append(string::from("-9", nothing {}));
|
||||
} else if program == "zstd" {
|
||||
words.append(string::from("-q", nothing {}));
|
||||
words.append(string::from("--rm", nothing {}));
|
||||
words.append(string::from("-19", nothing {}));
|
||||
}
|
||||
words.append(string::from("-f", nothing {}));
|
||||
words.append(string::from(at.span, nothing {}));
|
||||
val folder := at.parent() else { return path::new(); };
|
||||
var compressing := this.command(words.span, &folder);
|
||||
val status := compressing.run() else (why) {
|
||||
fatal(1, $"`{program}` can't be run, to compress `{at.span}`: {why}.".span);
|
||||
return path::new();
|
||||
};
|
||||
if !status.succeeded {
|
||||
fatal(1, $"`{program}` failed to compress `{at.span}`.".span);
|
||||
}
|
||||
return path::parse($"{at.span}{suffix}".span) else (_) { return path::new(); };
|
||||
}
|
||||
|
||||
# When a package's entries were written: `SOURCE_DATE_EPOCH`, where it's set,
|
||||
# else now.
|
||||
def build_time(): i64 {
|
||||
if process::variable("SOURCE_DATE_EPOCH") is some(given) {
|
||||
if integer::parse::<i64>(given) is value(seconds) {
|
||||
return seconds;
|
||||
}
|
||||
}
|
||||
return time::timestamp::now().unix;
|
||||
}
|
||||
|
||||
# The project's name as a system's package names it: in lower case, with `-`
|
||||
# for `_`, two or more letters, digits, `.`, `+`, or `-`, starting with a
|
||||
# letter or digit.
|
||||
def distro_name(holder: &models::project): string {
|
||||
var made := holder.name.lower();
|
||||
for k in 0..made.length {
|
||||
if made[k] == '_' {
|
||||
made[k] = '-';
|
||||
}
|
||||
}
|
||||
var fits := made.length >= 2 && made[0].is_alnum;
|
||||
for k in 0..made.length {
|
||||
val byte := made[k];
|
||||
fits = fits && (byte.is_alnum || byte == '.' || byte == '+' || byte == '-');
|
||||
}
|
||||
if !fits {
|
||||
fatal(2, $"`{holder.name.span}` can't name a system's package: it needs two or more letters, digits, `.`, `+`, or `-`.".span);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The project's version as a system's package takes it: starting with a digit,
|
||||
# with only letters, digits, `.`, `+`, or `~`.
|
||||
def distro_version(holder: &models::project): string {
|
||||
val text := holder.semv.span;
|
||||
var fits := text.length != 0 && text[0].is_digit;
|
||||
for k in 0..text.length {
|
||||
val byte := text[k];
|
||||
fits = fits && (byte.is_alnum || byte == '.' || byte == '+' || byte == '~');
|
||||
}
|
||||
if !fits {
|
||||
fatal(2, $"{holder.identity.span}'s version, `{text}`, can't be a system package's: it starts with a digit, and holds only letters, digits, `.`, `+`, or `~`.".span);
|
||||
}
|
||||
return string::from(text, nothing {});
|
||||
}
|
||||
|
||||
# The architecture a system's packages name x86-64 by, where they're for Linux.
|
||||
def distro_arch(platform: strview, format: strview, name: strview): strview {
|
||||
if platform != "linux-x86_64" {
|
||||
fatal(2, $"{format} packages are for Linux, not `{platform}`.".span);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
# A path in `.MTREE`, each byte but letters, digits, and `._/+-` written as `\`
|
||||
# and three octal digits.
|
||||
def mtree_escaped(text: strview): string {
|
||||
var made := string::new(nothing {});
|
||||
for k in 0..text.length {
|
||||
val byte := text[k];
|
||||
if byte.is_alnum || byte == '.' || byte == '_' || byte == '/' || byte == '+' || byte == '-' {
|
||||
made.push(byte);
|
||||
} else {
|
||||
made.push('\\');
|
||||
made.push('0' + (byte >> 6));
|
||||
made.push('0' + ((byte >> 3) & 7));
|
||||
made.push('0' + (byte & 7));
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A mode in octal, as `755`.
|
||||
def octal_text(mode: u32): string {
|
||||
var made := string::new(nothing {});
|
||||
made.push('0' + ((mode >> 6) & 7) as u8);
|
||||
made.push('0' + ((mode >> 3) & 7) as u8);
|
||||
made.push('0' + (mode & 7) as u8);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Text, or another where it's empty.
|
||||
def or_else(text: strview, fallback: strview): strview {
|
||||
return text.length != 0 ? text : fallback;
|
||||
}
|
||||
|
||||
# The whole of a file, stopping the command where it can't be read.
|
||||
def read_all(at: &path): string {
|
||||
return io::read_file(at) else (why) {
|
||||
fatal(1, $"`{at.span}` can't be read: {why}.".span);
|
||||
return string::new(nothing {});
|
||||
};
|
||||
}
|
||||
|
||||
# Writes a file whole, stopping the command where it can't be written.
|
||||
def write_whole(at: &path, bytes: strview): void {
|
||||
if !written(at, bytes) {
|
||||
fatal(1, $"`{at.span}` can't be written.".span);
|
||||
}
|
||||
}
|
||||
|
||||
# A prefix as a package holds it: without the `/` it starts or ends with.
|
||||
def trimmed(text: strview): strview {
|
||||
var start := 0u;
|
||||
var end := text.length;
|
||||
while start < end && text[start] == '/' {
|
||||
start++;
|
||||
}
|
||||
while end > start && text[end - 1] == '/' {
|
||||
end--;
|
||||
}
|
||||
return text.substr(start, end - start);
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
#! @file session.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the session a command runs in: the workspace, the `sakura` it runs
|
||||
#! and what it's given, the steps counted and said as they're done, and what
|
||||
#! decides a step is done already: the command that made its output, kept
|
||||
#! beside it, and the files `sakura` said it read, through `--deps`, none newer
|
||||
#! than the output.
|
||||
import std::io;
|
||||
import std::process;
|
||||
import system;
|
||||
import blossom::models;
|
||||
import blossom::management;
|
||||
|
||||
module blossom::pipeline;
|
||||
|
||||
#! What a command runs with, and how its steps went.
|
||||
pub struct session {
|
||||
pub var space: management::workspace;
|
||||
pub var sakura: string; #! The `sakura` run, absolute.
|
||||
pub var std: string; #! The standard library it's given, or empty for the one it finds.
|
||||
pub var threads: string; #! What `-j` says, or empty.
|
||||
pub var step: u64; #! The step being done, counted from 1.
|
||||
pub var steps: u64; #! How many there are.
|
||||
pub var failed: bool; #! Whether a step failed.
|
||||
}
|
||||
|
||||
#! Says a step is done, as `[2/3] Built hello`.
|
||||
#! @param this The session.
|
||||
#! @param what The step, as `Built`.
|
||||
#! @param name The target's name.
|
||||
#! @param more What follows, in parentheses, or empty.
|
||||
pub def session::said(&mut this, what: strview, name: strview, more: strview = ""): void {
|
||||
this.step++;
|
||||
if more.length != 0 {
|
||||
io::println($"[{this.step}/{this.steps}] {what} {name} ({more})".span);
|
||||
} else {
|
||||
io::println($"[{this.step}/{this.steps}] {what} {name}".span);
|
||||
}
|
||||
}
|
||||
|
||||
#! Says a step failed, as `Failed hello`, and notes it, which the command's
|
||||
#! status says.
|
||||
#! @param this The session.
|
||||
#! @param name The target's name.
|
||||
pub def session::fail(&mut this, name: strview): void {
|
||||
this.step++;
|
||||
this.failed = true;
|
||||
io::println($"Failed {name}".span);
|
||||
}
|
||||
|
||||
#! Says a step is skipped, as `Skipped hello`, since one it needs failed.
|
||||
#! @param this The session.
|
||||
#! @param name The target's name.
|
||||
pub def session::skip(&mut this, name: strview): void {
|
||||
this.step++;
|
||||
io::println($"Skipped {name}".span);
|
||||
}
|
||||
|
||||
#! The words starting a `sakura` command: the program, the command, and the
|
||||
#! standard library where the config names one, as `--lib std=DIR`.
|
||||
#! @param this The session.
|
||||
#! @param command `check`, `run`, or `build`.
|
||||
#! @returns The words.
|
||||
pub def session::words(&this, command: strview): vector<string> {
|
||||
var made := vector::<string>::new();
|
||||
made.append(string::copy_of(&this.sakura));
|
||||
made.append(string::from(command, nothing {}));
|
||||
if this.std.length != 0 {
|
||||
made.append(string::from("--lib", nothing {}));
|
||||
made.append($"std={this.std}");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Adds `-j N` where the command was given it.
|
||||
#! @param this The session.
|
||||
#! @param words The words to add to.
|
||||
pub def session::add_threads(&this, words: &mut vector<string>): void {
|
||||
if this.threads.length != 0 {
|
||||
words.append(string::from("-j", nothing {}));
|
||||
words.append(string::copy_of(&this.threads));
|
||||
}
|
||||
}
|
||||
|
||||
#! Runs a command in a project's folder, its output and error going where
|
||||
#! blossom's go, printing it first where `-v` asks.
|
||||
#! @param this The session.
|
||||
#! @param words The program, then its arguments.
|
||||
#! @param cwd The folder it runs in.
|
||||
#! @returns How it stopped, or why it couldn't start.
|
||||
pub def session::run(&mut this, words: span<string>, cwd: &path): result<process::status, io_error> {
|
||||
var made := this.command(words, cwd);
|
||||
return made.run();
|
||||
}
|
||||
|
||||
#! A command, ready to run in a project's folder, with `SAKURA` set to the
|
||||
#! `sakura` blossom runs, so what it runs can run `sakura` as blossom does,
|
||||
#! printed first where `-v` asks.
|
||||
#! @param this The session.
|
||||
#! @param words The program, then its arguments.
|
||||
#! @param cwd The folder it runs in.
|
||||
#! @returns The command.
|
||||
pub def session::command(&mut this, words: span<string>, cwd: &path): process::command {
|
||||
if this.space.verbose {
|
||||
io::println(spoken(words).span);
|
||||
}
|
||||
var made := process::command::new(words[0].span);
|
||||
for k in 1..words.length {
|
||||
made.argument(words[k].span);
|
||||
}
|
||||
made.directory(cwd);
|
||||
made.set_variable("SAKURA", this.sakura.span);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! A command as a shell would take it, each word quoted where it holds what a
|
||||
#! shell reads apart.
|
||||
#! @param words The words.
|
||||
#! @returns The line.
|
||||
pub def spoken(words: span<string>): string {
|
||||
var made := string::new(nothing {});
|
||||
for &each in words {
|
||||
if made.length != 0 {
|
||||
made.push(' ');
|
||||
}
|
||||
var plain := each.length != 0;
|
||||
for k in 0..each.length {
|
||||
val byte := each[k];
|
||||
plain = plain && (byte.is_alnum || byte == '/' || byte == '.' || byte == '-' || byte == '_' || byte == '=' || byte == ':' || byte == '+' || byte == ',' || byte == '@');
|
||||
}
|
||||
if plain {
|
||||
made.append(each.span);
|
||||
continue;
|
||||
}
|
||||
made.push('\'');
|
||||
for k in 0..each.length {
|
||||
if each[k] == '\'' {
|
||||
made.append("'\\''");
|
||||
} else {
|
||||
made.push(each[k]);
|
||||
}
|
||||
}
|
||||
made.push('\'');
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! What's kept beside an output to say how it was made: the `sakura` that made
|
||||
#! it, when that executable was written, and the command's words, a line each.
|
||||
#! @param this The session.
|
||||
#! @param words The command.
|
||||
#! @returns The text.
|
||||
pub def session::made_by(&this, words: span<string>): string {
|
||||
var made := string::copy_of(&this.sakura);
|
||||
var written: i64 = 0;
|
||||
if path::parse(this.sakura.span) is value(&at) {
|
||||
if io::info(&at) is value(&found) {
|
||||
written = found.modified;
|
||||
}
|
||||
}
|
||||
made.append($"\n{written}".span);
|
||||
for &each in words {
|
||||
made.push('\n');
|
||||
made.append(each.span);
|
||||
}
|
||||
made.push('\n');
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether an output is up to date: it's there, the command kept beside it is
|
||||
#! the one that would make it now, and no file `sakura` read making it, nor the
|
||||
#! blossomfile, is newer than it.
|
||||
#! @param output The output, as an executable or a stamp.
|
||||
#! @param kept The command kept beside it.
|
||||
#! @param now The command that would make it now, as `made_by` gives it.
|
||||
#! @param deps The files `sakura` read, as `--deps` wrote them, relative to
|
||||
#! the project's folder where they're not absolute.
|
||||
#! @param holder The project.
|
||||
#! @returns Whether it is.
|
||||
pub def is_fresh(output: &path, kept: &path, now: strview, deps: &path, holder: &models::project): bool {
|
||||
val made := io::info(output) else (_) { return false; };
|
||||
val before := io::read_file(kept) else (_) { return false; };
|
||||
return false if before.span != now;
|
||||
val read := io::read_file(deps) else (_) { return false; };
|
||||
return false if newer(&holder.file, made.modified);
|
||||
for line in read.span.lines() {
|
||||
continue if line.length == 0;
|
||||
val named := path::parse(line) else (_) { return false; };
|
||||
val file := holder.folder.join(&named);
|
||||
return false if newer(&file, made.modified);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Whether a file was written after a time, or isn't there to say.
|
||||
def newer(at: &path, than: i64): bool {
|
||||
val found := io::info(at) else (_) { return true; };
|
||||
return found.modified > than;
|
||||
}
|
||||
|
||||
#! Writes a file, making the folders above it first.
|
||||
#! @param at The file.
|
||||
#! @param text What it holds.
|
||||
#! @returns Whether it was written.
|
||||
pub def written(at: &path, text: strview): bool {
|
||||
if at.parent() is some(&above) {
|
||||
_ = io::create_directories(above);
|
||||
}
|
||||
return io::write_file(at, text) is value;
|
||||
}
|
||||
|
||||
#! A status as the code a program exits with: its own where it exited, and
|
||||
#! 128 and the signal's number where a signal stopped it, as shells give it.
|
||||
#! @param status How it stopped.
|
||||
#! @returns The code.
|
||||
pub def exit_code(status: &process::status): i32 {
|
||||
if status is exited(code) {
|
||||
return code;
|
||||
}
|
||||
if status is signaled(signal) {
|
||||
return 128 + signal;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
#! @file steps.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the steps of `check`, `build`, `test`, and `run`: a library checked,
|
||||
#! a program built, a test run, and a program run on the machine, each one
|
||||
#! `sakura` command run in the project's folder, with `--lib` for every library
|
||||
#! its `deps` reach.
|
||||
import std::io;
|
||||
import std::process;
|
||||
import system;
|
||||
import blossom::models;
|
||||
import blossom::management;
|
||||
|
||||
module blossom::pipeline;
|
||||
|
||||
# Bytes read at a time from a test's output, which is passed on as it comes.
|
||||
const passing_bytes: u64 = 4096;
|
||||
|
||||
#! Checks the project's libraries, then builds its programs, each where it
|
||||
#! isn't up to date, and, for `test`, runs its tests after.
|
||||
#! @param this The session.
|
||||
#! @param tests Whether the tests are run, as `test` asks.
|
||||
#! @param only Text a test's name holds to be run, or empty for every test.
|
||||
pub def session::build(&mut this, tests: bool, only: strview): void {
|
||||
val root := &this.space.projects[0];
|
||||
var chosen := vector::<u64>::new();
|
||||
if tests {
|
||||
for k in 0..root.tests.length {
|
||||
if only.length == 0 || root.tests[k].name.span.find(only) is some {
|
||||
chosen.append(k);
|
||||
}
|
||||
}
|
||||
if chosen.length == 0 {
|
||||
fatal(2, only.length == 0 ? $"{root.identity.span} has no tests." : $"No test of {root.identity.span} is named with `{only}`.");
|
||||
}
|
||||
}
|
||||
this.steps = root.targets.length + chosen.length;
|
||||
|
||||
var broken := vector::<u64>::new(); # The libraries that failed to check.
|
||||
for t in 0..root.targets.length {
|
||||
continue if this.space.projects[0].targets[t].kind != models::target_kind::library;
|
||||
if !this.check_library(t, true) {
|
||||
val name := &this.space.projects[0].targets[t].name;
|
||||
broken.append(this.space.library_named(name.span).some);
|
||||
}
|
||||
}
|
||||
for t in 0..root.targets.length {
|
||||
continue if this.space.projects[0].targets[t].kind != models::target_kind::program;
|
||||
_ = this.build_program(t, false, &broken);
|
||||
}
|
||||
for &k in chosen {
|
||||
_ = this.run_test(k, &broken);
|
||||
}
|
||||
}
|
||||
|
||||
#! Checks every target of the project, its libraries and programs, writing
|
||||
#! nothing.
|
||||
#! @param this The session.
|
||||
pub def session::check(&mut this): void {
|
||||
this.steps = this.space.projects[0].targets.length;
|
||||
for t in 0..this.steps {
|
||||
if this.space.projects[0].targets[t].kind == models::target_kind::library {
|
||||
_ = this.check_library(t, false);
|
||||
} else {
|
||||
_ = this.check_program(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Runs a program target on the machine, as `sakura run` does.
|
||||
#! @param this The session.
|
||||
#! @param name The target, or empty for the project's only program.
|
||||
#! @param args What the program is given.
|
||||
#! @returns The status it stopped with.
|
||||
pub def session::run_program(&mut this, name: strview, args: span<strview>): i32 {
|
||||
val root := &this.space.projects[0];
|
||||
var chosen := root.targets.length;
|
||||
for t in 0..root.targets.length {
|
||||
continue if root.targets[t].kind != models::target_kind::program;
|
||||
if name.length == 0 {
|
||||
if chosen != root.targets.length {
|
||||
fatal(2, $"{root.identity.span} has more than one program: name the one to run.");
|
||||
}
|
||||
chosen = t;
|
||||
} else if root.targets[t].name.span == name {
|
||||
chosen = t;
|
||||
}
|
||||
}
|
||||
if chosen == root.targets.length {
|
||||
fatal(2, name.length == 0 ? $"{root.identity.span} has no program to run." : $"`{name}` is no program of {root.identity.span}.");
|
||||
}
|
||||
|
||||
val each := &this.space.projects[0].targets[chosen];
|
||||
var words := this.words("run");
|
||||
this.add_libraries(each, &mut words);
|
||||
this.add_threads(&mut words);
|
||||
this.add_sources(each, &mut words);
|
||||
if args.length != 0 {
|
||||
words.append(string::from("--", nothing {}));
|
||||
for &arg in args {
|
||||
words.append(string::from(arg, nothing {}));
|
||||
}
|
||||
}
|
||||
val status := this.run(words.span, &this.space.projects[0].folder) else (why) {
|
||||
fatal(1, $"`{this.sakura.span}` can't be run: {why}.");
|
||||
return 1;
|
||||
};
|
||||
return exit_code(&status);
|
||||
}
|
||||
|
||||
# Checks a library: its files, with `--lib` for those it reads. Where `fresh` is
|
||||
# asked, a stamp kept with the command and the files read says it's checked, so
|
||||
# it's checked again only where those change.
|
||||
def session::check_library(&mut this, t: u64, fresh: bool): bool {
|
||||
val holder := &this.space.projects[0];
|
||||
val each := &holder.targets[t];
|
||||
var files := management::library_files(holder, each);
|
||||
if files is error(&why) {
|
||||
fatal(why.status, why.message.span);
|
||||
}
|
||||
|
||||
var words := this.words("check");
|
||||
this.add_libraries(each, &mut words);
|
||||
this.add_threads(&mut words);
|
||||
val base := $"{output_text(holder).span}/check/{each.name.span}";
|
||||
if fresh {
|
||||
words.append(string::from("--deps", nothing {}));
|
||||
words.append($"{base.span}.deps");
|
||||
}
|
||||
if files is value(&listed) {
|
||||
for &file in listed {
|
||||
words.append(string::copy_of(file));
|
||||
}
|
||||
}
|
||||
return this.made(t, words.span, fresh ? base.span : strview::blank(), "Checked", "");
|
||||
}
|
||||
|
||||
# Checks a program, as `check` does, writing nothing.
|
||||
def session::check_program(&mut this, t: u64): bool {
|
||||
val each := &this.space.projects[0].targets[t];
|
||||
var words := this.words("check");
|
||||
this.add_libraries(each, &mut words);
|
||||
val system := this.platform_of(each);
|
||||
if system.length != 0 {
|
||||
words.append(string::from("--target", nothing {}));
|
||||
words.append(string::from(system, nothing {}));
|
||||
}
|
||||
this.add_threads(&mut words);
|
||||
this.add_sources(each, &mut words);
|
||||
return this.made(t, words.span, strview::blank(), "Checked", "");
|
||||
}
|
||||
|
||||
# Builds a program, or a test that runs natively, into the output folder's
|
||||
# `bin`, unless it's up to date, or a library of the project it reads failed.
|
||||
def session::build_program(&mut this, t: u64, test: bool, broken: &vector<u64>): bool {
|
||||
val holder := &this.space.projects[0];
|
||||
val index := test ? holder.targets.length + t : t;
|
||||
val each := &holder.at(index);
|
||||
if this.reaches_broken(each, broken) {
|
||||
this.skip(each.name.span);
|
||||
return false;
|
||||
}
|
||||
|
||||
val system := this.platform_of(each);
|
||||
val output := $"{output_text(holder).span}/bin/{output_name(each.name.span, system).span}";
|
||||
var words := this.words("build");
|
||||
this.add_libraries(each, &mut words);
|
||||
words.append($"-O{each.level}");
|
||||
if each.petal {
|
||||
words.append(string::from("-g", nothing {}));
|
||||
}
|
||||
if system.length != 0 {
|
||||
words.append(string::from("--target", nothing {}));
|
||||
words.append(string::from(system, nothing {}));
|
||||
}
|
||||
if test && each.checks {
|
||||
words.append(string::from("--checks", nothing {}));
|
||||
}
|
||||
this.add_threads(&mut words);
|
||||
for &opt in each.opts {
|
||||
words.append(string::copy_of(opt));
|
||||
}
|
||||
words.append(string::from("--deps", nothing {}));
|
||||
words.append($"{output.span}.deps");
|
||||
words.append(string::from("-o", nothing {}));
|
||||
words.append(string::copy_of(&output));
|
||||
this.add_sources(each, &mut words);
|
||||
return this.made(index, words.span, output.span, test ? "" : "Built", output.span);
|
||||
}
|
||||
|
||||
# Runs a command that checks or builds a target, unless its output, where it
|
||||
# has one, is up to date, and says how it went. A test built is said by its
|
||||
# run, so its build is said only where it fails.
|
||||
def session::made(&mut this, index: u64, words: span<string>, output: strview, verb: strview, more: strview): bool {
|
||||
val holder := &this.space.projects[0];
|
||||
val name := &holder.at(index).name;
|
||||
val now := this.made_by(words);
|
||||
if output.length != 0 {
|
||||
val out := holder.folder.join(output);
|
||||
val kept := holder.folder.join($"{output}.command".span);
|
||||
val deps := holder.folder.join($"{output}.deps".span);
|
||||
if is_fresh(&out, &kept, now.span, &deps, holder) {
|
||||
if verb.length != 0 {
|
||||
this.said("Up to date", name.span);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
_ = io::remove(&kept);
|
||||
if out.parent() is some(&above) {
|
||||
_ = io::create_directories(above);
|
||||
}
|
||||
}
|
||||
|
||||
val status := this.run(words, &holder.folder) else (why) {
|
||||
fatal(1, $"`{this.sakura.span}` can't be run: {why}.");
|
||||
return false;
|
||||
};
|
||||
if !status.succeeded {
|
||||
this.fail(name.span);
|
||||
return false;
|
||||
}
|
||||
if output.length != 0 {
|
||||
val kept := holder.folder.join($"{output}.command".span);
|
||||
if verb == "Checked" {
|
||||
_ = written(&holder.folder.join(output), "");
|
||||
}
|
||||
_ = written(&kept, now.span);
|
||||
}
|
||||
if verb.length != 0 {
|
||||
this.said(verb, name.span, more);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Runs a test: on the machine, as `sakura run`, with `--checks` unless it says
|
||||
# otherwise, or built, then run. What it writes is passed on as it comes, and
|
||||
# the count its last line gives, as `14 passed`, is said with it.
|
||||
def session::run_test(&mut this, k: u64, broken: &vector<u64>): bool {
|
||||
val holder := &this.space.projects[0];
|
||||
val each := &holder.tests[k];
|
||||
var words := vector::<string>::new();
|
||||
if each.runs == models::runner::native {
|
||||
return false if !this.build_program(k, true, broken);
|
||||
val output := $"{output_text(holder).span}/bin/{output_name(each.name.span, this.platform_of(each)).span}";
|
||||
words.append(string::from(holder.folder.join(output.span).span, nothing {}));
|
||||
for &arg in each.args {
|
||||
words.append(string::copy_of(arg));
|
||||
}
|
||||
} else {
|
||||
if this.reaches_broken(each, broken) {
|
||||
this.skip(each.name.span);
|
||||
return false;
|
||||
}
|
||||
words = this.words("run");
|
||||
this.add_libraries(each, &mut words);
|
||||
if each.checks {
|
||||
words.append(string::from("--checks", nothing {}));
|
||||
}
|
||||
this.add_threads(&mut words);
|
||||
this.add_sources(each, &mut words);
|
||||
if each.args.length != 0 {
|
||||
words.append(string::from("--", nothing {}));
|
||||
for &arg in each.args {
|
||||
words.append(string::copy_of(arg));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var running := this.command(words.span, &holder.folder);
|
||||
running.output(process::stdio::piped);
|
||||
var child := running.spawn() else (why) {
|
||||
fatal(1, $"`{words[0].span}` can't be run: {why}.");
|
||||
return false;
|
||||
};
|
||||
|
||||
# The output is passed on as it comes, keeping its last line.
|
||||
var last := string::new(nothing {});
|
||||
var chunk: u8[passing_bytes];
|
||||
val into := slice::<u8>::over((&mut chunk) as *mut u8, passing_bytes);
|
||||
while true {
|
||||
val count := child.output().read(into) else (_) { break; };
|
||||
break if count == 0;
|
||||
val text := strview::over((&chunk) as *u8, count);
|
||||
_ = system::fd::stdout.put(text);
|
||||
last.append(text);
|
||||
if last.length > 1024 {
|
||||
val keep := last.span.substr(last.length - 512, 512);
|
||||
last = string::from(keep, nothing {});
|
||||
}
|
||||
}
|
||||
val status := child.wait() else (why) {
|
||||
fatal(1, $"`{words[0].span}` couldn't be waited for: {why}.");
|
||||
return false;
|
||||
};
|
||||
if !status.succeeded {
|
||||
this.fail(each.name.span);
|
||||
return false;
|
||||
}
|
||||
val counted := tally_of(last.span);
|
||||
this.said("Tested", each.name.span, counted);
|
||||
return true;
|
||||
}
|
||||
|
||||
# The count a test's last line gives, as `14 passed` of `14 passed, 0 failed,
|
||||
# 0 skipped`, or empty where it gives none.
|
||||
def tally_of(text: strview): strview {
|
||||
var end := text.length;
|
||||
while end > 0 && (text[end - 1] == '\n' || text[end - 1] == '\r') {
|
||||
end--;
|
||||
}
|
||||
var start := end;
|
||||
while start > 0 && text[start - 1] != '\n' {
|
||||
start--;
|
||||
}
|
||||
val line := text.substr(start, end - start);
|
||||
val comma := line.find(",") else { return strview::blank(); };
|
||||
val first := line.substr(0, comma);
|
||||
return first.ends_with(" passed") ? first : strview::blank();
|
||||
}
|
||||
|
||||
# Adds `--lib` for every library a target's `deps` reach.
|
||||
def session::add_libraries(&mut this, each: &models::target, words: &mut vector<string>): void {
|
||||
var reached := vector::<u64>::new();
|
||||
var found := this.space.reach(0, each.deps.span, each.name.span, &mut reached);
|
||||
if found is error(&why) {
|
||||
fatal(why.status, why.message.span);
|
||||
}
|
||||
for &library in reached {
|
||||
words.append(string::from("--lib", nothing {}));
|
||||
words.append(this.space.lib_option(library, 0));
|
||||
}
|
||||
}
|
||||
|
||||
# Adds a target's files, as its `srcs` give them.
|
||||
def session::add_sources(&mut this, each: &models::target, words: &mut vector<string>): void {
|
||||
var files := management::sources_of(&this.space.projects[0], each);
|
||||
if files is error(&why) {
|
||||
fatal(why.status, why.message.span);
|
||||
}
|
||||
if files is value(&mut listed) {
|
||||
for &mut file in listed {
|
||||
words.append(move(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Whether a target reads a library of the project that failed to check.
|
||||
def session::reaches_broken(&mut this, each: &models::target, broken: &vector<u64>): bool {
|
||||
return false if broken.length == 0;
|
||||
var reached := vector::<u64>::new();
|
||||
_ = this.space.reach(0, each.deps.span, each.name.span, &mut reached);
|
||||
for &library in reached {
|
||||
for &failed in broken {
|
||||
return true if library == failed;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# What a target is built for: what `--target` says, else its own `target`,
|
||||
# else nothing, for the system blossom runs on.
|
||||
def session::platform_of(&this, each: &models::target): strview {
|
||||
if this.space.platform.length != 0 {
|
||||
return this.space.platform.span;
|
||||
}
|
||||
return each.system.span;
|
||||
}
|
||||
|
||||
#! The file a program is built into: its name, with `.exe` where it's for
|
||||
#! Windows, or blossom runs there and it's built for the system blossom runs on.
|
||||
#! @param name The program's name.
|
||||
#! @param platform What it's built for, or empty.
|
||||
#! @returns The file's name.
|
||||
pub def output_name(name: strview, platform: strview): string {
|
||||
if platform == "windows-x86_64" {
|
||||
return $"{name}.exe";
|
||||
}
|
||||
if platform.length == 0 {
|
||||
return management::program_file(name);
|
||||
}
|
||||
return string::from(name, nothing {});
|
||||
}
|
||||
|
||||
#! A project's output folder, relative to its own: `.blossom/` and its key.
|
||||
#! @param holder The project.
|
||||
#! @returns The folder, written with `/`.
|
||||
pub def output_text(holder: &models::project): string {
|
||||
return $".blossom/{holder.key.span}";
|
||||
}
|
||||
|
||||
#! Says why the command can't go on, and stops it with a status.
|
||||
#! @param status 1 where something failed, and 2 where a blossomfile or the
|
||||
#! command is wrong.
|
||||
#! @param message What's wrong.
|
||||
pub def fatal(status: i32, message: strview): void {
|
||||
_ = system::fd::stderr.put($"blossom: {message}\n".span);
|
||||
system::exit(status);
|
||||
}
|
||||
Reference in new issue
Block a user