Here we describe our journey towards building with CMake in Docker.
This is inherently problematic in CMake because it expects only one C++ compiler
in the whole project. See [CMake] One build, multiple compilers and
packages. There is
one Stack Overflow question (Compile a compiler as an external project and use
it?) of a person with similar problem.
The answer there actually lists possible solutions pretty well. We decided to
manually invoke CMake for Clang and then for our project itself at configure
time. This is also known as a superbuild configuration. Then, we add
clang.exe
as a custom target that runs Ninja for Clang before building our
project.
Other related sites:
- SO: Using CMake with multiple compilers for the same language.
- SO: Building a tool immediately so it can be used later in same CMake run.
- Kitware Blog: CMake Superbuilds and Git Submodules.
- SO: How to use CMake ExternalProject_Add or alternatives in a cross platform way?.
- SO: Run a CMake superbuild once and only once.
This is a known issue: Add support for Clang targeting MSVC ABI but with GNU-like command line. See also Building with CMake, Ninja and Clang on Windows. Or maybe cmake-toolchains(7) could be useful. Anyway, in general, here are the ways to specify custom compiler in CMake: How do I use a different compiler?. Also see this SO answer which references that article.
- SO: CMake custom Link executable command, how to extract linker options?.
- SO: CMake: use a custom linker.
DEPENDS
of add_custom_target
just builds files listed if there is any
add_custom_command
that can build them. To add target dependencies, you have
to use add_dependencies
.
DEPENDS
of add_custom_command
, on the other hand, is far more advanced. It
can specify outputs of other custom commands, other targets and also arbitrary
files. If you specify an arbitrary file (i.e., file that is not an output of a
custom command), the custom command is re-run every time the file is changed.