stage1 解决重构哈希 nob优化支持头文件缓存以及win下exe缓存

This commit is contained in:
zzy committed 2026-09-17 20:55:21 +08:00
1 parent 70ac7380e9
commit fb0ff9b663
8 files changed
+236 -98

No files matched your search

+56 -8
View File
@@ -30,6 +30,7 @@ static const char *splc0_srcs[] = {
"stage1/spl_parser.c",
"stage1/spl_dumptree.c",
"stage1/spl_type.c",
"stage1/spl_fold.c",
"stage1/spl_lang_type.c",
"stage1/spl_attr.c",
"stage1/spl_scope.c",
@@ -78,6 +79,16 @@ static const char *obj_path(const char *src) {
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 bool sync_cflags_stamp(void) {
Nob_String_View cflags = nob_sv_from_cstr(CFLAGS_STR);
Nob_String_Builder old = {0};
@@ -95,10 +106,39 @@ static bool sync_cflags_stamp(void) {
return true;
}
// 头文件依赖:收集 stage0/stage1 下的所有 .h,作为每个 object 的输入。
// nob 不解析 #include,这里用“任一头文件变化则重建全部 object”的保守策略,
// 避免头文件改动后各 .o 结构体布局/常量不一致导致的隐蔽错误。
static Nob_Files g_headers = {0};
static bool collect_header_entry(Nob_Walk_Entry entry) {
if (entry.type != NOB_FILE_REGULAR)
return true;
const char *dot = strrchr(entry.path, '.');
if (!dot || strcmp(dot, ".h") != 0)
return true;
if (strstr(entry.path, ".old") != NULL)
return true;
da_append(&g_headers, nob_temp_strdup(entry.path));
return true;
}
static bool collect_headers(void) {
if (g_headers.count > 0)
return true;
if (!nob_walk_dir("stage0", collect_header_entry))
return false;
if (!nob_walk_dir("stage1", collect_header_entry))
return false;
return true;
}
static bool prepare_build(void) {
if (!nob_mkdir_if_not_exists(BUILD_FOLDER))
return false;
return sync_cflags_stamp();
if (!sync_cflags_stamp())
return false;
return collect_headers();
}
static bool build_object(const char *src) {
@@ -108,8 +148,16 @@ static bool build_object(const char *src) {
return false;
}
const char *inputs[] = {src, CFLAGS_STAMP};
int needs = nob_needs_rebuild(obj, inputs, NOB_ARRAY_LEN(inputs));
// 依赖:源文件 + 所有头文件 + cflags 戳。
size_t ninputs = g_headers.count + 2;
const char **inputs = (const char **)nob_temp_alloc(sizeof(const char *) * ninputs);
inputs[0] = src;
for (size_t i = 0; i < g_headers.count; ++i) {
inputs[i + 1] = g_headers.items[i];
}
inputs[ninputs - 1] = CFLAGS_STAMP;
int needs = nob_needs_rebuild(obj, inputs, ninputs);
if (needs < 0)
return false;
if (needs == 0)
@@ -125,7 +173,7 @@ static bool build_object(const char *src) {
}
static bool link_exe(const target_t *target) {
const char *exe = nob_temp_sprintf(BUILD_FOLDER "%s", target->name);
const char *exe = exe_path(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]);
@@ -225,7 +273,7 @@ static bool test_once(const char *fname, void *user_data) {
}
cmd.count = 0;
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", compiler_path));
nob_cmd_append(&cmd, exe_path(compiler_path));
nob_cmd_append(&cmd, fname, target_sir);
if (!nob_cmd_run(&cmd)) {
return false;
@@ -235,7 +283,7 @@ static bool test_once(const char *fname, void *user_data) {
return 1;
}
cmd.count = 0;
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", cli_path));
nob_cmd_append(&cmd, exe_path(cli_path));
nob_cmd_append(&cmd, target_sir);
// nob_cmd_run(&cmd);
@@ -267,7 +315,7 @@ static bool test_once_with_dump(const char *fname, void *user_data) {
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, exe_path(target_name));
nob_cmd_append(&cmd, "--dump", (char *)user_data, fname, BUILD_FOLDER "t.sir");
return nob_cmd_run(&cmd);
}
@@ -402,7 +450,7 @@ int main(int argc, char **argv) {
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, exe_path(target_name));
while (argc > 0) {
nob_cmd_append(&cmd, nob_shift(argv, argc));
}