meson: Add meson build

The intent behind this commit is build obmc-phosphor-buttons with
meson, and then remove files related to cmake.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Iedf8c49224032e016fed3471e6e41f6055b09065
diff --git a/.gitignore b/.gitignore
index ac35ff1..e21410c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,3 @@
-buttons
-button-handler
-CMakeFiles
-CMakeCache.txt
-cmake_install.cmake
-Makefile
-inc/settings.hpp
+build*/
+subprojects/*
+!subprojects/*.wrap
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
deleted file mode 100644
index c6c34ad..0000000
--- a/CMakeLists.txt
+++ /dev/null
@@ -1,88 +0,0 @@
-cmake_minimum_required(VERSION 2.8.10 FATAL_ERROR)
-project(buttons CXX)
-set(CMAKE_CXX_STANDARD 17)
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
-
-include(GNUInstallDirs)
-
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
-
-set(POWER_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/Power")
-set(RESET_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/Reset")
-set(ID_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/ID")
-set(HS_DBUS_OBJECT_NAME "xyz/openbmc_project/Chassis/Buttons/HostSelector")
-
-set(GPIO_BASE_LABEL_NAME "1e780000.gpio")
-set(LONG_PRESS_TIME_MS 3000)
-set(CHASSIS_STATE_OBJECT_NAME "xyz/openbmc_project/state/chassis")
-set(CHASSISSYSTEM_STATE_OBJECT_NAME "xyz/openbmc_project/state/chassis_system")
-
-set(HOST_STATE_OBJECT_NAME "xyz/openbmc_project/state/host")
-set(ID_LED_GROUP "enclosure_identify" CACHE STRING "The identify LED group name")
-
-add_definitions(-DPOWER_DBUS_OBJECT_NAME="/${POWER_DBUS_OBJECT_NAME}0")
-add_definitions(-DRESET_DBUS_OBJECT_NAME="/${RESET_DBUS_OBJECT_NAME}0")
-add_definitions(-DID_DBUS_OBJECT_NAME="/${ID_DBUS_OBJECT_NAME}0")
-add_definitions(-DHS_DBUS_OBJECT_NAME="/${HS_DBUS_OBJECT_NAME}")
-
-add_definitions(-DGPIO_BASE_LABEL_NAME="${GPIO_BASE_LABEL_NAME}")
-add_definitions(-DLONG_PRESS_TIME_MS=${LONG_PRESS_TIME_MS})
-add_definitions(-DHOST_STATE_OBJECT_NAME="/${HOST_STATE_OBJECT_NAME}")
-add_definitions(-DCHASSIS_STATE_OBJECT_NAME="/${CHASSIS_STATE_OBJECT_NAME}")
-add_definitions(-DCHASSISSYSTEM_STATE_OBJECT_NAME="/${CHASSISSYSTEM_STATE_OBJECT_NAME}")
-
-set(SRC_FILES src/power_button.cpp
-    src/reset_button.cpp
-    src/id_button.cpp
-    src/hostSelector_switch.cpp
-
-    src/main.cpp
-    src/gpio.cpp
-)
-
-set(HANDLER_SRC_FILES
-    src/button_handler_main.cpp
-    src/button_handler.cpp
-)
-
-option (LOOKUP_GPIO_BASE
-    "Look up the GPIO base value in /sys/class/gpio. Otherwise use a base of 0." ON
-)
-
-configure_file (settings.hpp.in ${CMAKE_BINARY_DIR}/inc/settings.hpp)
-include_directories(${CMAKE_BINARY_DIR}/inc)
-
-# import sdbusplus
-find_package(PkgConfig REQUIRED)
-pkg_check_modules(SDBUSPLUSPLUS sdbusplus REQUIRED)
-include_directories(${SDBUSPLUSPLUS_INCLUDE_DIRS})
-link_directories(${SDBUSPLUSPLUS_LIBRARY_DIRS})
-find_program(SDBUSPLUSPLUS sdbus++)
-
-# import phosphor-logging
-find_package(PkgConfig REQUIRED)
-pkg_check_modules(LOGGING phosphor-logging REQUIRED)
-include_directories(${LOGGING_INCLUDE_DIRS})
-link_directories(${LOGGING_LIBRARY_DIRS})
-
-# phosphor-dbus-interfaces
-find_package(PkgConfig REQUIRED)
-pkg_check_modules(DBUSINTERFACE phosphor-dbus-interfaces REQUIRED)
-include_directories(${DBUSINTERFACE_INCLUDE_DIRS})
-link_directories(${DBUSINTERFACE_LIBRARY_DIRS})
-
-add_executable(${PROJECT_NAME} ${SRC_FILES} )
-target_link_libraries(${PROJECT_NAME} "${SDBUSPLUSPLUS_LIBRARIES} -lphosphor_dbus")
-
-add_executable(button-handler ${HANDLER_SRC_FILES})
-target_link_libraries(button-handler "${SDBUSPLUSPLUS_LIBRARIES} -lphosphor_dbus")
-
-set (
-    SERVICE_FILES
-    ${PROJECT_SOURCE_DIR}/service_files/xyz.openbmc_project.Chassis.Buttons.service
-    ${PROJECT_SOURCE_DIR}/service_files/phosphor-button-handler.service
-)
-
-install (FILES ${SERVICE_FILES} DESTINATION /lib/systemd/system/)
-install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
-install (TARGETS button-handler DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/inc/button_handler.hpp b/inc/button_handler.hpp
index baa7400..c6dc127 100644
--- a/inc/button_handler.hpp
+++ b/inc/button_handler.hpp
@@ -1,5 +1,4 @@
 #pragma once
-
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/bus/match.hpp>
 
diff --git a/inc/hostSelector_switch.hpp b/inc/hostSelector_switch.hpp
index 3f62012..cb9eab7 100644
--- a/inc/hostSelector_switch.hpp
+++ b/inc/hostSelector_switch.hpp
@@ -1,4 +1,6 @@
 #pragma once
+#include "config.h"
+
 #include "button_factory.hpp"
 #include "button_interface.hpp"
 #include "common.hpp"
diff --git a/inc/id_button.hpp b/inc/id_button.hpp
index 3e9c9e4..41a2059 100644
--- a/inc/id_button.hpp
+++ b/inc/id_button.hpp
@@ -15,6 +15,8 @@
 */
 
 #pragma once
