Commit graph

116 commits

Author SHA1 Message Date
Philip Withnall
2ad16fa7ac pkg: Make ordering of output from print_package_list() deterministic
The iteration order of GHashTable changed in GLib 2.59, which broke the
check-print-options test, as it relied on a fixed output order from the
--list-all argument to pkg-config.

Fix that by making print_package_list() output in alphabetical order by
Package.key; and update the test to match. This should work with older
and newer versions of GLib.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-04-03 13:04:38 +01:00
Dan Nicholson
df6e4b6cf1 pkg: Check INCLUDE environment variable for MSVC
On Windows builds when --msvc-syntax is in use, add paths in the INCLUDE
environment variable to the system include search path and ignore the
various GCC environment variables. See
https://msdn.microsoft.com/en-us/library/73f9s62w.aspx for details.

https://bugs.freedesktop.org/show_bug.cgi?id=94729
2017-03-20 11:34:02 -05:00
Dan Nicholson
d5d8074893 Respect sysroot for -isystem and -idirafter
Treat -isystem and -idirafter as -I Cflags since they control the
compiler search path. Adjust the sysroot handling so that the arguments
to these options have the sysroot prefixed. However, leave them out of the
system Cflags handling since these directives are explicitly trying to
adjust the compiler's system header search behavior.

The special-flags test case output needs adjustment since all the flags
are now considered -I flags and come out in the order specified in the
pc file.

https://bugs.freedesktop.org/show_bug.cgi?id=97337
2017-03-20 07:13:23 -05:00
v4hn
4ade8fc901 pkg: Include CPATH in header search path
Look for the CPATH environment variable when constructing the -I header
search path like GCC does. See
https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html.

https://bugs.freedesktop.org/show_bug.cgi?id=99224
2017-03-19 09:13:59 -05:00
Marco Diego Aurélio Mesquita
c53385b5db Optimization to load only needed .pc files
Currently pkg-config scans all .pc files from the search path during
initialization. That makes some of the code simpler and works fine when
there are not many .pc files on the system. When there are a lot of .pc
files, then this represents a lot of wasted effort.

Rework the package gathering so that it happens as needed. To support
the --list-all mode, the scanning at initialization can still be done.

https://bugs.freedesktop.org/show_bug.cgi?id=98215
2016-12-22 13:29:12 -06:00
Dan Nicholson
983052ddf8 Remove unused package_list_strip_duplicates
After 908cdd0, there are no more duplicates in the package list and this
function is unused.
2016-08-22 14:36:50 -07:00
Matthew Hanna
908cdd062a Improve performance of package list expansion
Adds a hash table to the package list expansion to avoid iterating over
the children of package nodes that have already been visited. Without
this, the expansion is exponential. For library sets with a high degree
of dependency, iteration over the tree with revisiting results, in
practice, in significant slow down at best and pkg-config failure due to
memory exhaustion at worst. The resulting algorithm is equivalent to a
topological sort.
2016-08-22 14:34:30 -07:00
Dan Nicholson
5164b9dbab Revert "Quote pc_path virtual variable"
This reverts commit a6e8749ada. With the
--variable output only being unquoted when it appears needed, this can
return to being a normally defined value.

https://bugs.freedesktop.org/show_bug.cgi?id=93284
2016-02-26 08:56:52 -08:00
Dan Nicholson
1c564a3583 Only unquote --variable when it appears quoted
The change to unquote values in the --variable output broke users that
had shell special characters in the variable. Instead, only unquote if
the value starts with " or '. A larger fix to do a full unquote, split
and escaping like --cflags/--libs is possible, but that might break the
old semantics even further.

Add a new function, parse_package_variable(), to handle that logic.

https://bugs.freedesktop.org/show_bug.cgi?id=93284
2016-02-26 08:56:52 -08:00
Dan Nicholson
e6d33fb129 Revert "Unquote values of requested variables"
This reverts commit 50c2867f4a.
g_shell_quote doesn't match the quoting behavior of pkg-config when the
same variables are output via --cflags and similar.

