Skip to content

Commit 5883d8a

Browse files
otegamikou
andcommitted
Use Requires.private only for static linking
GitHub: - #41 (comment) - #41 (comment) By default, exclude `Requires.private` dependencies from cflags and libs output to match pkgconf's behavior. When the new `static: true` option is specified, include `Requires.private` dependencies for static linking scenarios. This change makes the default behavior consistent with pkgconf while still supporting static linking use-cases through an explicit option. Co-Authored-By: kou <[email protected]>
1 parent ca9ad8a commit 5883d8a

File tree

2 files changed

+133
-39
lines changed

2 files changed

+133
-39
lines changed

lib/pkg-config.rb

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,58 @@ def msvc?
3737
/mswin/.match(RUBY_PLATFORM) and /^cl\b/.match(RbConfig::CONFIG["CC"])
3838
end
3939

40-
def package_config(package)
40+
def package_config(package, **options)
4141
PackageConfig.new(package,
42-
:msvc_syntax => msvc?,
43-
:override_variables => @@override_variables,
44-
:paths => @@paths)
42+
{
43+
msvc_syntax: msvc?,
44+
override_variables: @@override_variables,
45+
paths: @@paths,
46+
}.merge(options))
4547
end
4648

47-
def exist?(pkg)
48-
package_config(pkg).exist?
49+
def exist?(pkg, **options)
50+
package_config(pkg, **options).exist?
4951
end
5052

51-
def libs(pkg)
52-
package_config(pkg).libs
53+
def libs(pkg, **options)
54+
package_config(pkg, **options).libs
5355
end
5456

55-
def libs_only_l(pkg)
56-
package_config(pkg).libs_only_l
57+
def libs_only_l(pkg, **options)
58+
package_config(pkg, **options).libs_only_l
5759
end
5860

59-
def libs_only_L(pkg)
60-
package_config(pkg).libs_only_L
61+
def libs_only_L(pkg, **options)
62+
package_config(pkg, **options).libs_only_L
6163
end
6264

63-
def cflags(pkg)
64-
package_config(pkg).cflags
65+
def cflags(pkg, **options)
66+
package_config(pkg, **options).cflags
6567
end
6668

67-
def cflags_only_I(pkg)
68-
package_config(pkg).cflags_only_I
69+
def cflags_only_I(pkg, **options)
70+
package_config(pkg, **options).cflags_only_I
6971
end
7072

71-
def cflags_only_other(pkg)
72-
package_config(pkg).cflags_only_other
73+
def cflags_only_other(pkg, **options)
74+
package_config(pkg, **options).cflags_only_other
7375
end
7476

75-
def modversion(pkg)
76-
package_config(pkg).version
77+
def modversion(pkg, **options)
78+
package_config(pkg, **options).version
7779
end
7880

79-
def description(pkg)
80-
package_config(pkg).description
81+
def description(pkg, **options)
82+
package_config(pkg, **options).description
8183
end
8284

83-
def variable(pkg, name)
84-
package_config(pkg).variable(name)
85+
def variable(pkg, name, **options)
86+
package_config(pkg, **options).variable(name)
8587
end
8688

87-
def check_version?(pkg, major=0, minor=0, micro=0)
88-
return false unless exist?(pkg)
89-
ver = modversion(pkg).split(".").collect {|item| item.to_i}
89+
def check_version?(pkg, major=0, minor=0, micro=0, **options)
90+
return false unless exist?(pkg, **options)
91+
ver = modversion(pkg, **options).split(".").collect {|item| item.to_i}
9092
(0..2).each {|i| ver[i] = 0 unless ver[i]}
9193

9294
(ver[0] > major ||
@@ -95,27 +97,27 @@ def check_version?(pkg, major=0, minor=0, micro=0)
9597
ver[2] >= micro))
9698
end
9799

98-
def have_package(pkg, major=nil, minor=0, micro=0)
100+
def have_package(pkg, major=nil, minor=0, micro=0, **options)
99101
message = "#{pkg}"
100102
unless major.nil?
101103
message << " version (>= #{major}.#{minor}.#{micro})"
102104
end
103105
major ||= 0
104106
result = checking_for(checking_message(message), "%s") do
105-
if check_version?(pkg, major, minor, micro)
106-
"yes (#{modversion(pkg)})"
107+
if check_version?(pkg, major, minor, micro, **options)
108+
"yes (#{modversion(pkg, **options)})"
107109
else
108-
if exist?(pkg)
109-
"no (#{modversion(pkg)})"
110+
if exist?(pkg, **options)
111+
"no (#{modversion(pkg, **options)})"
110112
else
111113
"no (nonexistent)"
112114
end
113115
end
114116
end
115117
enough_version = result.start_with?("yes")
116118
if enough_version
117-
libraries = libs_only_l(pkg)
118-
dldflags = libs(pkg)
119+
libraries = libs_only_l(pkg, **options)
120+
dldflags = libs(pkg, **options)
119121
dldflags = (Shellwords.shellwords(dldflags) -
120122
Shellwords.shellwords(libraries))
121123
dldflags = dldflags.map {|s| /\s/ =~ s ? "\"#{s}\"" : s }.join(" ")
@@ -125,11 +127,11 @@ def have_package(pkg, major=nil, minor=0, micro=0)
125127
else
126128
$LDFLAGS += " " + dldflags
127129
end
128-
$CFLAGS += " " + cflags_only_other(pkg)
130+
$CFLAGS += " " + cflags_only_other(pkg, **options)
129131
if defined?($CXXFLAGS)
130-
$CXXFLAGS += " " + cflags_only_other(pkg)
132+
$CXXFLAGS += " " + cflags_only_other(pkg, **options)
131133
end
132-
$INCFLAGS += " " + cflags_only_I(pkg)
134+
$INCFLAGS += " " + cflags_only_I(pkg, **options)
133135
end
134136
enough_version
135137
end
@@ -408,6 +410,7 @@ def initialize(name, options={})
408410
@paths.unshift(*(@options[:paths] || []))
409411
@paths = normalize_paths(@paths)
410412
@msvc_syntax = @options[:msvc_syntax]
413+
@static = @options[:static]
411414
@variables = @declarations = nil
412415
override_variables = self.class.custom_override_variables
413416
@override_variables = parse_override_variables(override_variables)
@@ -528,7 +531,12 @@ def collect_requires(&block)
528531

