meson: Add googletest as a subproject

Allow the tests to build without requiring a system-wide installation of
googletest.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Iaf685cd1886672a435ba66c4aae3a0772b714dd5
diff --git a/meson.build b/meson.build
index ede4138..97518b1 100644
--- a/meson.build
+++ b/meson.build
@@ -11,10 +11,44 @@
      version: '1.0'
 )
 
-build_tests = get_option('tests')
-
 add_global_arguments('-Wno-psabi', language : ['c', 'cpp'])
 
+# Disable FORTIFY_SOURCE when compiling with no optimization
+if(get_option('optimization') == '0')
+  add_project_arguments('-U_FORTIFY_SOURCE',language:['cpp','c'])
+  message('Disabling FORTIFY_SOURCE as optimization is set to 0')
+endif
+
+# Setup googletest before we import any projects that also depend on it to make
+# sure we have control over its configuration
+build_tests = get_option('tests')
+if not build_tests.disabled()
+    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()
+        cmake = import('cmake')
+        gtest_opts = cmake.subproject_options()
+        gtest_opts.set_override_option('warning_level', '1')
+        gtest_opts.set_override_option('werror', 'false')
+        gtest_proj = cmake.subproject('googletest',
+                                      options: gtest_opts,
+                                      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
+endif
+
 phosphor_dbus_interfaces = dependency('phosphor-dbus-interfaces',
     default_options: [ 'data_com_ibm=true', 'data_org_open_power=true' ],
     fallback: ['phosphor-dbus-interfaces', 'phosphor_dbus_interfaces_dep'])
@@ -25,18 +59,16 @@
 
 sdbusplus = dependency('sdbusplus', fallback: [ 'sdbusplus', 'sdbusplus_dep' ])
 
+if not build_tests.disabled()
+    subdir('test')
+endif
+
 compiler = meson.get_compiler('cpp')
 python = find_program('python3', required:true)
 
 compiler.has_header('CLI/CLI.hpp')
 compiler.has_header('nlohmann/json.hpp')
 
-# Disable FORTIFY_SOURCE when compiling with no optimization
-if(get_option('optimization') == '0')
-  add_project_arguments('-U_FORTIFY_SOURCE',language:['cpp','c'])
-  message('Disabling FORTIFY_SOURCE as optimization is set to 0')
-endif
-
 configure_file(output: 'config.h',
                        configuration :{
                        'INVENTORY_JSON_DEFAULT': '"'+get_option('INVENTORY_JSON_DEFAULT')+'"',
@@ -168,4 +200,3 @@
                 install: true,
         )
 endif
-subdir('test')
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 74bb079..4aaaeea 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -11,9 +11,7 @@
 else
   dynamic_linker = []
 endif
-gmock = dependency('gmock', disabler: true, required: build_tests)
-gtest = dependency('gtest', main: true, disabler: true, required: build_tests)
-dependecy_list = [gtest, gmock, sdbusplus, phosphor_logging, phosphor_dbus_interfaces]
+dependecy_list = [gtest_dep, gmock_dep, sdbusplus, phosphor_logging, phosphor_dbus_interfaces]
 
 configuration_inc = include_directories('..', '../vpd-manager', 'vpd-manager-test', '../vpd-parser')