https://bugs.freedesktop.org/show_bug.cgi?id=93284
2016-02-26 08:52:25 -08:00
Alexander Larsson
ae0a8b1ad5 Allow overriding package variables with env vars
pkg-config allows a way to override package variables through the
--define-prefix interface, but this is very cumbersome to do in a global
way since it always needs to be passed on the command line and the
override cannot be scoped to a single packge.

Allow overriding package variables using environment variables of the
form PKG_CONFIG_$PACKAGE_$VARIABLE. For example, setting
PKG_CONFIG_GLADEUI_2_0_CATALOGDIR will override the variable
"catalogdir" in the "gladeui-2.0" package.

https://bugs.freedesktop.org/show_bug.cgi?id=90917
2016-01-29 14:32:24 -08:00
Alexander Larsson
9ef2d3475d Pass key into parse_package_key so it can be set early
We will need this to properly pick up environment-overridden
per-package vairables.

https://bugs.freedesktop.org/show_bug.cgi?id=90917
2016-01-29 14:25:21 -08:00
Chun-wei Fan
dd57ff3547 pkg.c: Port from dirent to GDir
Port the code to use the GDir APIs from dirent so that we don't have to
depend on a dirent implementation on platforms that do not have it, such
as Visual Studio, so that it is possible to build pkg-config out of the
box on any platform that is supported by GLib (and the bundled GLib).
2015-10-26 09:15:53 -07:00
Dan Nicholson
a6e8749ada Quote pc_path virtual variable
Since we unquote variables on printing, we want to quote in before adding
to the database. For all other variables defined in .pc files, we assume
that the user has quoted the variable correctly.

This was broken with the fix for bug 67904, but seems to only affect
Windows builds.
2014-12-09 06:29:26 -08:00
Dan Nicholson
9294307b21 Handle pcfiledir variable like normal variable
Rather than responding to the pcfiledir variable specially, store it
in the hash table with all the other package specific variables. This
does result in pcfiledir showing up in the --print-variables output.
2014-12-09 05:24:43 -08:00
Daniel Macks
30437b47c8 Use same token for malloc as for string construction
The goal is for pkgname to hold the name without the trailing ".pc"
extension (according to the meaning of EXT_LEN as used in the
ends_in_dotpc() function). But the malloc is hardcoded as 2. If the
extension were to be changed, the number of characters being strncpy'ed
would not match the size of the target buffer. Instead, the malloc
should use the EXT_LEN token (adjusted for the trailing \0).

Freedesktop #80378 (https://bugs.freedesktop.org/show_bug.cgi?id=80378)
2014-11-12 07:47:57 -08:00
Daniel Macks
a4ef3552eb Bump scanned_dir_count even if dir cannot be read
This allows the --debug output to follow the actual order of the
directories specified in the path.

  $ PKG_CONFIG_PATH=/nosuch1:/nosuch2:/dir-with-foo.pc
  $ pkg-config --debug foo
  [...]
  Path position of 'foo' is 1

With this change, the path position above would be 3, matching the
position in PKG_CONFIG_PATH.

Freedesktop #80380 (https://bugs.freedesktop.org/show_bug.cgi?id=80380)
2014-11-12 07:45:19 -08:00
Andrew Oakley
ce1a2416ce Strip system library directories reliably
This loop was changed from a while loop to a for loop in commit
9bf6277b, but the iterator is now advanced twice each time round the
loop.
2014-09-27 16:20:37 -07:00
Marek Kasik
50c2867f4a Unquote values of requested variables
If a --variable is requested, make sure to remove the quoting since it's
likely the value will be used verbatim in shell command substitution. If
the quotes remain in the value, they'd get embedded in the shell
variable, too.

