Target install path question

Alexander Neundorf neundorf at kde.org
Mon Sep 20 21:16:43 CEST 2010


On Monday 20 September 2010, Andreas Pakulat wrote:
> On 20.09.10 14:25:25, Sebastian Trüg wrote:
> > On 09/20/2010 01:04 PM, Andreas Pakulat wrote:
> > > On 20.09.10 12:36:28, Sebastian Trüg wrote:
> > >> Hi,
> > >>
> > >> is there a way to get the exact path of a target install path?
> > >> If I have for example some installation rule like:
> > >>
> > >> install(TARGET foobar DESTINATION lib)
> > >>
> > >> I would like to use the path of the foobar lib in my
> > >> FooBarConfig.cmake to set a variable FOOBAR_LIBRARIES.
> > >> Is that possible without resorting to what the Akonadi version does,
> > >> ie. construct a different path for each architecture?
> > >
> > > I don't know what Akonadi does, but usually the config-files are
> > > installed to <prefix>/lib<suffix>/cmake/<yourmodule> so you can easily
> > > get at the lib or bin or share dir by using CMAKE_CURRENT_LIST_FILE
> > > variable and extracting the path from that. Then use a static relative
> > > path from that like "../../" (for lib<suffix>) or "../../bin|share" for
> > > bin/share.
> > >
> > > Using some 'heuristics' like guessing architecture and using that is
> > > not necessarily reliable as the person building the codebase can change
> > > all details of the target path by using global cmake variables.
> > >
> > > A quick look through the target properties list doesn't seem to
> > > indicate there is any to extract the final install location. Generally
> > > though if you have install(TARGET foobar DESTINATION lib) then it'll
> > > end up in DESTDIR/<prefix>/lib. But I guess you're using some
> > > pre-defined variable(s) which are platform-dependent?
> >
> > no, I do not. But since the file extension differs between platforms I
> > would need to have some heuristic unless cmake provides the full path.
> > This is true for libs as well as for executables.
> >
> > That is why Akonadi does the following which I would really like to
> > avoid:
> >
> > if(WIN32)
> > if(MINGW)
> >   set(AKONADI_COMMON_LIBRARIES
> > "@AKONADI_LIB_DIR@/libakonadiprotocolinternals.dll.a")
> > else(MINGW)
> >   set(AKONADI_COMMON_LIBRARIES
> > "@AKONADI_LIB_DIR@/akonadiprotocolinternals.lib")
> > endif(MINGW)
> > elseif(APPLE)
> >   set(AKONADI_COMMON_LIBRARIES
> > "@AKONADI_LIB_DIR@/libakonadiprotocolinternals.dylib")
> > else()
> >   set(AKONADI_COMMON_LIBRARIES
> > "@AKONADI_LIB_DIR@/libakonadiprotocolinternals.so")
> > endif()
> >
> > Maybe I am missing something. I am trying to write a SopranoConfig.cmake
> > which creates variables for the libraries and also the executables that
> > are installed.
>
> There are two ways:
>
> a) Use find_library with HINTS and provide the calculated absolute path to
> the lib directory (or bin) based off of the Config-file. The resulting
> variable will contain the absolute filename of the library and the function
> takes care of the platform-specifics.
>
> b) use exported targets with INSTALL(TARGETS foo EXPORT myNameSpace) and
> set the variables to the names of the import-targets.
>
> KDevPlatform uses option b) so you could use that as example together with
> the cmake manual (or ask here for more info).


Yes, use exported targets and you don't have to create the full paths yourself 
anymore.

Basic usage:

add_library(foo ${srcs} )


install(TARGETS foo DESTINATION ${somewhere}
                    EXPORT YourExports)

...
later on somewhere:
install(EXPORT YourExports DESTINATION lib/cmake/Foo
                            FILE FooTargets.cmake)

and your FooConfig.cmake should include() the installed FooTargets.cmake.

Alex


More information about the Kde-buildsystem mailing list