Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide option --define-variable to pkg-config from CMake / PkgConfig

In an attempt to port a Linux program on Windows (using cygwin and gcc), I am trying to properly find libraries that provide pkg-config files (in my case, the libraries are brought by Graphviz).

The straightforward method works on Linux:

find_package(PkgConfig)
pkg_check_modules(LIB_GRAPHVIZ libcdt libgvc libgraph)

On Windows, I manually installed Graphviz using the installer (cygwin path: /cygdrive/c/graphviz). In order to find the pkg-config files, I export the PKG_CONFIG_PATH variable so that the .pc files are found prior configuring CMake:

export PKG_CONFIG_PATH=/cygdrive/c/graphviz/pkgconfig
cmake $srcdir

Configuration works fine, however the .pc files do not have the appropriate prefix (which leads to invalid include and link directories):

$ pkg-config --variable=prefix libgraph
/c/graphviz-mingw/local.2.20.3

Regarding pkg-config, the solution is to invoke it with the --define-variable argument, for example here is how to get a valid includedir:

$ pkg-config --define-variable=prefix=/cygdrive/c/graphviz --variable=includedir libgraph
/cygdrive/c/graphviz/include/graphviz

My problem: how can I provide the --define-variable argument to pkg-config when it is invoked from within CMake?

PkgConfig defines PKG_CONFIG_EXECUTABLE that points to the pkg-config program; I tried to append my option to this variable:

set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_EXECUTABLE} --define-variable=/cygdrive/c/graphviz")

But pkg_check_modules() fails to detect my libraries. My understanding is that the call to execute_process() considers the content of ${PKG_CONFIG_EXECUTABLE} as an invalid program.

Any workaround I can use to address my problem?


Edit: actually, a simple workaround is to use different .pc files. But I am curious whether it is possible to solve the issue without having to alter these files.

like image 411
piwi Avatar asked Dec 09 '14 09:12

piwi


People also ask

Does CMake use pkg-config?

CMake can generate pkg-config . pc files for packages. The . pc file can be used by many build systems.

What is pkg-config -- cflags?

--cflags means the pkg-config should give the compilation flags for the listed packages. --libs means the pkg-config should give the linking information for the listed packages. and dbus-1 is the name of the package. Follow this answer to receive notifications.

What is pkg-config search path?

This variable is used to augment pkg-config's search path. On a typical Unix system, it will search in the directories /usr/lib/pkgconfig and /usr/share/pkgconfig. This will usually cover system installed modules. However, some local modules may be installed in a different prefix such as /usr/local.

What is Pkgconfig in Linux?

pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line so an application can use gcc -o test test. c `pkg-config --libs --cflags glib-2.0` for instance, rather than hard-coding values on where to find glib (or other libraries).


1 Answers

Thanks for your idea of overwriting PKG_CONFIG_EXECUTABLE! This does indeed work for me if the parameter is separated by a semicolon, like this:

set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_EXECUTABLE};--define-variable=/cygdrive/c/graphviz")

CMake will treat this as a list, and since execute_process() expects a list of command parameters, this works well (tested with CMake 3.17 under Linux).

like image 179
oliver Avatar answered Oct 03 '22 14:10

oliver