Files
blossom/pipeline/steps.sakura
T
2026-11-14 15:31:15 -06:00

527 lines
19 KiB
Plaintext

#! @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 suites after, the project's
#! `tests`.
#! @param this The session.
#! @param tests Whether the suites are run, as `test` asks.
#! @param suite Text a suite's name holds to be run, as `--suite` gives it, or
#! empty for every suite.
#! @param test Text a test's name holds to be run, as `--test` gives it,
#! which each suite is given as `--only TEXT`, or empty for every
#! test.
pub def session::build(&mut this, tests: bool, suite: strview = "", test: strview = ""): void {
val root := &this.space.projects[0];
val whose := this.whose();
var chosen := vector::<u64>::new();
if tests {
for k in 0..root.tests.length {
continue if !this.space.takes(&root.tests[k]);
if suite.length == 0 || root.tests[k].name.span.find(suite) is some {
chosen.append(k);
}
}
if chosen.length == 0 {
fatal(2, suite.length == 0 ? $"{whose.span} has no tests." : $"No suite of {whose.span} is named with `{suite}`.");
}
}
var taken: u64 = 0;
for &each in root.targets {
taken += this.space.takes(&each) ? 1 : 0;
}
this.steps = taken + 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;
continue if !this.space.takes(&this.space.projects[0].targets[t]);
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;
continue if !this.space.takes(&this.space.projects[0].targets[t]);
_ = this.build_program(t, false, &broken);
}
var none: u64 = 0; # The suites that said they ran no test.
for &k in chosen {
if this.run_test(k, &broken, test) is some(&count) && count == 0 {
none++;
}
}
# A test named in no suite run is a mistake in the command, as a suite named
# by none is.
if test.length != 0 && !this.failed && none == chosen.length {
fatal(2, $"No test of {whose.span} is named with `{test}`.");
}
}
# Who a command works on, as its mistakes name it: the project, as
# `malunal/hello@1.0.0`, or its member, as `engine of malunal/hello@1.0.0`.
def session::whose(&this): string {
val identity := this.space.projects[0].identity;
return string::copy_of(&identity) if this.space.member.length == 0;
return $"{this.space.member.span} of {identity.span}";
}
#! Checks every target of the project, its libraries and programs, writing
#! nothing; of its member alone, where it was run in a member's folder.
#! @param this The session.
pub def session::check(&mut this): void {
val count := this.space.projects[0].targets.length;
var taken: u64 = 0;
for t in 0..count {
taken += this.space.takes(&this.space.projects[0].targets[t]) ? 1 : 0;
}
this.steps = taken;
for t in 0..count {
continue if !this.space.takes(&this.space.projects[0].targets[t]);
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];
val whose := this.whose();
var chosen := root.targets.length;
for t in 0..root.targets.length {
continue if root.targets[t].kind != models::target_kind::program;
continue if !this.space.takes(&root.targets[t]);
if name.length == 0 {
if chosen != root.targets.length {
fatal(2, $"{whose.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 ? $"{whose.span} has no program to run." : $"`{name}` is no program of {whose.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);
if each.shown && for_windows(system) {
add_shown(each, holder.semv.span, &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 suite, a test of the project's: on the machine, as `sakura run`, with
# `--checks` unless it says otherwise, or built, then run, given its `args`, then
# `--only TEXT` where `--test` names its tests, which, coming last, `std::testing`
# takes over any `--only` of its `args`. What it writes is passed on as it comes,
# and the count its last line gives, as `14 passed`, is said with it. It gives
# how many tests ran, of the counts its last line gives, where the suite ran and
# gave them.
def session::run_test(&mut this, k: u64, broken: &vector<u64>, test: strview): maybe<u64> {
val holder := &this.space.projects[0];
val each := &holder.tests[k];
var words := vector::<string>::new();
if each.runs == models::runner::native {
return nothing {} 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));
}
add_test(test, &mut words);
} else {
if this.reaches_broken(each, broken) {
this.skip(each.name.span);
return nothing {};
}
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 || test.length != 0 {
words.append(string::from("--", nothing {}));
for &arg in each.args {
words.append(string::copy_of(arg));
}
add_test(test, &mut words);
}
}
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 nothing {};
};
# 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 nothing {};
};
if !status.succeeded {
this.fail(each.name.span);
return nothing {};
}
val ran := ran_of(last.span);
if test.length != 0 && ran is some(&count) && count == 0 {
this.said("Tested", each.name.span, $"none named with `{test}`".span);
} else {
this.said("Tested", each.name.span, tally_of(last.span));
}
return ran;
}
# Adds `--only TEXT`, where `--test` gives the text.
def add_test(test: strview, words: &mut vector<string>): void {
return if test.length == 0;
words.append(string::from("--only", nothing {}));
words.append(string::from(test, nothing {}));
}
# A test's last line, without the line's end.
def last_line(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--;
}
return text.substr(start, end - start);
}
# 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 {
val line := last_line(text);
val comma := line.find(",") else { return strview::blank(); };
val first := line.substr(0, comma);
return first.ends_with(" passed") ? first : strview::blank();
}
# How many tests a suite ran, of the counts its last line gives, as `std::testing`
# writes them, `14 passed, 1 failed, 2 skipped`, or none where it gives none.
def ran_of(text: strview): maybe<u64> {
var total: u64 = 0;
var counts: u64 = 0;
var at: u64 = 0;
val line := last_line(text);
for word in ["passed", "failed", "skipped"] {
var number: u64 = 0;
var digits: u64 = 0;
while at < line.length && line[at] >= '0' && line[at] <= '9' {
number = number * 10 + (line[at] - '0') as u64;
digits++;
at++;
}
return nothing {} if digits == 0;
val rest := line.substr(at, line.length - at);
return nothing {} if !rest.starts_with($" {word}".span);
at += word.length + 1;
if counts < 2 {
return nothing {} if !line.substr(at, line.length - at).starts_with(", ");
at += 2;
}
total += number;
counts++;
}
return nothing {} if at != line.length;
return total;
}
# 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;
}
#! Whether a program is built for Windows: it says so, or it says nothing and
#! blossom runs there.
#! @param platform What it's built for, or empty.
#! @returns Whether it is.
pub def for_windows(platform: strview): bool {
return true if platform == "windows-x86_64";
when os == windows {
return platform.length == 0;
}
return false;
}
# Gives `sakura build` what a program for Windows is shown by, where it gives
# any of it: its version, as the project's, its title, each of its icons' PNG
# and icon files, which an SVG isn't, and whether it's windowed. A program
# giving none is built as ever. A program for Linux is shown by what `install`
# puts beside it.
def add_shown(each: &models::target, semv: strview, words: &mut vector<string>): void {
words.append(string::from("--semv", nothing {}));
words.append(string::from(semv, nothing {}));
if each.title.length != 0 {
words.append(string::from("--title", nothing {}));
words.append(string::copy_of(&each.title));
}
for &file in each.icons {
continue if models::ends_with_any_case(file.span, ".svg");
words.append(string::from("--icon", nothing {}));
words.append(string::copy_of(&file));
}
if each.windowed {
words.append(string::from("--windowed", nothing {}));
}
}
#! 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);
}