Add mergeback for -D and -W cflags to match pkgconf behavior#43
Add mergeback for -D and -W cflags to match pkgconf behavior#43kou merged 9 commits intoruby-gnome:mainfrom
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.
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
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.
Could you add a comment that this may be slow because this checks mergebacked_flags N (the number of mergeable flags) times?
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.
Could you use merge_back not mergeback?
Could you use cflags not flags? We only support C flags for now, right?
test/test-pkg-config.rb
Outdated
| end | ||
| end | ||
|
|
||
| sub_test_case("cflags mergeback") do |
There was a problem hiding this comment.
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
|
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.
We can keep other_flags because -I is also C flags and this method is collect_cflags.
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.
Could you move this to just before mergebacked_cflags.delete(cflag)?
The code may be slow.
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.
| mergebacked_cflags = [] | |
| merge_backed_cflags = [] |
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.
| def test_mixed_flags | |
| def test_mixed |
|
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.