build: Refactor to separate out library

This makes it more clear what code is intended for use by the
phosphor-logging shared library. This is especially nice since it
isolates the `phosphor_logging_dep` to only provide the exported headers
instead of everything in the project.

Additionally, this adds an option to build only the library components
of the project when the services aren't needed.

Change-Id: Ied0858fc70e8054df4c056d91f35a6f0b3acfcb1
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/config.h.meson b/config/config.h.meson
similarity index 100%
rename from config.h.meson
rename to config/config.h.meson
diff --git a/config/meson.build b/config/meson.build
new file mode 100644
index 0000000..ff8f946
--- /dev/null
+++ b/config/meson.build
@@ -0,0 +1,13 @@
+# Create config.h with constants that use to come from autoconf.
+conf_data = configuration_data()
+conf_data.set('error_cap', get_option('error_cap'))
+conf_data.set('error_info_cap', get_option('error_info_cap'))
+conf_data.set('rsyslog_server_conf', get_option('rsyslog_server_conf'))
+conf_h_dep = declare_dependency(
+    include_directories: include_directories('.'),
+    sources: configure_file(
+        input: 'config.h.meson',
+        output: 'config.h',
+        configuration: conf_data,
+    )
+)
diff --git a/extensions/openpower-pels/meson.build b/extensions/openpower-pels/meson.build
index 1ef833e..8e1d038 100644
--- a/extensions/openpower-pels/meson.build
+++ b/extensions/openpower-pels/meson.build
@@ -77,6 +77,7 @@
 )
 
 libpel_deps = [
+    conf_h_dep,
     libpldm_dep,
     nlohmann_json,
     sdbusplus_dep,
@@ -136,6 +137,7 @@
 
 peltool_deps = [
     CLI11_dep,
+    conf_h_dep,
     python_dep,
 ]
 
diff --git a/elog.cpp b/lib/elog.cpp
similarity index 100%
rename from elog.cpp
rename to lib/elog.cpp
diff --git a/phosphor-logging/elog.hpp b/lib/include/phosphor-logging/elog.hpp
similarity index 100%
rename from phosphor-logging/elog.hpp
rename to lib/include/phosphor-logging/elog.hpp
diff --git a/phosphor-logging/log.hpp b/lib/include/phosphor-logging/log.hpp
similarity index 100%
rename from phosphor-logging/log.hpp
rename to lib/include/phosphor-logging/log.hpp
diff --git a/lib/include/phosphor-logging/meson.build b/lib/include/phosphor-logging/meson.build
new file mode 100644
index 0000000..490fad3
--- /dev/null
+++ b/lib/include/phosphor-logging/meson.build
@@ -0,0 +1,27 @@
+phosphor_logging_gen += custom_target(
+    'elog-errors.hpp'.underscorify(),
+    input: [elog_gen, template_elog_gen],
+    output: 'elog-errors.hpp',
+    command: [
+        python_prog, '@INPUT0@',
+        '-t', '',
+        '-m', '@INPUT1@',
+        '-y', yamldir,
+        '-u', tool_dir + '/',
+        '-o', '@OUTPUT0@',
+    ],
+    install: true,
+    install_dir: get_option('includedir') / 'phosphor-logging',
+)
+
+install_headers(
+    'elog.hpp',
+    'log.hpp',
+    'sdjournal.hpp',
+    subdir: 'phosphor-logging',
+)
+
+install_headers(
+    'test/sdjournal_mock.hpp',
+    subdir: 'phosphor-logging/test',
+)
diff --git a/phosphor-logging/sdjournal.hpp b/lib/include/phosphor-logging/sdjournal.hpp
similarity index 100%
rename from phosphor-logging/sdjournal.hpp
rename to lib/include/phosphor-logging/sdjournal.hpp
diff --git a/phosphor-logging/test/sdjournal_mock.hpp b/lib/include/phosphor-logging/test/sdjournal_mock.hpp
similarity index 100%
rename from phosphor-logging/test/sdjournal_mock.hpp
rename to lib/include/phosphor-logging/test/sdjournal_mock.hpp
diff --git a/lib/meson.build b/lib/meson.build
new file mode 100644
index 0000000..29ec542
--- /dev/null
+++ b/lib/meson.build
@@ -0,0 +1,48 @@
+phosphor_logging_includes = include_directories('include')
+
+phosphor_logging_gen = []
+
+subdir('include/phosphor-logging')
+
+phosphor_logging_deps = [
+    pdi_dep,
+    sdbusplus_dep,
+    systemd_dep,
+]
+
+phosphor_logging_lib = library(
+    'phosphor-logging',
+    'elog.cpp',
+    'sdjournal.cpp',
+    phosphor_logging_gen,
+    implicit_include_directories: false,
+    include_directories: phosphor_logging_includes,
+    dependencies: [
+        phosphor_logging_deps,
+        conf_h_dep,
+    ],
+    version: meson.project_version(),
+    install: true,
+)
+
+phosphor_logging_reqs = []
+foreach dep : phosphor_logging_deps
+    if dep.type_name() == 'pkgconfig'
+        phosphor_logging_reqs += dep
+    endif
+endforeach
+
+import('pkgconfig').generate(
+    phosphor_logging_lib,
+    version: meson.project_version(),
+    requires: phosphor_logging_reqs,
+    description: 'Phosphor logging utilities',
+)
+
+phosphor_logging_dep = declare_dependency(
+    include_directories: phosphor_logging_includes,
+    link_with: phosphor_logging_lib,
+    sources: phosphor_logging_gen,
+    dependencies: phosphor_logging_deps,
+)
+
diff --git a/sdjournal.cpp b/lib/sdjournal.cpp
similarity index 100%
rename from sdjournal.cpp
rename to lib/sdjournal.cpp
diff --git a/meson.build b/meson.build
index abde69b..7e3099c 100644
--- a/meson.build
+++ b/meson.build
@@ -5,38 +5,60 @@
       'cpp_std=c++20',
       'warning_level=3',
       'werror=true',
+      'libonly=' + (meson.is_subproject() ? 'true' : 'false'),
       'tests=' + (meson.is_subproject() ? 'disabled' : 'auto'),
     ],
     version: '1.0.0',
 )
 
