diff --git src/compiler/crystal/codegen/link.cr src/compiler/crystal/codegen/link.cr index 4caf0060c..d7b9b19ca 100644 --- src/compiler/crystal/codegen/link.cr +++ src/compiler/crystal/codegen/link.cr @@ -136,7 +136,7 @@ module Crystal static_build = has_flag?("static") # Instruct the linker to link statically if the user asks - flags << "-static" if static_build + flags << "-static" if static_build && !has_flag?("darwin") # Add CRYSTAL_LIBRARY_PATH locations, so the linker preferentially # searches user-given library paths. @@ -164,7 +164,32 @@ module Crystal end end - flags.join(" ") + process_flags(flags.join(" ")) + end + + private def process_flags(flags) + return flags if !(has_flag?("darwin") && has_flag?("static")) + # From `man 3 intro` + system_libs = ["c", "dbm", "dl", "info", "m", "poll", "pthread", "rpcsvc"] + new_flags = [] of String + flags.split.each do |flag| + if (lib_name = flag.lchop?("-l")) + static_lib = find_static_lib(lib_name) + if (!static_lib && !system_libs.includes?(lib_name)) + Crystal.error "could not find lib#{lib_name}.a", 1 + end + flag = static_lib if static_lib + end + new_flags << flag + end + new_flags.join(" ") + end + + private def find_static_lib(libname) + CrystalLibraryPath.paths.each do |libdir| + static_lib = "#{libdir}/lib#{libname}.a" + return static_lib if File.exists?(static_lib) + end end PKG_CONFIG_PATH = Process.find_executable("pkg-config") diff --git src/openssl/lib_crypto.cr src/openssl/lib_crypto.cr index 77fc322ab..80005b0c3 100644 --- src/openssl/lib_crypto.cr +++ src/openssl/lib_crypto.cr @@ -26,7 +26,7 @@ @[Link("crypt32")] # CertOpenStore, ... @[Link("user32")] # GetProcessWindowStation, GetUserObjectInformationW, _MessageBoxW {% else %} - @[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'`")] + @[Link("crypto", pkg_config: "libcrypto")] {% end %} lib LibCrypto alias Char = LibC::Char diff --git src/openssl/lib_ssl.cr src/openssl/lib_ssl.cr index f8c9d0908..cc961956a 100644 --- src/openssl/lib_ssl.cr +++ src/openssl/lib_ssl.cr @@ -33,7 +33,7 @@ require "./lib_crypto" @[Link("crypt32")] # CertOpenStore, ... @[Link("user32")] # GetProcessWindowStation, GetUserObjectInformationW, _MessageBoxW {% else %} - @[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libssl || printf %s '-lssl -lcrypto'`")] + @[Link("ssl", pkg_config: "libssl")] {% end %} lib LibSSL alias Int = LibC::Int