In my embedded project I have a use case in which the CMakelist.txt needs to generate two “quasi identical” binaries that share the same build process, same sources, headers and dependencies, but different linker scripts. I don’t know how to cleanly do this.
For now I’m using a variable defined when launching cmake:
# Compile option variables
set(BOOTLOADER_SPACE_EN ON CACHE BOOL "Enable additional space before the binary for fitting the bootloader")
And then using the output of this variable to generate either one binary or the other.
What I don’t like about this method is that the generated Makefile (if I understood correctly) doesn’t contain both options, therefore if one wanted to switch what binary must be generated, cmake must be re-issued with a different option.
I think that the best solution should be to decide that at build time (as makefile parameters instead of cmake) what you want to get, is this correct/the best way to do this?
What would you do in my shoes, is there something I could do better?
Makefile parameters would only work in Make, not in any other build tool. So CMake does not support that.
Changing the cache variable and recall the build, automatically does recall CMake. So your current solution is the straight forward one.
Apart from that I guess, it’s only possible to put all sources into libraries (static, object). And create two executables and add those libraries to both. Or maybe use a generator expression to “copy” the dependencies of the first exe to the second one.
A total different (CMake independent) solution may be, to always create the same executable. And use a dummy bootloader (e.g. a simple jump) for the other use case.
Apart from that I guess, it’s only possible to put all sources into libraries (static, object). And create two executables and add those libraries to both. Or maybe use a generator expression to “copy” the dependencies of the first exe to the second one.
Could you elaborate more on this part? what do you mean by libraries? and do you have a template for the code generation?
Because I like the idea of generating both executables but I don’t like to rewrite the same CMake code for both to generate them.