mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-06 13:07:59 +00:00
Make the link order of foreign imports deterministic
This commit is contained in:
592
src/main.cpp
592
src/main.cpp
@@ -164,341 +164,357 @@ i32 linker_stage(lbGenerator *gen) {
|
||||
build_context.keep_object_files = true;
|
||||
} else {
|
||||
#if defined(GB_SYSTEM_WINDOWS)
|
||||
String section_name = str_lit("msvc-link");
|
||||
if (build_context.use_lld) {
|
||||
section_name = str_lit("lld-link");
|
||||
}
|
||||
timings_start_section(timings, section_name);
|
||||
|
||||
gbString lib_str = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(lib_str));
|
||||
|
||||
gbString link_settings = gb_string_make_reserve(heap_allocator(), 256);
|
||||
defer (gb_string_free(link_settings));
|
||||
|
||||
// Add library search paths.
|
||||
if (build_context.build_paths[BuildPath_VS_LIB].basename.len > 0) {
|
||||
String path = {};
|
||||
auto add_path = [&](String path) {
|
||||
if (path[path.len-1] == '\\') {
|
||||
path.len -= 1;
|
||||
}
|
||||
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%.*s\"", LIT(path));
|
||||
};
|
||||
add_path(build_context.build_paths[BuildPath_Win_SDK_UM_Lib].basename);
|
||||
add_path(build_context.build_paths[BuildPath_Win_SDK_UCRT_Lib].basename);
|
||||
add_path(build_context.build_paths[BuildPath_VS_LIB].basename);
|
||||
}
|
||||
bool is_windows = true;
|
||||
#else
|
||||
bool is_windows = false;
|
||||
#endif
|
||||
#if defined(GB_SYSTEM_OSX)
|
||||
bool is_osx = true;
|
||||
#else
|
||||
bool is_osx = false;
|
||||
#endif
|
||||
|
||||
|
||||
StringSet libs = {};
|
||||
string_set_init(&libs, heap_allocator(), 64);
|
||||
defer (string_set_destroy(&libs));
|
||||
if (is_windows) {
|
||||
String section_name = str_lit("msvc-link");
|
||||
if (build_context.use_lld) {
|
||||
section_name = str_lit("lld-link");
|
||||
}
|
||||
timings_start_section(timings, section_name);
|
||||
|
||||
StringSet asm_files = {};
|
||||
string_set_init(&asm_files, heap_allocator(), 64);
|
||||
defer (string_set_destroy(&asm_files));
|
||||
gbString lib_str = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(lib_str));
|
||||
|
||||
for_array(j, gen->modules.entries) {
|
||||
lbModule *m = gen->modules.entries[j].value;
|
||||
for_array(i, m->foreign_library_paths) {
|
||||
String lib = m->foreign_library_paths[i];
|
||||
if (has_asm_extension(lib)) {
|
||||
string_set_add(&asm_files, lib);
|
||||
} else {
|
||||
string_set_add(&libs, lib);
|
||||
gbString link_settings = gb_string_make_reserve(heap_allocator(), 256);
|
||||
defer (gb_string_free(link_settings));
|
||||
|
||||
// Add library search paths.
|
||||
if (build_context.build_paths[BuildPath_VS_LIB].basename.len > 0) {
|
||||
String path = {};
|
||||
auto add_path = [&](String path) {
|
||||
if (path[path.len-1] == '\\') {
|
||||
path.len -= 1;
|
||||
}
|
||||
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%.*s\"", LIT(path));
|
||||
};
|
||||
add_path(build_context.build_paths[BuildPath_Win_SDK_UM_Lib].basename);
|
||||
add_path(build_context.build_paths[BuildPath_Win_SDK_UCRT_Lib].basename);
|
||||
add_path(build_context.build_paths[BuildPath_VS_LIB].basename);
|
||||
}
|
||||
|
||||
|
||||
StringSet libs = {};
|
||||
string_set_init(&libs, heap_allocator(), 64);
|
||||
defer (string_set_destroy(&libs));
|
||||
|
||||
StringSet asm_files = {};
|
||||
string_set_init(&asm_files, heap_allocator(), 64);
|
||||
defer (string_set_destroy(&asm_files));
|
||||
|
||||
for_array(j, gen->foreign_libraries) {
|
||||
Entity *e = gen->foreign_libraries[j];
|
||||
GB_ASSERT(e->kind == Entity_LibraryName);
|
||||
for_array(i, e->LibraryName.paths) {
|
||||
String lib = string_trim_whitespace(e->LibraryName.paths[i]);
|
||||
if (lib.len == 0) {
|
||||
continue;
|
||||
}
|
||||
// IMPORTANT NOTE(bill): calling `string_to_lower` here is not an issue because
|
||||
// we will never uses these strings afterwards
|
||||
string_to_lower(&lib);
|
||||
if (has_asm_extension(lib)) {
|
||||
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"));
|
||||
|
||||
result = system_exec_command_line_app("nasm",
|
||||
"\"%.*s\\bin\\nasm\\windows\\nasm.exe\" \"%.*s\" "
|
||||
"-f win64 "
|
||||
"-o \"%.*s\" "
|
||||
"%.*s "
|
||||
"",
|
||||
LIT(build_context.ODIN_ROOT), LIT(asm_file),
|
||||
LIT(obj_file),
|
||||
LIT(build_context.extra_assembler_flags)
|
||||
);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
array_add(&gen->output_object_paths, obj_file);
|
||||
}
|
||||
} else {
|
||||
if (!string_set_update(&libs, lib)) {
|
||||
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\"", LIT(lib));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for_array(i, gen->default_module.foreign_library_paths) {
|
||||
String lib = gen->default_module.foreign_library_paths[i];
|
||||
if (has_asm_extension(lib)) {
|
||||
string_set_add(&asm_files, lib);
|
||||
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /DLL");
|
||||
} else {
|
||||
string_set_add(&libs, lib);
|
||||
link_settings = gb_string_append_fmt(link_settings, " /ENTRY:mainCRTStartup");
|
||||
}
|
||||
}
|
||||
|
||||
for_array(i, libs.entries) {
|
||||
String lib = libs.entries[i].value;
|
||||
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\"", LIT(lib));
|
||||
}
|
||||
|
||||
|
||||
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /DLL");
|
||||
} else {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /ENTRY:mainCRTStartup");
|
||||
}
|
||||
|
||||
if (build_context.pdb_filepath != "") {
|
||||
String pdb_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_PDB]);
|
||||
link_settings = gb_string_append_fmt(link_settings, " /PDB:%.*s", LIT(pdb_path));
|
||||
}
|
||||
|
||||
if (build_context.no_crt) {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /nodefaultlib");
|
||||
} else {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /defaultlib:libcmt");
|
||||
}
|
||||
|
||||
if (build_context.ODIN_DEBUG) {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /DEBUG");
|
||||
}
|
||||
|
||||
for_array(i, asm_files.entries) {
|
||||
String asm_file = asm_files.entries[i].value;
|
||||
String obj_file = concatenate_strings(permanent_allocator(), asm_file, str_lit(".obj"));
|
||||
|
||||
result = system_exec_command_line_app("nasm",
|
||||
"\"%.*s\\bin\\nasm\\windows\\nasm.exe\" \"%.*s\" "
|
||||
"-f win64 "
|
||||
"-o \"%.*s\" "
|
||||
"%.*s "
|
||||
"",
|
||||
LIT(build_context.ODIN_ROOT), LIT(asm_file),
|
||||
LIT(obj_file),
|
||||
LIT(build_context.extra_assembler_flags)
|
||||
);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
if (build_context.pdb_filepath != "") {
|
||||
String pdb_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_PDB]);
|
||||
link_settings = gb_string_append_fmt(link_settings, " /PDB:%.*s", LIT(pdb_path));
|
||||
}
|
||||
array_add(&gen->output_object_paths, obj_file);
|
||||
}
|
||||
|
||||
gbString object_files = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(object_files));
|
||||
for_array(i, gen->output_object_paths) {
|
||||
String object_path = gen->output_object_paths[i];
|
||||
object_files = gb_string_append_fmt(object_files, "\"%.*s\" ", LIT(object_path));
|
||||
}
|
||||
if (build_context.no_crt) {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /nodefaultlib");
|
||||
} else {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /defaultlib:libcmt");
|
||||
}
|
||||
|
||||
String vs_exe_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_VS_EXE]);
|
||||
defer (gb_free(heap_allocator(), vs_exe_path.text));
|
||||
if (build_context.ODIN_DEBUG) {
|
||||
link_settings = gb_string_append_fmt(link_settings, " /DEBUG");
|
||||
}
|
||||
|
||||
char const *subsystem_str = build_context.use_subsystem_windows ? "WINDOWS" : "CONSOLE";
|
||||
if (!build_context.use_lld) { // msvc
|
||||
if (build_context.has_resource) {
|
||||
String rc_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]);
|
||||
String res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]);
|
||||
defer (gb_free(heap_allocator(), rc_path.text));
|
||||
defer (gb_free(heap_allocator(), res_path.text));
|
||||
gbString object_files = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(object_files));
|
||||
for_array(i, gen->output_object_paths) {
|
||||
String object_path = gen->output_object_paths[i];
|
||||
object_files = gb_string_append_fmt(object_files, "\"%.*s\" ", LIT(object_path));
|
||||
}
|
||||
|
||||
result = system_exec_command_line_app("msvc-link",
|
||||
"\"rc.exe\" /nologo /fo \"%.*s\" \"%.*s\"",
|
||||
LIT(res_path),
|
||||
LIT(rc_path)
|
||||
);
|
||||
String vs_exe_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_VS_EXE]);
|
||||
defer (gb_free(heap_allocator(), vs_exe_path.text));
|
||||
|
||||
char const *subsystem_str = build_context.use_subsystem_windows ? "WINDOWS" : "CONSOLE";
|
||||
if (!build_context.use_lld) { // msvc
|
||||
if (build_context.has_resource) {
|
||||
String rc_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]);
|
||||
String res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]);
|
||||
defer (gb_free(heap_allocator(), rc_path.text));
|
||||
defer (gb_free(heap_allocator(), res_path.text));
|
||||
|
||||
result = system_exec_command_line_app("msvc-link",
|
||||
"\"rc.exe\" /nologo /fo \"%.*s\" \"%.*s\"",
|
||||
LIT(res_path),
|
||||
LIT(rc_path)
|
||||
);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = system_exec_command_line_app("msvc-link",
|
||||
"\"%.*slink.exe\" %s \"%.*s\" -OUT:\"%.*s\" %s "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:%s "
|
||||
" %.*s "
|
||||
" %.*s "
|
||||
" %s "
|
||||
"",
|
||||
LIT(vs_exe_path), object_files, LIT(res_path), LIT(output_filename),
|
||||
link_settings,
|
||||
subsystem_str,
|
||||
LIT(build_context.link_flags),
|
||||
LIT(build_context.extra_linker_flags),
|
||||
lib_str
|
||||
);
|
||||
} else {
|
||||
result = system_exec_command_line_app("msvc-link",
|
||||
"\"%.*slink.exe\" %s -OUT:\"%.*s\" %s "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:%s "
|
||||
" %.*s "
|
||||
" %.*s "
|
||||
" %s "
|
||||
"",
|
||||
LIT(vs_exe_path), object_files, LIT(output_filename),
|
||||
link_settings,
|
||||
subsystem_str,
|
||||
LIT(build_context.link_flags),
|
||||
LIT(build_context.extra_linker_flags),
|
||||
lib_str
|
||||
);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = system_exec_command_line_app("msvc-link",
|
||||
"\"%.*slink.exe\" %s \"%.*s\" -OUT:\"%.*s\" %s "
|
||||
} else { // lld
|
||||
result = system_exec_command_line_app("msvc-lld-link",
|
||||
"\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:%s "
|
||||
" %.*s "
|
||||
" %.*s "
|
||||
" %s "
|
||||
"",
|
||||
LIT(vs_exe_path), object_files, LIT(res_path), LIT(output_filename),
|
||||
link_settings,
|
||||
subsystem_str,
|
||||
LIT(build_context.link_flags),
|
||||
LIT(build_context.extra_linker_flags),
|
||||
lib_str
|
||||
);
|
||||
} else {
|
||||
result = system_exec_command_line_app("msvc-link",
|
||||
"\"%.*slink.exe\" %s -OUT:\"%.*s\" %s "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:%s "
|
||||
" %.*s "
|
||||
" %.*s "
|
||||
" %s "
|
||||
"",
|
||||
LIT(vs_exe_path), object_files, LIT(output_filename),
|
||||
LIT(build_context.ODIN_ROOT), object_files, LIT(output_filename),
|
||||
link_settings,
|
||||
subsystem_str,
|
||||
LIT(build_context.link_flags),
|
||||
LIT(build_context.extra_linker_flags),
|
||||
lib_str
|
||||
);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
} else { // lld
|
||||
result = system_exec_command_line_app("msvc-lld-link",
|
||||
"\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s "
|
||||
"/nologo /incremental:no /opt:ref /subsystem:%s "
|
||||
" %.*s "
|
||||
" %.*s "
|
||||
" %s "
|
||||
"",
|
||||
LIT(build_context.ODIN_ROOT), object_files, LIT(output_filename),
|
||||
link_settings,
|
||||
subsystem_str,
|
||||
LIT(build_context.link_flags),
|
||||
LIT(build_context.extra_linker_flags),
|
||||
lib_str
|
||||
);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#else
|
||||
timings_start_section(timings, str_lit("ld-link"));
|
||||
|
||||
// NOTE(vassvik): get cwd, for used for local shared libs linking, since those have to be relative to the exe
|
||||
char cwd[256];
|
||||
getcwd(&cwd[0], 256);
|
||||
//printf("%s\n", cwd);
|
||||
|
||||
// NOTE(vassvik): needs to add the root to the library search paths, so that the full filenames of the library
|
||||
// files can be passed with -l:
|
||||
gbString lib_str = gb_string_make(heap_allocator(), "-L/");
|
||||
defer (gb_string_free(lib_str));
|
||||
|
||||
for_array(i, gen->default_module.foreign_library_paths) {
|
||||
String lib = gen->default_module.foreign_library_paths[i];
|
||||
|
||||
// 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
|
||||
String lib_name = lib;
|
||||
lib_name = remove_extension_from_path(lib_name);
|
||||
lib_str = gb_string_append_fmt(lib_str, " -framework %.*s ", LIT(lib_name));
|
||||
} else if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o")) || string_ends_with(lib, str_lit(".dylib"))) {
|
||||
// For:
|
||||
// object
|
||||
// dynamic lib
|
||||
// static libs, absolute full path relative to the file in which the lib was imported from
|
||||
lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
|
||||
} else {
|
||||
// dynamic or static system lib, just link regularly searching system library paths
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
|
||||
}
|
||||
} else {
|
||||
// NOTE(vassvik): static libraries (.a files) in linux can be linked to directly using the full path,
|
||||
// since those are statically linked to at link time. shared libraries (.so) has to be
|
||||
// available at runtime wherever the executable is run, so we make require those to be
|
||||
// local to the executable (unless the system collection is used, in which case we search
|
||||
// the system library paths for the library file).
|
||||
if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o"))) {
|
||||
// static libs and object files, absolute full path relative to the file in which the lib was imported from
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l:\"%.*s\" ", LIT(lib));
|
||||
} else if (string_ends_with(lib, str_lit(".so"))) {
|
||||
// dynamic lib, relative path to executable
|
||||
// NOTE(vassvik): it is the user's responsibility to make sure the shared library files are visible
|
||||
// at runtime to the executable
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l:\"%s/%.*s\" ", cwd, LIT(lib));
|
||||
} else {
|
||||
// dynamic or static system lib, just link regularly searching system library paths
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gbString object_files = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(object_files));
|
||||
for_array(i, gen->output_object_paths) {
|
||||
String object_path = gen->output_object_paths[i];
|
||||
object_files = gb_string_append_fmt(object_files, "\"%.*s\" ", LIT(object_path));
|
||||
}
|
||||
|
||||
gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
|
||||
|
||||
if (build_context.no_crt) {
|
||||
link_settings = gb_string_append_fmt(link_settings, "-nostdlib ");
|
||||
}
|
||||
|
||||
// NOTE(dweiler): We use clang as a frontend for the linker as there are
|
||||
// other runtime and compiler support libraries that need to be linked in
|
||||
// very specific orders such as libgcc_s, ld-linux-so, unwind, etc.
|
||||
// These are not always typically inside /lib, /lib64, or /usr versions
|
||||
// of that, e.g libgcc.a is in /usr/lib/gcc/{version}, and can vary on
|
||||
// the distribution of Linux even. The gcc or clang specs is the only
|
||||
// reliable way to query this information to call ld directly.
|
||||
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
||||
// NOTE(dweiler): Let the frontend know we're building a shared library
|
||||
// so it doesn't generate symbols which cannot be relocated.
|
||||
link_settings = gb_string_appendc(link_settings, "-shared ");
|
||||
|
||||
// NOTE(dweiler): _odin_entry_point must be called at initialization
|
||||
// time of the shared object, similarly, _odin_exit_point must be called
|
||||
// at deinitialization. We can pass both -init and -fini to the linker by
|
||||
// using a comma separated list of arguments to -Wl.
|
||||
//
|
||||
// This previously used ld but ld cannot actually build a shared library
|
||||
// correctly this way since all the other dependencies provided implicitly
|
||||
// by the compiler frontend are still needed and most of the command
|
||||
// line arguments prepared previously are incompatible with ld.
|
||||
link_settings = gb_string_appendc(link_settings, "-Wl,-init,'_odin_entry_point' ");
|
||||
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.
|
||||
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, "-lSystem -lm -Wl,-syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib");
|
||||
} else {
|
||||
platform_lib_str = gb_string_appendc(platform_lib_str, "-lc -lm");
|
||||
}
|
||||
timings_start_section(timings, str_lit("ld-link"));
|
||||
|
||||
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.
|
||||
// NOTE: If you change this (although this minimum is as low as you can go with Odin working)
|
||||
// make sure to also change the 'mtriple' param passed to 'opt'
|
||||
if (build_context.metrics.arch == TargetArch_arm64) {
|
||||
link_settings = gb_string_appendc(link_settings, " -mmacosx-version-min=12.0.0 ");
|
||||
} else {
|
||||
link_settings = gb_string_appendc(link_settings, " -mmacosx-version-min=10.8.0 ");
|
||||
// NOTE(vassvik): get cwd, for used for local shared libs linking, since those have to be relative to the exe
|
||||
char cwd[256];
|
||||
#if !defined(GB_SYSTEM_WINDOWS)
|
||||
getcwd(&cwd[0], 256);
|
||||
#endif
|
||||
//printf("%s\n", cwd);
|
||||
|
||||
// NOTE(vassvik): needs to add the root to the library search paths, so that the full filenames of the library
|
||||
// files can be passed with -l:
|
||||
gbString lib_str = gb_string_make(heap_allocator(), "-L/");
|
||||
defer (gb_string_free(lib_str));
|
||||
|
||||
StringSet libs = {};
|
||||
string_set_init(&libs, heap_allocator(), 64);
|
||||
defer (string_set_destroy(&libs));
|
||||
|
||||
for_array(j, gen->foreign_libraries) {
|
||||
Entity *e = gen->foreign_libraries[j];
|
||||
GB_ASSERT(e->kind == Entity_LibraryName);
|
||||
for_array(i, e->LibraryName.paths) {
|
||||
String lib = e->LibraryName.paths[i];
|
||||
if (string_set_update(&libs, lib)) {
|
||||
continue;
|
||||
}
|
||||
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\"", LIT(lib));
|
||||
|
||||
// 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
|
||||
String lib_name = lib;
|
||||
lib_name = remove_extension_from_path(lib_name);
|
||||
lib_str = gb_string_append_fmt(lib_str, " -framework %.*s ", LIT(lib_name));
|
||||
} else if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o")) || string_ends_with(lib, str_lit(".dylib"))) {
|
||||
// For:
|
||||
// object
|
||||
// dynamic lib
|
||||
// static libs, absolute full path relative to the file in which the lib was imported from
|
||||
lib_str = gb_string_append_fmt(lib_str, " %.*s ", LIT(lib));
|
||||
} else {
|
||||
// dynamic or static system lib, just link regularly searching system library paths
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
|
||||
}
|
||||
} else {
|
||||
// NOTE(vassvik): static libraries (.a files) in linux can be linked to directly using the full path,
|
||||
// since those are statically linked to at link time. shared libraries (.so) has to be
|
||||
// available at runtime wherever the executable is run, so we make require those to be
|
||||
// local to the executable (unless the system collection is used, in which case we search
|
||||
// the system library paths for the library file).
|
||||
if (string_ends_with(lib, str_lit(".a")) || string_ends_with(lib, str_lit(".o"))) {
|
||||
// static libs and object files, absolute full path relative to the file in which the lib was imported from
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l:\"%.*s\" ", LIT(lib));
|
||||
} else if (string_ends_with(lib, str_lit(".so"))) {
|
||||
// dynamic lib, relative path to executable
|
||||
// NOTE(vassvik): it is the user's responsibility to make sure the shared library files are visible
|
||||
// at runtime to the executable
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l:\"%s/%.*s\" ", cwd, LIT(lib));
|
||||
} else {
|
||||
// dynamic or static system lib, just link regularly searching system library paths
|
||||
lib_str = gb_string_append_fmt(lib_str, " -l%.*s ", LIT(lib));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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 ");
|
||||
defer (gb_string_free(link_command_line));
|
||||
|
||||
link_command_line = gb_string_appendc(link_command_line, object_files);
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " -o \"%.*s\" ", LIT(output_filename));
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " %s ", platform_lib_str);
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " %s ", lib_str);
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " %.*s ", LIT(build_context.link_flags));
|
||||
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);
|
||||
gbString object_files = gb_string_make(heap_allocator(), "");
|
||||
defer (gb_string_free(object_files));
|
||||
for_array(i, gen->output_object_paths) {
|
||||
String object_path = gen->output_object_paths[i];
|
||||
object_files = gb_string_append_fmt(object_files, "\"%.*s\" ", LIT(object_path));
|
||||
}
|
||||
|
||||
result = system_exec_command_line_app("ld-link", link_command_line);
|
||||
gbString link_settings = gb_string_make_reserve(heap_allocator(), 32);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
if (build_context.no_crt) {
|
||||
link_settings = gb_string_append_fmt(link_settings, "-nostdlib ");
|
||||
}
|
||||
|
||||
#if defined(GB_SYSTEM_OSX)
|
||||
if (build_context.ODIN_DEBUG) {
|
||||
// NOTE: macOS links DWARF symbols dynamically. Dsymutil will map the stubs in the exe
|
||||
// to the symbols in the object file
|
||||
result = system_exec_command_line_app("dsymutil", "dsymutil %.*s", LIT(output_filename));
|
||||
// NOTE(dweiler): We use clang as a frontend for the linker as there are
|
||||
// other runtime and compiler support libraries that need to be linked in
|
||||
// very specific orders such as libgcc_s, ld-linux-so, unwind, etc.
|
||||
// These are not always typically inside /lib, /lib64, or /usr versions
|
||||
// of that, e.g libgcc.a is in /usr/lib/gcc/{version}, and can vary on
|
||||
// the distribution of Linux even. The gcc or clang specs is the only
|
||||
// reliable way to query this information to call ld directly.
|
||||
if (build_context.build_mode == BuildMode_DynamicLibrary) {
|
||||
// NOTE(dweiler): Let the frontend know we're building a shared library
|
||||
// so it doesn't generate symbols which cannot be relocated.
|
||||
link_settings = gb_string_appendc(link_settings, "-shared ");
|
||||
|
||||
// NOTE(dweiler): _odin_entry_point must be called at initialization
|
||||
// time of the shared object, similarly, _odin_exit_point must be called
|
||||
// at deinitialization. We can pass both -init and -fini to the linker by
|
||||
// using a comma separated list of arguments to -Wl.
|
||||
//
|
||||
// This previously used ld but ld cannot actually build a shared library
|
||||
// correctly this way since all the other dependencies provided implicitly
|
||||
// by the compiler frontend are still needed and most of the command
|
||||
// line arguments prepared previously are incompatible with ld.
|
||||
link_settings = gb_string_appendc(link_settings, "-Wl,-init,'_odin_entry_point' ");
|
||||
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.
|
||||
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, "-lSystem -lm -Wl,-syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib");
|
||||
} 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.
|
||||
// NOTE: If you change this (although this minimum is as low as you can go with Odin working)
|
||||
// make sure to also change the 'mtriple' param passed to 'opt'
|
||||
if (build_context.metrics.arch == TargetArch_arm64) {
|
||||
link_settings = gb_string_appendc(link_settings, " -mmacosx-version-min=12.0.0 ");
|
||||
} else {
|
||||
link_settings = gb_string_appendc(link_settings, " -mmacosx-version-min=10.8.0 ");
|
||||
}
|
||||
// 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 ");
|
||||
defer (gb_string_free(link_command_line));
|
||||
|
||||
link_command_line = gb_string_appendc(link_command_line, object_files);
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " -o \"%.*s\" ", LIT(output_filename));
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " %s ", platform_lib_str);
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " %s ", lib_str);
|
||||
link_command_line = gb_string_append_fmt(link_command_line, " %.*s ", LIT(build_context.link_flags));
|
||||
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 (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
if (is_osx && build_context.ODIN_DEBUG) {
|
||||
// NOTE: macOS links DWARF symbols dynamically. Dsymutil will map the stubs in the exe
|
||||
// to the symbols in the object file
|
||||
result = system_exec_command_line_app("dsymutil", "dsymutil %.*s", LIT(output_filename));
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user