[CMake] Specifying both native and cross compiler at the same time

Lassi Niemistö lassi.niemisto at wapice.com
Tue Mar 19 05:55:49 EDT 2019


Hello,

I spent a lot of time figuring out how to support multiple compilers in parallel. Here is how I did it:


·        Write a toolchain file for each of your compiler setups. Specify any toolchain specific compiler flags there. See https://cmake.org/cmake/help/v3.14/manual/cmake-toolchains.7.html

·        Write one top-level CMakeLists which

o   Calls cmake for your actual CMakeLists with execute_process and each time specify different –DCMAKE_TOOLCHAIN_FILE and different build directory

o   Implement a mechanism how the actual cmake tree can publish target names to top-level. I wrote them to a file in the root of build directory. Of course you can also hardcode the target names on top-level but a dynamic mechanism makes it less cluttered.

o   In the top-level cmake, after generating the architecture specific trees, read the published targets and add a custom target for each of the published targets from the architecture specific trees. Each custom target just calls make in the respective architecture specific build directory.

The other point, using the compiled binary as part of the compilation is not necessarily very good idea (sounds like chicken-egg to me). Could you instead compile the native binary separately and put it as a “released version” somewhere and use the last released version in your compilation phase.

The “cmake way” would be to regenerate cmake each time you want to switch between the architectures. But if cmake generation and build is heavy, this is a major usability issue. Hence the above hack solution.

-Lassi

From: CMake <cmake-bounces at cmake.org> On Behalf Of Mojca Miklavec
Sent: tiistai 19. maaliskuuta 2019 10.16
To: cmake at cmake.org
Subject: [CMake] Specifying both native and cross compiler at the same time

Hi,

I would like a build setup for a project to work correctly for both native and cross compilation, however one part requires native compilation and execution of the binary to generate some data tables as part of the build process (the temporary native binary is then discarded / not installed).

What I seem to be unable to figure out is how to specify the compiler(s) and flags separately for the native and cross compiler. I want them to be specified by the user or package manager rather than the author of cmake files.

One particular problem is when cross-compiling on mac with mingw (gcc) while using clang as the native compiler (the two compilers have incompatible flags).

How can I specify two sets of compilers and flags at configure time? (The same question puzzles me in autoconf world as well. Please don't say that the native compiler should be found automatically as I really need to specify which one to use, ancient systems don't have support for C++11 with the default compiler etc.)

Thank you very much,
    Mojca

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190319/079ca7e4/attachment-0001.html>


More information about the CMake mailing list