Skip to content

Commit

Permalink
Support static linking of re2 library
Browse files Browse the repository at this point in the history
Currently when the re2 system library is updated by Homebrew, the
dynamically linked library is removed and moved to a new location.
This breaks the re2 gem and requires users to run `gem pristine re2`
to get back into a working state.

To avoid this hassle, this commit adds a `--enable-static` flag that
will statically link the library in the gem's C extension library.

Homebrew doesn't currently install a static library for re2. This will
be fixed via Homebrew/homebrew-core#119928.
  • Loading branch information
stanhu committed Jan 6, 2023
1 parent 39f6680 commit 0b151ac
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions ext/re2/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Released under the BSD Licence, please see LICENSE.txt

require 'mkmf'
require 'shellwords'

if ENV["CC"]
RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"]
Expand All @@ -22,13 +23,40 @@
"/usr/include"
]

lib_dirs = [
LIB_DIRS = [
"/usr/local/lib",
"/opt/homebrew/lib",
"/usr/lib"
"/usr/lib",
"/usr/lib/x86_64-linux-gnu"
]

dir_config("re2", header_dirs, lib_dirs)
def libflag_to_filename(ldflag)
case ldflag
when /\A-l(.+)/
"lib#{Regexp.last_match(1)}.#{$LIBEXT}"
end
end

def resolve_static_library(arg)
libs_path = pkg_config('re2', 'libs-only-L')

re2_dirs =
if libs_path.nil? || libs_path.empty?
LIB_DIRS
else
# There should only be one path, but assume there can be multiple.
libs_path.strip.split(' ').map { |lib| lib.gsub(/^-L/, '') }
end

filename = libflag_to_filename(arg)
dir = re2_dirs.find { |path| File.exist?(File.join(path, filename)) }

raise "Unable to find #{filename} in #{re2_dirs}" unless dir

File.join(dir, filename)
end

dir_config("re2", header_dirs, LIB_DIRS)

$CFLAGS << " -Wall -Wextra -funroll-loops"

Expand Down Expand Up @@ -112,4 +140,20 @@
end
end

static_p = enable_config('static', false)
message "Static linking is #{static_p ? 'enabled' : 'disabled'}.\n"

if static_p
append_cppflags('-fPIC')

$libs = $libs.shellsplit.map do |arg|
case arg
when '-lre2'
resolve_static_library(arg)
else
arg
end
end.shelljoin
end

create_makefile("re2")

0 comments on commit 0b151ac

Please sign in to comment.