Skip to content

Commit

Permalink
Add -linker:<string> to replace -lld and -radlink
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Nov 14, 2024
1 parent 26f6741 commit b9886df
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 20 deletions.
22 changes: 19 additions & 3 deletions src/build_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ struct BuildCacheData {
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
struct BuildContext {
// Constants
Expand Down Expand Up @@ -419,13 +435,13 @@ struct BuildContext {
bool no_rpath;
bool no_entry_point;
bool no_thread_local;
bool use_lld;
bool use_radlink;
bool cross_compiling;
bool different_os;
bool keep_object_files;
bool disallow_do;

LinkerChoice linker_choice;

StringSet custom_attributes;

bool strict_style;
Expand Down Expand Up @@ -1872,7 +1888,7 @@ gb_internal bool init_build_paths(String init_filename) {
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");
return false;
}
Expand Down
21 changes: 13 additions & 8 deletions src/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ gb_internal i32 linker_stage(LinkerData *gen) {

if (is_windows) {
String section_name = str_lit("msvc-link");
if (build_context.use_lld) {
section_name = str_lit("lld-link");
} else if (build_context.use_radlink) {
section_name = str_lit("rad-link");
switch (build_context.linker_choice) {
case Linker_Default: break;
case Linker_lld: section_name = str_lit("lld-link"); break;
case Linker_radlink: section_name = str_lit("rad-link"); break;
}
timings_start_section(timings, section_name);

Expand Down Expand Up @@ -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]);
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",
"\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%.*s "
Expand All @@ -325,7 +326,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (result) {
return result;
}
} else if (build_context.use_radlink) {
break;
case Linker_radlink:
result = system_exec_command_line_app("msvc-rad-link",
"\"%.*s\\bin\\radlink\" %s -OUT:\"%.*s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:%.*s "
Expand All @@ -344,7 +346,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (result) {
return result;
}
} else { // msvc
break;
default: { // msvc
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]);
defer (gb_free(heap_allocator(), res_path.text));
Expand Down Expand Up @@ -405,6 +408,8 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (result) {
return result;
}
break;
}
}
} else {
timings_start_section(timings, str_lit("ld-link"));
Expand Down Expand Up @@ -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 ", 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");
result = system_exec_command_line_app("lld-link", link_command_line);
} else {
Expand Down
47 changes: 38 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ enum BuildFlagKind {
BuildFlag_NoEntryPoint,
BuildFlag_UseLLD,
BuildFlag_UseRADLink,
BuildFlag_Linker,
BuildFlag_UseSeparateModules,
BuildFlag_NoThreadedChecker,
BuildFlag_ShowDebugMessages,
Expand Down Expand Up @@ -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_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_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_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);
Expand Down Expand Up @@ -1203,19 +1205,38 @@ gb_internal bool parse_build_flags(Array<String> args) {
build_context.no_thread_local = true;
break;
case BuildFlag_UseLLD:
if (build_context.use_radlink) {
gb_printf_err("-lld cannot be used along side -radlink\n");
bad_flags = true;
}
build_context.use_lld = true;
gb_printf_err("Warning: Use of -lld has been deprecated in favour of -linker:lld\n");
build_context.linker_choice = Linker_lld;
break;
case BuildFlag_UseRADLink:
if (build_context.use_lld) {
gb_printf_err("-radlink cannot be used along side -lld\n");
bad_flags = true;
gb_printf_err("Warning: Use of -lld has been deprecated in favour of -linker:radlink\n");
build_context.linker_choice = Linker_radlink;
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;
}
}
build_context.use_radlink = true;
break;


case BuildFlag_UseSeparateModules:
build_context.use_separate_modules = true;
break;
Expand Down Expand Up @@ -2400,6 +2421,14 @@ gb_internal void print_show_help(String const arg0, String const &command) {
}

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(2, "Uses the LLD linker rather than the default.");
print_usage_line(0, "");
Expand Down

0 comments on commit b9886df

Please sign in to comment.