FILE_SET: add_custom_command for generated file not called
When we use the following below the generatedFile
is not generated anymore.
If we use target_sources
without FILE_SET
, the add_custom_command
will be called for apiDefines
.
Is that a feature or a bug? And how to fix it?
cmake_minimum_required(VERSION 3.23)
project(test CXX)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp [[
#include <API.h>
int main(int, char* argv[])
{
return 0;
}
]]
)
set(generatedFile ${CMAKE_CURRENT_BINARY_DIR}/API.h)
# Add a custom command for generating the file.
add_custom_command(
OUTPUT ${generatedFile}
COMMAND touch ${generatedFile}
COMMENT "Generating API header at '${generatedFile}'."
VERBATIM)
set(linkTarget "api-defines")
add_library(api-defines INTERFACE)
# target_sources(api-defines PUBLIC ${generatedFile})
# With that it does not work:
target_sources(
api-defines
PUBLIC FILE_SET
"publicHeadersBin"
TYPE
HEADERS
FILES
${generatedFile}
BASE_DIRS
${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(
api-defines INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
# ==============================================================================
# Link the executable to the api-defines...
add_executable(test main.cpp)
target_link_libraries(test PRIVATE api-defines)
Edited by Gabriel Nützi