Skip to content

Commit

Permalink
Refactor extconf, inspired by MiniPortile2
Browse files Browse the repository at this point in the history
GitHub: #109

MiniPortile2 2.8.5 ships with a `mkmf_config` feature that can handle
statically linking libraries for us, however it doesn't currently work
with our use case due to Abseil shipping with over 180 separate `.pc`
files. That said, we can still update how we statically link RE2 into
the gem based on MiniPortile2's strategy and how Ruby's own
MakeMakefile's `pkg_config` works.

The key is that we now rely on the output of the following `pkg-config`
commands to populate `$LIBPATH`, `$libs`, `$LDFLAGS`, `$INCFLAGS`,
`$CFLAGS` and `$CXXFLAGS` respectively:

* `pkg-config --libs-only-L --static`
* `pkg-config --libs-only-l --static`
* `pkg-config --libs-only-other --static`
* `pkg-config --cflags-only-I --static`
* `pkg-config --cflags-only-other --static`

We transform any libraries into static ones by replacing them with their
absolute path wherever possible.

Note we _must not_ use MakeMakefile's `dir_config` to avoid accidentally
adding a non-absolute (and therefore dynamic) reference to RE2 which
risks accidentally linking against the wrong version of the library,
especially if it is found in Ruby's default `exec_prefix` (e.g.
`/usr/local`).

We also take a leaf from the Ruby SQLite3 gem's extconf
(https://github.com/sparklemotion/sqlite3-ruby/blob/ae5c13996f936fce07e8a5e9fb6aaf2ede5d82b7/ext/sqlite3/extconf.rb#L113)
and re-organise the configuration logic in our extconf.rb into a class
rather than a series of global functions.

Thanks to @flavorjones for his work on MiniPortile2 and @stanhu for
reviewing a draft of this change.
  • Loading branch information
mudge committed Apr 1, 2024
1 parent a0806ba commit 43b174e
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 310 deletions.
Loading

0 comments on commit 43b174e

Please sign in to comment.