Suppose you have a repository with a folder (named dataset) with several .csv files and a python script (named csvcut.py) that takes all the .csv in dataset and generates corresponding .h files.
Those .h files are included in some .cpp files to build an executable (add_executable(testlib...) used for testing.
Suppose you use add_custom_target(test_pattern... to make a target (named test_pattern) that runs csvcut.py, and add_dependencies(testlib test_pattern) to run the script before building testlib.
This works, but it would be better if:
- the script was run only when the files in dataset folder or the script itself changes (not when .cpp changes);
- the .h files was generated in a subfolder of the build folder (i.e. build/tests/dataset/), and included in the .cpp files like so
#include <tests/dataset/generated.h>.
Do you have any suggestions for making these improvements / optimizations?
Thanks, Alberto
This requires multiple steps, but can all be handled with standard CMake. First, we'll use
add_custom_commandto actually generate the files. I'm also adding a custom target, but only since I couldn't figure out how to make anINTERFACElibrary work without it.For my case,
gen.pyjust spits out a basic header file, but it shouldn't matter. List whatever files you need as output, and your csv file should be underDEPENDS(for me,foo.h.intries to simulate this).Since you only mentioned generating header files, I created an
INTERFACElibrary that depends on thegen_filestarget. I also added the appropriate include directory.If building a
STATIC/SHAREDlibrary, I was able to add the generated files directly as sources and dependencies worked, but theINTERFACElibrary required the extra target (even when I tried listing the files underadd_dependencies). Since you already have a custom target, I assume this won't be a huge issue.Lastly, I have an executable that links against
foo.Demo:
If either the input (
foo.h.in) or generation script (gen.py) change, the targets are rebuilt.