Skip to content

Commit

Permalink
feat: introduce MiniPortile.mkmf_config
Browse files Browse the repository at this point in the history
which will set $LDFLAGS and $CFLAGS for the recipe, including a new
feature to support pkg-config files for the library.

This should allow gems with C extensions that use mini_portile to more
easily configure compiler and linker flags.
  • Loading branch information
flavorjones committed Sep 13, 2023
1 parent b5dadf3 commit b17522b
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Gemfile.lock
pkg
ports
tmp
mkmf.log
20 changes: 12 additions & 8 deletions examples/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ require 'rbconfig'
$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
require "mini_portile2"

require "mkmf"

recipes = []
recipe_hooks = {}

Expand Down Expand Up @@ -130,17 +128,20 @@ yaml.files = [{
url: "https://github.com/yaml/libyaml/releases/download/0.2.5/yaml-0.2.5.tar.gz",
sha256: "c642ae9b75fee120b2d96c712538bd2cf283228d2337df2cf2988e3c02678ef4",
}]
recipes.push(yaml)
recipes.unshift(yaml)
recipe_hooks["yaml"] = lambda do |recipe|
conf = pkg_config(File.join(recipe.path, "lib", "pkgconfig", "yaml-0.1.pc"))
puts "pkg_config: #{conf.inspect}"
recipe.mkmf_config(pkg: "yaml-0.1")

expected = "-L" + MiniPortile.native_path(File.join(recipe.path, "lib"))
expected = "-L" + File.join(recipe.path, "lib")
$LDFLAGS.split.include?(expected) or raise(<<~MSG)
assertion failed: LDFLAGS not updated correctly:
#{$LDFLAGS}
should have included '#{expected}'
MSG

unless have_library("yaml", "yaml_get_version", "yaml.h")
raise("could not find libyaml development environment")
end
end

namespace :ports do
Expand All @@ -158,9 +159,10 @@ namespace :ports do
desc "Install port #{recipe.name} #{recipe.version}"
task recipe.name => ["ports"] do |t|
recipe.cook
recipe.activate
if hook = recipe_hooks[recipe.name]
hook.call(recipe)
else
recipe.activate
end
end

Expand All @@ -173,7 +175,9 @@ namespace :ports do
puts "Artifacts of '#{recipe.name}' in '#{recipe.path}'"
end
puts "LIBRARY_PATH: #{ENV['LIBRARY_PATH'].inspect}"
puts "LDFLAGS: #{ENV['LDFLAGS'].inspect}, #{$LDFLAGS.inspect}"
puts "LDFLAGS: #{ENV['LDFLAGS'].inspect}"
puts "$LDFLAGS: #{$LDFLAGS.inspect}"
puts "$CFLAGS: #{$CFLAGS.inspect}"
end
end

Expand Down
62 changes: 62 additions & 0 deletions lib/mini_portile2/mini_portile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,43 @@ def activate
end
end

def mkmf_config(pkg: nil, dir: nil)
require "mkmf"

if pkg
dir ||= File.join(path, "lib", "pkgconfig")
pcfile = File.join(dir, "#{pkg}.pc")
unless File.exist?(pcfile)
raise ArgumentError, "pkg-config file '#{pcfile}' does not exist"
end

output "Configuring MakeMakefile for #{File.basename(pcfile)} (in #{File.dirname(pcfile)})\n"

# on macos, pkg-config will not return --cflags without this
ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t"

# append to PKG_CONFIG_PATH as we go, so later pkg-config files can depend on earlier ones
ENV["PKG_CONFIG_PATH"] = [ENV["PKG_CONFIG_PATH"], dir].compact.join(File::PATH_SEPARATOR)

cflags = minimal_pkg_config(pcfile, "cflags")
ldflags = minimal_pkg_config(pcfile, "libs", "static")
else
output "Configuring MakeMakefile for #{@name} #{@version} (from #{path})\n"

include_path = File.join(path, "include")
lib_path = File.join(path, "lib")

lib_name = name.sub(/\Alib/, "") # TODO: use delete_prefix when we no longer support ruby 2.4