Freedesktop #67904 (https://bugs.freedesktop.org/show_bug.cgi?id=67904)
2013-09-15 10:27:35 -07:00
Dan Nicholson
f26a505826 Strip trailing space from --cflags/--libs output
pkg-config leaves a trailing space on the flags output as it may have to
do multiple iterations to collect all output and adding the space makes
concatenation simple. However, this leaves a trailing space on the full
returned string unless it's empty. Strip the trailing space after all
the processing is complete.
2013-05-31 05:53:47 -07:00
Dan Nicholson
a65ee007bb Avoid const warning on Win32
scan_dir() alters the directory name in place on Win32 to convert \ to
/, so it can't be treated as const.
2013-05-20 05:56:50 -07:00
Dan Nicholson
4623b5e85a Split rpmvercmp code into separate file
The rpmvercmp code is taken directly from upstream. Moving it to a
separate file allows it to be diffed and synced easily with the upstream
version. It also becomes really easy to switch to the actual rpmlib
version if that's ever desired.
2013-05-17 05:36:12 -07:00
Dan Nicholson
1694230365 Merge branch 'flag-order-fixes' 2013-01-22 05:11:59 -08:00
Dan Nicholson
ae2fef2c05 Greatly simplify circular Requires checking
Instead of keeping of list of packages in the current Requires chain and
searching it repeatedly, just mark each package as part of the chain or
not. This nearly cuts in half the time on a particularly rough torture
test I have.
2013-01-16 16:42:48 -08:00
Dan Nicholson
9adfd9ebfc Only strip duplicate arguments when they appear consecutively
pkg-config strips all duplicate arguments from the flag output string.
This is done for 2 reasons:

1. When a package shows up twice in the final package list after
resolving all Requires, stripping was used to ensure it's flags only
showed up once at the correct location.

2. An optimization so that the output string is not excessively long.

Since commit c6ec7869, 1. is no longer necessary as the final package
list only contains each package once. 2. causes problems when applied
too aggressively since some arguments have different semantics depending
on the prior or subsequent arguments.

To keep a bit of optimization, the stripping is reduced to only removing
consecutive duplicate arguments. This should ensure that the semantics
are kept intact while removing obviously unnecessary arguments.

The drawback is that some arguments will now appear multiple times in
the output when they previously would have only appeared once. Here we
have to rely on the tools using these arguments to handle the duplicates
appropriately since there is no way for pkg-config to encode all the
semantics of those arguments. Another thing that can help this situation
is if pkg-config is used for all packages in the Requires chain so that
the Libs/Cflags of each package only pertain to itself and don't encode
the compiling/linking rules of a 3rd party package.

Freedesktop #16101 (https://bugs.freedesktop.org/show_bug.cgi?id=16101)
2012-12-04 13:04:57 -08:00
Dan Nicholson
5b6ec1b6e9 Output -l and other Libs flags in the order they appear
Often other Libs flags have semantics that are based on their context
with -l arguments. For example, the GNU linker options
-Bdynamic/-Bstatic pertain only to the objects or link options that
follow them. So, a valid link command containing these options would get
mangled by pkg-config since it separates -l flags from others..

-Bdynamic -la -Bstatic -lb -> -Bdynamic -Bstatic -la -lb

Instead, output -l and other Libs flags in a single pass so they mantain
their ordering.

Freedesktop #19950 (https://bugs.freedesktop.org/show_bug.cgi?id=19950)
2012-12-04 13:04:57 -08:00
Dan Nicholson
9bf6277b9c Keep Libs and Cflags together to maintain ordering
Instead of splitting to -l/-L/other and -I/other, keep the args together
and mark each argument with its type. Then we can maintain order all the
way through.
2012-12-04 13:04:57 -08:00
Dan Nicholson
05f319d3e5 Output -L Libs flags before other Libs flags
Outputting other Libs flags such as -Wl,foo just prior to the -l Libs
flags gives a better chance the --libs output will be correct. This
should be no change in the usage of the output since pkg-config
currently groups all flag types together.
2012-12-04 13:04:57 -08:00
Dan Nicholson
28c6468405 Pass around flags types instead of function pointers
This makes the code uglier in the short term, but prepares for using
merged lists of Cflags and Libs in one go.
2012-12-04 13:04:57 -08:00
Dan Nicholson
75a95f6f8b Limit merging of packages and flags to path or dependency order
Unify the get_multi_merged functions since there are only two valid ways
to do the merging of packages and flags.

1. Packages are sorted by their position in the pkg-config path and then
   duplicate flags are stripped from the beginning of the list. This
   pertains to -I and -L flags.

