Skip to content

Commit

Permalink
flag quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Oct 18, 2024
1 parent 51bbb53 commit b98413d
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/compiler/crystal/codegen/link.cr
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ module Crystal
end

class Program
def lib_flags
has_flag?("msvc") ? lib_flags_windows : lib_flags_posix
def lib_flags(cross_compiling : Bool = false)
has_flag?("msvc") ? lib_flags_windows(cross_compiling) : lib_flags_posix(cross_compiling)
end

private def lib_flags_windows
private def lib_flags_windows(cross_compiling)
flags = [] of String

# Add CRYSTAL_LIBRARY_PATH locations, so the linker preferentially
# searches user-given library paths.
if has_flag?("msvc")
CrystalLibraryPath.paths.each do |path|
flags << Process.quote_windows("/LIBPATH:#{path}")
flags << quote_flag("/LIBPATH:#{path}", cross_compiling)
end
end

Expand All @@ -141,14 +141,14 @@ module Crystal
end

if libname = ann.lib
flags << Process.quote_windows("#{libname}.lib")
flags << quote_flag("#{libname}.lib", cross_compiling)
end
end

flags.join(" ")
end

private def lib_flags_posix
private def lib_flags_posix(cross_compiling)
flags = [] of String
static_build = has_flag?("static")

Expand All @@ -158,11 +158,7 @@ module Crystal
# Add CRYSTAL_LIBRARY_PATH locations, so the linker preferentially
# searches user-given library paths.
CrystalLibraryPath.paths.each do |path|
# we use `quote` rather than `quote_posix` because these flags are
# evaluated on the compiler host system and Windows rules could be used
# for MinGW-w64
# TODO: what about the opposite? (e.g. MSVC on Wine on Linux)
flags << Process.quote("-L#{path}")
flags << quote_flag("-L#{path}", cross_compiling)
end

link_annotations.reverse_each do |ann|
Expand All @@ -177,17 +173,25 @@ module Crystal
elsif (lib_name = ann.lib) && (flag = pkg_config(lib_name, static_build))
flags << flag
elsif (lib_name = ann.lib)
flags << Process.quote("-l#{lib_name}")
flags << quote_flag("-l#{lib_name}", cross_compiling)
end

if framework = ann.framework
flags << "-framework" << Process.quote(framework)
flags << "-framework" << quote_flag(framework, cross_compiling)
end
end

flags.join(" ")
end

private def quote_flag(flag, cross_compiling)
if cross_compiling
has_flag?("windows") ? Process.quote_windows(flag) : Process.quote_posix(flag)
else
Process.quote(flag)
end
end

# Searches among CRYSTAL_LIBRARY_PATH, the compiler's directory, and PATH
# for every DLL specified in the used `@[Link]` annotations. Yields the
# absolute path and `true` if found, the base name and `false` if not found.
Expand Down

0 comments on commit b98413d

Please sign in to comment.