Sample code is available in https://github.com/marcpawl/cmake_generated_source.git
I have two directories px and pxlib. px generates code, and pxlib consumes it.
For px I have:
cmake_minimum_required ( VERSION 3.12)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON )
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON )
add_custom_target(px_all ALL
DEPENDS /project/px/build /project/px/build/generated.cpp )
add_custom_command(
OUTPUT /project/px/build /project/px/build/generated.cpp
COMMAND /project/px/generator
MAIN_DEPENDENCY /project/px/generator
)
And for pxlib I have
cmake_minimum_required (VERSION 3.12)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
add_library( pxlib STATIC /project/px/build/generated.cpp)
set_target_properties(pxlib PROPERTIES CXX_STANDARD 17)
add_dependencies(pxlib px_all)
But I cannot generate the makefiles unless the generated code is present
cmake3 -G “Unix Makefiles” …
CMake Error at pxlib/CMakeLists.txt:4 (add_library):
Cannot find source file:
/project/px/build/generated.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx
CMake Error at pxlib/CMakeLists.txt:4 (add_library):
No SOURCES given to target: pxlib
If I run the generator first to provide the C++ flie then I can build my targets. What am I missing to say that the generator script needs to be executed before compiling the library?
P.S. The absolute paths is because I am in the middle of transforming hand written Makefiles to CMakeLists.txt files, they will be made into relative paths in a further step.