Make clangd work with toolchain's headers

This commit is contained in:
2026-08-25 18:26:42 +03:00
parent bdb7ada1ed
commit 1893b8f054
5 changed files with 89 additions and 9 deletions
+61 -5
View File
@@ -1,9 +1,66 @@
#define NOB_IMPLEMENTATION
#include "nob.h"
#include <stdbool.h>
#define BUILD_FOLDER "build/"
#define SRC_FOLDER "src/"
bool render_clangd_config(void) {
Nob_String_Builder tmpl = {0};
if (!nob_read_entire_file(".clangd.template", &tmpl))
return false;
nob_sb_append_null(&tmpl);
const char *include_path = getenv("I686_ELF_GCC_INCLUDE");
if (include_path == NULL) {
nob_log(NOB_ERROR, "I686_ELF_GCC_INCLUDE not set");
nob_sb_free(tmpl);
return false;
}
const char *needle = "${env:I686_ELF_GCC_INCLUDE}";
Nob_String_Builder out = {0};
const char *cursor = tmpl.items;
const char *found;
while ((found = strstr(cursor, needle)) != NULL) {
nob_sb_append_buf(&out, cursor, found - cursor);
nob_sb_append_cstr(&out, include_path);
cursor = found + strlen(needle);
}
nob_sb_append_cstr(&out, cursor);
bool ok = nob_write_entire_file(".clangd", out.items, out.count);
nob_sb_free(tmpl);
nob_sb_free(out);
return ok;
}
bool build_assembly(Nob_Cmd *cmd) {
nob_cmd_append(cmd, "i686-elf-as", "-o", BUILD_FOLDER "boot.o", SRC_FOLDER "boot.s");
if (!nob_cmd_run(cmd))
return false;
return true;
}
bool build_c(Nob_Cmd *cmd) {
nob_cmd_append(cmd,
"i686-elf-gcc",
"-std=gnu99",
"-ffreestanding",
"-Wall",
"-Wextra",
"-O2",
"-o", BUILD_FOLDER "kernel.o",
"-c", SRC_FOLDER "kernel.c");
if (!nob_cmd_run(cmd))
return false;
return true;
}
int main(int argc, char **argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
@@ -12,12 +69,11 @@ int main(int argc, char **argv) {
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "i686-elf-as", "-o", BUILD_FOLDER "boot.o", SRC_FOLDER "boot.s");
if (!nob_cmd_run(&cmd))
if (!render_clangd_config())
return 1;
nob_cmd_append(&cmd, "i686-elf-gcc", "-Wall", "-Wextra", "-o", BUILD_FOLDER "kernel", SRC_FOLDER "kernel.c");
if (!nob_cmd_run(&cmd))
if (!build_assembly(&cmd))
return 1;
if (!build_c(&cmd))
return 1;
return 0;