From be7e5f1f85685a757bea6c8dae4e30914a5eec67 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Fri, 2 Feb 2024 09:40:38 +0100 Subject: [PATCH] Implements [Feature #3456]: Support pkgconf on windows 1. Store the `PKG_CONFIG` variable in Makefile.sub (or try to get it from the ENV var PKG_CONFIG in mkmf.rb) 2. Try to use --msvc-syntax, with a fallback to replacing -Lxxx with -libpath:xxx. --msvc-syntax has been in pkgconf since 1.4.0 (released 7 years ago). pkg-config (freedesktop), does not support it, hence the fallback. 3. The `try_ldflags` passes these `ldflags` as the `opt` parameter to the `link_command`, not as `ldflags`. Unix systems are forgiving in that regard, MSVC is not: as a result as passing them as `opt`, they (specifically the `/libpath:xxx` ones) end up passed before the `-link` command to `cl.exe` and it throws because it ignores it and therefore can't find the lib. ``` cl : Command line warning D9002 : ignoring unknown option '-libpath:C:/Users/julien/.conan2/p/libff3726d89a6255c/p/lib' ``` --- lib/mkmf.rb | 33 ++++++++++++++++++++++++++++----- win32/Makefile.sub | 1 + 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/mkmf.rb b/lib/mkmf.rb index d970e9a6ad..0001d5926f 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -604,9 +604,9 @@ MSG yield(opt, opts) end - def try_link0(src, opt = "", **opts, &b) # :nodoc: + def try_link0(src, opt = "", ldflags: "", **opts, &b) # :nodoc: exe = CONFTEST+$EXEEXT - cmd = link_command("", opt) + cmd = link_command(ldflags, opt) if $universal require 'tmpdir' Dir.mktmpdir("mkmf_", oldtmpdir = ENV["TMPDIR"]) do |tmpdir| @@ -750,7 +750,7 @@ MSG # :nodoc: def try_ldflags(flags, werror: $mswin, **opts) - try_link(MAIN_DOES_NOTHING, flags, werror: werror, **opts) + try_link(MAIN_DOES_NOTHING, "", ldflags: flags, werror: werror, **opts) end # :startdoc: @@ -1968,7 +1968,7 @@ SRC if pkgconfig = with_config("#{pkg}-config") and find_executable0(pkgconfig) # if and only if package specific config command is given elsif ($PKGCONFIG ||= - (pkgconfig = with_config("pkg-config") {config_string("PKG_CONFIG") || "pkg-config"}) && + (pkgconfig = with_config("pkg-config") {config_string("PKG_CONFIG") || ENV["PKG_CONFIG"] || "pkg-config"}) && find_executable0(pkgconfig) && pkgconfig) and xsystem([*envs, $PKGCONFIG, "--exists", pkg]) # default to pkg-config command @@ -1980,11 +1980,34 @@ SRC pkgconfig = nil end if pkgconfig + has_ms_win_syntax = false + if $mswin + has_ms_win_syntax = xpopen([pkgconfig, "--help"]).read.include?('msvc-syntax') + if has_ms_win_syntax + args << "--msvc-syntax" + else + Logging.message("WARNING: #{pkgconfig} does not support the --msvc-syntax. Try using a recent pkgconf instead") + end + end get = proc {|opts| opts = Array(opts).map { |o| "--#{o}" } opts = xpopen([*envs, pkgconfig, *opts, *args], err:[:child, :out], &:read) Logging.open {puts opts.each_line.map{|s|"=> #{s.inspect}"}} - opts.strip if $?.success? + if $?.success? + opts = opts.strip + if $mswin and not has_ms_win_syntax + opts = Shellwords.shellwords(opts).map { |s| + if s.start_with?('-l') + "#{s[2..]}.lib" + elsif s.start_with?('-L') + "/libpath:#{s[2..]}" + else + s + end + }.quote.join(" ") + end + opts + end } end orig_ldflags = $LDFLAGS diff --git a/win32/Makefile.sub b/win32/Makefile.sub index 2864c8ec3f..e4c64ad1b4 100644 --- a/win32/Makefile.sub +++ b/win32/Makefile.sub @@ -1168,6 +1168,7 @@ s,@top_srcdir@,$(srcdir),;t t s,@try_header@,try_compile,;t t s,@ruby_pc@,$(ruby_pc),;t t s,@RJIT_SUPPORT@,$(RJIT_SUPPORT),;t t +s,@PKG_CONFIG@,$(PKG_CONFIG),;t t <