Skip to content

Commit

Permalink
Ensure we don't add lre2 to $LIBS
Browse files Browse the repository at this point in the history
To ensure we link against the correct versions of Abseil and RE2 instead
of any in Ruby's exec_prefix, ensure we don't call `have_library` (which
will add `lre2` to `LIBS`) when compiling the vendored dependencies.

Also: rather than having to set '-x c++' to force try_compile to compile
test programs as C++, use MakeMakefile's own native support for C++.
  • Loading branch information
mudge committed Mar 25, 2024
1 parent e72914f commit 4cd3700
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions ext/re2/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require 'mkmf'
require_relative 'recipes'

include MakeMakefile['C++']

module RE2
class Extconf
def configure
Expand Down Expand Up @@ -94,6 +96,10 @@ def build_with_system_libraries
]

dir_config("re2", header_dirs, lib_dirs)

unless have_library("re2")
abort "You must have re2 installed and specified with --with-re2-dir, please see https://github.com/google/re2/wiki/Install"
end
end

def build_with_vendored_libraries
Expand Down Expand Up @@ -130,23 +136,17 @@ def build_extension
$CFLAGS << " -Wall -Wextra -funroll-loops"
$CPPFLAGS << " -Wno-register"

# Pass -x c++ to force gcc to compile the test program
# as C++ (as it will end in .c by default).
compile_options = +"-x c++"

have_library("stdc++")
have_header("stdint.h")
have_func("rb_gc_mark_movable") # introduced in Ruby 2.7

unless have_library("re2")
abort "You must have re2 installed and specified with --with-re2-dir, please see https://github.com/google/re2/wiki/Install"
end

minimal_program = <<~SRC
#include <re2/re2.h>
int main() { return 0; }
SRC

compile_options = +''

re2_requires_version_flag = checking_for("re2 that requires explicit C++ version flag") do
!try_compile(minimal_program, compile_options)
end
Expand Down Expand Up @@ -232,20 +232,22 @@ def static_pkg_config(pc_file, pkg_config_paths)

# Replace all -l flags that can be found in one of the static library
# paths with the absolute path instead.
libflags = minimal_pkg_config(pc_file, '--libs-only-l', '--static')
minimal_pkg_config(pc_file, '--libs-only-l', '--static')
.shellsplit
.map do |flag|
next flag unless flag.start_with?('-l')

lib = "lib#{flag.delete_prefix('-l')}.#{$LIBEXT}"
static_lib_path = static_library_paths.find { |path| File.exist?(File.join(path, lib)) }
next flag unless static_lib_path

File.join(static_lib_path, lib)
.each do |flag|
if flag.start_with?('-l')
lib = "lib#{flag.delete_prefix('-l')}.#{$LIBEXT}"

if (static_lib_path = static_library_paths.find { |path| File.exist?(File.join(path, lib)) })
$LDFLAGS << ' ' << File.join(static_lib_path, lib)
else
append_ldflags(flag.shellescape)
end
else
append_ldflags(flag.shellescape)
end
end

$libs = [libflags, $libs].join(" ").strip

incflags = minimal_pkg_config(pc_file, '--cflags-only-I')
$INCFLAGS = [incflags, $INCFLAGS].join(" ").strip

Expand Down

0 comments on commit 4cd3700

Please sign in to comment.