aboutsummaryrefslogtreecommitdiff
path: root/src/linker.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/linker.cpp')
-rw-r--r--src/linker.cpp131
1 files changed, 76 insertions, 55 deletions
diff --git a/src/linker.cpp b/src/linker.cpp
index c0952d0e0..498a96c5f 100644
--- a/src/linker.cpp
+++ b/src/linker.cpp
@@ -139,9 +139,11 @@ gb_internal i32 linker_stage(LinkerData *gen) {
}
- StringSet libs = {};
- string_set_init(&libs, 64);
- defer (string_set_destroy(&libs));
+ StringSet min_libs_set = {};
+ string_set_init(&min_libs_set, 64);
+ defer (string_set_destroy(&min_libs_set));
+
+ String prev_lib = {};
StringSet asm_files = {};
string_set_init(&asm_files, 64);
@@ -149,6 +151,11 @@ gb_internal i32 linker_stage(LinkerData *gen) {
for (Entity *e : gen->foreign_libraries) {
GB_ASSERT(e->kind == Entity_LibraryName);
+ // NOTE(bill): Add these before the linking values
+ String extra_linker_flags = string_trim_whitespace(e->LibraryName.extra_linker_flags);
+ if (extra_linker_flags.len != 0) {
+ lib_str = gb_string_append_fmt(lib_str, " %.*s", LIT(extra_linker_flags));
+ }
for_array(i, e->LibraryName.paths) {
String lib = string_trim_whitespace(e->LibraryName.paths[i]);
// IMPORTANT NOTE(bill): calling `string_to_lower` here is not an issue because
@@ -162,12 +169,11 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (!string_set_update(&asm_files, lib)) {
String asm_file = asm_files.entries[i].value;
String obj_file = concatenate_strings(permanent_allocator(), asm_file, str_lit(".obj"));
- String obj_format;
-#if defined(GB_ARCH_64_BIT)
- obj_format = str_lit("win64");
-#elif defined(GB_ARCH_32_BIT)
+ String obj_format = str_lit("win64");
+ #if defined(GB_ARCH_32_BIT)
obj_format = str_lit("win32");
-#endif // GB_ARCH_*_BIT
+ #endif
+
result = system_exec_command_line_app("nasm",
"\"%.*s\\bin\\nasm\\windows\\nasm.exe\" \"%.*s\" "
"-f \"%.*s\" "
@@ -185,21 +191,16 @@ gb_internal i32 linker_stage(LinkerData *gen) {
}
array_add(&gen->output_object_paths, obj_file);
}
- } else {
- if (!string_set_update(&libs, lib)) {
+ } else if (!string_set_update(&min_libs_set, lib) ||
+ !build_context.min_link_libs) {
+ if (prev_lib != lib) {
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\"", LIT(lib));
}
+ prev_lib = lib;
}
}
}
- for (Entity *e : gen->foreign_libraries) {
- GB_ASSERT(e->kind == Entity_LibraryName);
- if (e->LibraryName.extra_linker_flags.len != 0) {
- lib_str = gb_string_append_fmt(lib_str, " %.*s", LIT(e->LibraryName.extra_linker_flags));
- }
- }
-
if (build_context.build_mode == BuildMode_DynamicLibrary) {
link_settings = gb_string_append_fmt(link_settings, " /DLL");
} else {
@@ -318,12 +319,19 @@ gb_internal i32 linker_stage(LinkerData *gen) {
string_set_init(&asm_files, 64);
defer (string_set_destroy(&asm_files));
- StringSet libs = {};
- string_set_init(&libs, 64);
- defer (string_set_destroy(&libs));
+ StringSet min_libs_set = {};
+ string_set_init(&min_libs_set, 64);
+ defer (string_set_destroy(&min_libs_set));
+
+ String prev_lib = {};
for (Entity *e : gen->foreign_libraries) {
GB_ASSERT(e->kind == Entity_LibraryName);
+ // NOTE(bill): Add these before the linking values
+ String extra_linker_flags = string_trim_whitespace(e->LibraryName.extra_linker_flags);
+ if (extra_linker_flags.len != 0) {
+ lib_str = gb_string_append_fmt(lib_str, " %.*s", LIT(extra_linker_flags));
+ }
for (String lib : e->LibraryName.paths) {
lib = string_trim_whitespace(lib);
if (lib.len == 0) {
@@ -336,19 +344,19 @@ gb_internal i32 linker_stage(LinkerData *gen) {
String asm_file = lib;
String obj_file = concatenate_strings(permanent_allocator(), asm_file, str_lit(".o"));
String obj_format;
-#if defined(GB_ARCH_64_BIT)
+ #if defined(GB_ARCH_64_BIT)
if (is_osx) {
obj_format = str_lit("macho64");
} else {
obj_format = str_lit("elf64");
}
-#elif defined(GB_ARCH_32_BIT)
+ #elif defined(GB_ARCH_32_BIT)
if (is_osx) {
obj_format = str_lit("macho32");
} else {
obj_format = str_lit("elf32");
}
-#endif // GB_ARCH_*_BIT
+ #endif // GB_ARCH_*_BIT
if (is_osx) {
// `as` comes with MacOS.
@@ -376,16 +384,29 @@ gb_internal i32 linker_stage(LinkerData *gen) {
LIT(obj_file),
LIT(build_context.extra_assembler_flags)
);
+ if (result) {
+ gb_printf_err("executing `nasm` to assemble foreing import of %.*s failed.\n\tSuggestion: `nasm` does not ship with the compiler and should be installed with your system's package manager.\n", LIT(asm_file));
+ return result;
+ }
}
array_add(&gen->output_object_paths, obj_file);
} else {
- if (string_set_update(&libs, lib)) {
+ if (string_set_update(&min_libs_set, lib) && build_context.min_link_libs) {
+ continue;
+ }
+
+ if (prev_lib == lib) {
+ continue;
+ }
+ prev_lib = lib;
+
+ // Do not add libc again, this is added later already, and omitted with
+ // the `-no-crt` flag, not skipping here would cause duplicate library
+ // warnings when linking on darwin and might link libc silently even with `-no-crt`.
+ if (lib == str_lit("System.framework") || lib == str_lit("c")) {
continue;
}
- // NOTE(zangent): Sometimes, you have to use -framework on MacOS.
- // This allows you to specify '-f' in a #foreign_system_library,
- // without having to implement any new syntax specifically for MacOS.
if (build_context.metrics.os == TargetOs_darwin) {
if (string_ends_with(lib, str_lit(".framework"))) {
// framework thingie
@@ -425,13 +446,6 @@ gb_internal i32 linker_stage(LinkerData *gen) {
}
}
- for (Entity *e : gen->foreign_libraries) {
- GB_ASSERT(e->kind == Entity_LibraryName);
- if (e->LibraryName.extra_linker_flags.len != 0) {
- lib_str = gb_string_append_fmt(lib_str, " %.*s", LIT(e->LibraryName.extra_linker_flags));
- }
- }
-
gbString object_files = gb_string_make(heap_allocator(), "");
defer (gb_string_free(object_files));
for (String object_path : gen->output_object_paths) {
@@ -474,45 +488,47 @@ gb_internal i32 linker_stage(LinkerData *gen) {
link_settings = gb_string_appendc(link_settings, "-Wl,-fini,'_odin_exit_point' ");
}
- } else if (build_context.metrics.os != TargetOs_openbsd) {
- // OpenBSD defaults to PIE executable. do not pass -no-pie for it.
+ } else if (build_context.metrics.os != TargetOs_openbsd && build_context.metrics.os != TargetOs_haiku) {
+ // OpenBSD and Haiku default to PIE executable. do not pass -no-pie for it.
link_settings = gb_string_appendc(link_settings, "-no-pie ");
}
gbString platform_lib_str = gb_string_make(heap_allocator(), "");
defer (gb_string_free(platform_lib_str));
if (build_context.metrics.os == TargetOs_darwin) {
- platform_lib_str = gb_string_appendc(platform_lib_str, "-Wl,-syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib");
+ platform_lib_str = gb_string_appendc(platform_lib_str, "-Wl,-syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib ");
// Homebrew's default library path, checking if it exists to avoid linking warnings.
if (gb_file_exists("/opt/homebrew/lib")) {
- platform_lib_str = gb_string_appendc(platform_lib_str, " -L/opt/homebrew/lib");
+ platform_lib_str = gb_string_appendc(platform_lib_str, "-L/opt/homebrew/lib ");
}
// MacPort's default library path, checking if it exists to avoid linking warnings.
if (gb_file_exists("/opt/local/lib")) {
- platform_lib_str = gb_string_appendc(platform_lib_str, " -L/opt/local/lib");
+ platform_lib_str = gb_string_appendc(platform_lib_str, "-L/opt/local/lib ");
}
- #if defined(GB_SYSTEM_OSX)
- if(!build_context.no_crt) {
- platform_lib_str = gb_string_appendc(platform_lib_str, " -lm ");
- if(gen->needs_system_library_linked == 1) {
- platform_lib_str = gb_string_appendc(platform_lib_str, " -lSystem ");
- }
+ // Only specify this flag if the user has given a minimum version to target.
+ // This will cause warnings to show up for mismatched libraries.
+ if (build_context.minimum_os_version_string_given) {
+ link_settings = gb_string_append_fmt(link_settings, "-mmacosx-version-min=%.*s ", LIT(build_context.minimum_os_version_string));
+ }
+
+ if (build_context.build_mode != BuildMode_DynamicLibrary) {
+ // This points the linker to where the entry point is
+ link_settings = gb_string_appendc(link_settings, "-e _main ");
}
- #endif
- } else {
- platform_lib_str = gb_string_appendc(platform_lib_str, "-lc -lm");
}
- if (build_context.metrics.os == TargetOs_darwin) {
- // This sets a requirement of Mountain Lion and up, but the compiler doesn't work without this limit.
- if (build_context.minimum_os_version_string.len) {
- link_settings = gb_string_append_fmt(link_settings, " -mmacosx-version-min=%.*s ", LIT(build_context.minimum_os_version_string));
+ if (!build_context.no_crt) {
+ platform_lib_str = gb_string_appendc(platform_lib_str, "-lm ");
+ if (build_context.metrics.os == TargetOs_darwin) {
+ // NOTE: adding this causes a warning about duplicate libraries, I think it is
+ // automatically assumed/added by clang when you don't do `-nostdlib`.
+ // platform_lib_str = gb_string_appendc(platform_lib_str, "-lSystem ");
+ } else {
+ platform_lib_str = gb_string_appendc(platform_lib_str, "-lc ");
}
- // This points the linker to where the entry point is
- link_settings = gb_string_appendc(link_settings, " -e _main ");
}
gbString link_command_line = gb_string_make(heap_allocator(), "clang -Wno-unused-command-line-argument ");
@@ -526,7 +542,12 @@ gb_internal i32 linker_stage(LinkerData *gen) {
link_command_line = gb_string_append_fmt(link_command_line, " %.*s ", LIT(build_context.extra_linker_flags));
link_command_line = gb_string_append_fmt(link_command_line, " %s ", link_settings);
- result = system_exec_command_line_app("ld-link", link_command_line);
+ if (build_context.use_lld) {
+ link_command_line = gb_string_append_fmt(link_command_line, " -fuse-ld=lld");
+ result = system_exec_command_line_app("lld-link", link_command_line);
+ } else {
+ result = system_exec_command_line_app("ld-link", link_command_line);
+ }
if (result) {
return result;