[CMake] detecting build configuration in Visual Studio

Michael Hertling mhertling at online.de
Tue Feb 15 20:33:07 EST 2011


On 02/15/2011 03:49 PM, Dominik Szczerba wrote:
> In MSVC I need to link different libraries depending on the chosen
> build type. I have two  questions:
> 
> 1) If and how can I register my own build types

>> http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_specify_my_own_configurations_.28for_generators_that_allow_it.29_.3F

> 2) How can I detect which one I am in for conditional linking

AFAIK, you can't, but you might do the following: Declare the differing
libraries for the different configurations as IMPORTED libraries and
set the IMPORTED_LOCATION_<CONFIG> properties accordingly, e.g.:

ADD_LIBRARY(xyz SHARED IMPORTED)
SET_TARGET_PROPERTIES(xyz PROPERTIES
    IMPORTED_LOCATION_CUSTOMIZED "..."
    IMPORTED_LOCATION_RELEASE "..."
    IMPORTED_LOCATION_DEBUG "..."
    ...
)
...
TARGET_LINK_LIBRARIES(... xyz ...)

If such a library should not be taken into account for a particular
configuration at all, things are more complex since you can't declare
an empty IMPORTED_LOCATION. A possible approach might look as follows:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(IMPORTEDDUMMY C)
ADD_LIBRARY(dummy STATIC "")
SET_TARGET_PROPERTIES(dummy PROPERTIES LINKER_LANGUAGE C)
EXPORT(TARGETS dummy NAMESPACE imported FILE importeddummy.cmake)
INCLUDE(${CMAKE_BINARY_DIR}/importeddummy.cmake)
SET_TARGET_PROPERTIES(importeddummy PROPERTIES
    IMPORTED_LINK_INTERFACE_LIBRARIES_CUSTOMIZED "..."
)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main importeddummy)

The target "dummy" is an empty static library and needed as anchor for
an imported static library "importeddummy" which refers to the former;
this is achieved by EXPORT() and the generated "importeddummy.cmake"
file. Finally, the importeddummy target is associated with additional
libraries via the IMPORTED_LINK_INTERFACE_LIBRARIES_<CONFIG> property.
This property is the reason for the intermediate importeddummy target
since it can't be imposed on non-imported ones. At the end of the day,
the additional libraries appear in the link command line only for the
CUSTOMIZED configuration. Maybe, it would be worth a feature request
to drop an imported library from the link command line completely if
there's no IMPORTED_LOCATION for the active configuration instead of
having "make" raise an error.

'hope that helps.

Regards,

Michael


More information about the CMake mailing list