2

I am new to cmake and having a lot of fun. I want a variable to contain a path, depending on whether or not I am in a debug or release configuration. My code looks like this:

set( LIBRARY_DIR
    $<$<CONFIG:DEBUG>: "${CMAKE_SOURCE_DIR}/lib/debug">
    $<$<CONFIG:RELEASE>:"${CMAKE_SOURCE_DIR}/lib/release">) # Directory is debug/release dependent
message (STATUS "Directory is ${LIBRARY_DIR}")

However, the message I get out is surprising:

Directory is $<$<CONFIG:Debug>:;C:/path/lib/debug;>;$<$<CONFIG:Release>:"C:/path/lib/release"

It's as though my generators aren't getting resolved at all, and are being interpreted as string literals. This seems weird to me, but as I said, I'm new to cmake and this is all strange to me. Have I made any obvious mistakes?

Danny
  • 354
  • 3
  • 13
  • @drescherjm If so, any reasons why the message I get back doesn't have the resolved path, but the literal strings for the generators? – Danny Dec 01 '20 at 03:54
  • @drescherjm Yeah, it sure seems that way. Is there a way to print out the final result instead of the internals? – Danny Dec 01 '20 at 04:06
  • I erased my comment because I am not 100% sure. – drescherjm Dec 01 '20 at 04:06
  • @drescherjm Yeah, I actually think you're incorrect. Pretty sure it's pulling all of that into the variable. – Danny Dec 01 '20 at 04:11
  • As an update, I did find a thread describing the same problem. Apparently, generators do not get evaluated until the build stage. I'm wondering if it is still possible to use generators the way I want (to build a library path), but it is not the way I'm currently doing it: https://stackoverflow.com/questions/51353110/how-do-i-output-the-result-of-a-generator-expression-in-cmake/51353286 – Danny Dec 01 '20 at 04:38

1 Answers1

0

Edit:

Upon second look, not what I originally wrote.

Your problem is with writing the arguments. You'd better checkout the command_arguments section of cmake-language doc.

Your problem is with how you write the arguments. As a result, tokens that cmake lexer gets is quite different from what you actually wishes. You should use so-called quoted argument or bracket argument to binding the generator expression as a whole.


Another problem is with the time of evaluation.

This is because generators are evaluated at generation step. Please refer to the Debugging section of cmake-generator-expression documentation.

add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "$<...>")

### OR

file(GENERATE OUTPUT filename CONTENT "$<...>")
TerryTsao
  • 565
  • 7
  • 16