Skip to content

Commit

Permalink
build(meson): use system deps when avalable
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Tachi107 authored and marzer committed Feb 17, 2022
1 parent 7306fd2 commit 36030ca
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,6 @@ $RECYCLE.BIN/
*.msm
*.msp
*.lnk

# Clangd cache
/.cache/clangd
1 change: 1 addition & 0 deletions examples/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
example_args = []
example_args += universal_args
example_args += devel_args
example_overrides = []
example_overrides += overrides
Expand Down
7 changes: 2 additions & 5 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions tests/lib_catch2.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#if __has_include(<Catch2/single_include/catch2/catch.hpp>)
#include <Catch2/single_include/catch2/catch.hpp>
#elif __has_include(<catch2/catch.hpp>)
#include <catch2/catch.hpp>
#else
#error Catch2 is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/Catch2")
#endif
Expand Down
2 changes: 2 additions & 0 deletions tests/lib_tloptional.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#if __has_include(<tloptional/include/tl/optional.hpp>)
#include <tloptional/include/tl/optional.hpp>
#elif __has_include(<tl/optional.hpp>)
#include <tl/optional.hpp>
#else
#error TartanLlama/optional is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/tloptional")
#endif
Expand Down
34 changes: 30 additions & 4 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
#######################################################################################################################
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
]]
Expand Down Expand Up @@ -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
)
9 changes: 7 additions & 2 deletions toml-test/meson.build
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
2 changes: 2 additions & 0 deletions toml-test/tt.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#if __has_include(<json/single_include/nlohmann/json.hpp>)
#include <json/single_include/nlohmann/json.hpp>
#elif __has_include(<nlohmann/json.hpp>)
#include <nlohmann/json.hpp>
#else
#error nlohmann/json is missing! You probably need to fetch submodules ("git submodule update --init --depth 1 external/json")
#endif
Expand Down

0 comments on commit 36030ca

Please sign in to comment.