VERSION 1
already automatically creates the subfolder generate
as expected. Still, I added the following code just before the existing add_custom_command
part:
# Create the directory before trying to use it!
if(EXISTS OUTPUT_DIRECTORY)
message("Creating output directory '${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_DIRECTORY}'")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_DIRECTORY}")
endif()
# Add command to generate source code files before building the target.
add_custom_command(
...
The configuration for VERSION 1
works fine, but building throws the error:
x86_64-w64-mingw32-g++.exe: error: <CMAKE_BINARY_DIR>\my-lib\test-source-1.cpp: No such file or directory
which is correct, since the path should be <CMAKE_BINARY_DIR>\my-lib\generate\test-source-1.cpp
.
The configuration for VERSION 2
does not work, it throws the error:
[cmake] -- Configuring done
[cmake] CMake Error at my-app/CMakeLists.txt:8 (add_executable):
[cmake] Cannot find source file:
[cmake]
[cmake] <CMAKE_SOURCES_DIR>/my-lib/generate/test-source-2.h
[cmake]
[cmake] Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
[cmake] .hpp .hxx .in .txx
[cmake]
[cmake]
[cmake] -- Generating done
which of course does not work, since the file is generated into the <CMAKE_BINARY_DIR>.
So I added a third version specifying the full absolute path to the generated files:
...
elseif(${VERSION} EQUAL 3)
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# This does NOT work.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
set(OUTPUT_DIRECTORY "generate")
set(GENERATED_HEADER "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_DIRECTORY}/test-source-${VERSION}.h")
set(GENERATED_IMPLEMENTATION "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_DIRECTORY}/test-source-${VERSION}.cpp")
set(INPUT_HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/test-source-${VERSION}.h.in")
set(OUTPUT_HEADER_FILE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_DIRECTORY}/test-source-${VERSION}.h")
set(INPUT_IMPLEMENTATION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/test-source-${VERSION}.cpp.in")
set(OUTPUT_IMPLEMENTATION_FILE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_DIRECTORY}/test-source-${VERSION}.cpp")
endif()
...
elseif(${VERSION} EQUAL 3)
set(TARGET_SOURCES_HEADER_FILE "${GENERATED_HEADER}")
set(TARGET_SOURCES_IMPLEMENTATION_FILE "${GENERATED_IMPLEMENTATION}")
endif()
message("INPUT_HEADER_FILE is: ${INPUT_HEADER_FILE}")
message("OUTPUT_HEADER_FILE is: ${OUTPUT_HEADER_FILE}")
...
set(TARGET_SOURCES_HEADER_FILE ${TARGET_SOURCES_HEADER_FILE} PARENT_SCOPE)
set(TARGET_SOURCES_IMPLEMENTATION_FILE ${TARGET_SOURCES_IMPLEMENTATION_FILE} PARENT_SCOPE)
and in the main CMakeLists.txt
I am marking the source files as GENERATED
as I have read somewhere in this forum:
...
add_subdirectory(my-lib)
message("Parent: TARGET_SOURCES_HEADER_FILE ${TARGET_SOURCES_HEADER_FILE}")
message("Parent: TARGET_SOURCES_IMPLEMENTATION_FILE ${TARGET_SOURCES_IMPLEMENTATION_FILE}")
set_source_files_properties(${TARGET_SOURCES_HEADER_FILE} GENERATED)
set_source_files_properties(${TARGET_SOURCES_IMPLEMENTATION_FILE} GENERATED)
# -------------------------------------------------------------------------------
# Executables
# -------------------------------------------------------------------------------
add_subdirectory(my-app)
Still this does not work.