Skip to content

Commit

Permalink
Improve nob on linux and fix linux static link api.
Browse files Browse the repository at this point in the history
  • Loading branch information
travisdoor committed Feb 6, 2025
1 parent 0295dbf commit 7520711
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 53 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ Compiler:
- Improved "Libraries" section in documentation.
- Add new examples into 'how-to' showing how to do static and dynamic linking of external
libraries.
- Nob on linux tries to fallback to `llvm-config` in case `llvm-config-X` wasnt found.
- Add LLVM version check on linux.
- Add more search paths for compiler dependencies on linux.

Modules:
- Add math module.
Expand Down
4 changes: 2 additions & 2 deletions doctor.bl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ MISC :: [_]Test.{
Test.{ name = "how-to/gunner", kind = TestKind.BUILD, platform = Platform.WINDOWS },
Test.{ name = "how-to/glwindow_gunner", kind = TestKind.BUILD, platform = Platform.WINDOWS },
Test.{ name = "how-to/glwindow_creation", kind = TestKind.BUILD, platform = Platform.WINDOWS },
Test.{ name = "how-to/static_library", kind = TestKind.BUILD, platform = Platform.WINDOWS },
Test.{ name = "how-to/dynamic_library", kind = TestKind.BUILD, platform = Platform.WINDOWS },
Test.{ name = "how-to/static_library", kind = TestKind.BUILD },
Test.{ name = "how-to/dynamic_library", kind = TestKind.BUILD },
Test.{ name = "tests/build_api_test", kind = TestKind.BUILD },
Test.{ name = "tests/library", kind = TestKind.BUILD, platform = Platform.WINDOWS },
};
Expand Down
2 changes: 1 addition & 1 deletion lib/bl/api/build/build.bl
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ link_static_library :: fn (target: *Target, name: string_view) {
}

LINUX, DARWIN {
append_linker_options(target, tprint("-llib%.a", name));
append_linker_options(target, tprint("lib%.a", name));
}

default { panic("Unsupported target for static linking!"); }
Expand Down
23 changes: 22 additions & 1 deletion src/nob/find_llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ void find_llvm(void) {
}
#endif

// Try llvm-config-N
if (!strlen(llvm_config)) {
cmd_append(&cmd, SHELL, "command -v llvm-config-" STR(LLVM_REQUIRED) " 2>/dev/null");
cmd_run_sync_read_and_reset(&cmd, &sb);
llvm_config = trim_and_dup(sb);
sb.count = 0;
}

// Try llvm-config
if (!strlen(llvm_config)) {
cmd_append(&cmd, SHELL, "command -v llvm-config 2>/dev/null");
cmd_run_sync_read_and_reset(&cmd, &sb);
llvm_config = trim_and_dup(sb);
sb.count = 0;
}

