I am trying to "translate" the flowing vanilla makefile lines into CMake syntax.
SRCS = $(wildcard *.foo)
OBJS = $(SRCS:.foo=.bar)
my_rule: $(OBJS)
%.bar: %.foo
make_bar_from_foo $@ $<
Working ugly attempt
FILE(GLOB SRCS "*.foo")
SET(outFiles)
FOREACH(SRC ${SRCS})
SET(OUTPUT_FILE_NAME "${SRC}.bar")
ADD_CUSTOM_COMMAND(
OUTPUT "${OUTPUT_FILE_NAME}"
COMMAND make_bar_from_foo ${SRC} ${OUTPUT_FILE_NAME}
DEPENDS "${SRC}")
SET(outFiles ${outFiles} "${OUTPUT_FILE_NAME}")
ENDFOREACH(SRC)
ADD_CUSTOM_TARGET(my_rule ALL DEPENDS ${outFiles})
I understand that it's going to generate one command per file instead of something generic. Any simplest/cleanest way to do it?
In general, with CMake you don't build all object files. Just add the right
.foo
files into the right CMake commands (add_library
oradd_executable
) and CMake will handle it for you.CMake is way more powerful compared to plain make. But you have to accept that this power comes with a change of how you use the tool.
In CMake you can always declare your own compiler language. So in your case you can e.g. do:
Then you can simply work with it as you would with other object library targets. But you will only get things like the
.bar
extension if youenable_language(FOO)
(and that requires more work, see below).Examples delivered with CMake itself are
ASM
orRC
compilers.The
enable_language(FOO)
VersionThis needs four more files you could put in e.g. your project's
CMake
folder:CMake\CMakeDetermineFOOCompiler.cmake
CMake\CMakeFOOCompiler.cmake.in
CMake\CMakeFOOInformation.cmake
CMake\CMakeTestFOOCompiler.cmake
Then your
CMakeLists.txt
file just looks like this:CMakeLists.txt
Your attempt is correct, and it hardly can be significantly improved.
This is just how CMake works: each of its "rules" is concrete; you cannot create a "template rule". The reason for that is platform independence: not every build systems supports "templates" for rules.
On the other side, CMake supports creation of the "rules" within functions/macros. So typing repeated parts can be easily reduced.