Compiling and using a code-generator with CMake
Using code-generators is quite common for C++ developers. As part of the preparations for releasing stbl, the program that converts this site from a handful of text files to a modern, reponsive website, I wanted to embed some html template files in the binary itself. One simple way to do that is to create a header file and a source file that creates a std::map
with the file name as the key and a pointer to a const char *
as the value. Since these templates are files that change rapidly as the project matures, I wanted to generate the code that embeds the files when the project is built. The code-generator is just a few lines of C++ code. But that also need to be built as part of the build process. So what I needed to happen was:
- Compile the code generaor
- Use the code generator to create a .cpp and header file
- Build a library using the generated .cpp and header file
The CMakeLists.txt file to build the code generator looks like:
1project (mkres)
2add_executable(mkres main.cpp)
3target_link_libraries(mkres ${DEFAULT_LIBRARIES})
The CMakeLists.txt file that use the tool and build the library looks like:
1project(libstbl)
2add_custom_command(
3 COMMAND mkres stbl embedded_templates_ templates_res.cpp templates_res.h ${STBL_ROOT_DIR}/examples/default/templates/*.html
4 DEPENDS ${STBL_ROOT_DIR}/examples/default/templates/*.html mkres
5 OUTPUT templates_res.cpp templates_res.h
6 COMMENT "Embedding templates..."
7 )
8
9set(SOURCES
10 ...
11 templates_res.cpp
12 )
13
14add_library(libstbl ${SOURCES})
15target_include_directories(libstbl PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
16
The add_custom_command
function DEPENDS on mkres, which makes CMake build it before it is used. The OUTPUT tells CMake what files are built, and when one of those files are specified as a source for the library, it calls the code generator prior to building the library.
The generated files ends up in a directory created by cmake. In order to #include
the header-file from the source code of the library I am building, I needed to call the target_include_directories
function to add it's path to the list of paths to scan for included files.
I have not done this (built the required tool as part of the build-process) with CMake before. However, it was easy and CMake takes care of the paths to the tool and the generated source code itself. Basically, I just state my intentions to CMake - and it just works.
Nice!