529532
private
530533
def collect_cflags
531-
target_packages = [self, *all_required_packages]
534+
target_packages = [self]
535+
if @static
536+
target_packages += all_required_packages
537+
else
538+
target_packages += required_packages
539+
end
532540
cflags_set = []
533541
target_packages.each do |package|
534542
cflags_set << package.declaration("Cflags")
@@ -580,7 +588,12 @@ def normalize_cflags(cflags)
580588
end
581589

582590
def collect_libs
583-
target_packages = [*required_packages, self]
591+
if @static
592+
target_packages = all_required_packages
593+
else
594+
target_packages = required_packages
595+
end
596+
target_packages += [self]
584597
libs_set = []
585598
target_packages.each do |package|
586599
libs_set << package.declaration("Libs")

test/test-pkg-config.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "mkmf"
22
require "tempfile"
3+
require "fileutils"
34

45
require "pkg-config"
56

@@ -314,4 +315,84 @@ def test_equals_to
314315
parse_requires("fribidi = 1.0"))
315316
end
316317
end
318+
319+
sub_test_case("static option") do
320+
def create_pc_file(name, content)
321+
pc_dir = File.join(@tmpdir, "pkgconfig")
322+
FileUtils.mkdir_p(pc_dir)
323+
pc_path = File.join(pc_dir, "#{name}.pc")
324+
File.write(pc_path, content)
325+
pc_dir
326+
end
327+
328+
def setup
329+
PackageConfig.clear_configure_args_cache
330+
@tmpdir = Dir.mktmpdir
331+
@pc_dir = create_pc_file("main-package", <<-PC)
332+
prefix=/usr/local
333+
libdir=${prefix}/lib
334+
includedir=${prefix}/include
335+
336+
Name: main-package
337+
Description: Main package for testing
338+
Version: 1.0.0
339+
Requires: dep-package
340+
Requires.private: private-dep-package
341+
Libs: -L${libdir} -lmain
342+
Cflags: -I${includedir}/main
343+
PC
344+
create_pc_file("dep-package", <<-PC)
345+
prefix=/usr/local
346+
libdir=${prefix}/lib
347+
includedir=${prefix}/include
348+
349+
Name: dep-package
350+
Description: Dependency package
351+
Version: 1.0.0
352+
Libs: -L${libdir} -ldep
353+
Cflags: -I${includedir}/dep
354+
PC
355+
create_pc_file("private-dep-package", <<-PC)
356+
prefix=/usr/local
357+
libdir=${prefix}/lib
358+
includedir=${prefix}/include
359+
360+
Name: private-dep-package
361+
Description: Private dependency package
362+
Version: 1.0.0
363+
Libs: -L${libdir} -lprivate-dep
364+
Cflags: -I${includedir}/private-dep
365+
PC
366+
end
367+
368+
def teardown
369+
FileUtils.rm_rf(@tmpdir)
370+
end
371+
372+
def test_cflags_without_static
373+
package = PackageConfig.new("main-package", paths: [@pc_dir])
374+
assert_equal("-I/usr/local/include/main -I/usr/local/include/dep",
375+
package.cflags)
376+
end
377+
378+
def test_cflags_with_static
379+
package = PackageConfig.new("main-package", paths: [@pc_dir], static: true)
380+
assert_equal("-I/usr/local/include/main " +
381+
"-I/usr/local/include/private-dep " +
382+
"-I/usr/local/include/dep",
383+
package.cflags)
384+
end
385+
386+
def test_libs_without_static
387+
package = PackageConfig.new("main-package", paths: [@pc_dir])
388+
assert_equal("-L/usr/local/lib -ldep -lmain",
389+
package.libs)
390+
end
391+
392+
def test_libs_with_static
393+
package = PackageConfig.new("main-package", paths: [@pc_dir], static: true)
394+
assert_equal("-L/usr/local/lib -lprivate-dep -ldep -lmain",
395+
package.libs)
396+
end
397+
end
317398
end

0 commit comments

Comments
 (0)