Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_package for both debug and release with Visual Studio

I'm tearing my hair out about how to include thrid party libraries in my cmake project. Currently I build Poco and a bunch of others that all generate their respective Config.cmake which I use with find_package. I have a wrapping build script that builds all of my dependencies and package them separately for debug and release (I don't want to tweak their cmake-scripts unless I really really really need to because maintanance).

I thought I could just do:

find_package(Foo
            HINTS "${CMAKE_SOURCE_DIR}/some/path/debug/libFoo/lib/cmake"
            REQUIRED
)
get_target_property(LIB_FOO_DEBUG lib_foo LOCATION)

find_package(Foo
            HINTS "${CMAKE_SOURCE_DIR}/some/path/release/libFoo/lib/cmake"
            REQUIRED
)
get_target_property(LIB_FOO_RELEASE lib_foo LOCATION)

set(LIB_FOO_LIBRARIES optimized "${LIB_FOO_RELEASE}" debug "${LIB_FOO_DEBUG}")

message("LIB_FOO_LIBRARIES: \"${LIB_FOO_LIBRARIES}\"")

This yeilds: LIB_FOO_LIBRARIES: "optimized;C:/path/to/proj/some/path/debug/libFoo/lib/foo.lib;debug;C:/path/to/proj/some/path/debug/libFoo/lib/foo.lib"

It seems like the first call to find_package for target Foo is cached, whis I don't really want.

Am I going about this the wrong way? How do I properly work with third party libraries with the Visual Studio generator?

Any pointers are greatly appreciated.

like image 593
JBarberU Avatar asked Sep 25 '15 10:09

JBarberU


1 Answers

the first call to find_package for target Foo is cached

Yes. So you cannot issue find_package twice and get different results (unless the first call failed).

It is third-party package who is responsible for multiconfig-usage, that is it should have properly written *Config.cmake/Find*.cmake file. (E.g., FindBoost.cmake support multi-config usage).

Otherwise, you should do some tricks for use package in multiconfig manner.

E.g., if you guess that only difference between configurations is debug/release substrings in paths, you can call find_package() for debug installation, then use string(REPLACE) for get release-specific paths:

find_package(Foo
        HINTS "${CMAKE_SOURCE_DIR}/some/path/debug/libFoo/lib/cmake"
        REQUIRED
)

get_target_property(LIB_FOO_DEBUG lib_foo LOCATION)
string(REPLACE debug release LIB_FOO_RELEASE ${LIB_FOO_DEBUG})

# Use generator expressions, so variable can be used not only by target_link_libraries.
set(LIB_FOO_LIBRARIES
        "$<$<NOT:$<CONFIG:DEBUG>>:${LIB_FOO_RELEASE}>"
        "$<$<CONFIG:DEBUG>:${LIB_FOO_DEBUG}>"
)
like image 178
Tsyvarev Avatar answered Oct 04 '22 16:10

Tsyvarev