2. Packages are sorted by dependency with most required last and then
   duplicate flags are stripped from the end of the list. This ensures
   that flags that come from packages required by multiple others come
   later in the output. This applies to all non-L/I flags.
2012-12-04 13:04:57 -08:00
Dan Nicholson
8f354125d1 Unify string list stripping functions and operate on list in place
There were two string list stripping functions, when the only difference
was to operate from the beginning or end of the list. Also, the function
was copying the list and operating on that unnecessarily.
2012-12-04 13:04:57 -08:00
Dan Nicholson
d0e4ba893a Cleanup prototypes for list operating functions
Instead of having a list as an out parameter when it they have no
elements, just return the list to the caller. Cleans up some code a bit.
2012-12-04 13:04:57 -08:00
Dan Nicholson
e0d3ce93a0 Sort -other Libs and Cflags by package order instead of path order
For some flags, pkg-config will sort them by the depth of their .pc path
before outputting. The idea is that flags from a deeper path should come
earlier in the command line. This makes sense for -L and -I flags, but
not for generic linker and compiler flags. For these flags and -l flags,
it makes sense to sort them only by package order.
2012-12-04 13:04:57 -08:00
Dan Nicholson
43f20b96a3 Fix one hash table optimization to use key=value
Commit 428335e changed some hash tables to be used as sets where the key
equals the value since glib has some optimization for that.
Unfortunately, the package list stripping table wasn't fixed corectly.
2012-12-04 13:04:01 -08:00
Dan Nicholson
428335e2b5 Optimize hash table when usage is only as a set
When searching for duplicate strings in the output list, a hash table is
used to keep track of which strings have been seen. This usage as a set
can be optimized as described in the documentation to not allocate or
free the hash table values:

http://developer.gnome.org/glib/stable/glib-Hash-Tables.html#glib-Hash-Tables.description
2012-12-03 08:25:40 -08:00
Dan Nicholson
a7b465806f Remove duplicate string list elements in place
Similar to the removal of duplicate packages, this can be more efficient
if we don't build a new list and instead just remove the elements in
place.
2012-12-03 07:34:00 -08:00
Dan Nicholson
360a614af8 Start from end of package lists when processing Requires
Prior to commit 6ecf318, the resolved list of required packages was
built in an appending way where each package on the command line or in
Requires would appear in the list in the order they appeared. With
6ecf318, that list building was changed to prepending, which had a
subtle change on the resolved order.

For example, suppose package a has "Requires: b c d". Previously, the
list would be built as a->b->c->d by appending each as they were
encountered. Now, the list is built by walking all the way down the
dependency chain for each package in a depth first manner and prepending
packages while unwinding. This would result in the package ilst being
a->d->c->b. This same effect happens with the command line packages
where previously requesting packages x and y would create a package list
of x->y and now produces a list of y->x.

While technically these should be the same since there are no
interdependencies, it's causes flags to be output in different order
than previously in pkg-config. This can be seen most readily in the
check-gtk test.

Instead, operate on the package lists backwards when building the
resolved package list.
2012-12-03 07:08:20 -08:00
Dan Nicholson
be455b79fe Traverse list backwards instead of copying and reversing
Walking a GSList backwards involved copying and reversing it so that the
the original list could remain undisturbed. This is wasteful with a
GList where we can just start at the end of the list and work backwards.
2012-12-03 07:05:43 -08:00
Dan Nicholson
428362266e Remove duplicate packages after resolving requires
Makes the resolved package list be correctly serialized with each
package only appearing once. This provides more consistency between the
various flag outputs by ensuring that the flags from each package are
only grabbed once. This makes a difference since the duplicate flag
stripping happens from the end of the output (-l) or the beginning of
the output (-L/-I/other).
2012-12-03 07:05:43 -08:00
Dan Nicholson
e8086bc54f Convert to doubly-linked GList
Using a doubly-linked list allows it to be easily traversed in both
directions and makes removing nodes in place much simpler. This adds an
extra pointer to each node and associated manipulation during any list
processing, but this trade seems acceptable over the repeated hacks to
work with singly-linked lists.
2012-12-03 07:02:27 -08:00
Dan Nicholson
840cf3c97e Use standard GSList functions to merge package lists
Using the GSList functions instead of manually adjusting the list
pointers seems safer and allows an easier path to using another glib
list type if necessary.
2012-11-29 05:54:29 -08:00
Dan Nicholson
e2910a6afd Remove extra spaces from list debugging output
Also, make pre- and post-sorting output aligned to easily ordering
differences.
2012-11-28 05:41:10 -08:00
Dan Nicholson
1d285978bb Use package filename rather than Name in debugging
The Package key member corresponds to the module filename with the .pc
stripped off while the name member corresponds to the Name field in the
.pc file. The latter is almost never used in practice and just makes
debugging more difficult.
2012-11-28 05:41:05 -08:00
Dan Nicholson
90ed8f193f Allow all combinations of --cflags and --libs variants
Use a bitmask to keep track of what Libs/Cflags to output. This makes it
simple to handle any combination of --cflags and --libs option variants.
A lot of excess code is removed in the process as all the flags options
can now be carried around in a single variable.

