user7610
user7610

Reputation: 28879

Expanding a variable, #cmakedefine, and generator expression in template file

I have the following template

config.c.in

#define QPID_DISPATCH_VERSION "${QPID_DISPATCH_VERSION}"
#define QPID_DISPATCH_LIB "$<TARGET_FILE_NAME:qpid-dispatch>"
#cmakedefine01 USE_MEMORY_POOL

And I wish to obtain the following, by expanding the variable, #cmakedefine, and generator expression.

config.c

#define QPID_DISPATCH_VERSION "1.6.0-SNAPSHOT"
#define QPID_DISPATCH_LIB "libqpid-dispatch.so"
#define USE_MEMORY_POOL 1

The problem is, there does not seem to be a CMake function which can expand all three.

What I can do is

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h.tmp)
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/config.h INPUT ${CMAKE_CURRENT_BINARY_DIR}/config.h.tmp)

Is there a way to accomplish this without creating a temporary file? Cleaning up the file is tricky, because file GENERATE does not run immediately.

Upvotes: 2

Views: 587

Answers (1)

user7610
user7610

Reputation: 28879

Based on @Tsyvarev's comment, I now have

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" CONFIG_H_IN)
string(CONFIGURE "${CONFIG_H_IN}" CONFIG_H_TMP)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/config.h" CONTENT "${CONFIG_H_TMP}")

Upvotes: 2

Related Questions