cflags = "-I#{include_path}" if Dir.exist?(include_path)
ldflags = "-L#{lib_path} -l#{lib_name}" if Dir.exist?(lib_path)
end

$CFLAGS << " " << cflags if cflags
$CXXFLAGS << " " << cflags if cflags
$LDFLAGS << " " << ldflags if ldflags
end

def path
File.expand_path(port_path)
end
Expand Down Expand Up @@ -656,4 +693,29 @@ def with_tempfile(filename, full_path)
FileUtils.mkdir_p File.dirname(full_path)
FileUtils.mv temp_file.path, full_path, :force => true
end

#
# this minimal version of pkg_config is based on ruby 29dc9378 (2023-01-09)
#
# specifically with the fix from b90e56e6 to support multiple pkg-config options, and removing
# code paths that aren't helpful for mini-portile's use case of parsing pc files.
#
def minimal_pkg_config(pkg, *pcoptions)
if pcoptions.empty?
raise ArgumentError, "no pkg-config options are given"
end

if ($PKGCONFIG ||=
(pkgconfig = MakeMakefile.with_config("pkg-config") {MakeMakefile.config_string("PKG_CONFIG") || "pkg-config"}) &&
MakeMakefile.find_executable0(pkgconfig) && pkgconfig)
pkgconfig = $PKGCONFIG
else
raise RuntimeError, "pkg-config is not found"
end

pcoptions = Array(pcoptions).map { |o| "--#{o}" }
response = IO.popen([pkgconfig, *pcoptions, pkg], err:[:child, :out], &:read)
raise RuntimeError, response unless $?.success?
response.strip
end
end
13 changes: 13 additions & 0 deletions test/assets/pkgconf/libxml2/libxml-2.0.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
prefix=/foo/libxml2/2.11.5
exec_prefix=${prefix}
libdir=/foo/libxml2/2.11.5/lib
includedir=${prefix}/include
modules=1

Name: libXML
Version: 2.11.5
Description: libXML library version2.
Requires:
Libs: -L${libdir} -lxml2
Libs.private: -L/foo/zlib/1.3/lib -lz -lm
Cflags: -I${includedir}/libxml2
13 changes: 13 additions & 0 deletions test/assets/pkgconf/libxslt/libexslt.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
prefix=/foo/libxslt/1.1.38
exec_prefix=${prefix}
libdir=/foo/libxslt/1.1.38/lib
includedir=${prefix}/include


Name: libexslt
Version: 0.8.21
Description: EXSLT Extension library
Requires: libxml-2.0, libxslt
Cflags: -I${includedir}
Libs: -L${libdir} -lexslt
Libs.private: -lm
13 changes: 13 additions & 0 deletions test/assets/pkgconf/libxslt/libxslt.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
prefix=/foo/libxslt/1.1.38
exec_prefix=${prefix}
libdir=/foo/libxslt/1.1.38/lib
includedir=${prefix}/include


Name: libxslt
Version: 1.1.38
Description: XSLT library version 2.
Requires: libxml-2.0
Cflags: -I${includedir}
Libs: -L${libdir} -lxslt
Libs.private: -lm
133 changes: 133 additions & 0 deletions test/test_mkmf_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require File.expand_path('../helper', __FILE__)

class TestMkmfConfig < TestCase
attr_reader :recipe, :include_path, :lib_path

LIBXML_PCP = File.join(__dir__, "assets", "pkgconf", "libxml2")
LIBXSLT_PCP = File.join(__dir__, "assets", "pkgconf", "libxslt")

def setup
super

@save_env = %w[PATH CPATH LIBRARY_PATH LDFLAGS PKG_CONFIG_PATH].inject({}) do |env, var|
env.update(var => ENV[var])
end
$LDFLAGS = ""
$CFLAGS = ""

FileUtils.rm_rf(["tmp", "ports"]) # remove any previous test files

@recipe = MiniPortile.new("libfoo", "1.0.0").tap do |recipe|
recipe.logger = StringIO.new
end
@include_path = File.join(@recipe.path, "include")
@lib_path = File.join(@recipe.path, "lib")
end

def teardown
FileUtils.rm_rf(["tmp", "ports"]) # remove any previous test files

