diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2017-11-10 21:17:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-11-10 21:17:16 +0000 |
| commit | 1d2eb8055eb8259c6cfbb0e901245fa5a7e76a17 (patch) | |
| tree | 5228118ba4ce80d0186380082bb319971480175f /src | |
| parent | bbddbba34062b18eba6b6ce53b31a350e0816ff1 (diff) | |
| parent | 9e0b69312b949f6f0e1e1851db3ecb79be25cfa1 (diff) | |
Merge pull request #140 from vassvik/master
Fixed foreign import for linux. Modified .gitignore to ignore temp files and files in shared/. Added a Makefile for linux
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 40 | ||||
| -rw-r--r-- | src/parser.cpp | 15 |
2 files changed, 47 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp index 154fa91bc..adb690dad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -811,9 +811,15 @@ int main(int arg_count, char **arg_ptr) { return exit_code; } - 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/"); - gbString lib_str = gb_string_make(heap_allocator(), ""); defer (gb_string_free(lib_str)); char lib_str_buf[1024] = {0}; for_array(i, ir_gen.module.foreign_library_paths) { @@ -825,15 +831,33 @@ int main(int arg_count, char **arg_ptr) { #if defined(GB_SYSTEM_OSX) isize len; if(lib.len > 2 && lib[0] == '-' && lib[1] == 'f') { - len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), - " -framework %.*s ", (int)(lib.len) - 2, lib.text + 2); + // framework thingie + len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), " -framework %.*s ", (int)(lib.len) - 2, lib.text + 2); + } else if (string_has_extension(lib, str_lit("dylib"))) { + // dynamic lib, relative path to executable + len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), " -l:%s/%.*s ", cwd, LIT(lib)); } else { - len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), - " -l%.*s ", LIT(lib)); + // dynamic or static system lib, just link regularly searching system library paths + len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), " -l%.*s ", LIT(lib)); } #else - isize len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), - " -l%.*s ", LIT(lib)); + // 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_has_extension(lib, str_lit("a"))) { + // static libs, absolute full path relative to the file in which the lib was imported from + isize len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), " -l:%.*s ", LIT(lib)); + } else if (string_has_extension(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 runtimeto the executable + isize len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), " -l:%s/%.*s ", cwd, LIT(lib)); + } else { + // dynamic or static system lib, just link regularly searching system library paths + isize len = gb_snprintf(lib_str_buf, gb_size_of(lib_str_buf), " -l%.*s ", LIT(lib)); + } #endif lib_str = gb_string_appendc(lib_str, lib_str_buf); } diff --git a/src/parser.cpp b/src/parser.cpp index f81bb1a39..1c90bf274 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4880,7 +4880,22 @@ bool determine_path_from_string(Parser *p, AstNode *node, String base_dir, Strin syntax_error(node, "Unknown library collection: `%.*s`", LIT(collection_name)); return false; } + } else { +#if !defined(GB_SYSTEM_WINDOWS) + // @NOTE(vassvik): foreign imports of shared libraries that are not in the system collection on + // linux/mac have to be local to the executable for consistency with shared libraries. + // Unix does not have a concept of "import library" for shared/dynamic libraries, + // so we need to pass the relative path to the linker, and add the current + // working directory of the exe to the library search paths. + // Static libraries can be linked directly with the full pathname + // + if (node->kind == AstNode_ForeignImportDecl && string_has_extension(file_str, str_lit("so"))) { + *path = file_str; + return true; + } +#endif } + String fullpath = string_trim_whitespace(get_fullpath_relative(a, base_dir, file_str)); *path = fullpath; |