Initial Commit
This commit is contained in:
24 files changed
+4796
No files matched your search
@@ -0,0 +1,401 @@
|
||||
#! @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;
|
||||
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.view().find(only) is some {
|
||||
chosen.append(k);
|
||||
}
|
||||
}
|
||||
if chosen.length == 0 {
|
||||
fatal(2, only.length == 0 ? $"{root.identity.view()} has no tests." : $"No test of {root.identity.view()} 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.view()).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.view()} has more than one program: name the one to run.");
|
||||
}
|
||||
chosen = t;
|
||||
} else if root.targets[t].name.view() == name {
|
||||
chosen = t;
|
||||
}
|
||||
}
|
||||
if chosen == root.targets.length {
|
||||
fatal(2, name.length == 0 ? $"{root.identity.view()} has no program to run." : $"`{name}` is no program of {root.identity.view()}.");
|
||||
}
|
||||
|
||||
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.view(), &this.space.projects[0].folder) else (why) {
|
||||
fatal(1, $"`{this.sakura.view()}` 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.view());
|
||||
}
|
||||
|
||||
var words := this.words("check");
|
||||
this.add_libraries(each, &mut words);
|
||||
this.add_threads(&mut words);
|
||||
val base := $"{output_text(holder).view()}/check/{each.name.view()}";
|
||||
if fresh {
|
||||
words.append(string::from("--deps", nothing {}));
|
||||
words.append($"{base.view()}.deps");
|
||||
}
|
||||
if files is value(&listed) {
|
||||
for &file in listed {
|
||||
words.append(string::copy_of(file));
|
||||
}
|
||||
}
|
||||
return this.made(t, words.view(), fresh ? base.view() : strview::empty(), "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.view(), strview::empty(), "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.view());
|
||||
return false;
|
||||
}
|
||||
|
||||
val system := this.platform_of(each);
|
||||
val output := $"{output_text(holder).view()}/bin/{output_name(each.name.view(), system).view()}";
|
||||
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.view()}.deps");
|
||||
words.append(string::from("-o", nothing {}));
|
||||
words.append(string::copy_of(&output));
|
||||
this.add_sources(each, &mut words);
|
||||
return this.made(index, words.view(), output.view(), test ? "" : "Built", output.view());
|
||||
}
|
||||
|
||||
# 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".view());
|
||||
val deps := holder.folder.join($"{output}.deps".view());
|
||||
if is_fresh(&out, &kept, now.view(), &deps, holder) {
|
||||
if verb.length != 0 {
|
||||
this.said("Up to date", name.view());
|
||||
}
|
||||
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.view()}` can't be run: {why}.");
|
||||
return false;
|
||||
};
|
||||
if !status.succeeded {
|
||||
this.fail(name.view());
|
||||
return false;
|
||||
}
|
||||
if output.length != 0 {
|
||||
val kept := holder.folder.join($"{output}.command".view());
|
||||
if verb == "Checked" {
|
||||
_ = written(&holder.folder.join(output), "");
|
||||
}
|
||||
_ = written(&kept, now.view());
|
||||
}
|
||||
if verb.length != 0 {
|
||||
this.said(verb, name.view(), 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).view()}/bin/{output_name(each.name.view(), this.platform_of(each)).view()}";
|
||||
words.append(string::from(holder.folder.join(output.view()).view(), nothing {}));
|
||||
for &arg in each.args {
|
||||
words.append(string::copy_of(arg));
|
||||
}
|
||||
} else {
|
||||
if this.reaches_broken(each, broken) {
|
||||
this.skip(each.name.view());
|
||||
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.view(), &holder.folder);
|
||||
running.output(process::stdio::piped);
|
||||
var child := running.spawn() else (why) {
|
||||
fatal(1, $"`{words[0].view()}` 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.view().substr(last.length - 512, 512);
|
||||
last = string::from(keep, nothing {});
|
||||
}
|
||||
}
|
||||
val status := child.wait() else (why) {
|
||||
fatal(1, $"`{words[0].view()}` couldn't be waited for: {why}.");
|
||||
return false;
|
||||
};
|
||||
if !status.succeeded {
|
||||
this.fail(each.name.view());
|
||||
return false;
|
||||
}
|
||||
val counted := tally_of(last.view());
|
||||
this.said("Tested", each.name.view(), 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::empty(); };
|
||||
val first := line.substr(0, comma);
|
||||
return first.ends_with(" passed") ? first : strview::empty();
|
||||
}
|
||||
|
||||
# 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.view(), each.name.view(), &mut reached);
|
||||
if found is error(&why) {
|
||||
fatal(why.status, why.message.view());
|
||||
}
|
||||
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.view());
|
||||
}
|
||||
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.view(), each.name.view(), &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.view();
|
||||
}
|
||||
return each.system.view();
|
||||
}
|
||||
|
||||
#! 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.view()}";
|
||||
}
|
||||
|
||||
#! 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".view());
|
||||
system::exit(status);
|
||||
}
|
||||
Reference in new issue
Block a user