-
Notifications
You must be signed in to change notification settings - Fork 14
Add mergeback for -D and -W cflags to match pkgconf behavior #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
When multiple packages have the same -D or -W flags, pkgconf
removes previous occurrences and keeps only the last one
(mergeback).
This prevents flag duplication like "-DNOMINMAX"
appearing many times.
```console$ pkgconf --cflags re2
-pthread -DNOMINMAX
$ ruby -r pkg-config -e 'puts PKGConfig.cflags("re2")'
-pthread -DNOMINMAX -DNOMINMAX -DNOMINMAX ...
```
Flags excluded from mergeback (kept as duplicates):
- -Wa, (assembler options)
- -Wl, (linker options)
- -Wp, (preprocessor options)
This PR only supports mergeback for -D and -W flags.
Other flags that pkgconf also mergebacks are not yet
supported.
lib/pkg-config.rb
Outdated
| normalized_cflags | ||
| end | ||
|
|
||
| def mergeback_flags(flags) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment that we want to implement pkgconf's pkgconf_fragment_copy() compatible behavior here?
https://github.com/pkgconf/pkgconf/blob/pkgconf-2.5.1/libpkgconf/fragment.c#L381-L416
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: 19cd8b8
lib/pkg-config.rb
Outdated
| mergebacked_flags = [] | ||
| flags.each do |flag| | ||
| if mergeable_flag?(flag) | ||
| mergebacked_flags.delete(flag) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment that this may be slow because this checks mergebacked_flags N (the number of mergeable flags) times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: de47ab7
lib/pkg-config.rb
Outdated
| flag.gsub(/\A-I/, "/I") | ||
| end | ||
| end | ||
| other_flags = mergeback_flags(other_flags) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use merge_back not mergeback?
Could you use cflags not flags? We only support C flags for now, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: c6b9da8
test/test-pkg-config.rb
Outdated
| end | ||
| end | ||
|
|
||
| sub_test_case("cflags mergeback") do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simplify this?
sub_test_case("#merge_back_cflags") do
def merge_back_cflags(cflags)
@glib.__send__(:merge_back_cflags, cflags)
end
def test_define
assert_equal("-I/main -I/dep -DFOO",
merge_back_cflags("-I/main -DFOO -I/dep -DFOO"))
end
endSee also:
pkg-config/test/test-pkg-config.rb
Lines 282 to 316 in ca9ad8a
| sub_test_case("#parse_requires") do | |
| def parse_requires(requires) | |
| @glib.__send__(:parse_requires, requires) | |
| end | |
| def test_broken_version | |
| assert_equal(["fribidi"], | |
| parse_requires("fribidi >= fribidi_required_dep")) | |
| end | |
| def test_greater_than_or_equals_to | |
| assert_equal(["fribidi"], | |
| parse_requires("fribidi >= 1.0")) | |
| end | |
| def test_greater_than | |
| assert_equal(["fribidi"], | |
| parse_requires("fribidi > 1.0")) | |
| end | |
| def test_less_than_or_equals_to | |
| assert_equal(["fribidi"], | |
| parse_requires("fribidi <= 1.0")) | |
| end | |
| def test_less_than | |
| assert_equal(["fribidi"], | |
| parse_requires("fribidi < 1.0")) | |
| end | |
| def test_equals_to | |
| assert_equal(["fribidi"], | |
| parse_requires("fribidi = 1.0")) | |
| end | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: 062f4be
|
Thank you for reviews. I've just addressed all of review comments, so far. |
lib/pkg-config.rb
Outdated
| end | ||
| all_cflags = normalize_cflags(Shellwords.split(cflags_set.join(" "))) | ||
| path_flags, other_flags = all_cflags.partition {|flag| /\A-I/ =~ flag} | ||
| path_flags, cflags = all_cflags.partition {|flag| /\A-I/ =~ flag} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep other_flags because -I is also C flags and this method is collect_cflags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: 5230529 Sure.
It's because this method collect cflags, so all of them are cflags too.
lib/pkg-config.rb
Outdated
| # NOTE: This may be slow because this checks merge_back_cflags N times (where | ||
| # N is the number of mergeable flags). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this to just before mergebacked_cflags.delete(cflag)?
The code may be slow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: b9a7cdc
lib/pkg-config.rb
Outdated
| # NOTE: This may be slow because this checks merge_back_cflags N times (where | ||
| # N is the number of mergeable flags). | ||
| def merge_back_cflags(cflags) | ||
| mergebacked_cflags = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| mergebacked_cflags = [] | |
| merge_backed_cflags = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: 6e7d245
test/test-pkg-config.rb
Outdated
| "-Wa,--noexecstack", "-Wl,--as-needed", "-Wp,-DFOO"])) | ||
| end | ||
|
|
||
| def test_mixed_flags |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def test_mixed_flags | |
| def test_mixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: c21530f
|
Thank you for reviews. I've just addressed all of review comments, so far. |
|
Update the PR description before I merge this. |
When multiple packages have the same -D or -W flags, pkgconf removes previous occurrences and keeps only the last one (merge back).
This prevents flag duplication like "-DNOMINMAX"
appearing many times.
Flags excluded from merge back (kept as duplicates):
This PR only supports merge back for -D and -W flags. Other flags that pkgconf also merges back are not yet supported.