2

I'm trying to use different target property based on build configuration. There is imported target called libmongocxx and it has 3 properties for different configs:

  • IMPORTED_LOCATION_DEBUG
  • IMPORTED_LOCATION_RELEASE
  • IMPORTED_LOCATION_RELWITHDEBINFO

So, to copy necessary dependencies to build directory I tried to use the following code:

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
    "$<TARGET_PROPERTY:libmongocxx,$<$<CONFIG:Debug>:IMPORTED_LOCATION_DEBUG>$<$<CONFIG:Release>:IMPORTED_LOCATION_RELEASE>$<$<CONFIG:RelWithDebInfo>:IMPORTED_LOCATION_RELWITHDEBINFO>>"
    $<TARGET_FILE_DIR:${PROJECT_NAME}>)

The problem is: this works great when I run configuration from IDE (I tried CLion and VS2017), but the same CMakeLists.txt fails to configure when I run cmake from command line. The error cmake shows:

CMake Error at CMakeLists.txt:93 (add_custom_command):
  Error evaluating generator expression:

    $<TARGET_PROPERTY:libmongocxx,$<$<CONFIG:Debug>:IMPORTED_LOCATION_DEBUG>$<$<CONFIG:Release>:IMPORTED_LOCATION_RELEASE>$<$<CONFIG:RelWithDebInfo>:IMPORTED_LOCATION_RELWITHDEBINFO>>

  $<TARGET_PROPERTY:...> expression requires a non-empty property name.

By the way, I already found out that the same task can be more easily solved by using TARGET_FILE generator expression, but still, why different behavior in IDE and from command line? I discovered this on CMake version 3.12.3, but later tested on 3.14.4 (cmd line only) and it still fails.

Update

Here is minimal example to reproduce the issue. No dependencides required. test.cpp is empty file. Configuration completes successfully from VS2017 but fails from cmd line.

CMakeLists.txt:

project(test LANGUAGES CXX)
cmake_minimum_required(VERSION 3.8.0)

add_executable(${PROJECT_NAME} test.cpp)

set_target_properties(${PROJECT_NAME} PROPERTIES
  IMPORTED_LOCATION_DEBUG "libd.dll"
  IMPORTED_LOCATION_RELEASE "lib.dll"
  IMPORTED_LOCATION_RELWITHDEBINFO "libi.dll"
)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    "$<TARGET_PROPERTY:${PROJECT_NAME},$<$<CONFIG:Debug>:IMPORTED_LOCATION_DEBUG>$<$<CONFIG:Release>:IMPORTED_LOCATION_RELEASE>$<$<CONFIG:RelWithDebInfo>:IMPORTED_LOCATION_RELWITHDEBINFO>>"
    $<TARGET_FILE_DIR:${PROJECT_NAME}>
)

Command line:

cmake -G "Visual Studio 15 2017 Win64" ..

Update2

Can't agree this question is a duplicate. The other question is about correct configuration of Visual Studio build. This question is about usage of cmake generator expressions and about using cmake from command line.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Sergey Sokolov
  • 131
  • 1
  • 8
  • Both `CMakeLists.txt` and `Findlibmongocxx.cmake` scripts can diffirentiate command line CMake and IDE. E.g., by checking a generator. You show us none of these scripts, so we could only guess why there results are different in different use-cases. Please, prepare [mcve] which demonstrates your problem. – Tsyvarev May 22 '19 at 12:39
  • Which generator causes the problem? Or are you saying that Visual Studio generator fails when using CMake at the command line but not when using Visual Studio CMake plugin? – fdk1342 May 22 '19 at 12:43
  • Well, when command line has been posted, it explains the problem. See duplicate question about correct setting build type for Visual Studio. – Tsyvarev May 22 '19 at 15:57
  • @Tsyvarev I don't agree that this is a duplicate. This is NOT about changing the build type. It's about generator expressions and having to account for all build types in `CMAKE_CONFIGURATION_TYPES`. You'll notice the same error occurs even if `CMAKE_BUILD_TYPE` is NOT on the command line. It's use is just a distraction of the actual problem. – fdk1342 May 22 '19 at 17:07
  • "This question is about usage of cmake generator expressions and about using cmake from command line." - The duplicate is about the way you attempt to set `CMAKE_BUILD_TYPE` for Visual Studio in the command line. But I agree with a @Fred that without that attempt your question would definitely be different. Please, remove that attempt from the post (or just remove `-DCMAKE_BUILD_TYPE=Debug` part, as it has no effect), and I will be happy to reopen the question. – Tsyvarev May 22 '19 at 17:22

1 Answers1

2

The Visual Studio IDE CMake plugin only uses RelWithDebInfo and Debug configurations (at least for me). When invoking by the command line the default is all four standard configurations: Debug;Release;MinSizeRel;RelWithDebInfo.

Your CMakeLists.txt is incomplete because MinSizeRel is not defined or being used so there is no information for the MinSizeRel configuration.

BTW, CMAKE_BUILD_TYPE is ignored for multi-configuration generators. CMAKE_CONFIGURATION_TYPES defines which build types should be considered during generation.

So either add in the missing values or change CMAKE_CONFIGURATION_TYPES.

fdk1342
  • 3,274
  • 1
  • 16
  • 17