I have a requirement where I need to auto generate the list of source files from a python script during the build time and build them. Please note that the file names are generated within the python script and the list is huge. Here is the: directory structure
ROOT
|
-- build
|
-- CMakeLists.txt
__ my_python_script.py
|
-- sub_folder
|
-- build
|
-- CMakeLists.txt - This Cmake file will glob the sources and run target_source command to generate the library
-- auto_gen_dir
|
-- Script will generate the source/header files here
I tried using add_custom_command and add_custom_target but both are generating the source files but are not compiling them. Due to this the build fails with undefined reference errors. When I run the build command second time then it goes through the unbuilt files [auto generated ones] and build it successfully. This is always a problem with a clean/first time build.
Just FYI. The other method that I tried is to auto generate the source files during the configuration time using execute_process() command and it generates the source files and are compiled in the build time. This has no issues.
This is the code snippet that I tried in my CMake from ROOT/build/CMakeLists.txt:
add_custom_command(TARGET my_target
POST_BUILD #Please note I have used PRE_LINK too. PRE_BUILD will work with only Visual Studio geneartor
COMMENT "Auto generating source/header files for 'amdb_core' library"
COMMAND ${CMAKE_COMMAND} -E env
${PYTHON3} ${CMAKE_CURRENT_SOURCE_DIR}/my_python_script.py
COMMENT "Auto generating source/headers is complete"
VERBATIM)