On 6/22/23 1:15 PM, 'Dlugosz, John M' via The Meson Build System wrote:
> We're running RHEL on AWS, and have installed gcc 12, so it now appears
> in /opt/rh/gcc-toolset-12/root/usr/bin/gcc . If we set the PATH and
> LD_LIBRARY_PATH for the users in the etc/profile file, it works on any old
> Bash prompt, e.g. typing >gcc --version will give me 12.2.1. But from that
> _same_ prompt, running >meson build.debug will show that it's finding gcc
> version 8.5.0, and I see pathnames inside the generated build.ninja
> like /usr/lib/gcc/x86_64-redhat-linux/8/libstdc
There's a subtle oddity when it comes to having multiple compilers in
different locations on $PATH. It probably helps to get an overview of
how meson internally detects a compiler. So here it is:
- first, check if meson's machine file toolchain config files are
defined in this build:
https://mesonbuild.com/Machine-files.html
and if so, use whatever binaries are specified there
- next, check for the industry standard $CC variable, and use it if set
- if neither a machine file "c" binary nor $CC is set, check for
compilers by probing for some common names:
- cc
- gcc
- clang
- nvc
- pgcc
- icc
- icx
So keeping that in mind -- what happens when the first "gcc" in $PATH is
in /opt, but there is also a "cc" in /usr/bin ? Meson finds "cc" and
uses it -- it is probably a symlink to /usr/bin/gcc though.
The build log should hint to what happened -- you'll see a line that
looks something like this:
```
C compiler for the host machine: ccache cc (gcc 8.5.0 "cc (GCC) 11.1.0")
```
The part in the parentheses is the compiler ID and version string. The
part outside of the parentheses is the executable name that will be run.
In my case, ccache was automatically detected as available, and "cc" was
the first compiler found -- so the generated build.ninja will contain
compiler rules that run e.g.
```
rule c_COMPILER
command = ccache cc $ARGS -MD -MQ $out -MF $DEPFILE -o $out -c $in
deps = gcc
depfile = $DEPFILE_UNQUOTED
description = Compiling C object $out
```
or
```
ccache cc -MD -MQ progname.p/foo.o -MF progname.p/foo.o.d -o
progname.p/foo.o -c ../foo.c
```
> How do I tell meson to use the desired installed compiler version?
You should be able to force meson to use whichever gcc is first, by
setting `CC=gcc` as an environment variable and then setting $PATH to
have gcc-toolset-12 come at the beginning. Alternatively, you could set
CC=/opt/rh/gcc-toolset-12/root/usr/bin/gcc of course. :)
(You may want to set other programs from the same location, e.g. $AR --
this will always detect the right one if $PATH is set, because there's
only one name to lookup, "ar", not seven of them.)
--
Eli Schwartz