test/catch2: Use split headers and static library

This is required for compatability with Catch2 3.x

Change-Id: I53ecff8911345e8f037aee825b7abf30b90552d2
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/meson.build b/test/meson.build
index a63240f..0de7168 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -36,24 +36,44 @@
   endforeach
 endif
 
-# catch2 might not have a pkg-config. It is header only so just make
-# sure we can access the needed symbols from the header.
-catch2_dep = dependency('Catch2', required: false)
-has_catch2 = meson.get_compiler('cpp').has_header_symbol(
-  'catch2/catch.hpp',
-  'Approx',
-  dependencies: catch2_dep,
-  required: false)
-if not has_catch2
+# Switch to `catch2-with-main.pc` when 2.x supports it
+catch2 = dependency('catch2-with-main', required: false)
+if not catch2.found()
+  catch2 = dependency('catch2', required: false)
+  if catch2.found()
+    catch2_main = meson.get_compiler('cpp').find_library(
+      'Catch2WithMain', required: false)
+    if catch2_main.found()
+      catch2 = declare_dependency(
+        dependencies: [
+          catch2,
+          catch2_main,
+        ])
+    else
+      catch2 = declare_dependency(
+        link_with: static_library(
+          'catch2', 'catch2_main.cpp',
+          implicit_include_directories: false,
+          build_by_default: false,
+          dependencies: catch2),
+        dependencies: catch2)
+    endif
+  endif
+endif
+if not catch2.found()
   catch2_proj = import('cmake').subproject(
     'Catch2',
     cmake_options: [
-      '-DBUILD_TESTING=OFF'
+      '-DBUILD_TESTING=OFF',
+      '-DCMAKE_CXX_FLAGS=-Wno-non-virtual-dtor',
     ],
     required: false)
   if catch2_proj.found()
-    catch2_dep = catch2_proj.dependency('Catch2')
-    has_catch2 = true
+    catch2 = declare_dependency(
+      dependencies: [
+        catch2_proj.dependency('Catch2'),
+        catch2_proj.dependency('Catch2WithMain'),
+      ])
   else
     assert(not build_tests.enabled(), 'Catch2 is required')
   endif
@@ -64,18 +84,11 @@
   'raw',
 ]
 
-if has_catch2
-  libcatch2 = static_library(
-    'catch2', 'catch2_main.cpp',
-    implicit_include_directories: false,
-    build_by_default: false,
-    dependencies: catch2_dep)
-
+if catch2.found()
   foreach t : catch2_tests
     test(t, executable(t.underscorify(), t + '.cpp',
                        build_by_default: false,
                        implicit_include_directories: false,
-                       link_with: libcatch2,
-                       dependencies: [stdplus, span_dep, catch2_dep]))
+                       dependencies: [stdplus, span_dep, catch2]))
   endforeach
 endif
diff --git a/test/raw.cpp b/test/raw.cpp
index b781054..79fb2ad 100644
--- a/test/raw.cpp
+++ b/test/raw.cpp
@@ -1,5 +1,9 @@
 #include <array>
+#if __has_include(<catch2/catch.hpp>)
 #include <catch2/catch.hpp>
+#else
+#include <catch2/catch_test_macros.hpp>
+#endif
 #include <endian.h>
 #include <stdexcept>
 #include <stdplus/raw.hpp>
diff --git a/test/signal.cpp b/test/signal.cpp
index 91c717f..4778dac 100644
--- a/test/signal.cpp
+++ b/test/signal.cpp
@@ -1,4 +1,8 @@
+#if __has_include(<catch2/catch.hpp>)
 #include <catch2/catch.hpp>
+#else
+#include <catch2/catch_test_macros.hpp>
+#endif
 #include <cstring>
 #include <signal.h>
 #include <stdplus/signal.hpp>