Skip to content

remove -L/ and -l: from linker flags #4719

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/gb/gb.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ extern "C" {
#include <semaphore.h>
#endif

#if defined(GB_SYSTEM_LINUX)
#include <grp.h>
#include <pwd.h>
#endif


////////////////////////////////////////////////////////////////
//
Expand Down
24 changes: 23 additions & 1 deletion src/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,23 @@ try_cross_linking:;
#if !defined(GB_SYSTEM_WINDOWS)
lib_str = gb_string_appendc(lib_str, "-L/ ");
#endif

// Check if Odin is called inside Nix build environment.
bool nix_build = false;
#if defined(GB_SYSTEM_LINUX)
__uid_t uid = getuid();
passwd *passw = getpwuid(uid);
if (passw != NULL) {
int groups_len = 0;
getgrouplist(passw->pw_name, passw->pw_gid, NULL, &groups_len);
if (groups_len == 1) {
__gid_t groups[2];
getgrouplist(passw->pw_name, passw->pw_gid, groups, &groups_len);
const char *gr_name = getgrgid(groups[0])->gr_name;
nix_build = gb_strcmp(gr_name, "nixbld") == 0;
}
}
#endif

StringSet asm_files = {};
string_set_init(&asm_files, 64);
Expand Down Expand Up @@ -615,7 +632,12 @@ try_cross_linking:;
// 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")) || string_ends_with(lib, str_lit(".so")) || string_contains_string(lib, str_lit(".so."))) {
lib_str = gb_string_append_fmt(lib_str, " -l:\"%.*s\" ", LIT(lib));
// Inside Nix build environment, the linker will not be able to find the libraries if `-l:` is specified.
if (nix_build) {
lib_str = gb_string_append_fmt(lib_str, " \"%.*s\" ", LIT(lib));
} else {
lib_str = gb_string_append_fmt(lib_str, " -l:\"%.*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));
Expand Down