From 36030cace8fae114f7955bb7f239733d6dbe9f8b Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Mon, 14 Feb 2022 18:41:52 +0100 Subject: [PATCH] build(meson): use system deps when avalable When building tests, Meson will now look for system dependencies and use them if found. Otherwise, it will check if the submodules are cloned and create a dependency object wrapping the include path of each external dependency (this breaks Meson's sandbox, but the previous approach did too). This also allows passing actual dependencies to the various build targets instead of include paths (a bit more idiomatic). I've also made it possible to skip the tl-optional tests when the lib is not found, so that it is still possible to run tests even from a tarball (i.e. no git checkout) --- .gitignore | 3 +++ examples/meson.build | 1 + meson.build | 7 ++----- tests/lib_catch2.h | 2 ++ tests/lib_tloptional.h | 2 ++ tests/meson.build | 34 ++++++++++++++++++++++++++++++---- toml-test/meson.build | 9 +++++++-- toml-test/tt.h | 2 ++ 8 files changed, 49 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 72aa02c6..96219e78 100644 --- a/.gitignore +++ b/.gitignore @@ -367,3 +367,6 @@ $RECYCLE.BIN/ *.msm *.msp *.lnk + +# Clangd cache +/.cache/clangd diff --git a/examples/meson.build b/examples/meson.build index b002b0a2..2e7022d3 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,4 +1,5 @@ example_args = [] +example_args += universal_args example_args += devel_args example_overrides = [] example_overrides += overrides diff --git a/meson.build b/meson.build index 632f9015..1c92db76 100644 --- a/meson.build +++ b/meson.build @@ -4,6 +4,7 @@ project( version: '3.0.1', meson_version: '>=0.54.0', license: 'MIT', + subproject_dir: 'external', default_options: [ # https://mesonbuild.com/Builtin-options.html # core options 'buildtype=release', @@ -42,7 +43,7 @@ is_windows = host_machine.system() == 'windows' is_x64 = host_machine.cpu_family() == 'x86_64' is_subproject = meson.is_subproject() has_exceptions = get_option('cpp_eh') != 'none' -include_dirs = include_directories('include', 'external') +include_dir = include_directories('include') overrides = [] universal_args = [] # args used in tests, examples, lib, everything devel_args = [] # args used in everything *but* the lib @@ -446,8 +447,6 @@ endif # subdirectories ####################################################################################################################### -include_dir = include_directories('include') - # Empty dependency that will be filled either in src/ or include/ tomlplusplus_dep = dependency('', required: false) @@ -459,8 +458,6 @@ endif build_tests = get_option('build_tests') and not is_subproject if build_tests - run_command('git', 'submodule', 'update', '--init', '--depth', '1', 'external/Catch2') - run_command('git', 'submodule', 'update', '--init', '--depth', '1', 'external/tloptional') subdir('tests') endif diff --git a/tests/lib_catch2.h b/tests/lib_catch2.h index f14529e8..acd97959 100644 --- a/tests/lib_catch2.h +++ b/tests/lib_catch2.h @@ -24,6 +24,8 @@ #if __has_include() #include +#elif __has_include() +#include #else #error Catch2 is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/Catch2") #endif diff --git a/tests/lib_tloptional.h b/tests/lib_tloptional.h index 4fef01fb..d66b1da6 100644 --- a/tests/lib_tloptional.h +++ b/tests/lib_tloptional.h @@ -17,6 +17,8 @@ #if __has_include() #include +#elif __has_include() +#include #else #error TartanLlama/optional is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/tloptional") #endif diff --git a/tests/meson.build b/tests/meson.build index ca7b64d0..efbe4817 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -26,6 +26,33 @@ test_sources = [ 'windows_compat.cpp' ] +test_deps = [tomlplusplus_dep] + +fs = import('fs') + +catch2_dep = dependency('catch2', required: false) +if catch2_dep.found() + test_deps += catch2_dep +elif fs.exists('..'/'external'/'Catch2'/'single_include') + catch2_dep = declare_dependency(include_directories: '..'/'external'/'Catch2'/'single_include') + test_deps += catch2_dep +else + error('Catch2 is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/Catch2"') +endif + +has_tl_optional = false +tl_optional_dep = dependency('tl-optional', required: false) +if tl_optional_dep.found() + test_deps += tl_optional_dep + has_tl_optional = true +elif fs.exists('..'/'external'/'tloptional'/'include') + tl_optional_dep = declare_dependency(include_directories: '..'/'external'/'tloptional'/'include') + test_deps += tl_optional_dep + has_tl_optional = true +else + warning('tl-optional not found, skipping its tests') +endif + ####################################################################################################################### # fast math check ####################################################################################################################### @@ -87,7 +114,7 @@ foreach cpp20 : cpp20_modes test_args += test_base_args single_header = (counter % 2 == 1) - tl_optional = (counter % 4 == 2 and exceptions and not get_option('compile_library')) + tl_optional = (counter % 4 == 2 and exceptions and not get_option('compile_library') and has_tl_optional) address_sanitizer = (is_clang and not (single_header or tl_optional or fast_math)) and get_option('asan_tests') if cpp20 @@ -161,9 +188,8 @@ foreach cpp20 : cpp20_modes executable( test_name, test_sources, - include_directories: include_dirs, cpp_args: test_args, - dependencies: tomlplusplus_dep, + dependencies: test_deps, override_options: test_overrides ) ]] @@ -203,7 +229,7 @@ endforeach executable( 'odr_test', [ 'odr_test_1.cpp', 'odr_test_2.cpp' ], - include_directories: include_dirs, cpp_args: test_base_args, + dependencies: tomlplusplus_dep, override_options: overrides ) diff --git a/toml-test/meson.build b/toml-test/meson.build index 949b6c2a..77228c91 100644 --- a/toml-test/meson.build +++ b/toml-test/meson.build @@ -1,9 +1,14 @@ +tt_deps = [ + tomlplusplus_dep, + dependency('nlohmann_json', fallback: ['json', 'nlohmann_json_dep']) +] + if get_option('build_tt_encoder') executable( 'tt_encoder', 'tt_encoder.cpp', cpp_args: devel_args, - include_directories: include_dirs + dependencies: tt_deps ) endif @@ -12,6 +17,6 @@ if get_option('build_tt_decoder') 'tt_decoder', 'tt_decoder.cpp', cpp_args: devel_args, - include_directories: include_dirs + dependencies: tt_deps ) endif diff --git a/toml-test/tt.h b/toml-test/tt.h index e04f5bef..cae2f304 100644 --- a/toml-test/tt.h +++ b/toml-test/tt.h @@ -17,6 +17,8 @@ #if __has_include() #include +#elif __has_include() +#include #else #error nlohmann/json is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/json") #endif