Freedesktop #54388 (https://bugs.freedesktop.org/show_bug.cgi?id=54388)
2012-11-03 11:07:59 -07:00
Dan Nicholson
90e0ec0f6f Nest copying and merging of flags to avoid repeated traversals
When merging the flags from all the packages together, each flags list
was being copied and then concatenated to then end of the combined list.
This was extremely inefficient because it caused the combined list to be
traversed multiple times to find the end. Instead, nest the copying and
merging of the flags together so the last element is always tracked and
can easily be appended to.

Freedesktop #54716 (https://bugs.freedesktop.org/show_bug.cgi?id=54716)
2012-10-30 06:01:42 -07:00
Dan Nicholson
3aa62167be Only match uninstalled packages that end in "-uninstalled" with hyphen
pkg-config(1) states that installed packages should be appended with
"-uninstalled". However, the code was checking only for trailing
"uninstalled" without the hyphen. Make the code consistent with the
documentation.

Freedesktop #54379
2012-10-13 09:25:34 -07:00
Dan Nicholson
6d6dd43e75 Support circular Requires loops
After the packages are parsed, pkg-config recurses through all the
required packages to generate one list. Before descending another level,
check to see if the package has already been handled and skip it. This
allows packages to require each other circularly by breaking the loop.

A test has been added resolving a two level deep circular dependency.

Freedesktop #7331
2012-10-13 08:14:35 -07:00
Dan Nicholson
3f1f6e79b7 Delay converting Requires entries to Packages until after parsing
When the parser encounters Requires or Requires.private, it immediately
tries to sees if we have a parsed package for that entry. If not it
tries to locate the needed file and parse it out. If there's a circular
dependency, this will eventually error opening too many files.

Instead, just store the requires entries so the parsing completes and
the package is added to the database. After parsing, the entries can be
resolved into Packages and any circular requires entries will find the
first package in the database.

This is a partial fix for Freedesktop #7331.
2012-10-13 08:14:35 -07:00
Dan Nicholson
6ecf318c92 Consistently resolve requires depth-first to fix non-l flag ordering
recursive_fill_list() is used to order Requires and Requires.private,
but it relied on fill_one_level() to make the list adjustments as it
descended the package tree. There were two issues with this approach:

1. It added all the dependencies from a package immediately rather than
   descending through each dependency first. This made it sort of mix
   between depth- and breadth-first resolving.

2. It did not add the requested package to the list, forcing the caller
   to add it.

This simplifies the code so that it descends all the way to the least
dependent package and prepends them as it unwinds. This ensures the
ordering will be sorted from most dependent to least dependent package.

Ordering of -l flags is corrected by a later sorting, but this fixes
ordering on non-l flags. Add a new test specifically for non-l Libs
flags.

Freedesktop #34504
2012-10-13 08:14:35 -07:00
Dan Nicholson
02ca585fa6 Make sure recursion only happens with requires
The function recursive_fill_list() is designed to descend lists of
packages, so it only makes sense to use with Requires and
Requires.private. Ensure it to make later code additions simpler.
2012-10-13 08:14:35 -07:00