Skip to content

Commit

Permalink
fix: add the recipe lib path to $LDFLAGS
Browse files Browse the repository at this point in the history
This probably should have always been the behavior. This works around
the behavior of Fedora's pkgconf described in #118.

I'm a little worried that this might break packages in some subtle
way, so the `set_ldflags` option will turn it off if needed.
  • Loading branch information
flavorjones committed Sep 9, 2023
1 parent 62b9a6e commit aa982b2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
8 changes: 2 additions & 6 deletions examples/Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'rbconfig'
require 'mkmf'

$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
require "mini_portile2"

$LDFLAGS = "-mimic-being-in-extconf" # mimic being in an extconf (mkmf.rb sets this global variable)
recipes = []
recipe_hooks = {}

Expand Down Expand Up @@ -132,11 +132,6 @@ yaml.files = [{
recipes.push(yaml)
recipe_hooks["yaml"] = lambda do |recipe|
expected = "-L" + File.join(recipe.path, "lib")
!$LDFLAGS.split.include?(expected) or raise "assertion failed on setup"

conf = pkg_config(File.join(recipe.path, "lib", "pkgconfig", "yaml-0.1.pc"))
puts "pkg_config: #{conf.inspect}"

$LDFLAGS.split.include?(expected) or raise(<<~MSG)
assertion failed: LDFLAGS not updated correctly:
#{$LDFLAGS}
Expand Down Expand Up @@ -173,6 +168,7 @@ namespace :ports do
recipes.each do |recipe|
puts "Artifacts of '#{recipe.name}' in '#{recipe.path}'"
end
puts "LIBRARY_PATH: #{ENV['LIBRARY_PATH'].inspect}"
puts "LDFLAGS: #{ENV['LDFLAGS'].inspect}, #{$LDFLAGS.inspect}"
end
end
Expand Down
27 changes: 20 additions & 7 deletions lib/mini_portile2/mini_portile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MiniPortile

attr_reader :name, :version, :original_host
attr_writer :configure_options
attr_accessor :host, :files, :patch_files, :target, :logger, :source_directory
attr_accessor :host, :files, :patch_files, :target, :logger, :source_directory, :set_ldflags

def self.windows?
target_os =~ /mswin|mingw/
Expand Down Expand Up @@ -85,6 +85,7 @@ def initialize(name, version, **kwargs)
@log_files = {}
@logger = STDOUT
@source_directory = nil
@set_ldflags = true

@original_host = @host = detect_host

Expand Down Expand Up @@ -238,14 +239,26 @@ def activate
end
end

# rely on LDFLAGS when cross-compiling
if File.exist?(lib_path) && (@host != @original_host)
full_path = File.expand_path(lib_path)
if set_ldflags && File.exist?(lib_path)
full_lib_path = File.expand_path(lib_path)
flag = "-L#{full_lib_path}"

old_value = ENV.fetch("LDFLAGS", "")
# rely on LDFLAGS when cross-compiling
if (@host != @original_host)
old_value = ENV.fetch("LDFLAGS", "")

unless old_value.include?(full_path)
ENV["LDFLAGS"] = "-L#{full_path} #{old_value}".strip
unless old_value.include?(full_lib_path)
ENV["LDFLAGS"] = "#{flag} #{old_value}".strip
end
end

# if we're in an extconf (mkmf.rb sets $LDFLAGS), append the library path. this works around
# an issue with fedora's pkgconf as detailed in:
#
# https://github.com/flavorjones/mini_portile/issues/118
#
if $LDFLAGS && !$LDFLAGS.split.include?(flag)
$LDFLAGS << " #{flag}"
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions test/test_activate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup
@save_env = %w[PATH CPATH LIBRARY_PATH].inject({}) do |env, var|
env.update(var => ENV[var])
end
$LDFLAGS = nil

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

Expand All @@ -20,6 +21,7 @@ def setup
def teardown
FileUtils.rm_rf(["tmp", "ports"]) # remove any previous test files

$LDFLAGS = nil
@save_env.each do |var, val|
ENV[var] = val
end
Expand Down Expand Up @@ -103,6 +105,34 @@ def test_LDFLAGS_env_var_when_cross_compiling
assert(flag_elements('LDFLAGS').include?("-L#{lib_path}"))
end

def test_LDFLAGS_global_when_not_set
$LDFLAGS = nil
FileUtils.mkdir_p(lib_path)

recipe.activate

assert_nil($LDFLAGS)
end

def test_LDFLAGS_global_when_set
$LDFLAGS = "-lm"
FileUtils.mkdir_p(lib_path)

recipe.activate

assert($LDFLAGS.split.include?("-L#{lib_path}"))
end

def test_LDFLAGS_global_when_set_but_option_is_turned_off
$LDFLAGS = "-lm"
FileUtils.mkdir_p(lib_path)

recipe.set_ldflags = false
recipe.activate

refute($LDFLAGS.split.include?("-L#{lib_path}"))
end

private

def path_elements(varname)
Expand Down

0 comments on commit aa982b2

Please sign in to comment.