From aa982b2e7dd0736f4692e1f1042dec35df0b9efc Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 9 Sep 2023 14:29:39 -0400 Subject: [PATCH] fix: add the recipe lib path to $LDFLAGS 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. --- examples/Rakefile | 8 ++------ lib/mini_portile2/mini_portile.rb | 27 ++++++++++++++++++++------- test/test_activate.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/examples/Rakefile b/examples/Rakefile index 2db3eee..b55c8f0 100644 --- a/examples/Rakefile +++ b/examples/Rakefile @@ -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 = {} @@ -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} @@ -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 diff --git a/lib/mini_portile2/mini_portile.rb b/lib/mini_portile2/mini_portile.rb index 4a30181..e16e02a 100644 --- a/lib/mini_portile2/mini_portile.rb +++ b/lib/mini_portile2/mini_portile.rb @@ -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/ @@ -85,6 +85,7 @@ def initialize(name, version, **kwargs) @log_files = {} @logger = STDOUT @source_directory = nil + @set_ldflags = true @original_host = @host = detect_host @@ -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 diff --git a/test/test_activate.rb b/test/test_activate.rb index 4be659a..84c3f96 100644 --- a/test/test_activate.rb +++ b/test/test_activate.rb @@ -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 @@ -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 @@ -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)