mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.59k stars 1.62k forks source link

Coverage flags are not passed to compiler test methods (links(), has_function()) #13610

Open detly opened 2 months ago

detly commented 2 months ago

The flags that meson adds to targets for coverage are (a) not passed to the compiler "behavioural" test methods such as links() or has_function(), and (b) are not (AFAIK) available in some other way to pass manually.

This makes it impossible to test for the availability of Gcov functions like __gcov_dump() before trying to compile in extra source files to use it.

Example

meson.build:

project(
    'detly', 'c',
    version : '1.0',
    meson_version: '>=1.3.0',
    default_options: ['c_std=c11']
)

c_compiler = meson.get_compiler('c')

if c_compiler.links('#include <gcov.h>\nint main(void) { __gcov_dump(); return 0; }', name : 'gcov')
    exe = executable('detly', sources : files('main.c'))
endif

main.c:

#include <gcov.h>

int main(void) {
    __gcov_dump();
    return 0;
}

If I configure with:

meson setup build

...then in the output I see:

Checking if "gcov" : links: NO 
Build targets in project: 0

...as expected. However, with:

meson setup -Db_coverage=true build

...I also see:

Checking if "gcov" : links: NO 
Build targets in project: 0

If I remove the check, the target builds with this configuration without error.

For the latter case, looking in build/meson-logs/meson-log.txt, I see:

Command line: `cc /home/jason/Code/meson-cov-test/build/meson-private/tmp92d42xau/testfile.c -o /home/jason/Code/meson-cov-test/build/meson-private/tmp92d42xau/output.exe -D_FILE_OFFSET_BITS=64 -O0 -std=c11` -> 1
stderr:
/usr/bin/ld: /tmp/ccU80lfd.o: in function `main':
testfile.c:(.text+0x9): undefined reference to `__gcov_dump'
collect2: error: ld returned 1 exit status

Note that the command line only includes the flags -D_FILE_OFFSET_BITS=64 -O0 -std=c11. However, if a target were actually generated with that configuration, it would also have the --coverage flag (or whatever Meson deemed appropriate for the compiler).

Expected behavior

The flags used for c_compiler.links() should be the same as for other targets before c_args are added in ie. they should contain Meson's internally computed, compiler-tailored instrumentation flags.

System parameters

amcn commented 1 month ago

This seems to affect both coverage and sanitizer flags in the same way: __asan_init() fails in the same way as __gcov_dump() if one specifies -Db_sanitize=address.