From 3c93eea02d4a536770ab271e0c65d956e7d1e27e Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Sat, 2 Dec 2023 07:57:21 +0100 Subject: [PATCH] tests/ctests: Build ctests separately At present the ctests are not properly linked with the built code, this causes issues when there are interdependencies between the modules. The meson build system does not appear to have support for internal build order or dependncies, so the only way I have found to make this work is to break out the build of the ctests as a separate project. This is required to avoid undefined symbols in the test code for the next patch in this series. Signed-off-by: Frode Nordahl --- meson.build | 7 ++++++- tests/ctests/build.sh | 9 +++++++++ tests/ctests/meson.build | 23 ++++++++++++++++++++--- tests/ctests/meson_options.txt | 2 ++ tests/ctests/run.sh | 1 + tools/run_asan.sh | 13 +++++++++++-- 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100755 tests/ctests/build.sh create mode 100644 tests/ctests/meson_options.txt create mode 120000 tests/ctests/run.sh diff --git a/meson.build b/meson.build index 66cda8b5a..53d17623a 100644 --- a/meson.build +++ b/meson.build @@ -71,7 +71,12 @@ test_env = [ ] if get_option('unit_testing') - subdir('tests/ctests') + test('ctests', + find_program('tests/ctests/run.sh'), + workdir: join_paths(meson.current_source_dir(), 'tests/ctests'), + args: [join_paths(meson.current_build_dir(), '_ctestsbuild'), + '-Dparent_current_source_dir=' + meson.current_source_dir(), + '-Dparent_current_build_dir=' + meson.current_build_dir()]) endif #FIXME: exclude doc/env/ diff --git a/tests/ctests/build.sh b/tests/ctests/build.sh new file mode 100755 index 000000000..093e5bcf7 --- /dev/null +++ b/tests/ctests/build.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +BUILDDIR="$1"; shift +meson setup ${BUILDDIR} $* +meson compile -C ${BUILDDIR} --verbose + +if [ "$(basename $0)" = "run.sh" ]; then + meson test -C ${BUILDDIR} +fi diff --git a/tests/ctests/meson.build b/tests/ctests/meson.build index 8a41e9a30..ab503153f 100644 --- a/tests/ctests/meson.build +++ b/tests/ctests/meson.build @@ -1,3 +1,10 @@ +project('ctests', 'c') + +add_project_arguments( + '-DSBINDIR="' + join_paths(get_option('prefix'), get_option('sbindir')) + '"', + '-D_GNU_SOURCE', + language: 'c') + tests = { 'test_netplan_parser': false, 'test_netplan_state': false, @@ -10,15 +17,25 @@ tests = { 'test_netplan_openvswitch': false, } +glib = dependency('glib-2.0') +gio = dependency('gio-2.0') +yaml = dependency('yaml-0.1') +uuid = dependency('uuid') cmocka = dependency('cmocka', required: true) +netplan = meson.get_compiler('c').find_library( + 'netplan', + dirs: [join_paths(get_option('parent_current_build_dir'), 'src')]) foreach name, should_fail: tests exe = executable(name, '@0@.c'.format(name), - include_directories: [inc, inc_internal], - dependencies: [cmocka, glib, gio, yaml, uuid], + include_directories: [ + join_paths(get_option('parent_current_source_dir'), 'include'), + join_paths(get_option('parent_current_source_dir'), 'src'), + ], + dependencies: [cmocka, glib, gio, yaml, uuid, netplan], c_args: [ - '-DFIXTURESDIR="' + meson.project_source_root() + '/tests/ctests/fixtures"', + '-DFIXTURESDIR="' + meson.project_source_root() + '/fixtures"', '-Wno-deprecated-declarations', '-D_GNU_SOURCE', ], diff --git a/tests/ctests/meson_options.txt b/tests/ctests/meson_options.txt new file mode 100644 index 000000000..da1127f6a --- /dev/null +++ b/tests/ctests/meson_options.txt @@ -0,0 +1,2 @@ +option('parent_current_source_dir', type: 'string', value: '') +option('parent_current_build_dir', type: 'string', value: '') diff --git a/tests/ctests/run.sh b/tests/ctests/run.sh new file mode 120000 index 000000000..c07a74de4 --- /dev/null +++ b/tests/ctests/run.sh @@ -0,0 +1 @@ +build.sh \ No newline at end of file diff --git a/tools/run_asan.sh b/tools/run_asan.sh index 73ed35e38..7541ca3ad 100755 --- a/tools/run_asan.sh +++ b/tools/run_asan.sh @@ -5,11 +5,19 @@ set -x BUILDDIR="_leakcheckbuild" CLEANBUILDDIR="_cleanbuild" +CTESTSBUILDDIR="_ctestsbuild" CC=gcc meson setup ${BUILDDIR} -Db_sanitize=address,undefined meson compile -C ${BUILDDIR} --verbose +tests/ctests/build.sh \ + ${CTESTSBUILDDIR} \ + tests/ctests \ + -Dparent_current_source_dir=$(realpath .) \ + -Dparent_current_build_dir=$(realpath ${BUILDDIR}) \ + -Db_sanitize=address,undefined + meson setup ${CLEANBUILDDIR} meson compile -C ${CLEANBUILDDIR} --verbose @@ -18,10 +26,11 @@ ${CC} tools/keyfile_to_yaml.c -o tools/keyfile_to_yaml \ -Iinclude -L${BUILDDIR}/src \ -fsanitize=address,undefined -g -TESTS=$(find ${BUILDDIR}/tests/ctests/ -executable -type f) +TESTS=$(find ${CTESTSBUILDDIR} -executable -type f) for test in ${TESTS} do - ./${test} + # https://github.com/google/sanitizers/issues/1017 + ASAN_OPTIONS=detect_odr_violation=0 ./${test} done mkdir -p ${BUILDDIR}/fakeroot/{etc/netplan,run}