[cmake-developers] Idea for Multi-Toolchain Support

Kyle Edwards kyle.edwards at kitware.com
Tue Dec 18 09:34:00 EST 2018


On Tue, 2018-12-18 at 06:14 -0500, frodak17 wrote:
> What first cross my mind with '"utilities" needed for build' is when
> you have to build the cross-compiler in the first place before you
> build anything for the target.
> If so how do you handle verification of the tool-chain can build a
> working executable or library?
> Normally that is done at the time before project files are generated
> but it has to either be skipped or delayed because the toolchain
> doesn't even exist because it has to be built first by the host
> tools. For tool-chains that already exist on the host machine then
> add_toolchain() command is when you can test and verify its
> functionality?
Yes, add_toolchain() would do all the same checks that happen on the
"main" toolchain when CMake starts up. The process would be:
1) add_toolchain() gets called with a FILE argument
2) the file gets loaded the same way CMAKE_TOOLCHAIN_FILE does on
startup
3) CMake performs all the same checks that it did with the "main"
toolchain
4) add_toolchain() returns and script execution resumes
So you could build the cross toolchain during configure-time and then
add it with add_toolchain(). However, I would recommend having the
toolchain build and the actual project be separate CMake projects in
order to maintain separation of responsibilities. At the very least,
I'd recommend that the cross-compiled project be a subdirectory of the
superbuild via add_subdirectory(), and the toolchain would be built in
the top directory.
> Do these targets automatically get separate binary directories so
> that the outputs don't overwrite each other?
No. In this feature, each target would get one toolchain, and *only*
one toolchain. If you want to build the same target with multiple
toolchains, you would build them as separate targets. Example:
add_executable(foo-arm foo.c bar.c TOOLCHAIN ArmToolchain)
add_executable(foo-x86_64 foo.c bar.c TOOLCHAIN x86_64Toolchain)
This could of course be abstracted away with a function():
function(make_foo toolchain)
  add_executable(foo-${toolchain} foo.c bar.c TOOLCHAIN ${toolchain})
endfunction()
make_foo(arm)
make_foo(x86_64)
My reasoning for this is that each .c/.cxx file has to be built
multiple times (once for each toolchain) anyway, so why bother trying
to make them part of the same target? Just make them separate targets.

> I assume this feature is limited only to ninja or makefile
> generators?
If Visual Studio and/or Xcode support multiple toolchains (I don't know
if they do) then it could work for them too. If not then they would
unfortunately be out of luck for multi-toolchain projects.
Kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake-developers/attachments/20181218/5201b5de/attachment.html>


More information about the cmake-developers mailing list