427 lines
13 KiB
C
427 lines
13 KiB
C
|
|
#define SPL_CFLAGS "-Wall", "-Wextra", "-O0", "-g", "-D_CRT_SECURE_NO_WARNINGS"
|
|
#define nob_cc_flags(cmd) nob_cmd_append(cmd, SPL_CFLAGS)
|
|
#undef UNICODE
|
|
#define NOB_IMPLEMENTATION
|
|
#include "nob.h"
|
|
#undef NOB_IMPLEMENTATION
|
|
#include "nob_popen.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";
|
|
|
|
/* clang-format off */
|
|
#define VM_SRCS "stage0/spl_mcode.c", "stage0/spl_syscall.c", "stage0/spl_vm.c",
|
|
static const char *spl_cli_srcs[] = {"stage0/spl_cli.c", VM_SRCS};
|
|
static const char *test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS};
|
|
static const char *splc0_srcs[] = {
|
|
"stage1/splc0.c",
|
|
"stage1/spl_diag.c",
|
|
"stage1/spl_lexer.c",
|
|
"stage1/spl_ast.c",
|
|
"stage1/spl_dumptree.c",
|
|
"stage1/spl_type.c",
|
|
"stage1/spl_attr.c",
|
|
"stage1/spl_sema.c",
|
|
"stage1/spl_intrin.c",
|
|
"stage1/spl_ir.c",
|
|
"stage1/spl_ast2ir.c",
|
|
"stage1/spl_layout.c",
|
|
"stage1/spl_ir2vm.c",
|
|
"stage0/spl_mcode.c",
|
|
};
|
|
/* clang-format on */
|
|
|
|
typedef struct {
|
|
const char *name;
|
|
const char **srcs;
|
|
size_t srcs_count;
|
|
} target_t;
|
|
|
|
static target_t exe_targets[] = {
|
|
{"spl_cli", spl_cli_srcs, NOB_ARRAY_LEN(spl_cli_srcs)},
|
|
{"test", test_srcs, NOB_ARRAY_LEN(test_srcs)},
|
|
{"splc0", splc0_srcs, NOB_ARRAY_LEN(splc0_srcs)},
|
|
};
|
|
|
|
static const target_t *find_exe_target(const char *name) {
|
|
for (size_t i = 0; i < NOB_ARRAY_LEN(exe_targets); ++i) {
|
|
if (strcmp(exe_targets[i].name, name) == 0) {
|
|
return &exe_targets[i];
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
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) {
|
|
*w++ = (*p == '/' || *p == '\\') ? '_' : *p;
|
|
}
|
|
memcpy(w, ".o", 3);
|
|
return out;
|
|
}
|
|
|
|
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;
|
|
return sync_cflags_stamp();
|
|
}
|
|
|
|
static bool build_object(const char *src) {
|
|
const char *obj = obj_path(src);
|
|
if (!nob_file_exists(src)) {
|
|
nob_log(NOB_ERROR, "source file `%s` does not exist", src);
|
|
return false;
|
|
}
|
|
|
|
const char *inputs[] = {src, CFLAGS_STAMP};
|
|
int needs = nob_needs_rebuild(obj, inputs, NOB_ARRAY_LEN(inputs));
|
|
if (needs < 0)
|
|
return false;
|
|
if (needs == 0)
|
|
return true;
|
|
|
|
Nob_Cmd cmd = {0};
|
|
nob_cc(&cmd);
|
|
nob_cc_flags(&cmd);
|
|
nob_cmd_append(&cmd, "-c");
|
|
nob_cc_inputs(&cmd, src);
|
|
nob_cc_output(&cmd, obj);
|
|
return nob_cmd_run(&cmd);
|
|
}
|
|
|
|
static bool link_exe(const target_t *target) {
|
|
const char *exe = nob_temp_sprintf(BUILD_FOLDER "%s", target->name);
|
|
const char **objs = (const char **)nob_temp_alloc(sizeof(const char *) * target->srcs_count);
|
|
for (size_t i = 0; i < target->srcs_count; ++i) {
|
|
objs[i] = obj_path(target->srcs[i]);
|
|
}
|
|
|
|
int needs = nob_needs_rebuild(exe, objs, target->srcs_count);
|
|
if (needs < 0)
|
|
return false;
|
|
if (needs == 0)
|
|
return true;
|
|
|
|
Nob_Cmd cmd = {0};
|
|
nob_cc(&cmd);
|
|
nob_cc_flags(&cmd);
|
|
for (size_t i = 0; i < target->srcs_count; ++i) {
|
|
nob_cc_inputs(&cmd, objs[i]);
|
|
}
|
|
nob_cc_output(&cmd, exe);
|
|
return nob_cmd_run(&cmd);
|
|
}
|
|
|
|
static bool build_target(const char *name) {
|
|
const target_t *target = find_exe_target(name);
|
|
if (target == NULL) {
|
|
nob_log(NOB_ERROR, "unknown target `%s`", name);
|
|
return false;
|
|
}
|
|
|
|
for (size_t i = 0; i < target->srcs_count; ++i) {
|
|
if (!build_object(target->srcs[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return link_exe(target);
|
|
}
|
|
|
|
static bool build_all(void) {
|
|
for (size_t i = 0; i < NOB_ARRAY_LEN(exe_targets); ++i) {
|
|
if (!build_target(exe_targets[i].name)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
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); // 去掉前导空格
|
|
|
|
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, nob_temp_sprintf(BUILD_FOLDER "%s", compiler_path));
|
|
nob_cmd_append(&cmd, fname, target_sir);
|
|
if (!nob_cmd_run(&cmd)) {
|
|
return false;
|
|
}
|
|
|
|
if (!build_target(cli_path)) {
|
|
return 1;
|
|
}
|
|
cmd.count = 0;
|
|
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", 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);
|
|
return false;
|
|
}
|
|
|
|
nob_cmd_result_free(result);
|
|
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, nob_temp_sprintf(BUILD_FOLDER "%s", target_name));
|
|
nob_cmd_append(&cmd, "--dump", (char *)user_data, fname, BUILD_FOLDER "t.sir");
|
|
return nob_cmd_run(&cmd);
|
|
}
|
|
|
|
typedef bool (*test_fn_t)(const char *fname, void *user_data);
|
|
static bool run_test(Nob_Files *files, test_fn_t test_once, void *user_data) {
|
|
const char *test_folder = "./tests/stage1/";
|
|
|
|
if (!prepare_build()) {
|
|
return 1;
|
|
}
|
|
|
|
Nob_File_Paths paths = {0};
|
|
nob_read_entire_dir(test_folder, &paths);
|
|
for (int i = 0; i < files->count; i += 1) {
|
|
for (int j = 0; j < paths.count; j += 1) {
|
|
String_View sv = sv_from_cstr(paths.items[j]);
|
|
// printf("--- test " SV_Fmt " ---\n", SV_Arg(sv));
|
|
if (!strstr(paths.items[j], files->items[i])) {
|
|
continue;
|
|
}
|
|
const char *tname = nob_temp_sprintf("%s%s", test_folder, paths.items[j]);
|
|
printf("--- test %s ---\n", tname);
|
|
if (test_once(tname, user_data) == false)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
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 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 build and run VM unit 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");
|
|
nob_log(NOB_INFO, "%s", " ");
|
|
nob_log(NOB_INFO, "Targets:");
|
|
for (size_t i = 0; i < NOB_ARRAY_LEN(exe_targets); ++i) {
|
|
nob_log(NOB_INFO, " %s", exe_targets[i].name);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
#ifdef _WIN32
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
#endif
|
|
NOB_GO_REBUILD_URSELF_PLUS(argc, argv, "nob.h", "nob_popen.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);
|
|
}
|
|
|
|
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_all())
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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);
|
|
if (!prepare_build())
|
|
return 1;
|
|
if (!build_target(target_name))
|
|
return 1;
|
|
Nob_Cmd cmd = {0};
|
|
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", target_name));
|
|
while (argc > 0) {
|
|
nob_cmd_append(&cmd, nob_shift(argv, argc));
|
|
}
|
|
return nob_cmd_run(&cmd) ? 0 : 1;
|
|
}
|
|
|
|
if (strcmp(command_name, "test") == 0) {
|
|
Nob_Files files = {0};
|
|
test_fn_t tfn = test_once;
|
|
const char *mode_name = NULL;
|
|
if (argc > 0) {
|
|
mode_name = nob_shift(argv, argc);
|
|
if (strcmp(mode_name, "lex") == 0 || strcmp(mode_name, "ast") == 0 ||
|
|
strcmp(mode_name, "sema") == 0 || strcmp(mode_name, "ir") == 0 ||
|
|
strcmp(mode_name, "vm") == 0) {
|
|
tfn = test_once_with_dump;
|
|
} else if (strcmp(mode_name, "run") == 0) {
|
|
// HACK
|
|
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;
|
|
} else {
|
|
da_append(&files, mode_name);
|
|
}
|
|
}
|
|
|
|
while (argc > 0) {
|
|
da_append(&files, nob_shift(argv, argc));
|
|
}
|
|
|
|
return run_test(&files, tfn, (void *)mode_name) ? 0 : 1;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
if (strcmp(command_name, "list") == 0) {
|
|
for (size_t i = 0; i < NOB_ARRAY_LEN(exe_targets); ++i) {
|
|
nob_log(NOB_INFO, "%s:", exe_targets[i].name);
|
|
for (size_t j = 0; j < exe_targets[i].srcs_count; ++j) {
|
|
nob_log(NOB_INFO, " %s", exe_targets[i].srcs[j]);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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;
|
|
}
|