[cmake-developers] Improved support for using cmake-based libraries in non-cmake projects

Alexander Neundorf neundorf at kde.org
Sat Jun 18 09:41:14 EDT 2011


On Friday 17 June 2011, Alexander Neundorf wrote:
> On Tuesday 07 June 2011, Alexander Neundorf wrote:
> > On Monday, June 06, 2011 03:44:20 PM Brad King wrote:
> > > On 06/05/2011 07:14 PM, Eric Noulard wrote:
> > > >>> 2011/6/4 Alexander Neundorf <neundorf at kde.org>:
> > > >>>> * if so, it will check that for FOO_INCLUDES and FOO_LIBRARIES
> > > >>>> * create the command line arguments for the compiler from that
> > > >>>> * print "-I/opt/foo/include" to stdout
> > > 
> > > [snip]
> > > 
> > > > cmake-config would be a [portable] script (or even minimalistic
> > > > binary), which basically call
> > > > a cmake script (for portability).
> > > 
> > > This may be technically challenging, especially for the link options.
> > > CMake's link line generation happens at generation time and is not
> > > accessible to the CMake language.  Furthermore, it depends on a lot
> > > of information about the final library or executable because it is
> > > an expert system [1] (e.g. implicit language runtime libraries for
> > > mixed executables, transitive dependencies, etc.).
> > 
> > I have it basically working here now, including a cmake.m4 macro so you
> > can do CMAKE_CHECK_MODULE() in configure.in.
> > 
> > Checking whether a package has been found is straightforward.
> > 
> > For creating the compile flags I'm using
> > localGen->GetIncludeFlags(language.c_str(), false);
> > 
> > For the linking I currently get the FOO_LIBRARIES variables, create an
> > empty executable target, set the LINKER_LANGUAGE to what the user wants,
> > add all the libraries to the executable and then call
> > localGen->GetTargetFlags()
> > 
> > This is what I get currently, the stuff for libxml2 is from cmake via
> > FindLibXml2.cmake (with an additional libbar.so):
> > 
> > ~/src/tests/pkgconfig/pkgconfig> ./configure
> > checking for pkg-config... /usr/bin/pkg-config
> > checking pkg-config is at least version 0.9.0... yes
> > checking for QtSQL... yes
> > Result: CFLAGS: -DQT_SHARED -I/usr/include/QtSql -I/usr/include/QtCore
> > LIBS: -lQtSql -lQtCore
> > checking for QtXML... yes
> > Result: CFLAGS: -DQT_SHARED -I/usr/include/QtXml -I/usr/include/QtCore
> > LIBS: -lQtXml -lQtCore
> > checking for XFT... yes
> > Result: CFLAGS: -I/usr/include/freetype2   LIBS: -lXft
> > checking for LIBXML2... LibXml2 found.
> > LibXml2 found.
> > yes
> > Result: CFLAGS: -I/usr/include/libxml2   LIBS: -rdynamic -lxml2
> > /opt/bar/lib/libbar.so -Wl,-rpath,/opt/bar/lib   -fPIC
> > configure: creating ./config.status
> > config.status: creating Makefile
> 
> I improved it somewhat, so IMO it is basically working now.
> There is now a branch UsingCMakeLikePkgConfig on stage.
> Would be nice if you could have a look.
> Having this in cmake would make it unnecessary that library developers need
> to install both a FooConfig.cmake file and a foo.pc file.
> 
> And it works IMO better than pkg-config :-)

Usage is like this:

$ cmake --find-package -DNAME=LibXml2 -DMODE=EXIST -DLANGUAGE=C -
DCOMPILER_ID=GNU
LibXml2 found

$ cmake --find-package -DNAME=LibXml2 -DMODE=COMPILE -DLANGUAGE=C -
DCOMPILER_ID=GNU
-I/usr/include

$ cmake --find-package -DNAME=LibXml2 -DMODE=LINK -DLANGUAGE=C -
DCOMPILER_ID=GNU
-rdynamic -lxml2


and the m4 macro looks like
CMAKE_FIND_PACKAGE(VARIABLE-PREFIX, PackageName, Language, CompilerID,
                   [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])


Alex



More information about the cmake-developers mailing list