-cpp = meson.get_compiler('cpp')
-python_prog = find_program('python3')
+python_prog = find_program('python3', native: true)
+systemd_dep = dependency('systemd')
 
-# Get sdbusplus dependency.
 sdbusplus_dep = dependency('sdbusplus', required: false)
-if sdbusplus_dep.found()
-    sdbusplusplus_prog = find_program('sdbus++')
-    sdbuspp_gen_meson_prog = find_program('sdbus++-gen-meson')
-else
-    sdbusplus_proj = subproject('sdbusplus', required: true)
+sdbusplus_proj = dependency('', required: false)
+if not sdbusplus_dep.found() or sdbusplus_dep.type_name() == 'internal'
+    sdbusplus_proj = subproject('sdbusplus')
+endif
+if not sdbusplus_dep.found()
     sdbusplus_dep = sdbusplus_proj.get_variable('sdbusplus_dep')
-    sdbusplusplus_prog = sdbusplus_proj.get_variable('sdbusplusplus_prog')
-    sdbuspp_gen_meson_prog = sdbusplus_proj.get_variable(
-        'sdbuspp_gen_meson_prog'
-    )
 endif
 
-# Get PDI and sdeventplus dependency.
 pdi_vars = []
 if not get_option('openpower-pel-extension').disabled()
-    pdi_vars += [ 'data_org_open_power=true' ]
+    pdi_vars += ['data_org_open_power=true']
 endif
 pdi_dep = dependency(
     'phosphor-dbus-interfaces',
     fallback: ['phosphor-dbus-interfaces', 'phosphor_dbus_interfaces_dep'],
     default_options: pdi_vars,
 )
+
+# Find the installed YAML directory, either from a configure option or
+# by pulling it from the PDI dependency.
+yamldir = get_option('yamldir')
+if yamldir == ''
+    yamldir = pdi_dep.get_variable(pkgconfig: 'yamldir', internal: 'yamldir')
+endif
+
+subdir('config')
+subdir('tools')
+subdir('lib')
+
+if get_option('libonly')
+  subdir_done()
+endif
+
+cpp = meson.get_compiler('cpp')
+
+if sdbusplus_proj.found()
+    sdbusplusplus_prog = sdbusplus_proj.get_variable('sdbusplusplus_prog')
+    sdbuspp_gen_meson_prog = sdbusplus_proj.get_variable(
+        'sdbuspp_gen_meson_prog')
+else
+    sdbusplusplus_prog = find_program('sdbus++', native: true)
+    sdbuspp_gen_meson_prog = find_program('sdbus++-gen-meson', native: true)
+endif
+
 sdeventplus_dep = dependency(
     'sdeventplus',
     fallback: ['sdeventplus', 'sdeventplus_dep' ],
@@ -60,25 +82,6 @@
     cereal_dep = cereal_proj.get_variable('cm_cereal_dep')
 endif
 
-# Find the installed YAML directory, either from a configure option or
-# by pulling it from the PDI dependency.
-yamldir = get_option('yamldir')
-if yamldir == ''
-    yamldir = pdi_dep.get_variable(pkgconfig: 'yamldir', internal: 'yamldir')
-endif
-
-# Create config.h with constants that use to come from autoconf.
-conf_data = configuration_data()
-conf_data.set('error_cap', get_option('error_cap'))
-conf_data.set('error_info_cap', get_option('error_info_cap'))
-conf_data.set('rsyslog_server_conf', get_option('rsyslog_server_conf'))
-
-configure_file(
-    input: 'config.h.meson',
-    output: 'config.h',
-    configuration: conf_data,
-)
-
 # Generate sdbus++ files.
 generated_sources = []
 generated_others = []
@@ -128,63 +131,16 @@
     ],
 )
 
