cmeson-build

How do I enable Meson options, sanitizers, for debug build only


I am trying to enable the sanitizers for the for debug build only, but I can't find any references in the Meson manual for this.

I have seen people manually appending flags to the C compiler but compared to the default_options this does not provide a cross-platform / cross-compiler way of activating features.

When doing it the way below, it gets passed as a file to the compiler and therefore an error is thrown. I expected the options to be resolved and passed as the corresponding compiler flags.

project(
  'Foo',
  'c',
  version: '0.0.1',
  license: 'GPL-3.0-or-later',
  meson_version: '>= 0.64.0',
  default_options: [
      'c_std=c2x',
      'warning_level=everything',
  ],
)

# This is the relevant part
# The sanitizer should only be used in debug builds.
# Using e.g. -Werror or LTO in release builds only.

if get_option('buildtype').startswith('debug')
  message('Building a debug release')
  add_project_arguments('b_sanitize=address,undefined', language: 'c')
else
  add_project_arguments(['werror=true'], language: 'c')
endif

foo_sources = files('main.c')

executable(
  meson.project_name().to_lower(),
  foo_sources,
)

I am using Meson v1.3.1 on Linux (Fedora Asahi Remix).

How would I archive that?

Thanks in advance ^^

This is the error output from Meson

$ LC_MESSAGES=C meson compile --verbose -C builddir
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/bin/ninja -C /home/jan/Projekte/Own/Tmp/builddir -v
ninja: Entering directory `/home/jan/Projekte/Own/Tmp/builddir'
[1/2] ccache cc -Ifoo.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Wpedantic -Wcast-qual -Wconversion -Wfloat-equal -Wformat=2 -Winline -Wmissing-declarations -Wredundant-decls -Wshadow -Wundef -Wuninitialized -Wwrite-strings -Wdisabled-optimization -Wpacked -Wpadded -Wmultichar -Wswitch-default -Wswitch-enum -Wunused-macros -Wmissing-include-dirs -Wunsafe-loop-optimizations -Wstack-protector -Wstrict-overflow=5 -Warray-bounds=2 -Wlogical-op -Wstrict-aliasing=3 -Wvla -Wdouble-promotion -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines -Wvector-operation-performance -Wsuggest-attribute=format -Wdate-time -Wformat-signedness -Wnormalized=nfc -Wduplicated-cond -Wnull-dereference -Wshift-negative-value -Wshift-overflow=2 -Wunused-const-variable=2 -Walloca -Walloc-zero -Wformat-overflow=2 -Wformat-truncation=2 -Wstringop-overflow=3 -Wduplicated-branches -Wcast-align=strict -Wsuggest-attribute=cold -Wsuggest-attribute=malloc -Wattribute-alias=2 -Wanalyzer-too-complex -Warith-conversion -Wbidi-chars=ucn -Wopenacc-parallelism -Wtrivial-auto-var-init -Wbad-function-cast -Wmissing-prototypes -Wnested-externs -Wstrict-prototypes -Wold-style-definition -Winit-self -Wc++-compat -Wunsuffixed-float-constants -std=c2x -O0 -g b_sanitize=address,undefined -MD -MQ foo.p/main.c.o -MF foo.p/main.c.o.d -o foo.p/main.c.o -c ../main.c
FAILED: foo.p/main.c.o 
ccache cc -Ifoo.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Wpedantic -Wcast-qual -Wconversion -Wfloat-equal -Wformat=2 -Winline -Wmissing-declarations -Wredundant-decls -Wshadow -Wundef -Wuninitialized -Wwrite-strings -Wdisabled-optimization -Wpacked -Wpadded -Wmultichar -Wswitch-default -Wswitch-enum -Wunused-macros -Wmissing-include-dirs -Wunsafe-loop-optimizations -Wstack-protector -Wstrict-overflow=5 -Warray-bounds=2 -Wlogical-op -Wstrict-aliasing=3 -Wvla -Wdouble-promotion -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wtrampolines -Wvector-operation-performance -Wsuggest-attribute=format -Wdate-time -Wformat-signedness -Wnormalized=nfc -Wduplicated-cond -Wnull-dereference -Wshift-negative-value -Wshift-overflow=2 -Wunused-const-variable=2 -Walloca -Walloc-zero -Wformat-overflow=2 -Wformat-truncation=2 -Wstringop-overflow=3 -Wduplicated-branches -Wcast-align=strict -Wsuggest-attribute=cold -Wsuggest-attribute=malloc -Wattribute-alias=2 -Wanalyzer-too-complex -Warith-conversion -Wbidi-chars=ucn -Wopenacc-parallelism -Wtrivial-auto-var-init -Wbad-function-cast -Wmissing-prototypes -Wnested-externs -Wstrict-prototypes -Wold-style-definition -Winit-self -Wc++-compat -Wunsuffixed-float-constants -std=c2x -O0 -g b_sanitize=address,undefined -MD -MQ foo.p/main.c.o -MF foo.p/main.c.o.d -o foo.p/main.c.o -c ../main.c
cc: warning: b_sanitize=address,undefined: linker input file unused because linking not done
cc: error: b_sanitize=address,undefined: linker input file not found: No such file or directory
ninja: build stopped: subcommand failed.

Solution

  • Instead of this:

    add_project_arguments('b_sanitize=address,undefined', language: 'c')
    

    You need:

    add_project_arguments('-fsanitize=address,undefined', language: 'c')
    

    That's because add_project_arguments takes compiler options not Meson options. The b_sanitize syntax is a Meson option, which when passed directly to the compiler is not valid. So you need -fsanitize which is the actual compiler option.