I've been looking into CMake trying to understand it. From what I understand its really good in combining C++ packages.
However, I don't see how to include code generators into it. With GNU Makefiles, I would write a simple rule:
%.h: %.fbs
flatc ... $< -o $@
How do I write this same rule in CMake?
Unlike to Make, CMake has neither pattern rules nor automatic variables which are expanded according to the current target or the command.
Instead, CMake allows to define a function or a macro, which encapsulates creation of a target or a command with corresponded content.
Your command can be wrapped into the following function:
function(add_fbs_header name)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}.h
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.fbs
COMMAND flatc ${CMAKE_CURRENT_SOURCE_DIR}/${name}.fbs -o ${CMAKE_CURRENT_BINARY_DIR}/${name}.h
)
endfunction(add_fbs_header name)
with fillowing usage:
# equivalent to
# flatc <source_dir>/foo.fbs -o <binary_dir>/foo.h
# with proper dependencies and output.
add_fbs_header(foo)
BTW, a functionality for generate headers files using flatbuffers is already provided in FindFlatBuffers.cmake script, which comes with FlatBuffers package.
For include FindXXX.cmake
script, use find_package(XXX)
.
Command, created with add_custom_command
, will be executed only when some target (or other command) depends on the command's OUTPUT. E.g. in the given way:
add_fbs_header(foo)
add_executable(my_exe my.cpp foo.h)