stage1 重构代码 重构nob构建方式

This commit is contained in:
zzy committed 2026-10-22 19:58:15 +08:00
1 parent 3714c2d142
commit 4c0317fcb5
14 files changed
+4004 -2160

No files matched your search

+526 -318
View File
@@ -8,6 +8,11 @@
#include "nob/nob.h"
#undef NOB_IMPLEMENTATION
#include "nob/nob_popen.h"
#define SCC_ARGPARSE_IMPLEMENTATION
#define SCC_OPTPARSER_IMPLEMENTATION
#include "nob/argparse.h"
#ifdef _WIN32
#include <consoleapi2.h>
#endif
@@ -29,6 +34,7 @@ static const char *obj_path(const char *src) {
memcpy(w, BUILD_FOLDER, prefix_len);
w += prefix_len;
for (const char *p = src; *p && *p != '.'; ++p) {
assert(w != NULL);
*w++ = (*p == '/' || *p == '\\') ? '_' : *p;
}
memcpy(w, ".o", 3);
@@ -227,10 +233,14 @@ bool run_target_dry(const char *name, Nob_Cmd *args, Nob_Cmd *result) {
continue;
}
const char *exe = build_target(runs[i].name);
if (!exe) {
nob_log(NOB_ERROR, "failed to build executable `%s`", name);
return false;
}
const char *interpreter = runs[i].interpreter;
if (interpreter) {
const char *inter = build_target(interpreter);
if (!exe) {
if (!inter) {
nob_log(NOB_ERROR, "failed to build interpreter `%s` for `%s`", interpreter, name);
return false;
}
@@ -255,214 +265,220 @@ bool run_target(const char *name, Nob_Cmd *args) {
return ok;
}
static void parse_expect(const char *name, Nob_Cmd_Result *expect) {
Nob_String_Builder sb = {0};
nob_read_entire_file(name, &sb);
Nob_String_View sv = nob_sb_to_sv(sb);
while (sv.count > 0) {
Nob_String_View line = nob_sv_chop_by_delim(&sv, '\n');
line = nob_sv_trim_left(line); // 去掉前导空格
// ---- 测试 ----
// Expect 协议:扫整个文件的注释行找 `// Expect retcode = N`,取第一条命中(注解可以在任意位置)。
// `// Expect stdout = ...` 暂不比较,遇到跳过。
// 返回 false 只表示文件读不出来或 retcode 写坏了;没写注解则 has_retcode 为假,由调用方按 0 处理。
typedef struct {
bool has_retcode;
int retcode;
} test_expect_t;
if (!nob_sv_starts_with(line, nob_sv_from_cstr("//"))) {
break;
}
Nob_String_View rest = nob_sv_from_parts(line.data + 2, line.count - 2);
rest = nob_sv_trim_left(rest);
if (rest.count == 0)
continue;
if (!nob_sv_starts_with(rest, nob_sv_from_cstr("Expect"))) {
break;
}
rest = nob_sv_from_parts(rest.data + 6, rest.count - 6);
rest = nob_sv_trim_left(rest);
// 按 '=' 分割
Nob_String_View key = nob_sv_chop_by_delim(&rest, '=');
key = nob_sv_trim(key);
Nob_String_View value = nob_sv_trim(rest);
if (nob_sv_eq(key, nob_sv_from_cstr("retcode"))) {
int val = 0;
for (size_t i = 0; i < value.count && isdigit(value.data[i]); ++i) {
val = val * 10 + (value.data[i] - '0');
}
// printf("expected code %d\n", val);
expect->exit_code = val;
} else if (nob_sv_eq(key, nob_sv_from_cstr("stdout"))) {
}
}
}
static bool test_once(const char *fname, void *user_data) {
(void)user_data;
const char *compiler_path = "splc0";
const char *cli_path = "spl_cli";
const char *target_sir = BUILD_FOLDER "test.sir";
Nob_Cmd cmd = {0};
if (!build_target(compiler_path)) {
return 1;
}
cmd.count = 0;
nob_cmd_append(&cmd, exe_path(compiler_path));
nob_cmd_append(&cmd, fname, target_sir);
if (!nob_cmd_run(&cmd)) {
static bool test_parse_int(Nob_String_View value, int *out) {
if (value.count == 0)
return false;
size_t i = 0;
bool negative = false;
if (value.data[0] == '-' || value.data[0] == '+') {
negative = value.data[0] == '-';
i = 1;
}
if (!build_target(cli_path)) {
return 1;
}
cmd.count = 0;
nob_cmd_append(&cmd, exe_path(cli_path));
nob_cmd_append(&cmd, target_sir);
// nob_cmd_run(&cmd);
Nob_Cmd_Result result = {0};
Nob_String_Builder cmd_str = {0};
nob_cmd_render(cmd, &cmd_str);
Nob_String_View sv = nob_sb_to_sv(cmd_str);
nob_cmd_capture_popen(nob_temp_sv_to_cstr(sv), &result);
// Expect retcode = number
Nob_Cmd_Result expect = {0};
parse_expect(fname, &expect);
if (expect.exit_code != result.exit_code) {
nob_log(NOB_ERROR, "Expect code `%d`, Result code `%d`", expect.exit_code,
result.exit_code);
if (i >= value.count)
return false;
}
nob_cmd_result_free(result);
int v = 0;
for (; i < value.count; i += 1) {
if (!isdigit((unsigned char)value.data[i]))
return false;
v = v * 10 + (value.data[i] - '0');
}
*out = negative ? -v : v;
return true;
}
static bool test_once_with_dump(const char *fname, void *user_data) {
const char *target_name = "splc0";
if (!prepare_build())
return 1;
if (!build_target(target_name))
return 1;
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, exe_path(target_name));
nob_cmd_append(&cmd, "--dump", (char *)user_data, fname, BUILD_FOLDER "t.sir");
return nob_cmd_run(&cmd);
static bool test_parse_expect(const char *fname, test_expect_t *expect) {
Nob_String_Builder sb = {0};
if (!nob_read_entire_file(fname, &sb)) {
nob_sb_free(sb);
return false;
}
Nob_String_View sv = nob_sb_to_sv(sb);
bool malformed = false;
while (sv.count > 0) {
Nob_String_View line = nob_sv_trim(nob_sv_chop_by_delim(&sv, '\n'));
if (!nob_sv_starts_with(line, nob_sv_from_cstr("//")))
continue;
line = nob_sv_trim_left(nob_sv_from_parts(line.data + 2, line.count - 2));
if (!nob_sv_starts_with(line, nob_sv_from_cstr("Expect")))
continue;
line = nob_sv_trim_left(nob_sv_from_parts(line.data + 6, line.count - 6));
Nob_String_View key = nob_sv_trim(nob_sv_chop_by_delim(&line, '='));
Nob_String_View value = nob_sv_trim(line);
if (!nob_sv_eq(key, nob_sv_from_cstr("retcode")) || expect->has_retcode)
continue;
int parsed = 0;
if (!test_parse_int(value, &parsed)) {
malformed = true;
break;
}
expect->retcode = parsed;
expect->has_retcode = true;
}
nob_sb_free(sb);
return !malformed;
}
// 由源路径派生构建产物路径:build/<basename>.sir
static const char *artifact_sir(const char *src) {
const char *base = src;
for (const char *p = src; *p; ++p)
if (*p == '/' || *p == '\\')
base = p + 1;
const char *dot = strrchr(base, '.');
size_t len = dot ? (size_t)(dot - base) : strlen(base);
return nob_temp_sprintf(BUILD_FOLDER "%.*s.sir", (int)len, base);
// suite 只管"跑哪个目录的文件",用哪个编译器完全由命令行 target 决定。
typedef struct {
const char *name;
const char *dir;
} test_suite_t;
static const test_suite_t TEST_SUITES[] = {
{"stage1", "./tests/stage1/"},
{"stage2", "./tests/stage2/"},
{"stage3", "./tests/stage3/"},
};
static const char *TEST_MODE_NAMES[] = {"lex", "ast", "sema", "ir", "vm"};
// 复用 runs[] 与 run_target_dry:构建一次,并拿到"怎么把这个 target 跑起来"的 argv 前缀。
// test/diff 每条用例复制这份前缀再补自己的参数,运行语义因此和 `nob run` 完全一致。
static bool build_run_prefix(const char *name, Nob_Cmd *prefix) {
Nob_Cmd no_args = {0};
return run_target_dry(name, &no_args, prefix);
}
// `--dump <mode> <file>` 之后统一补一个 <out> 占位:splc0 强制要它,splc1 标成可选,splc2 忽略多余参数。
// 参差的是那几个程序自己的 CLI,不是 exe/.sir 的区别。
static void append_dump_args(Nob_Cmd *cmd, const char *mode, const char *fname) {
nob_cmd_append(cmd, "--dump", mode, fname, BUILD_FOLDER "dump.sir");
}
static bool run_capture(Nob_Cmd *cmd, Nob_Cmd_Result *result) {
Nob_String_Builder rendered = {0};
nob_cmd_render(*cmd, &rendered);
Nob_String_View sv = nob_sb_to_sv(rendered);
bool ok = nob_cmd_capture_popen(nob_temp_sv_to_cstr(sv), result);
bool ok = nob_cmd_capture_popen(nob_temp_sv_to_cstr(nob_sb_to_sv(rendered)), result);
nob_sb_free(rendered);
return ok;
}
// diff 测试:对同一文件跑 `splc0 --dump <mode>` 与 `spl_cli splc1.sir --dump <mode>`,
// 逐字节比较 stdout。
static bool test_once_diff(const char *fname, void *user_data) {
const char *mode = (const char *)user_data;
if (!mode) {
nob_log(NOB_ERROR, "diff test requires a mode: lex|ast|sema|ir|vm");
return false;
}
if (!build_target("splc0"))
return false;
if (!build_target("spl_cli"))
return false;
if (!build_target("splc1"))
return false;
Nob_Cmd c_cmd = {0};
nob_cmd_append(&c_cmd, exe_path("splc0"), "--dump", (char *)mode, (char *)fname,
BUILD_FOLDER "diff_c.sir");
Nob_Cmd_Result c_res = {0};
if (!run_capture(&c_cmd, &c_res)) {
nob_cmd_result_free(c_res);
return false;
}
Nob_Cmd s_cmd = {0};
nob_cmd_append(&s_cmd, exe_path("spl_cli"), BUILD_FOLDER "splc1.sir", "--dump", (char *)mode,
(char *)fname);
Nob_Cmd_Result s_res = {0};
if (!run_capture(&s_cmd, &s_res)) {
nob_cmd_result_free(c_res);
nob_cmd_result_free(s_res);
return false;
}
Nob_String_View c_out = nob_sb_to_sv(c_res.out);
Nob_String_View s_out = nob_sb_to_sv(s_res.out);
bool same = nob_sv_eq(c_out, s_out);
if (!same) {
nob_log(NOB_ERROR, "diff mismatch: %s (c=%zu bytes, spl=%zu bytes)", fname, c_out.count,
s_out.count);
// 打印首个不相同行(按行比较)。
Nob_String_View ca = c_out;
Nob_String_View sa = s_out;
size_t lineno = 1;
while (ca.count > 0 || sa.count > 0) {
Nob_String_View cl = nob_sv_chop_by_delim(&ca, '\n');
Nob_String_View sl = nob_sv_chop_by_delim(&sa, '\n');
if (!nob_sv_eq(cl, sl)) {
nob_log(NOB_ERROR, " line %zu:", lineno);
nob_log(NOB_ERROR, " c : %.*s", (int)cl.count, cl.data);
nob_log(NOB_ERROR, " spl: %.*s", (int)sl.count, sl.data);
break;
}
lineno++;
}
nob_log(NOB_ERROR, "--- c stderr/stdout merged above; exit=%d vs spl exit=%d ---",
c_res.exit_code, s_res.exit_code);
}
nob_cmd_result_free(c_res);
nob_cmd_result_free(s_res);
return same;
}
static bool is_stage_name(const char *s) {
return strcmp(s, "stage1") == 0 || strcmp(s, "stage2") == 0 || strcmp(s, "diff") == 0;
}
static bool is_dump_mode(const char *s) {
return strcmp(s, "lex") == 0 || strcmp(s, "ast") == 0 || strcmp(s, "sema") == 0 ||
strcmp(s, "ir") == 0 || strcmp(s, "vm") == 0;
}
static const char *stage_dir_of(const char *stage) {
if (strcmp(stage, "stage2") == 0)
return "./stage2/";
return "./tests/stage1/";
}
typedef bool (*test_fn_t)(const char *fname, void *user_data);
typedef struct {
Nob_Files *files;
test_fn_t test_once;
void *user_data;
} test_walk_ctx_t;
Nob_Cmd compiler; // 选定编译器的调用前缀(已构建)
Nob_Cmd vm; // 跑编译产物的 spl_cli 前缀(--mode 时不用)
const char *out_sir;
const char *mode; // 非 NULL:只做 `--dump <mode>` 冒烟
const scc_argparse_list_t *filters; // 按文件名子串过滤,空则全跑
int passed;
int failed;
int no_expect; // 没写 Expect 注解、按 0 期望的文件数
} test_ctx_t;
static bool test_walk_entry(Nob_Walk_Entry entry) {
static void test_compile_and_run(const char *fname, test_ctx_t *ctx) {
Nob_Cmd compile = {0};
nob_cmd_extend(&compile, &ctx->compiler);
nob_cmd_append(&compile, fname, ctx->out_sir);
bool compiled = nob_cmd_run(&compile);
nob_cmd_free(compile);
if (!compiled) {
nob_log(NOB_ERROR, "%s: compile failed", fname);
ctx->failed += 1;
return;
}
Nob_Cmd run = {0};
nob_cmd_extend(&run, &ctx->vm);
nob_cmd_append(&run, ctx->out_sir);
Nob_Cmd_Result result = {0};
if (!run_capture(&run, &result)) {
nob_log(NOB_ERROR, "%s: failed to run", fname);
ctx->failed += 1;
nob_cmd_free(run);
return;
}
nob_cmd_free(run);
test_expect_t expect = {0};
if (!test_parse_expect(fname, &expect)) {
nob_log(NOB_ERROR, "%s: broken `// Expect retcode = N`", fname);
ctx->failed += 1;
} else {
if (!expect.has_retcode) {
expect.retcode = 0; // 没写注解的库测试按"跑完退出 0"处理
ctx->no_expect += 1;
}
if (expect.retcode != result.exit_code) {
nob_log(NOB_ERROR, "%s: expect retcode %d, got %d", fname, expect.retcode,
result.exit_code);
ctx->failed += 1;
} else {
ctx->passed += 1;
}
}
nob_cmd_result_free(result);
}
// --mode:只看选定的 target 能不能把这个文件 dump 出来,不比内容
static void test_dump(const char *fname, test_ctx_t *ctx) {
Nob_Cmd cmd = {0};
nob_cmd_extend(&cmd, &ctx->compiler);
append_dump_args(&cmd, ctx->mode, fname);
if (nob_cmd_run(&cmd)) {
ctx->passed += 1;
} else {
nob_log(NOB_ERROR, "%s: `--dump %s` failed", fname, ctx->mode);
ctx->failed += 1;
}
nob_cmd_free(cmd);
}
// 只负责跑:编译器由 cli_test 在建 suite 时构建一次。
static void test_run_file(const char *fname, void *user_data) {
test_ctx_t *ctx = (test_ctx_t *)user_data;
nob_log(NOB_INFO, "--- test %s ---", fname);
if (ctx->mode)
test_dump(fname, ctx);
else
test_compile_and_run(fname, ctx);
}
// ---- 目录遍历(test / diff 共用)----
typedef void (*walk_file_fn)(const char *fname, void *user_data);
typedef struct {
const scc_argparse_list_t *filters;
walk_file_fn fn;
void *user_data;
} walk_ctx_t;
static const char *path_basename(const char *path) {
const char *base = path;
for (const char *p = path; *p; p += 1) {
if (*p == '/' || *p == '\\')
base = p + 1;
}
return base;
}
static bool walk_match_filter(const char *path, const scc_argparse_list_t *filters) {
if (filters->count == 0)
return true;
const char *base = path_basename(path); // 只比文件名,不碰目录名
for (usize i = 0; i < filters->count; i += 1) {
if (strstr(base, filters->items[i]))
return true;
}
return false;
}
// 单个用例失败只计数,遍历继续。
static bool walk_entry(Nob_Walk_Entry entry) {
if (entry.type != NOB_FILE_REGULAR)
return true;
@@ -470,39 +486,95 @@ static bool test_walk_entry(Nob_Walk_Entry entry) {
if (!dot || strcmp(dot, ".spl") != 0)
return true;
test_walk_ctx_t *ctx = (test_walk_ctx_t *)entry.data;
bool run_all = (ctx->files->count == 0);
bool matched = run_all;
for (int i = 0; !matched && i < ctx->files->count; i += 1) {
if (strstr(entry.path, ctx->files->items[i]))
matched = true;
}
if (!matched)
walk_ctx_t *ctx = (walk_ctx_t *)entry.data;
if (!walk_match_filter(entry.path, ctx->filters))
return true;
printf("--- test %s ---\n", entry.path);
if (ctx->test_once(entry.path, ctx->user_data) == false)
return false;
ctx->fn(entry.path, ctx->user_data);
return true;
}
static bool run_test(const char *stage, Nob_Files *files, test_fn_t test_once, void *user_data) {
const char *test_folder = stage_dir_of(stage);
if (!prepare_build())
return false;
if (!nob_file_exists(test_folder)) {
nob_log(NOB_INFO, "%s does not exist, skip", test_folder);
return true;
}
test_walk_ctx_t ctx = {
.files = files,
.test_once = test_once,
static bool walk_spl_files(const char *dir, const scc_argparse_list_t *filters, walk_file_fn fn,
void *user_data) {
walk_ctx_t ctx = {
.filters = filters,
.fn = fn,
.user_data = user_data,
};
return nob_walk_dir(test_folder, test_walk_entry, .data = &ctx);
return nob_walk_dir(dir, walk_entry, .data = &ctx);
}
// ---- diff:两条流水线对撞 ----
// 对每份匹配的文件跑同一个 `--dump <mode>`,逐字节比 stdout。
typedef struct {
const char *expect_name; // 仅用于报错文案
const char *got_name;
Nob_Cmd expect; // 两侧的调用前缀(已构建)
Nob_Cmd got;
const char *mode;
const scc_argparse_list_t *filters;
int passed;
int failed;
} diff_ctx_t;
static void report_first_diff(const char *name, Nob_String_View expect, Nob_String_View got) {
nob_log(NOB_ERROR, "diff mismatch: %s (expect %zu bytes, got %zu bytes)", name, expect.count,
got.count);
Nob_String_View el = expect;
Nob_String_View gl = got;
size_t lineno = 1;
while (el.count > 0 || gl.count > 0) {
Nob_String_View e = nob_sv_chop_by_delim(&el, '\n');
Nob_String_View g = nob_sv_chop_by_delim(&gl, '\n');
if (!nob_sv_eq(e, g)) {
nob_log(NOB_ERROR, " line %zu:", lineno);
nob_log(NOB_ERROR, " expect: %.*s", (int)e.count, e.data);
nob_log(NOB_ERROR, " got : %.*s", (int)g.count, g.data);
break;
}
lineno += 1;
}
}
static void diff_run_file(const char *fname, void *user_data) {
diff_ctx_t *ctx = (diff_ctx_t *)user_data;
Nob_Cmd expect_cmd = {0};
nob_cmd_extend(&expect_cmd, &ctx->expect);
append_dump_args(&expect_cmd, ctx->mode, fname);
Nob_Cmd_Result expect = {0};
if (!run_capture(&expect_cmd, &expect)) {
nob_log(NOB_ERROR, "%s: failed to run `%s`", fname, ctx->expect_name);
ctx->failed += 1;
nob_cmd_free(expect_cmd);
return;
}
nob_cmd_free(expect_cmd);
Nob_Cmd got_cmd = {0};
nob_cmd_extend(&got_cmd, &ctx->got);
append_dump_args(&got_cmd, ctx->mode, fname);
Nob_Cmd_Result got = {0};
if (!run_capture(&got_cmd, &got)) {
nob_log(NOB_ERROR, "%s: failed to run `%s`", fname, ctx->got_name);
ctx->failed += 1;
nob_cmd_free(got_cmd);
nob_cmd_result_free(expect);
return;
}
nob_cmd_free(got_cmd);
if (nob_sv_eq(nob_sb_to_sv(expect.out), nob_sb_to_sv(got.out))) {
ctx->passed += 1;
} else {
report_first_diff(fname, nob_sb_to_sv(expect.out), nob_sb_to_sv(got.out));
nob_log(NOB_ERROR, " `%s` exit=%d, `%s` exit=%d", ctx->expect_name, expect.exit_code,
ctx->got_name, got.exit_code);
ctx->failed += 1;
}
nob_cmd_result_free(expect);
nob_cmd_result_free(got);
}
static bool delete_walk_entry(Nob_Walk_Entry entry) { return nob_delete_file(entry.path); }
@@ -511,18 +583,144 @@ static bool delete_directory_recursively(const char *dir_path) {
return nob_walk_dir(dir_path, delete_walk_entry, .post_order = true);
}
static void print_help(const char *program_name) {
nob_log(NOB_INFO, "SPL C-side bootstrap build system (nob)");
nob_log(NOB_INFO, "%s", " ");
nob_log(NOB_INFO, "Usage: %s <command> [args...]", program_name);
nob_log(NOB_INFO, "%s", " ");
nob_log(NOB_INFO, "Commands:");
nob_log(NOB_INFO, " build [targets...] build all targets or the given ones");
nob_log(NOB_INFO, " run <target> [args] build and run a target");
nob_log(NOB_INFO, " test <kind> build and run tests");
nob_log(NOB_INFO, " clean remove build artifacts");
nob_log(NOB_INFO, " list list all targets");
nob_log(NOB_INFO, " help/-h/--help print this help message");
typedef struct {
cbool build, run, test, diff, clean, list; // 命中的子命令
scc_argparse_list_t build_targets;
const char *run_target;
scc_argparse_list_t run_args;
int test_suite; // TEST_SUITES 下标
const char *test_target; // 用哪个编译器编,NULL 用 suite 默认
int test_mode; // TEST_MODE_NAMES 下标,-1 表示不做 dump
scc_argparse_list_t test_filters;
const char *diff_expect;
const char *diff_got;
int diff_suite; // TEST_SUITES 下标
int diff_mode; // TEST_MODE_NAMES 下标,-1 表示没给
scc_argparse_list_t diff_filters;
} nob_cli_t;
static int cli_build(nob_cli_t *cli) {
if (!prepare_build())
return 1;
int rc = 0;
if (cli->build_targets.count == 0) {
rc = build_target(NULL) ? 0 : 1;
} else {
for (usize i = 0; i < cli->build_targets.count && rc == 0; i += 1) {
if (!build_target(cli->build_targets.items[i]))
rc = 1;
}
}
return rc;
}
static int cli_run(nob_cli_t *cli) {
Nob_Cmd runcmd = {0};
for (usize i = 0; i < cli->run_args.count; i += 1)
nob_cmd_append(&runcmd, cli->run_args.items[i]);
return run_target(cli->run_target, &runcmd) ? 0 : 1;
}
static int cli_test(nob_cli_t *cli) {
const test_suite_t *suite = &TEST_SUITES[cli->test_suite];
const char *target = cli->test_target;
const char *mode = (cli->test_mode >= 0) ? TEST_MODE_NAMES[cli->test_mode] : NULL;
if (!prepare_build())
return 1;
test_ctx_t ctx = {
.out_sir = BUILD_FOLDER "t.sir",
.mode = mode,
.filters = &cli->test_filters,
};
// build_run_prefix 会连带构建解释器;每个 target 只在这里构建一次
// (BUILD_SIR 没有新鲜度检查,放进逐用例循环会真重编)。
if (!build_run_prefix(target, &ctx.compiler))
return 1;
if (mode == NULL && !build_run_prefix("spl_cli", &ctx.vm))
return 1; // 编译产物由 spl_cli 跑
if (!nob_file_exists(suite->dir)) {
nob_log(NOB_INFO, "%s does not exist, skip", suite->dir);
return 0;
}
bool walked = walk_spl_files(suite->dir, &cli->test_filters, test_run_file, &ctx);
if (ctx.no_expect > 0)
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed (%d without `// Expect`)", suite->name,
target, ctx.passed, ctx.failed, ctx.no_expect);
else
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed", suite->name, target, ctx.passed,
ctx.failed);
nob_cmd_free(ctx.compiler);
nob_cmd_free(ctx.vm);
return (walked && ctx.failed == 0) ? 0 : 1;
}
// diff:expect 目标(参考实现)与本目标各自 `--dump <mode>` 同一批文件,逐字节比 stdout。
static int cli_diff(nob_cli_t *cli) {
const test_suite_t *suite = &TEST_SUITES[cli->diff_suite];
if (!prepare_build())
return 1;
diff_ctx_t ctx = {
.expect_name = cli->diff_expect,
.got_name = cli->diff_got,
.mode = TEST_MODE_NAMES[cli->diff_mode],
.filters = &cli->diff_filters,
};
// 同样两个 target 各只构建一次
if (!build_run_prefix(cli->diff_expect, &ctx.expect))
return 1;
if (!build_run_prefix(cli->diff_got, &ctx.got))
return 1;
if (!nob_file_exists(suite->dir)) {
nob_log(NOB_INFO, "%s does not exist, skip", suite->dir);
return 0;
}
nob_log(NOB_INFO, "--- diff `%s` vs `%s` (--dump %s) ---", ctx.expect_name, ctx.got_name,
ctx.mode);
bool walked = walk_spl_files(suite->dir, &cli->diff_filters, diff_run_file, &ctx);
nob_log(NOB_INFO, "diff: %d matched, %d mismatched", ctx.passed, ctx.failed);
nob_cmd_free(ctx.expect);
nob_cmd_free(ctx.got);
return (walked && ctx.failed == 0) ? 0 : 1;
}
static int cli_clean(void) {
if (nob_file_exists(BUILD_FOLDER) && !delete_directory_recursively(BUILD_FOLDER))
return 1;
nob_log(NOB_INFO, "cleaned %s", BUILD_FOLDER);
return 0;
}
static int cli_list(void) {
for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) {
nob_log(NOB_INFO, "%s:", targets[i].name);
}
return 0;
}
static int cli_dispatch(nob_cli_t *cli) {
if (cli->clean)
return cli_clean();
if (cli->list)
return cli_list();
if (cli->run)
return cli_run(cli);
if (cli->test)
return cli_test(cli);
if (cli->diff)
return cli_diff(cli);
return cli_build(cli); // build,或没给命令:默认构建全部
}
int main(int argc, char **argv) {
@@ -533,106 +731,116 @@ int main(int argc, char **argv) {
NOB_GO_REBUILD_URSELF_PLUS(argc, argv, "nob/nob.h", "nob/nob_popen.h", "nob/nob_data.h");
set_log_handler(nob_cancer_log_handler);
const char *program_name = nob_shift(argv, argc);
const char *command_name = "build";
if (argc > 0) {
command_name = nob_shift(argv, argc);
}
nob_cli_t cli = {0};
cli.test_mode = -1;
cli.diff_mode = -1;
if (strcmp(command_name, "build") == 0) {
if (!prepare_build())
return 1;
if (argc > 0) {
while (argc > 0) {
const char *target_name = nob_shift(argv, argc);
if (!build_target(target_name))
return 1;
}
} else {
if (!build_target(NULL))
return 1;
}
return 0;
}
scc_argparse_t parser = {0};
scc_argparse_init(&parser, argv[0], "SPL C-side bootstrap build system (nob)");
scc_argparse_cmd_t *root = scc_argparse_get_root(&parser);
if (strcmp(command_name, "run") == 0) {
if (argc <= 0) {
nob_log(NOB_ERROR, "usage: %s run <target> [args...]", program_name);
return 1;
}
const char *target_name = nob_shift(argv, argc);
Nob_Cmd runcmd = {0};
while (argc > 0) {
nob_cmd_append(&runcmd, nob_shift(argv, argc));
}
return !run_target(target_name, &runcmd);
}
scc_argparse_cmd_t build_cmd;
scc_argparse_cmd_init(&build_cmd, "build", "build all targets or the given ones");
scc_argparse_cmd_set_triggered(&build_cmd, &cli.build);
scc_argparse_arg_t targets_arg;
scc_argparse_arg_init(&targets_arg, "targets", "target names, default all");
scc_argparse_spec_setup_list(&targets_arg.spec, &cli.build_targets);
scc_argparse_cmd_add_arg(&build_cmd, &targets_arg);
scc_argparse_cmd_add_subcmd(root, &build_cmd);
if (strcmp(command_name, "test") == 0) {
// 1. stage(可选,默认 stage1)
const char *stage = "stage1";
if (argc > 0 && is_stage_name(argv[0])) {
stage = nob_shift(argv, argc);
}
scc_argparse_cmd_t run_cmd;
scc_argparse_cmd_init(&run_cmd, "run", "build and run a target");
scc_argparse_cmd_set_triggered(&run_cmd, &cli.run);
scc_argparse_arg_t run_target_arg;
scc_argparse_arg_init(&run_target_arg, "target", "target name");
scc_argparse_spec_setup_string(&run_target_arg.spec, &cli.run_target);
scc_argparse_spec_set_required(&run_target_arg.spec, true);
scc_argparse_arg_t run_args_arg;
scc_argparse_arg_init(&run_args_arg, "args", "arguments for the target");
scc_argparse_spec_setup_list(&run_args_arg.spec, &cli.run_args);
scc_argparse_cmd_add_arg(&run_cmd, &run_target_arg);
scc_argparse_cmd_add_arg(&run_cmd, &run_args_arg);
scc_argparse_cmd_add_subcmd(root, &run_cmd);
// 2. mode(可选)
test_fn_t tfn = test_once;
const char *mode_name = NULL;
if (argc > 0) {
if (is_dump_mode(argv[0])) {
tfn = test_once_with_dump;
mode_name = nob_shift(argv, argc);
} else if (strcmp(argv[0], "run") == 0) {
nob_shift(argv, argc);
// HACK:只对 stage1 有意义,跑上次生成的 build/test.sir
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, BUILD_FOLDER "spl_cli", "-d");
nob_cmd_append(&cmd, BUILD_FOLDER "test.sir");
return nob_cmd_run(&cmd) ? 0 : 1;
}
}
scc_argparse_cmd_t test_cmd;
scc_argparse_cmd_init(&test_cmd, "test", "build and run tests");
scc_argparse_cmd_set_triggered(&test_cmd, &cli.test);
const char *suite_names[NOB_ARRAY_LEN(TEST_SUITES)];
for (size_t i = 0; i < NOB_ARRAY_LEN(TEST_SUITES); i += 1)
suite_names[i] = TEST_SUITES[i].name;
scc_argparse_arg_t test_target_arg;
scc_argparse_arg_init(&test_target_arg, "target", "compiler target, e.g. splc0|splc1|splc2");
scc_argparse_spec_setup_string(&test_target_arg.spec, &cli.test_target);
scc_argparse_spec_set_required(&test_target_arg.spec, true);
scc_argparse_cmd_add_arg(&test_cmd, &test_target_arg);
scc_argparse_opt_t suite_opt;
scc_argparse_opt_init(&suite_opt, 's', "suite", "test suite, default stage1");
scc_argparse_spec_setup_choices(&suite_opt.spec, suite_names, (int)NOB_ARRAY_LEN(TEST_SUITES),
&cli.test_suite);
scc_argparse_cmd_add_opt(&test_cmd, &suite_opt);
scc_argparse_opt_t mode_opt;
scc_argparse_opt_init(&mode_opt, 'm', "mode", "dump smoke test through the target");
scc_argparse_spec_setup_choices(&mode_opt.spec, TEST_MODE_NAMES,
(int)NOB_ARRAY_LEN(TEST_MODE_NAMES), &cli.test_mode);
scc_argparse_cmd_add_opt(&test_cmd, &mode_opt);
scc_argparse_opt_t filters_opt;
scc_argparse_opt_init(&filters_opt, 'f', "filter", "file name filter, repeatable");
scc_argparse_spec_setup_list(&filters_opt.spec, &cli.test_filters);
scc_argparse_cmd_add_opt(&test_cmd, &filters_opt);
scc_argparse_cmd_add_subcmd(root, &test_cmd);
// diff 需要显式 mode,且使用专门的双编译器比较函数。
if (strcmp(stage, "diff") == 0) {
if (!mode_name) {
nob_log(NOB_ERROR, "diff requires a mode: lex|ast|sema|ir|vm");
return 1;
}
tfn = test_once_diff;
}
scc_argparse_cmd_t diff_cmd;
scc_argparse_cmd_init(&diff_cmd, "diff", "compare two targets' `--dump` output");
scc_argparse_cmd_set_triggered(&diff_cmd, &cli.diff);
scc_argparse_arg_t expect_arg;
scc_argparse_arg_init(&expect_arg, "expect", "reference build target");
scc_argparse_spec_setup_string(&expect_arg.spec, &cli.diff_expect);
scc_argparse_spec_set_required(&expect_arg.spec, true);
scc_argparse_arg_t got_arg;
scc_argparse_arg_init(&got_arg, "got", "build target under test");
scc_argparse_spec_setup_string(&got_arg.spec, &cli.diff_got);
scc_argparse_spec_set_required(&got_arg.spec, true);
scc_argparse_cmd_add_arg(&diff_cmd, &expect_arg);
scc_argparse_cmd_add_arg(&diff_cmd, &got_arg);
scc_argparse_opt_t diff_mode_opt;
scc_argparse_opt_init(&diff_mode_opt, 'm', "mode", "dump mode");
scc_argparse_spec_setup_choices(&diff_mode_opt.spec, TEST_MODE_NAMES,
(int)NOB_ARRAY_LEN(TEST_MODE_NAMES), &cli.diff_mode);
scc_argparse_spec_set_required(&diff_mode_opt.spec, true);
scc_argparse_cmd_add_opt(&diff_cmd, &diff_mode_opt);
scc_argparse_opt_t diff_suite_opt;
scc_argparse_opt_init(&diff_suite_opt, 's', "suite", "test suite, default stage1");
scc_argparse_spec_setup_choices(&diff_suite_opt.spec, suite_names,
(int)NOB_ARRAY_LEN(TEST_SUITES), &cli.diff_suite);
scc_argparse_cmd_add_opt(&diff_cmd, &diff_suite_opt);
scc_argparse_opt_t diff_filter_opt;
scc_argparse_opt_init(&diff_filter_opt, 'f', "filter", "file name filter, repeatable");
scc_argparse_spec_setup_list(&diff_filter_opt.spec, &cli.diff_filters);
scc_argparse_cmd_add_opt(&diff_cmd, &diff_filter_opt);
scc_argparse_cmd_add_subcmd(root, &diff_cmd);
// 3. 剩下的都当 pattern
Nob_Files files = {0};
while (argc > 0) {
da_append(&files, nob_shift(argv, argc));
}
scc_argparse_cmd_t clean_cmd;
scc_argparse_cmd_init(&clean_cmd, "clean", "remove build artifacts");
scc_argparse_cmd_set_triggered(&clean_cmd, &cli.clean);
scc_argparse_cmd_add_subcmd(root, &clean_cmd);
return run_test(stage, &files, tfn, (void *)mode_name) ? 0 : 1;
}
scc_argparse_cmd_t list_cmd;
scc_argparse_cmd_init(&list_cmd, "list", "list all targets");
scc_argparse_cmd_set_triggered(&list_cmd, &cli.list);
scc_argparse_cmd_add_subcmd(root, &list_cmd);
if (strcmp(command_name, "clean") == 0) {
if (nob_file_exists(BUILD_FOLDER)) {
if (!delete_directory_recursively(BUILD_FOLDER))
return 1;
}
nob_log(NOB_INFO, "cleaned %s", BUILD_FOLDER);
return 0;
}
int rc = scc_argparse_parse(&parser, argc, (const char **)argv);
if (parser.out.count > 0)
fwrite(parser.out.items, 1, parser.out.count, stdout);
if (rc != SCC_ARGPARSE_ERR_NONE)
return rc == SCC_ARGPARSE_ERR_PNT_DEFAULT ? 0 : 1;
if (strcmp(command_name, "list") == 0) {
for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) {
nob_log(NOB_INFO, "%s:", targets[i].name);
}
return 0;
}
rc = cli_dispatch(&cli);
if (strcmp(command_name, "help") == 0 || strcmp(command_name, "-h") == 0 ||
strcmp(command_name, "--help") == 0) {
print_help(program_name);
return 0;
}
nob_log(NOB_ERROR, "unknown command `%s`", command_name);
print_help(program_name);
return 1;
scc_vec_free(cli.build_targets);
scc_vec_free(cli.run_args);
scc_vec_free(cli.test_filters);
scc_vec_free(cli.diff_filters);
scc_argparse_drop(&parser);
return rc;
}