From 04370a1dae33dcfddc1a688c34d095f5f14d07fa Mon Sep 17 00:00:00 2001
From: Dylan Baker <dylan@pnwbakers.com>
Date: Thu, 7 May 2020 14:40:53 -0700
Subject: [PATCH 1/2] meson: Add a meson build system

meson is a build system generator similar to cmake or autotools, but
without the crazy of either of those languages. It provides a pleasant
scripting language that is inspired by languages like python, but is not
python. It has a non-turing complete language, with an emphasis on
upstream functionality instead of downstream scripts. It has support for
most Unix-like OSes, including linux, the four major BSDs, and macOS.
this includes support for abstracting dependency discovery, using
pkg-config, macOS frameworks, cmake, and some hand coded extensions.

It provides nice features like builtin support for debug builds,
changing from static to shared library builds, turning warning arguments
on and off, generates for pkg-config, and other modern niceties.

For g810-led this provides a number of advantages for distro packaging.
Distros already use meson for projects like mesa, systemd, and gnome, so
they're packaging wrappers already know how to configure, build, and
install meson based packages. It also provides advantages when moving to
other platforms, as meson understands the difference between clang,
apple's clang fork, gcc, and a host of other compilers.
---
 install_link.sh   |   4 ++
 makerelease       |   3 +-
 meson.build       | 101 ++++++++++++++++++++++++++++++++++++++++++++++
 meson_options.txt |  21 ++++++++++
 4 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100755 install_link.sh
 create mode 100644 meson.build
 create mode 100644 meson_options.txt

diff --git a/install_link.sh b/install_link.sh
new file mode 100755
index 0000000..5c94bc7
--- /dev/null
+++ b/install_link.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+test -e $MESON_INSTALL_DESTDIR_PREFIX/$2 && rm -f $MESON_INSTALL_DESTDIR_PREFIX/$2
+ln -s $1 $MESON_INSTALL_DESTDIR_PREFIX/$2
diff --git a/makerelease b/makerelease
index 148e1bf..1267c39 100755
--- a/makerelease
+++ b/makerelease
@@ -34,12 +34,13 @@ esac
 version=$1
 
 sed -i "/string version = /c\\\tstring version = \"$version\";" src/helpers/help.cpp
+sed -i -E "s/(^\s*version :).*/\1 '$version',/" meson.build
 IFS='.' read -ra VPART <<< "$version"
 sed -i "/MAJOR=/cMAJOR=${VPART[0]}" makefile
 sed -i "/MINOR=/cMINOR=${VPART[1]}" makefile
 sed -i "/MICRO=/cMICRO=${VPART[2]}" makefile
 
