mesonbuild / mesonbuild/meson

meson ignores host_machine cpu_family when cross compiling with msvc

Open
#12,640 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6.6k
Forks
1.9k
Avg merge
2d 6h
Merged PRs (30d)
33

Description

**Describe the bug**

when `cpu_family = x86`.
Meson does not specify `/machine:x86`, uses the wrong cl toolchain, includes the wrong windows headers, and links to the wrong system libraries.

**To Reproduce**
```c
// lib.c
int whatever()
{
return 99;
}
```

```c
// temp.c
#include

#define PROJECT_NAME "temp"

extern int whatever();

int main(int argc, char **argv) {
if(argc != 1) {
printf("%s takes no arguments.\n", argv[0]);
return 1;
}
printf("This is project %s.\n", PROJECT_NAME);
return 0;
}
```

```py
# meson.build
project('temp', 'c',
version : '0.1',
default_options : ['warning_level=3'])

lib = static_library('lib', 'lib.c')

exe = executable('temp', 'temp.c',
link_with : lib,
install : true)

test('basic', exe)
```

```ini
[binaries]
c = 'cl'

[host_machine]
system = 'windows'
cpu_family = 'x86'
cpu = 'i686'
endian = 'little'
```

```sh
meson setup build --cross-file cross.ini
```

Inspecting `build/build.ninja` the `rule STATIC_LINKER` and `rule c_LINKER` both specify `/MACHINE:x64`. additionally the `rule c_COMPILER` specifies `cl` rather than the correct compiler from `C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x86`.

if the ninja file is modifed to specify `/MACHINE:X86` then linking fails with `fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'x86'` as the wrong CL is used. this also applies to linking stub `.lib` libraries for system dlls.

**Expected behavior**
Meson figures out that the target is 32 bit and uses the correct toolchain

**system parameters**
* windows 11 pro build 22621 (x64)
* meson 1.3.0
* ninja 1.11.1
* cross compiling to x86
* python 3.11.7

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the provided lib.c, temp.c, meson.build, and cross.ini, then inspect the generated build/build.ninja rules. Trace how the host_machine cpu_family selects the MSVC compiler and linker settings. Done means an x86 target selects the matching toolchain and emits x86 machine flags without architecture conflicts.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
build-system, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.