+#include "config.h"
+
 #include "button_factory.hpp"
 #include "button_interface.hpp"
 #include "common.hpp"
diff --git a/inc/power_button.hpp b/inc/power_button.hpp
index 9bf8744..73336ac 100644
--- a/inc/power_button.hpp
+++ b/inc/power_button.hpp
@@ -15,6 +15,8 @@
 */
 
 #pragma once
+#include "config.h"
+
 #include "button_factory.hpp"
 #include "button_interface.hpp"
 #include "common.hpp"
diff --git a/inc/reset_button.hpp b/inc/reset_button.hpp
index 3bd7e3f..fea82b6 100644
--- a/inc/reset_button.hpp
+++ b/inc/reset_button.hpp
@@ -15,6 +15,8 @@
 */
 
 #pragma once
+#include "config.h"
+
 #include "button_factory.hpp"
 #include "button_interface.hpp"
 #include "common.hpp"
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..ff5ac12
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,100 @@
+project(
+    'phosphor-buttons', 'cpp',
+    version: '1.0.0',
+    meson_version: '>=0.58.0',
+    default_options: [
+        'warning_level=3',
+        'werror=true',
+        'cpp_std=c++20',
+    ]
+)
+
+conf_data = configuration_data()
+conf_data.set_quoted('POWER_DBUS_OBJECT_NAME', 'xyz/openbmc_project/Chassis/Buttons/Power')
+conf_data.set_quoted('RESET_DBUS_OBJECT_NAME', 'xyz/openbmc_project/Chassis/Buttons/Reset')
+conf_data.set_quoted('ID_DBUS_OBJECT_NAME', 'xyz/openbmc_project/Chassis/Buttons/ID')
+conf_data.set_quoted('HS_DBUS_OBJECT_NAME', 'xyz/openbmc_project/Chassis/Buttons/HostSelector')
+conf_data.set_quoted('GPIO_BASE_LABEL_NAME', '/com/inspur/cpld/motherboard/acfail')
+conf_data.set_quoted('BUTTON_PATH', '1e780000.gpio')
+conf_data.set_quoted('CHASSIS_STATE_OBJECT_NAME', 'xyz/openbmc_project/state/chassis')
+conf_data.set_quoted('CHASSISSYSTEM_STATE_OBJECT_NAME', 'xyz/openbmc_project/state/chassis_system')
+conf_data.set_quoted('HOST_STATE_OBJECT_NAME', 'xyz/openbmc_project/state/host')
+conf_data.set_quoted('ID_LED_GROUP', get_option('id-led-group'))
+
+conf_data.set('LONG_PRESS_TIME_MS', 3000)
+
+configure_file(output: 'config.h',
+    configuration: conf_data
+)
+
+sdbusplus_dep = dependency('sdbusplus')
+phosphor_dbus_interfaces_dep = dependency('phosphor-dbus-interfaces')
+phosphor_logging_dep = dependency('phosphor-logging')
+gpioplus_dep = dependency('gpioplus')
+
+cpp = meson.get_compiler('cpp')
+if cpp.has_header_symbol(
+        'nlohmann/json.hpp',
+        'nlohmann::json::string_t',
+        required:false)
+    nlohmann_json_dep = declare_dependency()
+else
+    nlohmann_json_dep = dependency('nlohmann-json')
+endif
+
+deps = [
+    sdbusplus_dep,
+    phosphor_dbus_interfaces_dep,
+    phosphor_logging_dep,
+    nlohmann_json_dep,
+    gpioplus_dep,
+]
+
+sources_buttons = [
+    'src/gpio.cpp',
+    'src/hostSelector_switch.cpp',
+    'src/id_button.cpp',
+    'src/main.cpp',
+    'src/power_button.cpp',
+    'src/reset_button.cpp',
+]
+
+sources_handler = [
+    'src/button_handler_main.cpp',
+    'src/button_handler.cpp',
+]
+
+executable(
+    'buttons',
+    sources_buttons,
+    implicit_include_directories: true,
+    include_directories: ['inc'],
+    dependencies: deps,
+    install: true,
+    install_dir: get_option('bindir')
+)
+
+executable(
+    'button-handler',
+    sources_handler,
+    implicit_include_directories: true,
+    include_directories: ['inc'],
+    dependencies: deps,
+    install: true,
+    install_dir: get_option('bindir')
+)
+
+systemd = dependency('systemd')
+systemd_system_unit_dir = systemd.get_variable(
+        pkgconfig: 'systemdsystemunitdir',
+        pkgconfig_define: ['prefix', get_option('prefix')])
+
+configure_file(input: 'service_files/phosphor-button-handler.service',
+                output: 'phosphor-button-handler.service',
+                copy: true,
+                install_dir: systemd_system_unit_dir)
+
+configure_file(input: 'service_files/xyz.openbmc_project.Chassis.Buttons.service',
+                output: 'xyz.openbmc_project.Chassis.Buttons.service',
+                copy: true,
+                install_dir: systemd_system_unit_dir)
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..fa7aa00
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,6 @@
+option(
+    'id-led-group',
+    type : 'string',
+    value: 'enclosure_identify',
+    description : 'The identify LED group name'
+)
diff --git a/settings.hpp.in b/settings.hpp.in
deleted file mode 100644
index f0ddd03..0000000
--- a/settings.hpp.in
+++ /dev/null
@@ -1,4 +0,0 @@
-#pragma once
-
-#cmakedefine LOOKUP_GPIO_BASE
-#cmakedefine ID_LED_GROUP "@ID_LED_GROUP@"
diff --git a/src/button_handler.cpp b/src/button_handler.cpp
index 764c807..4072e1d 100644
--- a/src/button_handler.cpp
+++ b/src/button_handler.cpp
@@ -1,6 +1,6 @@
-#include "button_handler.hpp"
+#include "config.h"
 
-#include "settings.hpp"
+#include "button_handler.hpp"
 
 #include <phosphor-logging/log.hpp>
 #include <xyz/openbmc_project/State/Chassis/server.hpp>
diff --git a/src/gpio.cpp b/src/gpio.cpp
index 52ffed6..faf5639 100644
--- a/src/gpio.cpp
+++ b/src/gpio.cpp
@@ -14,9 +14,9 @@
 // limitations under the License.
 */
 
-#include "gpio.hpp"
+#include "config.h"
 
-#include "settings.hpp"
+#include "gpio.hpp"
 
 #include <fcntl.h>
 #include <unistd.h>