-git commit -m "Version $version" makefile src/*
+git commit -m "Version $version" makefile meson.build src/*
 git push
 
 git checkout master
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..662a5d9
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,101 @@
+# This file is part of g810-led.
+
+# g810-led is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+
+# g810-led is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with g810-led.  If not, see <https://www.gnu.org/licenses/>.
+
+project(
+  'g810-led',
+  'cpp',
+  version : '0.4.1',
+  meson_version : '>= 0.52.0',
+  default_options : ['cpp_std=gnu++11'],
+)
+
+if get_option('usb') == 'hidapi'
+  dep_usb = dependency('hidapi-hidraw')
+  add_project_arguments('-Dhidapi', language : 'cpp')
+else
+  dep_usb = dependency('libusb-1.0')
+  add_project_arguments('-Dlibusb', language : 'cpp')
+endif
+
+add_project_arguments(
+  [
+    '-DVERSION="@0@"'.format(meson.project_version()),
+  ],
+  language : 'cpp'
+)
+
+libg810_led = library(
+  meson.project_name(),
+  [
+    'src/classes/Keyboard.cpp',
+  ],
+  version : meson.project_version(),
+  dependencies : [dep_usb],
+  install : true,
+)
+
+g810_ld = executable(
+  meson.project_name(),
+  [
+    'src/main.cpp',
+    'src/helpers/help.cpp',
+    'src/helpers/utils.cpp',
+  ],
+  link_with : libg810_led,
+  install : true,
+)
+
+installed_name = get_option('prefix') / get_option('bindir') / meson.project_name()
+
+foreach name : ['g213-led', 'g410-led', 'g413-led', 'g512-led', 'g513-led',
+                'g610-led', 'g815-led', 'g910-led', 'gpro-led']
+  meson.add_install_script('install_link.sh', installed_name,
+                           get_option('bindir') / name)
+endforeach
+
+install_headers(
+  'src/classes/Keyboard.h',
+  subdir : meson.project_name()
+)
+
+install_data(
+  'systemd/@0@-reboot.service'.format(meson.project_name()),
+  install_dir : get_option('prefix') / get_option('libdir') / 'systemd' / 'system',
+  install_mode : 'r--r--r--',
+)
+
+install_data(
+  'udev/@0@.rules'.format(meson.project_name()),
+  install_dir : get_option('sysconfdir') / 'udev' / 'rules.d',
+  install_mode : 'r--r--r--',
+)
+
+install_data(
+  'sample_profiles/all_blue',
+  'sample_profiles/all_green',
+  'sample_profiles/all_red',
+  'sample_profiles/fx_breathing_red',
+  'sample_profiles/fx_cwave',
+  'sample_profiles/fx_hwave',
+  'sample_profiles/group_keys',
+  'sample_profiles/all_blue_fxl_breathing_red',
+  'sample_profiles/all_off',
+  'sample_profiles/colors',
+  'sample_profiles/fx_color_green',
+  'sample_profiles/fx_cycle',
+  'sample_profiles/fx_vwave',
+  'sample_profiles/keys_v_gradiant_fr_ch-latin1',
+  install_dir : get_option('sysconfdir') / meson.project_name() / 'samples',
+  install_mode : 'r--r--r--',
+)
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..b710157
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,21 @@
+# This file is part of g810-led.
+
+# g810-led is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+
+# g810-led is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with g810-led.  If not, see <https://www.gnu.org/licenses/>.
+
+option(
+  'usb',
+  type : 'combo',
+  choices : ['hidapi', 'libusb'],
+  value : 'hidapi',
+  description : 'Which USB implementation to use'
+)

From 69b70ee41751c5337350c17782b43341861be4b5 Mon Sep 17 00:00:00 2001
From: Dylan Baker <dylan@pnwbakers.com>
Date: Thu, 7 May 2020 14:59:57 -0700
Subject: [PATCH 2/2] meson: Add a pkgconfig for libg810-led

This makes it much easier to link against libg810-led than manually
specifying compiler options, especially since in some cases you need to
pass extra cppflags.
---
 meson.build | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/meson.build b/meson.build
index 662a5d9..7159640 100644
--- a/meson.build
+++ b/meson.build
@@ -20,12 +20,12 @@ project(
   default_options : ['cpp_std=gnu++11'],
 )
 
+extra_cpp_args = []
 if get_option('usb') == 'hidapi'
   dep_usb = dependency('hidapi-hidraw')
-  add_project_arguments('-Dhidapi', language : 'cpp')
+  extra_cpp_args = ['-Dhidapi']
 else
   dep_usb = dependency('libusb-1.0')
-  add_project_arguments('-Dlibusb', language : 'cpp')
 endif
 
 add_project_arguments(
@@ -40,11 +40,19 @@ libg810_led = library(
   [
     'src/classes/Keyboard.cpp',
   ],
+  cpp_args : extra_cpp_args,
   version : meson.project_version(),
   dependencies : [dep_usb],
   install : true,
 )
 
+pkg = import('pkgconfig')
+pkg.generate(
+  libg810_led,
+  extra_cflags : extra_cpp_args,
+  description : 'library to control various logitech keyboard backlights',
+)
+
 g810_ld = executable(
   meson.project_name(),
   [