Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static on macosx #4332

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions src/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,6 @@ gb_internal i32 linker_stage(LinkerData *gen) {
link_settings = gb_string_append_fmt(link_settings, "-nostdlib ");
}

if (build_context.build_mode == BuildMode_StaticLibrary) {
compiler_error("TODO(bill): -build-mode:static on non-windows targets");
}

// 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.
Expand Down Expand Up @@ -631,7 +627,7 @@ gb_internal i32 linker_stage(LinkerData *gen) {
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) {
if (build_context.build_mode != BuildMode_DynamicLibrary && build_context.build_mode != BuildMode_StaticLibrary) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look needed? link_settings isn't used when you do a static library from what I can tell.

// This points the linker to where the entry point is
link_settings = gb_string_appendc(link_settings, "-e _main ");
}
Expand All @@ -657,39 +653,56 @@ gb_internal i32 linker_stage(LinkerData *gen) {
platform_lib_str = gb_string_appendc(platform_lib_str, "-lc ");
}
}


gbString link_command_line = gb_string_make(heap_allocator(), clang_path);
defer (gb_string_free(link_command_line));

link_command_line = gb_string_appendc(link_command_line, " -Wno-unused-command-line-argument ");
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);

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);
if (build_context.build_mode == BuildMode_StaticLibrary) {
if (is_osx) {
gbString static_link_command_line = gb_string_make(heap_allocator(), "libtool");
defer (gb_string_free(static_link_command_line));
static_link_command_line = gb_string_append_fmt(static_link_command_line, " -o \"%.*s\" ", LIT(output_filename));
static_link_command_line = gb_string_appendc(static_link_command_line, " -static ");
static_link_command_line = gb_string_appendc(static_link_command_line, object_files);
return system_exec_command_line_app("libtool", static_link_command_line);
} else {
compiler_error("-build-mode:static on Linux and similar targets");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
compiler_error("-build-mode:static on Linux and similar targets");
compiler_error("TODO: -build-mode:static on this target");

}
} else {
result = system_exec_command_line_app("ld-link", link_command_line);
}

if (result) {
return result;
}

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));
gbString link_command_line = gb_string_make(heap_allocator(), clang_path);
defer (gb_string_free(link_command_line));

link_command_line = gb_string_appendc(link_command_line, " -Wno-unused-command-line-argument ");
link_command_line = gb_string_append_fmt(link_command_line, " -o \"%.*s\" ", LIT(output_filename));

link_command_line = gb_string_appendc(link_command_line, object_files);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you move the object files from before the -o to after it?

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);

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;
}

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;
}
}

}

}
}

Expand Down