obmc-console: Convert build to meson

The project is seeing some activity recently, so let's align it with the
desires here:

https://gerrit.openbmc.org/c/openbmc/docs/+/47732

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I96941365440c9c164d222b4d18e6a57409819308
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..81f13a2
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,74 @@
+project('obmc-console', 'c',
+    default_options: [
+      'buildtype=debugoptimized',
+      'warning_level=3',
+      'werror=true',
+      'c_std=gnu17',
+      'tests=' + (meson.is_subproject() ? 'false' : 'true'),
+    ],
+    version: '1.0.0',
+    meson_version: '>=0.63.0',
+)
+
+systemdsystemunitdir = dependency('systemd').get_variable('systemdsystemunitdir')
+install_data('conf/obmc-console@.service.in',
+             'conf/obmc-console@.socket.in',
+             'conf/obmc-console-ssh@.service.in',
+             rename: [
+               'obmc-console@.service',
+               'obmc-console@.socket',
+               'obmc-console-ssh@.service'
+             ],
+             install_dir: systemdsystemunitdir,
+           )
+
+if get_option('concurrent-servers')
+  install_data('conf/client.2200.conf.in',
+               rename: [ 'client.2200.conf' ],
+               install_dir: systemdsystemunitdir)
+else
+  install_data('conf/obmc-console-ssh.socket.in',
+               rename: [ 'obmc-console-ssh.socket' ],
+               install_dir: systemdsystemunitdir)
+  install_data('conf/obmc-console-ssh@.service.d/use-socket.conf.in',
+               rename: [ 'use-socket.conf' ],
+               install_dir: systemdsystemunitdir / 'obmc-console-ssh@.service.d')
+endif
+
+udev = dependency('udev', required: get_option('udev'))
+if udev.found()
+  install_data('conf/80-obmc-console-uart.rules.in',
+               rename: [ '80-obmc-console-uart.rules' ],
+               install_dir: udev.get_variable('udevdir') / 'rules.d')
+endif
+
+executable('obmc-console-server',
+           'config.c',
+           'console-server.c',
+           'console-socket.c',
+           'log-handler.c',
+           'ringbuffer.c',
+           'socket-handler.c',
+           'tty-handler.c',
+           'util.c',
+           c_args: [
+             '-DLOCALSTATEDIR="@0@"'.format(get_option('localstatedir')),
+             '-DSYSCONFDIR="@0@"'.format(get_option('sysconfdir'))
+           ],
+           dependencies: [
+             dependency('libsystemd'),
+             meson.get_compiler('c').find_library('rt')
+           ],
+           install: true)
+
+executable('obmc-console-client',
+           'config.c',
+           'console-client.c',
+           'console-socket.c',
+           'util.c',
+           c_args: [
+             '-DSYSCONFDIR="@0@"'.format(get_option('sysconfdir'))
+           ],
+           install: true)
+
+subdir('test')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..5ebc9f4
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,3 @@
+option('udev', type: 'feature', description: 'Install console udev rules')
+option('concurrent-servers', type: 'boolean', value: 'false', description: 'Support multiple concurrent obmc-console instances')
+option('tests', type: 'boolean', description: 'Enable the test suite')
diff --git a/test/meson.build b/test/meson.build
new file mode 100644
index 0000000..149322f
--- /dev/null
+++ b/test/meson.build
@@ -0,0 +1,17 @@
+tests = [
+	'test-client-escape',
+	'test-config-parse',
+	'test-config-parse-logsize',
+	'test-ringbuffer-boundary-poll',
+	'test-ringbuffer-boundary-read',
+	'test-ringbuffer-contained-offset-read',
+	'test-ringbuffer-contained-read',
+	'test-ringbuffer-poll-force',
+	'test-ringbuffer-read-commit',
+	'test-ringbuffer-simple-poll',
+]
+
+foreach t : tests
+  test(t, executable(t, f'@t@.c', c_args: [ '-DSYSCONFDIR=""' ],
+		     include_directories: '..'))
+endforeach
diff --git a/test/test-config-parse-logsize.c b/test/test-config-parse-logsize.c
index 49aef3a..964d894 100644
--- a/test/test-config-parse-logsize.c
+++ b/test/test-config-parse-logsize.c
@@ -3,8 +3,10 @@
 #include <stdint.h>
 #include <stdio.h>
 
+#ifndef SYSCONFDIR
 // Bypass compilation error due to -DSYSCONFDIR not provided
 #define SYSCONFDIR
+#endif
 
 #include "config.c"