-# Install elog-gen.py and its template for others to use
-install_data('tools/elog-gen.py',
-    install_dir : get_option('datadir') / 'phosphor-logging/elog/tools'
-)
-
-install_data('tools/phosphor-logging/templates/elog-gen-template.mako.hpp',
-    install_dir : get_option('datadir') /
-        'phosphor-logging/elog/tools/phosphor-logging/templates'
-)
-
-subdir('phosphor-logging')
-
 log_manager_ext_sources = []
 log_manager_ext_deps = []
 
 subdir('extensions')
 subdir('phosphor-rsyslog-config')
 
-# Build libphosphor-logging.
-libphosphor_logging = library(
-    'phosphor_logging',
-    files(
-        'elog.cpp',
-        'elog_meta.cpp',
-        'sdjournal.cpp',
-    ),
-    callouts_gen,
-    elog_errors_gen,
-    dependencies: [ sdbusplus_dep, pdi_dep, sdeventplus_dep ],
-    version: meson.project_version(),
-    install: true,
-)
-
-import('pkgconfig').generate(
-    libphosphor_logging,
-    name: meson.project_name(),
-    version: meson.project_version(),
-    requires: [
-        'libsystemd',
-        'phosphor-dbus-interfaces',
-        'sdbusplus',
-    ],
-    description: 'Phosphor logging utilities',
-)
-
-# Create dependency for use as subproject.
-phosphor_logging_dep = declare_dependency(
-    include_directories: include_directories('.'),
-    link_with: libphosphor_logging,
-    sources: [ callouts_gen, elog_errors_gen ],
-    dependencies: [ sdbusplus_dep, pdi_dep, sdeventplus_dep],
-)
-
 # Generate daemon.
 log_manager_sources = [
     generated_sources,
-    elog_errors_gen,
+    callouts_gen,
     elog_lookup_gen,
     elog_process_gen,
     files(
@@ -193,13 +149,14 @@
         'elog_serialize.cpp',
         'extensions.cpp',
         'log_manager.cpp',
-        'sdjournal.cpp',
         'util.cpp',
     )
 ]
 log_manager_deps = [
     cereal_dep,
+    conf_h_dep,
     pdi_dep,
+    phosphor_logging_dep,
     sdbusplus_dep,
     sdeventplus_dep,
 ]
@@ -208,7 +165,6 @@
     log_manager_ext_sources,
     'log_manager_main.cpp',
     include_directories: include_directories('gen'),