if (!strlen(llvm_config)) {
nob_log(NOB_ERROR,
"Unable to find 'llvm-config-" STR(LLVM_REQUIRED) "'. LLVM might not be installed on your system, it's missing from PATH or you have incorrect version. Expected LLVM version " STR(LLVM_REQUIRED) ".");
Expand All @@ -77,6 +86,19 @@ void find_llvm(void) {
if (sb.count == 0) exit(1);
LLVM_VERSION = trim_and_dup(sb);
sb.count = 0;

// Check the version
int llvm_major, llvm_minor, llvm_patch;
if (sscanf(LLVM_VERSION, "%d.%d.%d", &llvm_major, &llvm_minor, &llvm_patch) != 3) {
nob_log(NOB_ERROR, "Unable to parse LLVM version '%s'.", LLVM_VERSION);
exit(1);
}

if (llvm_major != LLVM_REQUIRED) {
nob_log(NOB_ERROR, "Unsupported LLVM version '%s', expected is %d.", LLVM_VERSION, LLVM_REQUIRED);
exit(1);
}

nob_log(NOB_INFO, "Using LLVM %s.", LLVM_VERSION);

// include dir
Expand All @@ -93,7 +115,6 @@ void find_llvm(void) {
if (sb.count == 0) exit(1);
LLVM_LIBS = trim_and_dup(sb);
sb.count = 0;
// nob_log(NOB_INFO, "LLVM " STR(LLVM_REQUIRED) " libs: %s", LLVM_LIBS);

// libdir
cmd_append(&cmd, llvm_config, "--libdir");
Expand Down
103 changes: 54 additions & 49 deletions src/nob/nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,50 @@ void blc(void) {
nob_log(NOB_INFO, temp_sprintf("Compiling blc-" BL_VERSION " (%s).", config_name));

const char *src[] = {
"./src/arena.c",
"./src/asm_writer.c",
"./src/assembly.c",
"./src/ast_printer.c",
"./src/ast.c",
"./src/bc_writer.c",
"./src/bldebug.c",
"./src/blmemory.c",
"./src/build_api.c",
"./src/builder.c",
"./src/common.c",
"./src/conf.c",
"./src/docs.c",
"./src/file_loader.c",
"./src/intrinsic.c",
"./src/ir_opt.c",
"./src/ir.c",
"./src/lexer.c",
"./src/linker.c",
"./src/lld_ld.c",
"./src/lld_link.c",
"./src/llvm_api.cpp",
"./src/main.c",
"./src/mir_printer.c",
"./src/mir_writer.c",
"./src/mir.c",
"./src/native_bin.c",
"./src/obj_writer.c",
"./src/parser.c",
"./src/scope_printer.c",
"./src/scope.c",
"./src/setup.c",
"./src/table.c",
"./src/threading.c",
"./src/tinycthread.c",
"./src/token_printer.c",
"./src/tokens.c",
"./src/unit.c",
"./src/vm_runner.c",
"./src/vm.c",
"./src/vmdbg.c",
"./src/x86_64.c",
"./src/arena.c",
"./src/asm_writer.c",
"./src/assembly.c",
"./src/ast_printer.c",
"./src/ast.c",
"./src/bc_writer.c",
"./src/bldebug.c",
"./src/blmemory.c",
"./src/build_api.c",
"./src/builder.c",
"./src/common.c",
"./src/conf.c",
"./src/docs.c",
"./src/file_loader.c",
"./src/intrinsic.c",
"./src/ir_opt.c",
"./src/ir.c",
"./src/lexer.c",
"./src/linker.c",
"./src/lld_ld.c",
"./src/lld_link.c",
"./src/llvm_api.cpp",
"./src/main.c",
"./src/mir_printer.c",
"./src/mir_writer.c",
"./src/mir.c",
"./src/native_bin.c",
"./src/obj_writer.c",
"./src/parser.c",
"./src/scope_printer.c",
"./src/scope.c",
"./src/setup.c",
"./src/table.c",
"./src/threading.c",
"./src/tinycthread.c",
"./src/token_printer.c",
"./src/tokens.c",
"./src/unit.c",
"./src/vm_runner.c",
"./src/vm.c",
"./src/vmdbg.c",
"./src/x86_64.c",
#if BL_RPMALLOC_ENABLE
"./deps/rpmalloc-" RPMALLOC_VERSION "/rpmalloc/rpmalloc.c",
"./deps/rpmalloc-" RPMALLOC_VERSION "/rpmalloc/rpmalloc.c",
#endif
};

Expand Down Expand Up @@ -312,30 +312,35 @@ void cleanup(void) {
}

#ifdef __linux__

void find_deps(void) {
LIBZ = shell("find /usr/lib /usr/local/lib -name \"libz.a\" -print -quit 2>/dev/null");

#define LIB_PATH "/lib /usr/lib /usr/local/lib /lib64 /usr/lib64 /usr/lib/x86_64-linux-gnu"

LIBZ = shell("find " LIB_PATH " -name \"libz.a\" -print -quit 2>/dev/null");
if (!strok(LIBZ)) {
nob_log(NOB_ERROR, "Unable to find 'libz'.");
nob_log(NOB_ERROR, "Unable to find 'libz.a' in non of following paths: '" LIB_PATH "'.");
exit(1);
}
nob_log(NOB_INFO, "Using 'libz' '%s'.", LIBZ);

LIBZSTD = shell("find /usr/lib /usr/local/lib -name \"libzstd.a\" -print -quit 2>/dev/null");
LIBZSTD = shell("find " LIB_PATH " -name \"libzstd.a\" -print -quit 2>/dev/null");
if (!strok(LIBZSTD)) {
nob_log(NOB_ERROR, "Unable to find 'libzstd'.");
nob_log(NOB_ERROR, "Unable to find 'libzstd.a' in non of following paths: '" LIB_PATH "'.");
exit(1);
}
nob_log(NOB_INFO, "Using 'libzstd' '%s'.", LIBZSTD);

LIBTINFO = shell("find /usr/lib /usr/local/lib -name \"libtinfo.a\" -print -quit 2>/dev/null");
LIBTINFO = shell("find " LIB_PATH " -name \"libtinfo.a\" -print -quit 2>/dev/null");
if (!strok(LIBTINFO)) {
nob_log(NOB_ERROR, "Unable to find 'libtinfo'.");
nob_log(NOB_ERROR, "Unable to find 'libtinfo.a' in non of following paths: '" LIB_PATH "'.");
exit(1);
}
nob_log(NOB_INFO, "Using 'libtinfo' '%s'.", LIBTINFO);
}

#elif __APPLE__

void find_deps(void) {
if (!strok(shell("which brew"))) {
nob_log(NOB_ERROR, "Homebrew package manager not found. We currently require dependencies installed using homebrew because in some cases the MacOS Command Line tools does not provide static libraries.");
Expand Down

0 comments on commit 7520711

Please sign in to comment.