Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initializing languages per-machine #14368

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dcbaker
Copy link
Member

@dcbaker dcbaker commented Mar 14, 2025

When cross compiling, it's often not useful or interesting to have the same toolchain for the host and build machine. Often, a project only needs a host toolchain, and needs no build toolchain at all. In some situations (like MSVC) having to set up a build machine toolchain is painful and annoying when not needed. Alternatively, sometimes a specific language is only needed for the build machine and isn't useful for the host machine.

Currently, the only way to handle this is the non-obvious solution of not setting languages at all in the project() call, and then adding them with add_languages(), like so:

project('foo')

add_languages('c', native : false)
add_languages('cpp', native : true)

This isn't obvious, and since many developers don't cross compile often, they don't think about cross compiling, so they don't do that, they just set the languages and everything works exactly as they expect.

Having an explicit, project call oriented way to set up the languages, makes it easier for a developer to do the right thing the first time, since they are asked to consider whether they need a toolchain for both the host and and build machines, or just for one or the other. The above could be written as:

project(
  'foo',
  host_machine_languages : ['c'],
  build_machine_languages : ['cpp'],
  meson_version : '>= 1.8.0',
)

This is abundantly clear when reading the meson.build file what is necessary, and the developer had to consider what was needed. It would also open a path to using a linter to identify unused languages, or languages unused on a given machine, with a path to easily fixing that.

dcbaker added 2 commits March 14, 2025 11:26
It's quite common to only need languages for the host machine in a cross
compile setup, or to need only a subset of languages on the build
machine that are needed for the host. While having build machine
compilers isn't a problem for most unix-like OSes, it's particularly
annoying for MSVC users on Windows, who often need to setup a mingw
toolchain that they'll never even use. It also means that Meson will go
looking for compilers (and probe them) then never use them, wasting time
at setup.

This can be achieved with the following setup, but it's non obvious:
```meson
project('foo')
add_languages('c', native : false, required : true)
```
but this isn't really what `add_languages` is for, it's really for
languages that are conditional in ways that cannot be described, like
"when building for MacOS ObjC is required".

```meson
project('foo', host_machine_languages : 'c')
```
is much more obvious, it's adding 'c', as a required language, for the
host machine.
Since there are advantages, we should document that people do this.
@dcbaker dcbaker requested a review from jpakkane as a code owner March 14, 2025 18:38
Copy link
Member

@bruchar1 bruchar1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the first commit message (langauges)

@bonzini
Copy link
Collaborator

bonzini commented Mar 17, 2025

I don't want this to either become bikeshedding or balloon into a full design discussion, but what about making the second argument a dictionary (keyed by 'host' or 'build')? I still dream of replacing native: true/false with machine: 'host'/'build'...

@dcbaker
Copy link
Member Author

dcbaker commented Mar 17, 2025

I still dream of replacing native: true/false with machine: 'host'/'build'...

This is on my list of things todo, but that last never seems to get shorter.

@bonzini
Copy link
Collaborator

bonzini commented Mar 20, 2025

This is on my list of things todo

So does it make sense to use host/build already, as the keys in a languages dictionary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants