Add -linker:<string> to replace -lld and -radlink

This commit is contained in:
gingerBill
2024-12-01 11:54:54 +11:00
committed by flysand7
parent 6963b0f3f2
commit e58ac3cb8d
3 changed files with 71 additions and 21 deletions
+19 -3
View File
@@ -344,6 +344,22 @@ struct BuildCacheData {
bool copy_already_done; bool copy_already_done;
}; };
enum LinkerChoice : i32 {
Linker_Invalid = -1,
Linker_Default = 0,
Linker_lld,
Linker_radlink,
Linker_COUNT,
};
String linker_choices[Linker_COUNT] = {
str_lit("default"),
str_lit("lld"),
str_lit("radlink"),
};
// This stores the information for the specify architecture of this build // This stores the information for the specify architecture of this build
struct BuildContext { struct BuildContext {
// Constants // Constants
@@ -419,13 +435,13 @@ struct BuildContext {
bool no_rpath; bool no_rpath;
bool no_entry_point; bool no_entry_point;
bool no_thread_local; bool no_thread_local;
bool use_lld;
bool use_radlink;
bool cross_compiling; bool cross_compiling;
bool different_os; bool different_os;
bool keep_object_files; bool keep_object_files;
bool disallow_do; bool disallow_do;
LinkerChoice linker_choice;
StringSet custom_attributes; StringSet custom_attributes;
bool strict_style; bool strict_style;
@@ -1872,7 +1888,7 @@ gb_internal bool init_build_paths(String init_filename) {
return false; return false;
} }
if (!build_context.use_lld && !build_context.use_radlink && find_result.vs_exe_path.len == 0) { if (build_context.linker_choice != Linker_Default && find_result.vs_exe_path.len == 0) {
gb_printf_err("link.exe not found.\n"); gb_printf_err("link.exe not found.\n");
return false; return false;
} }
+13 -8
View File
@@ -167,10 +167,10 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (is_windows) { if (is_windows) {
String section_name = str_lit("msvc-link"); String section_name = str_lit("msvc-link");
if (build_context.use_lld) { switch (build_context.linker_choice) {
section_name = str_lit("lld-link"); case Linker_Default: break;
} else if (build_context.use_radlink) { case Linker_lld: section_name = str_lit("lld-link"); break;
section_name = str_lit("rad-link"); case Linker_radlink: section_name = str_lit("rad-link"); break;
} }
timings_start_section(timings, section_name); timings_start_section(timings, section_name);
@@ -306,7 +306,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
String windows_sdk_bin_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Win_SDK_Bin_Path]); String windows_sdk_bin_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Win_SDK_Bin_Path]);
defer (gb_free(heap_allocator(), windows_sdk_bin_path.text)); defer (gb_free(heap_allocator(), windows_sdk_bin_path.text));
if (build_context.use_lld) { // lld switch (build_context.linker_choice) {
case Linker_lld:
result = system_exec_command_line_app("msvc-lld-link", result = system_exec_command_line_app("msvc-lld-link",
"\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s " "\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%.*s " "/nologo /incremental:no /opt:ref /subsystem:%.*s "
@@ -325,7 +326,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (result) { if (result) {
return result; return result;
} }
} else if (build_context.use_radlink) { break;
case Linker_radlink:
result = system_exec_command_line_app("msvc-rad-link", result = system_exec_command_line_app("msvc-rad-link",
"\"%.*s\\bin\\radlink\" %s -OUT:\"%.*s\" %s " "\"%.*s\\bin\\radlink\" %s -OUT:\"%.*s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%.*s " "/nologo /incremental:no /opt:ref /subsystem:%.*s "
@@ -344,7 +346,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (result) { if (result) {
return result; return result;
} }
} else { // msvc break;
default: { // msvc
String res_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RES]); String res_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RES]);
String rc_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RC]); String rc_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RC]);
defer (gb_free(heap_allocator(), res_path.text)); defer (gb_free(heap_allocator(), res_path.text));
@@ -405,6 +408,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (result) { if (result) {
return result; return result;
} }
break;
}
} }
} else { } else {
timings_start_section(timings, str_lit("ld-link")); timings_start_section(timings, str_lit("ld-link"));
@@ -700,7 +705,7 @@ 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 ", LIT(build_context.extra_linker_flags));
link_command_line = gb_string_append_fmt(link_command_line, " %s ", link_settings); link_command_line = gb_string_append_fmt(link_command_line, " %s ", link_settings);
if (build_context.use_lld) { if (build_context.linker_choice == Linker_lld) {
link_command_line = gb_string_append_fmt(link_command_line, " -fuse-ld=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); result = system_exec_command_line_app("lld-link", link_command_line);
} else { } else {
+39 -10
View File
@@ -329,6 +329,7 @@ enum BuildFlagKind {
BuildFlag_NoEntryPoint, BuildFlag_NoEntryPoint,
BuildFlag_UseLLD, BuildFlag_UseLLD,
BuildFlag_UseRADLink, BuildFlag_UseRADLink,
BuildFlag_Linker,
BuildFlag_UseSeparateModules, BuildFlag_UseSeparateModules,
BuildFlag_NoThreadedChecker, BuildFlag_NoThreadedChecker,
BuildFlag_ShowDebugMessages, BuildFlag_ShowDebugMessages,
@@ -541,6 +542,7 @@ gb_internal bool parse_build_flags(Array<String> args) {
add_flag(&build_flags, BuildFlag_NoEntryPoint, str_lit("no-entry-point"), BuildFlagParam_None, Command__does_check &~ Command_test); add_flag(&build_flags, BuildFlag_NoEntryPoint, str_lit("no-entry-point"), BuildFlagParam_None, Command__does_check &~ Command_test);
add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None, Command__does_build);
add_flag(&build_flags, BuildFlag_UseRADLink, str_lit("radlink"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_UseRADLink, str_lit("radlink"), BuildFlagParam_None, Command__does_build);
add_flag(&build_flags, BuildFlag_Linker, str_lit("Linker"), BuildFlagParam_String, Command__does_build);
add_flag(&build_flags, BuildFlag_UseSeparateModules, str_lit("use-separate-modules"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_UseSeparateModules, str_lit("use-separate-modules"), BuildFlagParam_None, Command__does_build);
add_flag(&build_flags, BuildFlag_NoThreadedChecker, str_lit("no-threaded-checker"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_NoThreadedChecker, str_lit("no-threaded-checker"), BuildFlagParam_None, Command__does_check);
add_flag(&build_flags, BuildFlag_ShowDebugMessages, str_lit("show-debug-messages"), BuildFlagParam_None, Command_all); add_flag(&build_flags, BuildFlag_ShowDebugMessages, str_lit("show-debug-messages"), BuildFlagParam_None, Command_all);
@@ -1203,19 +1205,38 @@ gb_internal bool parse_build_flags(Array<String> args) {
build_context.no_thread_local = true; build_context.no_thread_local = true;
break; break;
case BuildFlag_UseLLD: case BuildFlag_UseLLD:
if (build_context.use_radlink) { gb_printf_err("Warning: Use of -lld has been deprecated in favour of -linker:lld\n");
gb_printf_err("-lld cannot be used along side -radlink\n"); build_context.linker_choice = Linker_lld;
bad_flags = true;
}
build_context.use_lld = true;
break; break;
case BuildFlag_UseRADLink: case BuildFlag_UseRADLink:
if (build_context.use_lld) { gb_printf_err("Warning: Use of -lld has been deprecated in favour of -linker:radlink\n");
gb_printf_err("-radlink cannot be used along side -lld\n"); build_context.linker_choice = Linker_radlink;
bad_flags = true;
}
build_context.use_radlink = true;
break; break;
case BuildFlag_Linker:
{
GB_ASSERT(value.kind == ExactValue_String);
LinkerChoice linker_choice = Linker_Invalid;
for (i32 i = 0; i < Linker_COUNT; i++) {
if (linker_choices[i] == value.value_string) {
linker_choice = cast(LinkerChoice)i;
break;
}
}
if (linker_choice == Linker_Invalid) {
gb_printf_err("Invalid option for -linker:<string>. Expected one of the following\n");
for (i32 i = 0; i < Linker_COUNT; i++) {
gb_printf_err("\t%.*s\n", LIT(linker_choices[i]));
}
bad_flags = true;
} else {
build_context.linker_choice = linker_choice;
}
}
break;
case BuildFlag_UseSeparateModules: case BuildFlag_UseSeparateModules:
build_context.use_separate_modules = true; build_context.use_separate_modules = true;
break; break;
@@ -2400,6 +2421,14 @@ gb_internal void print_show_help(String const arg0, String const &command) {
} }
if (run_or_build) { if (run_or_build) {
print_usage_line(1, "-linker:<string>");
print_usage_line(2, "Specify the linker to use.");
print_usage_line(2, "Choices:");
for (i32 i = 0; i < Linker_COUNT; i++) {
print_usage_line(3, "%.*s", LIT(linker_choices[i]));
}
print_usage_line(0, "");
print_usage_line(1, "-lld"); print_usage_line(1, "-lld");
print_usage_line(2, "Uses the LLD linker rather than the default."); print_usage_line(2, "Uses the LLD linker rather than the default.");
print_usage_line(0, ""); print_usage_line(0, "");