build: Allow for vendored googletest

This makes it easier to run the test suite outside of the CI
environment but still attempts to use the system version of googletest
first.

Change-Id: I32193f08243db2177b8200b0ff7e3e47ef01691f
Signed-off-by: William A. Kennington III <wak@google.com>
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/.clang-ignore b/.clang-ignore
new file mode 100644
index 0000000..36f17f2
--- /dev/null
+++ b/.clang-ignore
@@ -0,0 +1 @@
+./subprojects
diff --git a/.gitignore b/.gitignore
index 24ddad5..5917395 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 # Meson build directories.
 /build*/
+/subprojects/*/
 
 # Created by https://www.gitignore.io/api/vim,python
 # Edit at https://www.gitignore.io/?templates=vim,python
diff --git a/meson.build b/meson.build
index 7edf7de..e089391 100644
--- a/meson.build
+++ b/meson.build
@@ -7,6 +7,7 @@
       'examples=' + (meson.is_subproject() ? 'disabled' : 'auto'),
     ],
     version: '1.0.0',
+    meson_version: '>=0.53.2',
 )
 
 libsystemd_pkg = dependency('libsystemd')
diff --git a/subprojects/googletest.wrap b/subprojects/googletest.wrap
new file mode 100644
index 0000000..56da9ef
--- /dev/null
+++ b/subprojects/googletest.wrap
@@ -0,0 +1,3 @@
+[wrap-git]
+url = https://github.com/google/googletest
+revision = HEAD
diff --git a/test/meson.build b/test/meson.build
index 1784e4c..374dada 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -1,5 +1,23 @@
-gtest_dep = dependency('gtest', main: true, disabler: true, required: get_option('tests'))
-gmock_dep = dependency('gmock', disabler: true, required: get_option('tests'))
+gtest_dep = dependency('gtest', main: true, disabler: true, required: false)
+gmock_dep = dependency('gmock', disabler: true, required: false)
+if not gtest_dep.found() or not gmock_dep.found()
+    gtest_proj = import('cmake').subproject('googletest', required: false)
+    if gtest_proj.found()
+        gtest_dep = declare_dependency(
+            dependencies: [
+                dependency('threads'),
+                gtest_proj.dependency('gtest'),
+                gtest_proj.dependency('gtest_main'),
+            ]
+        )
+        gmock_dep = gtest_proj.dependency('gmock')
+  else
+        assert(
+            not get_option('tests').enabled(),
+            'Googletest is required if tests are enabled'
+        )
+  endif
+endif
 
 tests = [
     'bus/list_names',