Search code examples
ccmakebuild-systemmeson-build

Meson: Change the C compiler based on the target


I am building an operating system that supports both 32 and 64 bit, using the Meson build system. I have just started adding support for 64 bit, but I came accross a problem. When I use the 64 bit C compiler, both the kernel and stage2 gets compiled using that compiler. Which is the problem, the stage2 folder, has to be compiled with the 32 bit C compiler, not the 64 bit compiler. Is there any way I can achieve this in meson? Should I switch to CMake?


Solution

  • Which is the problem, the stage2 folder, has to be compiled with the 32 bit C compiler, not the 64 bit compiler.

    Neither build system will let you do this in a single build... both CMake and Meson have a deeply held assumption that there is one compiler per language1. If you need to use multiple compilers, you will need to split your build into multiple independent projects. How you orchestrate building them is up to you... with CMake, I would suggest using a superbuild with ExternalProject. With Meson, I'm not sure what the standard approach is.

    1. Technically Meson allows you to define a host/native compiler, too (for compiling build-time tools), but it is not suitable for this case since the host might not be either target and you might well end up wanting three or more compilers in use (e.g. for ARM).

    Should I switch to CMake?

    Probably not, since you already have something working with Meson. But if your build rules get complex enough, you might find CMake's ability to write functions and abstractions to be a greater benefit than a liability.