[CMake] intercomponent link dependencies?
Nico Schlömer
nico.schloemer at gmail.com
Thu Jan 7 10:19:23 EST 2010
Hi Michael,
I added to the FindMypackage.cmake in the FOREACH(COMPONENT) loop the
following snipped
ADD_LIBRARY(${COMPONENT} STATIC IMPORTED)
SET_TARGET_PROPERTIES( ${COMPONENT} PROPERTIES
IMPORTED_LOCATION "${${UPPERCOMPONENT}_LIBRARY}"
LINK_INTERFACE_LIBRARIES
"${${COMPONENT}_LINK_INTERFACE_LIBRARIES}"
)
where "${${UPPERCOMPONENT}_LIBRARY}" is the path of the respective
library in the filesystem, and
"${${COMPONENT}_LINK_INTERFACE_LIBRARIES}" a semicolon-separated list
of dependencies of ${COMPONENT} (being either another component, or
something of the form /path/to/libfancy.a). CMake does create all the
Makefiles -- good -- but the dependency information doesn't sit in
there (?).
I expected that I can go like
FIND_PACKAGE( Mypackage COMPONENTS foocomp barcomp )
and that all the necessary stuff would be included automatically. --
Well, now that I think about it this is probably a pipe dream. What
would I further have to include?
Cheers,
Nico
On Thu, Jan 7, 2010 at 12:47 PM, Michael Wild <themiwi at gmail.com> wrote:
> Hi Nico
>
> In that case you need to do this:
>
> add_library(${COMPONENT} IMPORTED)
> set_target_properties(${COMPONENT} PROPERTIES
> IMPORTED_LOCATION "${${COMPONENT}_LIBRARY}"
> LINK_INTERFACE_LIBRARIES "${${COMPONENT}_LINK_INTERFACE_LIBRARIES}"
> )
>
> where each of the components X has a variable X_LIBRARY containing its path on the system (e.g. determined by find_library(X_LIBRARY ...)) and a variable X_LINK_INTERFACE_LIBRARIES which you have to set outside the foreach loop.
>
> HTH
>
> Michael
>
> On 7. Jan, 2010, at 11:51 , Nico Schlömer wrote:
>
>> Hi Michael,
>>
>> thanks for the explanations.
>>
>> I'm indeed about to write a FindModule.cmake for a library which I did
>> *not* write, but to certain components of which I'd like to link
>> against using CMake (in a clean fashion).
>> I'm FOREACHing through the components of the library, and upon trying to set
>>
>> ADD_LIBRARY( ${COMPONENT} )
>>
>> cmake complains about
>>
>> add_library cannot create target "A" because another target with the
>> same name already exists. The existing target is a static library created
>> in source directory "/path/to/A". See
>> documentation for policy CMP0002 for more details.
>>
>> Okay, so it exists already? Fine. Let's then
>>
>> SET_TARGET_PROPERTIES( ${COMPONENT} PROPERTIES
>> LINK_INTERFACE_LIBRARIES "B;C;" )
>>
>> -- but again, cmake complains that
>>
>> set_target_properties Can not find target to add properties to: A
>>
>> TARGET_LINK_LIBRARIES doesn't work anyway as I'm not *building the
>> libraries (cmake says).
>>
>> Hmmm...
>> Nico
>>
>>
>>
>> On Thu, Jan 7, 2010 at 11:08 AM, Michael Wild <themiwi at gmail.com> wrote:
>>>
>>> One quick question: Are these libraries created by you? In that case you shouldn't write a FindMymodule.cmake, but a MymoduleConfig.cmake (see the documentation of find_package).
>>>
>>> Anyhow, to define the transitive link dependencies you can either use target_link_libraries(A LINK_INTERFACE_LIBRARIES B C) if your CMake is new enough, or you can use set_target_properties(A PROPERTIES LINK_INTERFACE_LIBRARIES "B;C"). Both solutions require that you use add_library(X IMPORTED) (where X = A, B and C) and set their IMPORTED_LOCATION property to the location on the file system. All of this is full automatic if you write a MymoduleConfig.cmake, because then you can use
>>>
>>> install_targets(A B C EXPORT MymoduleExports
>>> ARCHIVE DESTINATION lib
>>> LIBRARY DESTINATION lib
>>> RUNTIME DESTINATION bin
>>> PUBLIC_HEADER DESTINATION include
>>> )
>>>
>>> install(EXPORT MymoduleExports
>>> NAMESPACE Mymodule_
>>> DESTINATION share/mymodule/cmake
>>> )
>>>
>>> which will install a file ${CMAKE_INSTALL_PREFIX}/share/mymodule/cmake/MymoduleExports.cmake containing all the IMPORT stuff and which you can INCLUDE in your MymoduleConfig.cmake.
>>>
>>>
>>> I hope I could help a bit, otherwise tell us which case it is and I'll be able to help more.
>>>
>>> Michael
>>>
>>>
>
>
More information about the CMake
mailing list