From e690383a494e6f2b63283a2eb1c17c5cf93e3a0c Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Thu, 29 May 2025 16:26:55 -0600 Subject: [PATCH] More serious LLVM linking --- CMakeLists.txt | 2 +- src/compiler.bbb | 3 +++ src/compiler.cpp | 9 +++++++++ src/compiler.hpp | 4 ++++ src/emitter.cpp | 43 ++++++++++++++++++++++--------------------- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e65a14e..0b24259 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,5 +48,5 @@ target_link_libraries(llvm_bindings PUBLIC target_link_libraries(bb PUBLIC llvm_bindings) -add_compile_options(-Wall -Wextra -pedantic -Wpedantic -Werror -Wno-c99-extensions -Wno-unused-function -Wno-missing-designated-field-initializers -funsigned-char -fwrapv -fno-strict-aliasing) +add_compile_options(-v -Wall -Wextra -pedantic -Wpedantic -Werror -Wno-c99-extensions -Wno-unused-function -Wno-missing-designated-field-initializers -funsigned-char -fwrapv -fno-strict-aliasing) add_compile_definitions(CMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}") diff --git a/src/compiler.bbb b/src/compiler.bbb index 2adb247..2aa9a77 100644 --- a/src/compiler.bbb +++ b/src/compiler.bbb @@ -832,6 +832,8 @@ LLVMIntrinsicIndex = enum va_copy, } +[extern] LLVMContextCreate = fn [cc(c)] () &LLVMContext; + ModuleLLVM = struct { context: &LLVMContext, @@ -2612,6 +2614,7 @@ parse = fn (module: &Module) void emit = fn (module: &Module) void { + >context = LLVMContextCreate(); } compile = fn (arena: &Arena, options: CompileOptions) void diff --git a/src/compiler.cpp b/src/compiler.cpp index 35434f6..241d9d9 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -97,6 +97,7 @@ fn void compile(Arena* arena, Options options) .library_directories = options.library_directories, .library_names = options.library_names, .library_paths = options.library_paths, + .link_libcpp = options.link_libcpp, .target = options.target, .build_mode = options.build_mode, .has_debug_info = options.has_debug_info, @@ -250,6 +251,13 @@ fn String compile_file(Arena* arena, Compile options) library_list = library_list(space + 1); } + library_buffer[library_count] = string_literal("gcc"); + library_count += 1; + library_buffer[library_count] = string_literal("gcc_s"); + library_count += 1; + library_buffer[library_count] = string_literal("m"); + library_count += 1; + library_names = { library_buffer, library_count }; } else if (base_name.equal(string_literal("c_abi"))) @@ -266,6 +274,7 @@ fn String compile_file(Arena* arena, Compile options) .library_paths = library_paths, .library_names = library_names, .library_directories = library_directories, + .link_libcpp = is_compiler, .target = { .cpu = CPUArchitecture::x86_64, .os = OperatingSystem::linux_, diff --git a/src/compiler.hpp b/src/compiler.hpp index d3e4b03..cf3442b 100644 --- a/src/compiler.hpp +++ b/src/compiler.hpp @@ -1213,10 +1213,13 @@ struct Module String name; String path; String executable; + Slice objects; Slice library_directories; Slice library_names; Slice library_paths; + bool link_libc = true; + bool link_libcpp = false; Target target; BuildMode build_mode; @@ -1288,6 +1291,7 @@ struct Options Slice library_paths; Slice library_names; Slice library_directories; + bool link_libcpp; Target target; BuildMode build_mode; bool has_debug_info; diff --git a/src/emitter.cpp b/src/emitter.cpp index 43919b8..2a5db43 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -8326,11 +8326,6 @@ fn void link(Module* module) assert(module->executable.pointer[module->executable.length] == 0); builder.add((char*)module->executable.pointer); - for (String object: module->objects) - { - builder.add(arena, object); - } - for (String library_directory: module->library_directories) { String parts[] = { @@ -8340,20 +8335,6 @@ fn void link(Module* module) builder.add(arena, arena_join_string(arena, array_to_slice(parts))); } - for (String library_path: module->library_paths) - { - builder.add(arena, library_path); - } - - for (String library_name: module->library_names) - { - String parts[] = { - string_literal("-l"), - library_name, - }; - builder.add(arena, arena_join_string(arena, array_to_slice(parts))); - } - String candidate_library_paths[] = { string_literal("/usr/lib"), string_literal("/usr/lib/x86_64-linux-gnu"), @@ -8391,8 +8372,28 @@ fn void link(Module* module) builder.add((char*)arena_join_string(arena, array_to_slice(parts)).pointer); } - auto link_libcpp = false; - if (link_libcpp) + builder.add("-L/usr/lib/gcc/x86_64-pc-linux-gnu/15.1.1"); + + for (String object: module->objects) + { + builder.add(arena, object); + } + + for (String library_path: module->library_paths) + { + builder.add(arena, library_path); + } + + for (String library_name: module->library_names) + { + String parts[] = { + string_literal("-l"), + library_name, + }; + builder.add(arena, arena_join_string(arena, array_to_slice(parts))); + } + + if (module->link_libcpp) { builder.add("-lstdc++"); }