$LDFLAGS = ""
$CFLAGS = ""
@save_env.each do |var, val|
ENV[var] = val
end

super
end

def test_mkmf_config_recipe_LDFLAGS_global_lib_dir_does_not_exist
recipe.mkmf_config

refute_includes($LDFLAGS.split, "-L#{lib_path}")
refute_includes($LDFLAGS.split, "-lfoo")
end

def test_mkmf_config_recipe_LDFLAGS_global
FileUtils.mkdir_p(lib_path)

recipe.mkmf_config

assert_includes($LDFLAGS.split, "-L#{lib_path}")
assert_includes($LDFLAGS.split, "-lfoo") # note the recipe name is "libfoo"
end

def test_mkmf_config_recipe_CFLAGS_global_include_dir_does_not_exist
recipe.mkmf_config

refute_includes($CFLAGS.split, "-I#{include_path}")
end

def test_mkmf_config_recipe_CFLAGS_global
FileUtils.mkdir_p(include_path)

recipe.mkmf_config

assert_includes($CFLAGS.split, "-I#{include_path}")
end

def test_mkmf_config_pkgconf_does_not_exist
assert_raises(ArgumentError) do
recipe.mkmf_config(pkg: "foo")
end
end

def test_mkmf_config_pkgconf_LDFLAGS_global
# can't get the pkgconf utility to install on windows with ruby 2.3 in CI
skip if MiniPortile.windows? && RUBY_VERSION < "2.4"

recipe.mkmf_config(pkg: "libxml-2.0", dir: LIBXML_PCP)

assert_includes($LDFLAGS.split, "-L/foo/libxml2/2.11.5/lib")
assert_includes($LDFLAGS.split, "-lxml2")
end

def test_mkmf_config_pkgconf_CFLAGS_global
# can't get the pkgconf utility to install on windows with ruby 2.3 in CI
skip if MiniPortile.windows? && RUBY_VERSION < "2.4"

recipe.mkmf_config(pkg: "libxml-2.0", dir: LIBXML_PCP)

assert_includes($CFLAGS.split, "-I/foo/libxml2/2.11.5/include/libxml2")
end

def test_mkmf_config_pkgconf_path_accumulation
# can't get the pkgconf utility to install on windows with ruby 2.3 in CI
skip if MiniPortile.windows? && RUBY_VERSION < "2.4"

(ENV["PKG_CONFIG_PATH"] || "").split(File::PATH_SEPARATOR).tap do |pcpaths|
refute_includes(pcpaths, LIBXML_PCP)
refute_includes(pcpaths, LIBXSLT_PCP)
end

recipe.mkmf_config(pkg: "libxml-2.0", dir: LIBXML_PCP)

ENV["PKG_CONFIG_PATH"].split(File::PATH_SEPARATOR).tap do |pcpaths|
assert_includes(pcpaths, LIBXML_PCP)
refute_includes(pcpaths, LIBXSLT_PCP)
end

recipe.mkmf_config(pkg: "libxslt", dir: LIBXSLT_PCP)

ENV["PKG_CONFIG_PATH"].split(File::PATH_SEPARATOR).tap do |pcpaths|
assert_includes(pcpaths, LIBXML_PCP)
assert_includes(pcpaths, LIBXSLT_PCP)
end

recipe.mkmf_config(pkg: "libexslt", dir: LIBXSLT_PCP)

$CFLAGS.split.tap do |cflags|
assert_includes(cflags, "-I/foo/libxml2/2.11.5/include/libxml2")
assert_includes(cflags, "-I/foo/libxslt/1.1.38/include")
end
$LDFLAGS.split.tap do |ldflags|
assert_includes(ldflags, "-L/foo/libxml2/2.11.5/lib")
assert_includes(ldflags, "-lxml2")
assert_includes(ldflags, "-L/foo/libxslt/1.1.38/lib")
assert_includes(ldflags, "-lxslt")
assert_includes(ldflags, "-lexslt")
assert_includes(ldflags, "-L/foo/zlib/1.3/lib") # from `--static`
assert_includes(ldflags, "-lz") # from `--static`
end
end
end

0 comments on commit b17522b

Please sign in to comment.