meson: Fix meson builds

When building the project from scratch (from a new workstation without
docker) I found that there were dependencies missing. Fixed them up.

Tested: Was able to build " tools/" and "bmc/"

Signed-off-by: Brandon Kim <brandonkim@google.com>
Change-Id: Id2250c481a37d3cd4ed6915b1b2b0978df6e4649
diff --git a/.gitignore b/.gitignore
index e6abb17..8c2434d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,7 @@
-/build*/
-/subprojects/*/
\ No newline at end of file
+### Meson ###
+# subproject directories
+/subprojects/*/
+-/subprojects/*.wrap
+
+# build directory
+/build*/*
diff --git a/bmc/meson.build b/bmc/meson.build
index a65d888..461d03c 100644
--- a/bmc/meson.build
+++ b/bmc/meson.build
@@ -1,6 +1,7 @@
 bmc_inc = include_directories('.')
 
 common_pre = declare_dependency(
+  dependencies: [function2_dep, json_dep],
   include_directories: [root_inc, bmc_inc])
 
 common_lib = static_library(
@@ -23,4 +24,4 @@
 
 subdir('firmware-handler')
 subdir('version-handler')
-subdir('log-handler')
\ No newline at end of file
+subdir('log-handler')
diff --git a/meson.build b/meson.build
index ea2de60..cfaaf28 100644
--- a/meson.build
+++ b/meson.build
@@ -128,6 +128,34 @@
 
 blobs_dep = dependency('phosphor-ipmi-blobs')
 
+cpp = meson.get_compiler('cpp')
+# Function2 might not have a pkg-config. It is header only so just make
+# sure we can access the needed symbols from the header.
+function2_dep = dependency('function2', required: false)
+has_function2 = cpp.has_header_symbol(
+  'function2/function2.hpp',
+  'fu2::unique_function',
+  dependencies: function2_dep,
+  required: false)
+if not has_function2
+  function2_opts = import('cmake').subproject_options()
+  function2_opts.add_cmake_defines({'BUILD_TESTING': 'OFF'})
+  function2_proj = import('cmake').subproject(
+    'function2',
+    options: function2_opts,
+    required: false)
+  assert(function2_proj.found(), 'function2 is required')
+  if function2_proj.found()
+    function2_dep = function2_proj.dependency('function2')
+  endif
+endif
+
+if cpp.has_header('nlohmann/json.hpp')
+    json_dep = declare_dependency()
+else
+    json_dep = dependency('nlohmann_json')
+endif
+
 if not get_option('tests').disabled()
   gtest = dependency('gtest', main: true, disabler: true, required: false)
   gmock = dependency('gmock', disabler: true, required: false)
diff --git a/subprojects/function2.wrap b/subprojects/function2.wrap
new file mode 100644
index 0000000..3e495a8
--- /dev/null
+++ b/subprojects/function2.wrap
@@ -0,0 +1,3 @@
+[wrap-git]
+url = https://github.com/Naios/function2
+revision = HEAD
diff --git a/subprojects/nlohmann_json.wrap b/subprojects/nlohmann_json.wrap
new file mode 100644
index 0000000..477e844
--- /dev/null
+++ b/subprojects/nlohmann_json.wrap
@@ -0,0 +1,6 @@
+[wrap-git]
+url = https://github.com/nlohmann/json
+revision = HEAD
+
+[provide]
+nlohmann_json = nlohmann_json_dep
diff --git a/tools/meson.build b/tools/meson.build
index 826d04c..58fff65 100644
--- a/tools/meson.build
+++ b/tools/meson.build
@@ -5,7 +5,8 @@
   dependency('pciaccess', fallback: ['pciaccess', 'dep_pciaccess']),
   dependency('stdplus', fallback: ['stdplus', 'stdplus_dep']),
   blobs_dep,
-  sys_dep]
+  sys_dep,
+  function2_dep]
 
 updater_lib = static_library(
   'updater_lib',