1246 lines
42 KiB
C
1246 lines
42 KiB
C
|
||
#define COMMON_CFLAGS "-Wall", "-Wextra", "-D_CRT_SECURE_NO_WARNINGS", "-D_CRT_NONSTDC_NO_DEPRECATE"
|
||
#define SPL_CFLAGS COMMON_CFLAGS, "-O0", "-g"
|
||
#define SPL_OPTIMIZATION_FLAG COMMON_CFLAGS, "-O2"
|
||
#define nob_cc_flags(cmd) nob_cmd_append(cmd, SPL_CFLAGS)
|
||
#undef UNICODE
|
||
#define NOB_IMPLEMENTATION
|
||
#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
|
||
#define BUILD_FOLDER "build/"
|
||
|
||
// Content of the .cflags stamp file; must match SPL_CFLAGS above.
|
||
#define CFLAGS_STR "-Wall -Wextra -O0 -g"
|
||
// Stamp file whose mtime is refreshed whenever the flags change so that all
|
||
// object files get rebuilt.
|
||
static const char *CFLAGS_STAMP = BUILD_FOLDER ".cflags";
|
||
|
||
#include "nob/nob_data.h"
|
||
|
||
static const char *obj_path(const char *src) {
|
||
// "stage0/spl_vm.c" -> "build/stage0_spl_vm.o"
|
||
size_t prefix_len = strlen(BUILD_FOLDER);
|
||
char *out = (char *)nob_temp_alloc(prefix_len + strlen(src) + 2 + 1);
|
||
char *w = out;
|
||
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);
|
||
return out;
|
||
}
|
||
|
||
// Windows 上 clang/gcc 会输出 build/foo.exe;这里统一带上后缀,
|
||
// 否则 nob_needs_rebuild 找不到输出会每次重新链接。
|
||
static const char *exe_path(const char *name) {
|
||
#ifdef _WIN32
|
||
return nob_temp_sprintf(BUILD_FOLDER "%s.exe", name);
|
||
#else
|
||
return nob_temp_sprintf(BUILD_FOLDER "%s", name);
|
||
#endif
|
||
}
|
||
|
||
static const char *sir_path(const char *name) {
|
||
return nob_temp_sprintf(BUILD_FOLDER "%s.sir", name);
|
||
}
|
||
|
||
static const char *c_path(const char *name) { return nob_temp_sprintf(BUILD_FOLDER "%s.c", name); }
|
||
|
||
static bool sync_cflags_stamp(void) {
|
||
Nob_String_View cflags = nob_sv_from_cstr(CFLAGS_STR);
|
||
Nob_String_Builder old = {0};
|
||
bool changed = true;
|
||
if (nob_file_exists(CFLAGS_STAMP) && nob_read_entire_file(CFLAGS_STAMP, &old)) {
|
||
changed = !nob_sv_eq(nob_sb_to_sv(old), cflags);
|
||
}
|
||
nob_sb_free(old);
|
||
|
||
if (changed) {
|
||
if (!nob_write_entire_file(CFLAGS_STAMP, cflags.data, cflags.count)) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
static bool prepare_build(void) {
|
||
if (!nob_mkdir_if_not_exists(BUILD_FOLDER))
|
||
return false;
|
||
if (!sync_cflags_stamp())
|
||
return false;
|
||
return true;
|
||
}
|
||
|
||
typedef struct {
|
||
const char **items;
|
||
size_t count;
|
||
size_t capacity;
|
||
} str_da_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 wildcard_match(const char *pat, const char *s) {
|
||
const char *star = NULL;
|
||
const char *resume = NULL;
|
||
while (*s) {
|
||
if (*pat == '*') {
|
||
star = pat++;
|
||
resume = s;
|
||
} else if (*pat == *s) {
|
||
pat++;
|
||
s++;
|
||
} else if (star) {
|
||
pat = star + 1;
|
||
s = ++resume;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
while (*pat == '*')
|
||
pat++;
|
||
return *pat == '\0';
|
||
}
|
||
|
||
// 依赖模式的展开:只支持两种写法
|
||
// dir/*.ext —— 只看 dir 的直接子文件
|
||
// dir/**/*.ext —— 递归 dir 的子目录
|
||
// 不含 '*' 的条目按字面路径原样返回(跨目录的显式依赖用这个)。
|
||
typedef struct {
|
||
size_t dir_len;
|
||
const char *name_pattern;
|
||
bool recursive;
|
||
str_da_t *out;
|
||
} glob_ctx_t;
|
||
|
||
static bool collect_glob_entry(Nob_Walk_Entry entry) {
|
||
if (entry.type != NOB_FILE_REGULAR)
|
||
return true;
|
||
|
||
glob_ctx_t *ctx = (glob_ctx_t *)entry.data;
|
||
const char *rel = entry.path + ctx->dir_len;
|
||
while (*rel == '/' || *rel == '\\')
|
||
rel++;
|
||
|
||
if (!ctx->recursive) {
|
||
for (const char *p = rel; *p; p += 1) {
|
||
if (*p == '/' || *p == '\\')
|
||
return true;
|
||
}
|
||
}
|
||
if (!wildcard_match(ctx->name_pattern, path_basename(rel)))
|
||
return true;
|
||
|
||
da_append(ctx->out, nob_temp_strdup(entry.path));
|
||
return true;
|
||
}
|
||
|
||
// 某个模式一条都没匹配到就报错:依赖悄悄变空比多编一次危险得多。
|
||
static bool expand_patterns(const char **patterns, size_t count, str_da_t *out) {
|
||
for (size_t i = 0; i < count; i += 1) {
|
||
const char *pat = patterns[i];
|
||
if (strchr(pat, '*') == NULL) {
|
||
da_append(out, pat);
|
||
continue;
|
||
}
|
||
|
||
const char *slash = NULL;
|
||
for (const char *p = pat; *p; p += 1) {
|
||
if (*p == '/' || *p == '\\')
|
||
slash = p;
|
||
}
|
||
if (slash == NULL || slash == pat) {
|
||
nob_log(NOB_ERROR, "pattern `%s` needs a directory part", pat);
|
||
return false;
|
||
}
|
||
|
||
size_t dir_len = (size_t)(slash - pat);
|
||
const char *name_pattern = slash + 1;
|
||
bool recursive = false;
|
||
if (dir_len >= 2 && pat[dir_len - 1] == '*' && pat[dir_len - 2] == '*') {
|
||
recursive = true;
|
||
dir_len -= 2;
|
||
while (dir_len > 0 && (pat[dir_len - 1] == '/' || pat[dir_len - 1] == '\\'))
|
||
dir_len -= 1;
|
||
}
|
||
if (dir_len == 0) {
|
||
nob_log(NOB_ERROR, "pattern `%s` needs a directory part", pat);
|
||
return false;
|
||
}
|
||
|
||
char *dir = nob_temp_sprintf("%.*s", (int)dir_len, pat);
|
||
glob_ctx_t ctx = {
|
||
.dir_len = dir_len,
|
||
.name_pattern = name_pattern,
|
||
.recursive = recursive,
|
||
.out = out,
|
||
};
|
||
size_t before = out->count;
|
||
if (!nob_walk_dir(dir, collect_glob_entry, .data = &ctx))
|
||
return false;
|
||
if (out->count == before) {
|
||
nob_log(NOB_ERROR, "pattern `%s` matched nothing", pat);
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool run_target(const char *name, Nob_Cmd *args);
|
||
const char *build_target(const char *name);
|
||
|
||
static const char *build_exe(build_t *target) {
|
||
const char *result = NULL;
|
||
Nob_Cmd cmd = {0};
|
||
str_da_t objs = {0};
|
||
str_da_t deps = {0};
|
||
str_da_t srcs = {0};
|
||
str_da_t headers = {0};
|
||
|
||
// srcs/headers 里可以写通配模式(见 expand_patterns),展开一次给后面所有源文件用。
|
||
if (!expand_patterns(target->exe.srcs, target->exe.srcs_count, &srcs))
|
||
goto defer;
|
||
if (!expand_patterns(target->exe.headers, target->exe.headers_count, &headers))
|
||
goto defer;
|
||
|
||
for (size_t i = 0; i < srcs.count; ++i) {
|
||
const char *src = srcs.items[i];
|
||
const char *obj = obj_path(src);
|
||
if (!nob_file_exists(src)) {
|
||
nob_log(NOB_ERROR, "source file `%s` does not exist", src);
|
||
goto defer;
|
||
}
|
||
|
||
da_append(&objs, obj);
|
||
|
||
da_append(&deps, src);
|
||
da_append(&deps, CFLAGS_STAMP);
|
||
for (size_t j = 0; j < headers.count; ++j) {
|
||
da_append(&deps, headers.items[j]);
|
||
}
|
||
|
||
int needs = nob_needs_rebuild(obj, deps.items, deps.count);
|
||
deps.count = 0; // 清空 deps,下一轮循环重新收集
|
||
|
||
if (needs < 0)
|
||
goto defer;
|
||
if (needs == 0)
|
||
continue;
|
||
nob_cc(&cmd);
|
||
nob_cc_flags(&cmd);
|
||
nob_cmd_append(&cmd, "-c");
|
||
nob_cc_inputs(&cmd, src);
|
||
nob_cc_output(&cmd, obj);
|
||
if (!nob_cmd_run(&cmd)) {
|
||
nob_log(NOB_ERROR, "failed to compile `%s`", src);
|
||
goto defer;
|
||
}
|
||
cmd.count = 0; // 清空 cmd,下一轮循环重新构建
|
||
}
|
||
|
||
const char *exe = exe_path(target->name);
|
||
|
||
int needs = nob_needs_rebuild(exe, objs.items, objs.count);
|
||
if (needs < 0)
|
||
goto defer;
|
||
if (needs == 0) {
|
||
result = exe;
|
||
goto defer;
|
||
}
|
||
|
||
nob_cc(&cmd);
|
||
nob_cmd_append(&cmd, SPL_OPTIMIZATION_FLAG);
|
||
for (size_t i = 0; i < objs.count; ++i) {
|
||
nob_cc_inputs(&cmd, objs.items[i]);
|
||
}
|
||
nob_cc_output(&cmd, exe);
|
||
if (!nob_cmd_run(&cmd))
|
||
goto defer;
|
||
|
||
result = exe;
|
||
defer:
|
||
nob_cmd_free(cmd);
|
||
da_free(objs);
|
||
da_free(deps);
|
||
da_free(srcs);
|
||
da_free(headers);
|
||
return result;
|
||
}
|
||
|
||
const char *build_target(const char *name) {
|
||
if (!prepare_build())
|
||
return NULL;
|
||
const char *splc_libs = "./src/libs";
|
||
for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) {
|
||
if (name != NULL && strcmp(targets[i].name, name) != 0) {
|
||
continue;
|
||
}
|
||
build_t *target = &targets[i];
|
||
switch (target->kind) {
|
||
case BUILD_EXE_WITH_OPTIMIZATION:
|
||
case BUILD_EXE: {
|
||
return build_exe(target);
|
||
}
|
||
case BUILD_SPLC_SIR:
|
||
case BUILD_SIR: {
|
||
const char *target_sir = sir_path(name);
|
||
// 编译器先建好:它的产物也要算进依赖,编译器变了就得重编 .sir。
|
||
const char *compiler = build_target(target->sir.compiler);
|
||
if (!compiler) {
|
||
nob_log(NOB_ERROR, "failed to build `%s`", name);
|
||
return NULL;
|
||
}
|
||
|
||
str_da_t deps = {0};
|
||
bool ok = expand_patterns(target->sir.deps, target->sir.deps_count, &deps);
|
||
if (ok) {
|
||
da_append(&deps, target->sir.src);
|
||
da_append(&deps, compiler);
|
||
}
|
||
int needs = ok ? nob_needs_rebuild(target_sir, deps.items, deps.count) : -1;
|
||
da_free(deps);
|
||
if (needs < 0)
|
||
return NULL;
|
||
if (needs == 0)
|
||
return target_sir;
|
||
|
||
Nob_Cmd cmd = {0};
|
||
if (target->kind == BUILD_SPLC_SIR) {
|
||
nob_cmd_append(&cmd, "-I", splc_libs);
|
||
}
|
||
nob_cmd_append(&cmd, target->sir.src, target_sir);
|
||
if (!run_target(target->sir.compiler, &cmd)) {
|
||
nob_log(NOB_ERROR, "failed to build `%s`", name);
|
||
nob_cmd_free(cmd);
|
||
return NULL;
|
||
}
|
||
nob_cmd_free(cmd);
|
||
return target_sir;
|
||
}
|
||
case BUILD_SPLC_NATIVE:
|
||
case BUILD_NATIVE: {
|
||
Nob_Cmd cmd = {0};
|
||
const char *target_exe = exe_path(name);
|
||
const char *target_c = c_path(name);
|
||
const char *compiler = build_target(target->native.compiler);
|
||
if (!compiler) {
|
||
nob_log(NOB_ERROR, "failed to build `%s`", name);
|
||
return NULL;
|
||
}
|
||
|
||
str_da_t deps = {0};
|
||
bool ok = expand_patterns(target->native.deps, target->native.deps_count, &deps);
|
||
if (ok) {
|
||
da_append(&deps, target->native.src);
|
||
da_append(&deps, compiler);
|
||
}
|
||
|
||
int needs = ok ? nob_needs_rebuild(target_c, deps.items, deps.count) : -1;
|
||
if (needs < 0) {
|
||
da_free(deps);
|
||
return NULL;
|
||
}
|
||
if (needs != 0) {
|
||
if (target->kind == BUILD_SPLC_NATIVE) {
|
||
nob_cmd_append(&cmd, "-I", splc_libs);
|
||
}
|
||
nob_cmd_append(&cmd, "--dump", "c", target->native.src, target_c);
|
||
if (!run_target(target->native.compiler, &cmd)) {
|
||
nob_log(NOB_ERROR, "failed to build `%s`", name);
|
||
nob_cmd_free(cmd);
|
||
da_free(deps);
|
||
return NULL;
|
||
}
|
||
cmd.count = 0;
|
||
}
|
||
|
||
// exe 的输入:生成的 .c、SPL 依赖、crt 头。
|
||
da_append(&deps, target_c);
|
||
for (size_t i = 0; i < target->native.crt_deps_count; i += 1)
|
||
da_append(&deps, target->native.crt_deps[i]);
|
||
needs = nob_needs_rebuild(target_exe, deps.items, deps.count);
|
||
da_free(deps);
|
||
if (needs < 0)
|
||
return NULL;
|
||
if (needs == 0)
|
||
return target_exe;
|
||
|
||
nob_cc(&cmd);
|
||
/* 生成的 C 里有跨类型指针访问;-O2 严格别名会误优化。 */
|
||
nob_cmd_append(&cmd, "-I./stage0", "-O2", "-fno-strict-aliasing",
|
||
"-D_CRT_SECURE_NO_WARNINGS", "-D_CRT_NONSTDC_NO_DEPRECATE");
|
||
nob_cc_inputs(&cmd, target_c);
|
||
nob_cc_output(&cmd, target_exe);
|
||
if (!nob_cmd_run(&cmd)) {
|
||
nob_log(NOB_ERROR, "failed to build `%s`", name);
|
||
nob_cmd_free(cmd);
|
||
return NULL;
|
||
}
|
||
nob_cmd_free(cmd);
|
||
return target_exe;
|
||
}
|
||
default:
|
||
NOB_UNREACHABLE("build_t.kind");
|
||
}
|
||
}
|
||
nob_log(NOB_ERROR, "unknown build target `%s`", name);
|
||
return NULL;
|
||
}
|
||
|
||
bool run_target_dry(const char *name, Nob_Cmd *args, Nob_Cmd *result) {
|
||
for (size_t i = 0; i < NOB_ARRAY_LEN(runs); ++i) {
|
||
if (strcmp(runs[i].name, name) != 0) {
|
||
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 (!inter) {
|
||
nob_log(NOB_ERROR, "failed to build interpreter `%s` for `%s`", interpreter, name);
|
||
return false;
|
||
}
|
||
nob_cmd_append(result, inter);
|
||
}
|
||
nob_cmd_append(result, exe);
|
||
nob_cmd_extend(result, args);
|
||
return true;
|
||
}
|
||
nob_log(NOB_ERROR, "unknown run target `%s`", name);
|
||
return false;
|
||
}
|
||
|
||
bool run_target(const char *name, Nob_Cmd *args) {
|
||
Nob_Cmd result = {0};
|
||
if (!run_target_dry(name, args, &result)) {
|
||
return false;
|
||
}
|
||
nob_log(NOB_INFO, "running `%s`", name);
|
||
bool ok = nob_cmd_run(&result);
|
||
nob_cmd_free(result);
|
||
return ok;
|
||
}
|
||
|
||
// ---- 测试 ----
|
||
// Expect 协议:扫整个文件的注释行找 `// Expect retcode = N`,取第一条命中(注解可以在任意位置)。
|
||
// `// Expect compile_error` 表示编译必须失败;后面可跟一个或多个 E#### 错误码。
|
||
// `// Expect stdout = ...` 暂不比较,遇到跳过。
|
||
// 返回 false 只表示文件读不出来、retcode 写坏或错误码格式非法;没写注解则 has_retcode
|
||
// 为假,由调用方按 0 处理。
|
||
typedef struct {
|
||
bool has_retcode;
|
||
int retcode;
|
||
bool compile_error;
|
||
int error_codes[8];
|
||
size_t error_code_count;
|
||
bool has_location;
|
||
int line;
|
||
int col;
|
||
bool pending;
|
||
} test_expect_t;
|
||
|
||
static bool test_parse_error_code(Nob_String_View value, int *out) {
|
||
value = nob_sv_trim(value);
|
||
if (value.count != 5 || value.data[0] != 'E')
|
||
return false;
|
||
int v = 0;
|
||
size_t i = 1;
|
||
for (; i < value.count; i += 1) {
|
||
if (!isdigit((unsigned char)value.data[i]))
|
||
return false;
|
||
v = v * 10 + (value.data[i] - '0');
|
||
}
|
||
*out = v;
|
||
return true;
|
||
}
|
||
|
||
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 (i >= value.count)
|
||
return false;
|
||
|
||
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_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("compile_error"))) {
|
||
expect->compile_error = true;
|
||
if (nob_sv_starts_with(value, nob_sv_from_cstr("="))) {
|
||
value = nob_sv_trim_left(nob_sv_from_parts(value.data + 1, value.count - 1));
|
||
}
|
||
while (value.count > 0) {
|
||
Nob_String_View code = nob_sv_trim(nob_sv_chop_by_delim(&value, ' '));
|
||
if (code.count == 0)
|
||
continue;
|
||
int parsed_code = 0;
|
||
if (expect->error_code_count >= NOB_ARRAY_LEN(expect->error_codes) ||
|
||
!test_parse_error_code(code, &parsed_code)) {
|
||
malformed = true;
|
||
break;
|
||
}
|
||
expect->error_codes[expect->error_code_count++] = parsed_code;
|
||
}
|
||
if (malformed)
|
||
break;
|
||
continue;
|
||
}
|
||
if (nob_sv_eq(key, nob_sv_from_cstr("pending"))) {
|
||
expect->pending = true;
|
||
continue;
|
||
}
|
||
if (nob_sv_eq(key, nob_sv_from_cstr("location"))) { if (expect->has_location) {
|
||
malformed = true;
|
||
break;
|
||
}
|
||
if (nob_sv_starts_with(value, nob_sv_from_cstr("="))) {
|
||
value = nob_sv_trim_left(nob_sv_from_parts(value.data + 1, value.count - 1));
|
||
}
|
||
Nob_String_View line_sv = nob_sv_trim(nob_sv_chop_by_delim(&value, ':'));
|
||
Nob_String_View col_sv = nob_sv_trim(value);
|
||
if (!test_parse_int(line_sv, &expect->line) || !test_parse_int(col_sv, &expect->col) ||
|
||
expect->line <= 0 || expect->col <= 0) {
|
||
malformed = true;
|
||
break;
|
||
}
|
||
expect->has_location = true;
|
||
continue;
|
||
}
|
||
if (!nob_sv_eq(key, nob_sv_from_cstr("retcode")) || expect->has_retcode)
|
||
continue;
|
||
if (nob_sv_starts_with(value, nob_sv_from_cstr("="))) {
|
||
value = nob_sv_trim_left(nob_sv_from_parts(value.data + 1, value.count - 1));
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
// 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"};
|
||
|
||
// 可运行的 target 名(来自 nob_data 的 runs[]),main 里填一次。
|
||
static const char *RUN_NAMES[NOB_ARRAY_LEN(runs)];
|
||
|
||
// 复用 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);
|
||
bool ok = nob_cmd_capture_popen(nob_temp_sv_to_cstr(nob_sb_to_sv(rendered)), result);
|
||
nob_sb_free(rendered);
|
||
return ok;
|
||
}
|
||
|
||
typedef struct {
|
||
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 pending; // `// Expect pending` 的预期失败数
|
||
int no_expect; // 没写 Expect 注解、按 0 期望的文件数
|
||
} test_ctx_t;
|
||
|
||
static bool test_has_error_code(const Nob_Cmd_Result *result, int code) {
|
||
char needle[24] = {0};
|
||
snprintf(needle, sizeof(needle), "error[E%04d]", code);
|
||
return result->out.items != NULL && strstr(result->out.items, needle) != NULL;
|
||
}
|
||
|
||
static void test_pending_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);
|
||
Nob_Cmd_Result compile_result = {0};
|
||
if (!run_capture(&compile, &compile_result)) {
|
||
nob_log(NOB_ERROR, "%s: failed to launch compiler", fname);
|
||
nob_cmd_free(compile);
|
||
ctx->failed += 1;
|
||
return;
|
||
}
|
||
nob_cmd_free(compile);
|
||
if (compile_result.exit_code != 0) {
|
||
nob_log(NOB_INFO, "%s: pending (compile failed as expected)", fname);
|
||
nob_cmd_result_free(compile_result);
|
||
ctx->pending += 1;
|
||
return;
|
||
}
|
||
nob_cmd_result_free(compile_result);
|
||
|
||
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);
|
||
nob_cmd_free(run);
|
||
ctx->failed += 1;
|
||
return;
|
||
}
|
||
nob_cmd_free(run);
|
||
if (result.exit_code != 0) {
|
||
nob_log(NOB_INFO, "%s: pending (run failed as expected)", fname);
|
||
ctx->pending += 1;
|
||
} else {
|
||
nob_log(NOB_ERROR, "%s: pending test passed unexpectedly; remove `// Expect pending`", fname);
|
||
ctx->failed += 1;
|
||
}
|
||
nob_cmd_result_free(result);
|
||
}
|
||
|
||
static void test_compile_and_run(const char *fname, test_ctx_t *ctx) {
|
||
test_expect_t expect = {0};
|
||
if (!test_parse_expect(fname, &expect)) {
|
||
nob_log(NOB_ERROR, "%s: broken `// Expect` annotation", fname);
|
||
ctx->failed += 1;
|
||
return;
|
||
}
|
||
if (expect.pending) {
|
||
test_pending_compile_and_run(fname, ctx);
|
||
return;
|
||
}
|
||
|
||
Nob_Cmd compile = {0};
|
||
nob_cmd_extend(&compile, &ctx->compiler);
|
||
nob_cmd_append(&compile, fname, ctx->out_sir);
|
||
Nob_Cmd_Result compile_result = {0};
|
||
if (!run_capture(&compile, &compile_result)) {
|
||
nob_log(NOB_ERROR, "%s: failed to launch compiler", fname);
|
||
nob_cmd_free(compile);
|
||
ctx->failed += 1;
|
||
return;
|
||
}
|
||
nob_cmd_free(compile);
|
||
bool compiled = compile_result.exit_code == 0;
|
||
if (expect.compile_error) {
|
||
if (compiled) {
|
||
nob_log(NOB_ERROR, "%s: expected compile failure, but it compiled", fname);
|
||
ctx->failed += 1;
|
||
} else {
|
||
size_t i = 0;
|
||
bool missing = false;
|
||
for (; i < expect.error_code_count; i += 1) {
|
||
if (!test_has_error_code(&compile_result, expect.error_codes[i])) {
|
||
nob_log(NOB_ERROR, "%s: missing expected error E%04d", fname,
|
||
expect.error_codes[i]);
|
||
missing = true;
|
||
}
|
||
}
|
||
if (expect.has_location && !missing) {
|
||
char needle[64];
|
||
snprintf(needle, sizeof(needle), ":%d:%d:", expect.line, expect.col);
|
||
if (strstr(compile_result.out.items, needle) == NULL) {
|
||
nob_log(NOB_ERROR, "%s: missing expected source location %d:%d", fname,
|
||
expect.line, expect.col);
|
||
missing = true;
|
||
}
|
||
}
|
||
if (missing) {
|
||
nob_log(NOB_ERROR, "%s: compiler output:\n%.*s", fname,
|
||
(int)compile_result.out.count, compile_result.out.items);
|
||
ctx->failed += 1;
|
||
} else {
|
||
ctx->passed += 1;
|
||
}
|
||
}
|
||
nob_cmd_result_free(compile_result);
|
||
return;
|
||
}
|
||
if (!compiled) {
|
||
nob_log(NOB_ERROR, "%s: compile failed:\n%.*s", fname, (int)compile_result.out.count,
|
||
compile_result.out.items);
|
||
nob_cmd_result_free(compile_result);
|
||
ctx->failed += 1;
|
||
return;
|
||
}
|
||
nob_cmd_result_free(compile_result);
|
||
|
||
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);
|
||
|
||
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 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;
|
||
|
||
const char *dot = strrchr(entry.path, '.');
|
||
if (!dot || strcmp(dot, ".spl") != 0)
|
||
return true;
|
||
|
||
walk_ctx_t *ctx = (walk_ctx_t *)entry.data;
|
||
if (!walk_match_filter(entry.path, ctx->filters))
|
||
return true;
|
||
|
||
ctx->fn(entry.path, ctx->user_data);
|
||
return true;
|
||
}
|
||
|
||
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(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); }
|
||
|
||
static bool delete_directory_recursively(const char *dir_path) {
|
||
return nob_walk_dir(dir_path, delete_walk_entry, .post_order = true);
|
||
}
|
||
|
||
static void add_opt_choices(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
|
||
const char *group, const char **values, int count, int *store,
|
||
cbool required) {
|
||
scc_argparse_opt_t opt;
|
||
scc_argparse_opt_init(&opt, sn, ln, desc);
|
||
opt.group = group;
|
||
scc_argparse_spec_setup_choices(&opt.spec, values, count, store);
|
||
opt.spec.flag_required = required;
|
||
scc_argparse_cmd_add_opt(cmd, &opt);
|
||
}
|
||
|
||
static void add_opt_list(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
|
||
const char *group, scc_argparse_list_t *vec, cbool required) {
|
||
scc_argparse_opt_t opt;
|
||
scc_argparse_opt_init(&opt, sn, ln, desc);
|
||
opt.group = group;
|
||
scc_argparse_spec_setup_list(&opt.spec, vec);
|
||
opt.spec.flag_required = required;
|
||
scc_argparse_cmd_add_opt(cmd, &opt);
|
||
}
|
||
|
||
static void add_arg_string(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
|
||
const char **store, cbool required) {
|
||
scc_argparse_arg_t arg;
|
||
scc_argparse_arg_init(&arg, name, desc);
|
||
scc_argparse_spec_setup_string(&arg.spec, store);
|
||
arg.spec.flag_required = required;
|
||
scc_argparse_cmd_add_arg(cmd, &arg);
|
||
}
|
||
|
||
static void add_arg_list(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
|
||
scc_argparse_list_t *vec) {
|
||
scc_argparse_arg_t arg;
|
||
scc_argparse_arg_init(&arg, name, desc);
|
||
scc_argparse_spec_setup_list(&arg.spec, vec);
|
||
scc_argparse_cmd_add_arg(cmd, &arg);
|
||
}
|
||
|
||
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 下标
|
||
int test_target; // RUN_NAMES 下标,required
|
||
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 = RUN_NAMES[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 pending (%d without `// Expect`)", suite->name,
|
||
target, ctx.passed, ctx.failed, ctx.pending, ctx.no_expect);
|
||
else
|
||
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed, %d pending", suite->name, target,
|
||
ctx.passed, ctx.failed, ctx.pending);
|
||
|
||
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;
|
||
}
|
||
|
||
// 全部从 nob_data 的 targets[] / runs[] 生成:加目标只需改数据,这里不用动。
|
||
static const char *build_kind_name(int kind) {
|
||
switch (kind) {
|
||
case BUILD_EXE:
|
||
return "exe";
|
||
case BUILD_EXE_WITH_OPTIMIZATION:
|
||
return "exe -O2";
|
||
case BUILD_SIR:
|
||
return "sir";
|
||
case BUILD_NATIVE:
|
||
return "native";
|
||
default:
|
||
return "?";
|
||
}
|
||
}
|
||
|
||
// list 显示展开后的真实文件数,而不是"模式条数"。
|
||
static size_t count_files(const char **patterns, size_t count) {
|
||
str_da_t out = {0};
|
||
bool ok = expand_patterns(patterns, count, &out);
|
||
size_t n = ok ? out.count : count;
|
||
da_free(out);
|
||
return n;
|
||
}
|
||
|
||
static int cli_list(void) {
|
||
printf("targets (%zu):\n", (size_t)NOB_ARRAY_LEN(targets));
|
||
printf(" %-12s %-9s %s\n", "NAME", "KIND", "DETAIL");
|
||
for (size_t i = 0; i < NOB_ARRAY_LEN(targets); ++i) {
|
||
const build_t *t = &targets[i];
|
||
const char *kind = build_kind_name(t->kind);
|
||
switch (t->kind) {
|
||
case BUILD_EXE:
|
||
case BUILD_EXE_WITH_OPTIMIZATION:
|
||
printf(" %-12s %-9s srcs=%zu headers=%zu\n", t->name, kind,
|
||
count_files(t->exe.srcs, t->exe.srcs_count),
|
||
count_files(t->exe.headers, t->exe.headers_count));
|
||
break;
|
||
case BUILD_SPLC_SIR:
|
||
case BUILD_SIR:
|
||
printf(" %-12s %-9s %s <- %s deps=%zu\n", t->name, kind, t->sir.src, t->sir.compiler,
|
||
count_files(t->sir.deps, t->sir.deps_count));
|
||
break;
|
||
case BUILD_SPLC_NATIVE:
|
||
case BUILD_NATIVE:
|
||
printf(" %-12s %-9s %s <- %s deps=%zu crt=%zu\n", t->name, kind, t->native.src,
|
||
t->native.compiler, count_files(t->native.deps, t->native.deps_count),
|
||
t->native.crt_deps_count);
|
||
break;
|
||
}
|
||
}
|
||
|
||
printf("\nrun targets (%zu):\n", (size_t)NOB_ARRAY_LEN(runs));
|
||
printf(" %-12s %s\n", "NAME", "COMMAND");
|
||
for (size_t i = 0; i < NOB_ARRAY_LEN(runs); ++i) {
|
||
if (runs[i].interpreter)
|
||
printf(" %-12s %s %s\n", runs[i].name, exe_path(runs[i].interpreter),
|
||
sir_path(runs[i].name));
|
||
else
|
||
printf(" %-12s %s\n", runs[i].name, exe_path(runs[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) {
|
||
#ifdef _WIN32
|
||
SetConsoleOutputCP(CP_UTF8);
|
||
SetConsoleCP(CP_UTF8);
|
||
#endif
|
||
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);
|
||
|
||
nob_cli_t cli = {0};
|
||
cli.test_target = -1;
|
||
cli.test_mode = -1;
|
||
cli.diff_mode = -1;
|
||
|
||
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);
|
||
|
||
for (size_t i = 0; i < NOB_ARRAY_LEN(runs); i += 1)
|
||
RUN_NAMES[i] = runs[i].name;
|
||
|
||
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);
|
||
add_arg_list(&build_cmd, "targets", "target names, default all", &cli.build_targets);
|
||
scc_argparse_cmd_add_subcmd(root, &build_cmd);
|
||
|
||
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);
|
||
add_arg_string(&run_cmd, "target", "target name", &cli.run_target, true);
|
||
add_arg_list(&run_cmd, "args", "arguments for the target", &cli.run_args);
|
||
scc_argparse_cmd_add_subcmd(root, &run_cmd);
|
||
|
||
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;
|
||
add_opt_choices(&test_cmd, 't', "target", "compiler target, from nob_data runs[]", NULL,
|
||
RUN_NAMES, (int)NOB_ARRAY_LEN(runs), &cli.test_target, true);
|
||
add_opt_choices(&test_cmd, 's', "suite", "test suite, default stage1", NULL, suite_names,
|
||
(int)NOB_ARRAY_LEN(TEST_SUITES), &cli.test_suite, false);
|
||
add_opt_choices(&test_cmd, 'm', "mode", "dump smoke test through the target", "Dump",
|
||
TEST_MODE_NAMES, (int)NOB_ARRAY_LEN(TEST_MODE_NAMES), &cli.test_mode, false);
|
||
add_opt_list(&test_cmd, 'f', "filter", "file name filter, repeatable", "Filtering",
|
||
&cli.test_filters, false);
|
||
scc_argparse_cmd_add_subcmd(root, &test_cmd);
|
||
|
||
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);
|
||
add_arg_string(&diff_cmd, "expect", "reference build target", &cli.diff_expect, true);
|
||
add_arg_string(&diff_cmd, "got", "build target under test", &cli.diff_got, true);
|
||
add_opt_choices(&diff_cmd, 'm', "mode", "dump mode", NULL, TEST_MODE_NAMES,
|
||
(int)NOB_ARRAY_LEN(TEST_MODE_NAMES), &cli.diff_mode, true);
|
||
add_opt_choices(&diff_cmd, 's', "suite", "test suite, default stage1", NULL, suite_names,
|
||
(int)NOB_ARRAY_LEN(TEST_SUITES), &cli.diff_suite, false);
|
||
add_opt_list(&diff_cmd, 'f', "filter", "file name filter, repeatable", NULL, &cli.diff_filters,
|
||
false);
|
||
scc_argparse_cmd_add_subcmd(root, &diff_cmd);
|
||
|
||
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);
|
||
|
||
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);
|
||
|
||
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;
|
||
|
||
rc = cli_dispatch(&cli);
|
||
|
||
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;
|
||
}
|