-    link_with: libphosphor_logging,
     dependencies: [
         log_manager_deps,
         log_manager_ext_deps,
@@ -218,20 +174,19 @@
 # Generate test executables which run in obmc env (qemu, real hardware).
 executable('logging-test',
     'logging_test.cpp',
-    link_with: libphosphor_logging,
     dependencies: [
-        elog_errors_dep,
         pdi_dep,
+        phosphor_logging_dep,
         sdbusplus_dep,
     ],
     install: true,
 )
 executable('callout-test',
     'callouts/callout_test.cpp',
-    link_with: libphosphor_logging,
     dependencies: [
-        elog_errors_dep,
+        conf_h_dep,
         pdi_dep,
+        phosphor_logging_dep,
         sdbusplus_dep,
         sdeventplus_dep,
     ],
diff --git a/meson_options.txt b/meson_options.txt
index 3eb946e..0dd7c3e 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1,3 +1,4 @@
+option('libonly', type: 'boolean', description: 'Build library only')
 option('tests', type: 'feature', description: 'Build tests')
 option(
     'openpower-pel-extension',
diff --git a/phosphor-logging/meson.build b/phosphor-logging/meson.build
deleted file mode 100644
index 13bd34a..0000000
--- a/phosphor-logging/meson.build
+++ /dev/null
@@ -1,35 +0,0 @@
-# Generate elog-errors.hpp.
-elog_errors_gen = custom_target('elog-errors.hpp'.underscorify(),
-    input: files(
-        '../tools/elog-gen.py',
-        '../tools/phosphor-logging/templates/elog-gen-template.mako.hpp',
-    ),
-    output: 'elog-errors.hpp',
-    command: [
-        python_prog, '@INPUT0@',
-        '-t', '',
-        '-m', '@INPUT1@',
-        '-y', yamldir,
-        '-u', meson.current_source_dir() / '../tools/',
-        '-o', '@OUTPUT0@',
-    ],
-    install: true,
-    install_dir: get_option('includedir') / 'phosphor-logging',
-)
-
-elog_errors_dep = declare_dependency(
-    include_directories: include_directories('..'),
-    sources: [elog_errors_gen],
-)
-
-# Install headers.
-install_headers(
-    'elog.hpp',
-    'log.hpp',
-    'sdjournal.hpp',
-    subdir: 'phosphor-logging',
-)
-install_headers(
-    'test/sdjournal_mock.hpp',
-    subdir: 'phosphor-logging/test',
-)
diff --git a/phosphor-rsyslog-config/meson.build b/phosphor-rsyslog-config/meson.build
index 087fc2d..a24baf9 100644
--- a/phosphor-rsyslog-config/meson.build
+++ b/phosphor-rsyslog-config/meson.build
@@ -1,12 +1,12 @@
 executable('phosphor-rsyslog-conf',
     'main.cpp',
     'server-conf.cpp',
-    '../sdjournal.cpp',
     '../elog_meta.cpp',
     elog_process_gen,
     include_directories: include_directories('..'),
     dependencies: [
-        elog_errors_dep,
+        conf_h_dep,
+        phosphor_logging_dep,
         pdi_dep,
         sdbusplus_dep,
         sdeventplus_dep,
diff --git a/test/meson.build b/test/meson.build
index 6bd81ac..57e8164 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -44,11 +44,11 @@
             'common.cpp',
             log_manager_sources,
             '../phosphor-rsyslog-config/server-conf.cpp',
-            link_with: libphosphor_logging,
             dependencies: [
                 gmock_dep,
                 gtest_dep,
                 log_manager_deps,
+                phosphor_logging_dep,
             ],
             include_directories: include_directories('..', '../gen'),
         )
diff --git a/test/openpower-pels/meson.build b/test/openpower-pels/meson.build
index f8121f8..923ead6 100644
--- a/test/openpower-pels/meson.build
+++ b/test/openpower-pels/meson.build
@@ -24,6 +24,7 @@
     'pel_manager': {
         'sources': [
             '../../elog_entry.cpp',
+            '../../elog_meta.cpp',
             '../../elog_serialize.cpp',
             '../../extensions.cpp',
             '../../log_manager.cpp',
@@ -90,12 +91,12 @@
             openpower_pels.get(t).get('sources', []),
             link_with: [
                 openpower_test_lib,
-                libphosphor_logging,
             ],
             link_args: [ '-lpython' + python_ver ],
             dependencies: [
                 gtest_dep,
                 gmock_dep,
+                phosphor_logging_dep,
                 libpel_deps,
                 peltool_deps,
                 openpower_pels.get(t).get('deps', []),
diff --git a/tools/meson.build b/tools/meson.build
new file mode 100644
index 0000000..3b06af0
--- /dev/null
+++ b/tools/meson.build
@@ -0,0 +1,9 @@
+tool_dir = meson.current_source_dir()
+
+elog_gen = files('elog-gen.py')
+install_data(
+    elog_gen,
+    install_dir: get_option('datadir') / 'phosphor-logging' / 'elog' / 'tools',
+)
+
+subdir('phosphor-logging/templates')
diff --git a/tools/phosphor-logging/templates/meson.build b/tools/phosphor-logging/templates/meson.build
new file mode 100644
index 0000000..ce48798
--- /dev/null
+++ b/tools/phosphor-logging/templates/meson.build
@@ -0,0 +1,11 @@
+template_elog_gen = files('elog-gen-template.mako.hpp')
+template_elog_lookup = files('elog-lookup-template.mako.cpp')
+template_elog_process_metadata = files('elog-process-metadata.mako.cpp')
+
+install_data(
+    template_elog_gen,
+    template_elog_lookup,
+    template_elog_process_metadata,
+    install_dir : get_option('datadir') / 'phosphor-logging' / 'elog' /
+        'tools' / 'phosphor-logging' / 'templates',
+)