From 2619164b6998b2143e776a1c2f10140af9fe878b Mon Sep 17 00:00:00 2001
From: Kieran Bingham <kieran.bingham@ideasonboard.com>
Date: Tue, 1 Nov 2022 21:19:20 +0000
Subject: meson: Convert CMake to Meson build infrastructure

Provide infrastructure to be able to build the uvc-gadget library as a
shared object and an executable which links against this as a dependency
to implment the reference 'uvc-gadget' application.

All existing cmake infrastructure is removed. This removes custom glob
support that was previously used by an early development for Android,
which is expected to be re-added later when required.

Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 CMakeLists.txt                | 14 -----------
 README.md                     | 39 ++++++-----------------------
 include/meson.build           |  3 +++
 include/uvcgadget/meson.build | 16 ++++++++++++
 lib/CMakeLists.txt            | 18 --------------
 lib/compat/CMakeLists.txt     | 12 ---------
 lib/meson.build               | 31 +++++++++++++++++++++++
 meson.build                   | 58 +++++++++++++++++++++++++++++++++++++++++++
 src/CMakeLists.txt            |  3 ---
 src/meson.build               | 10 ++++++++
 10 files changed, 126 insertions(+), 78 deletions(-)
 delete mode 100644 CMakeLists.txt
 create mode 100644 include/meson.build
 create mode 100644 include/uvcgadget/meson.build
 delete mode 100644 lib/CMakeLists.txt
 delete mode 100644 lib/compat/CMakeLists.txt
 create mode 100644 lib/meson.build
 create mode 100644 meson.build
 delete mode 100644 src/CMakeLists.txt
 create mode 100644 src/meson.build

diff --git a/CMakeLists.txt b/CMakeLists.txt
deleted file mode 100644
index 5291256..0000000
--- a/CMakeLists.txt
+++ /dev/null
@@ -1,14 +0,0 @@
-cmake_minimum_required(VERSION 3.0)
-project(uvc-gadget)
-set(CMAKE_BUILD_TYPE Release)
-
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter -Werror")
-
-include_directories(${CMAKE_BINARY_DIR}/generated)
-
-add_subdirectory(lib)
-add_subdirectory(src)
-
-# Generate config.h
-configure_file(${CMAKE_SOURCE_DIR}/include/config.h.in
-	       ${CMAKE_BINARY_DIR}/generated/config.h)
diff --git a/README.md b/README.md
index 932db55..f0728ff 100644
--- a/README.md
+++ b/README.md
@@ -11,41 +11,18 @@ uvcgadget is a pure C library that implements handling of UVC gadget functions.
 To compile:
 
 ```
-$ mkdir build
-$ cd build
-$ cmake ..
-$ make -j4
+$ meson build
+$ ninja -C build
 ```
 
 ## Cross compiling instructions:
 
-Directions for cross compiling depend on the build environment.
-
-For buildroot-based builds, cmake can be pointed to the toolchain file provided
-by buildroot:
-
-```
-$ mkdir build
-$ cd build
-$ cmake -DCMAKE_TOOLCHAIN_FILE=<buildrootpath>/output/host/usr/share/buildroot/toolchainfile.cmake ..
-$ make -j4
-```
-
-If your build environment doesn't provide a CMake toolchain file, the following
-template can be used as a starting point.
+Cross compilation can be managed by meson. Please read the directions at
+https://mesonbuild.com/Cross-compilation.html for detailed guidance on using
+meson.
 
+In brief summary:
 ```
-set(CMAKE_SYSTEM_NAME Linux)
-
-set(BUILD_ENV_ROOT "/path/to/your/build/enviroment/root/")
-
-# Specify the cross compiler
-set(CMAKE_C_COMPILER   ${BUILD_ENV_ROOT}/host/usr/bin/arm-buildroot-linux-gnueabihf-gcc)
-
-# Where is the target environment
-set(CMAKE_FIND_ROOT_PATH ${BUILD_ENV_ROOT}/target ${BUILD_ENV_ROOT}/host)
-
-set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
-set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
-set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+$ meson build --cross <meson cross file>
+$ ninja -C build
 ```
diff --git a/include/meson.build b/include/meson.build
new file mode 100644
index 0000000..a4e0f08
--- /dev/null
+++ b/include/meson.build
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: CC0-1.0
+
+subdir('uvcgadget')
diff --git a/include/uvcgadget/meson.build b/include/uvcgadget/meson.build
new file mode 100644
index 0000000..c79ea18
--- /dev/null
+++ b/include/uvcgadget/meson.build
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: CC0-1.0
+
+uvcgadget_public_headers = files([
+  'configfs.h',
+  'events.h',
+  'list.h',
+  'stream.h',
+  'v4l2-source.h',
+  'video-source.h',
+  ])
+
+
+install_headers(uvcgadget_public_headers,
+                subdir : 'uvcgadget')
+
+includes = include_directories('.')
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
deleted file mode 100644
index eacfb9c..0000000
--- a/lib/CMakeLists.txt
+++ /dev/null
@@ -1,18 +0,0 @@
-file(GLOB SOURCES "*.c" "*.h")
-file(GLOB HEADERS "../include/uvcgadget/*.h")
-
-add_subdirectory(compat)
-
-add_library(uvcgadget ${SOURCES})
-
-set_target_properties(uvcgadget PROPERTIES PUBLIC_HEADER "${HEADERS}")
-target_include_directories(uvcgadget
-	PUBLIC
-	$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
-	$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/uvcgadget>
-	$<INSTALL_INTERFACE:include>)
-
-install(TARGETS uvcgadget
-	LIBRARY DESTINATION lib
-	ARCHIVE DESTINATION lib
-	PUBLIC_HEADER DESTINATION include/uvcgadget)
diff --git a/lib/compat/CMakeLists.txt b/lib/compat/CMakeLists.txt
deleted file mode 100644
index 5fd6db4..0000000
--- a/lib/compat/CMakeLists.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-include(CheckFunctionExists)
-include(CheckIncludeFile)
-
-check_include_file(dirent.h HAVE_DIRENT_H)
-
-check_function_exists(glob HAVE_GLOB)
-if(NOT HAVE_GLOB)
-	set(SOURCES "${SOURCES}"
-	    "${CMAKE_CURRENT_SOURCE_DIR}/glob.c"
-	    "${CMAKE_SOURCE_DIR}/include/compat/glob.h"
-	    PARENT_SCOPE)
-endif()
diff --git a/lib/meson.build b/lib/meson.build
new file mode 100644
index 0000000..7db7519
--- /dev/null
+++ b/lib/meson.build
@@ -0,0 +1,31 @@
+# SPDX-License-Identifier: CC0-1.0
+
+libuvcgadget_sources = files([
+  'configfs.c',
+  'events.c',
+  'stream.c',
+  'uvc.c',
+  'v4l2.c',
+  'v4l2-source.c',
+  'video-buffers.c',
+  'video-source.c',
+])
+
+libuvcgadget = shared_library('uvcgadget',
+                              libuvcgadget_sources,
+                              version : uvc_gadget_version,
+                              install : true,
+                              include_directories : includes)
+
+libuvcgadget_dep = declare_dependency(sources : [
+                                          uvcgadget_public_headers,
+                                     ],
+                                     include_directories : includes,
+                                     link_with : libuvcgadget)
+
+pkg_mod = import('pkgconfig')
+pkg_mod.generate(libuvcgadget,
+                 description : 'UVC Gadget support library',
+                 subdirs : 'uvcgadget')
+
+meson.override_dependency('uvcgadget', libuvcgadget_dep)
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..5a9d3fb
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: CC0-1.0
+
+project('uvc-gadget', 'c',
+    meson_version : '>= 0.56',
+    version : '0.0.0',
+    default_options : [
+        'werror=true',
+        'warning_level=2',
+    ],
+    license : 'LGPL 2.1+')
+
+# Generate version information. The uvc_gadget_git_version variable contains the
+# full version with git patch count and SHA1 (e.g. 1.2.3+211-c94a24f4), while
+# the uvc_gadget_version variable contains the major.minor.patch (e.g. 1.2.3)
+# only. If the source tree isn't under git control, or if it matches the last
+# git version tag, the build metadata (e.g. +211-c94a24f4) is omitted from
+# uvc_gadget_git_version.
+uvc_gadget_git_version = run_command('scripts/gen-version.sh',
+                                    meson.project_build_root(),
+                                    meson.project_source_root(),
+                                    check: false).stdout().strip()
+if uvc_gadget_git_version == ''
+    uvc_gadget_git_version = meson.project_version()
+endif
+
+uvc_gadget_version = uvc_gadget_git_version.split('+')[0]
+
+# A shallow clone, or a clone without a reachable tag equivalent to the
+# meson.project_version() could leave the project in a mis-described state.
+# Produce a warning in this event, and fix to a best effort.
+if uvc_gadget_version != meson.project_version()
+    warning('The sources disagree about the version: '
+            + uvc_gadget_version + ' != ' + meson.project_version())
+
+    summary({'uvc-gadget git version' : uvc_gadget_git_version,
+             'Source version match' : false,
+            },
+            bool_yn : true, section : 'Versions')
+
+    # Replace the version components reported by git with the release version,
+    # but keep all trailing information supplied by git.
+    uvc_gadget_git_version = (meson.project_version() +
+                              uvc_gadget_git_version.strip(uvc_gadget_version))
+    uvc_gadget_version = meson.project_version()
+
+    # Append a marker to show we have modified this version string
+    uvc_gadget_git_version += '-nvm'
+endif
+
+summary({ 'Sources': uvc_gadget_git_version, }, section : 'Versions')
+
+# Configure the build environment.
+cc = meson.get_compiler('c')
+
+subdir('include')
+
+subdir('lib')
+subdir('src')
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
deleted file mode 100644
index 9f94788..0000000
--- a/src/CMakeLists.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-add_executable(uvc-gadget main.c)
-target_link_libraries(uvc-gadget uvcgadget)
-install(TARGETS uvc-gadget DESTINATION bin)
diff --git a/src/meson.build b/src/meson.build
new file mode 100644
index 0000000..a457c28
--- /dev/null
+++ b/src/meson.build
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: CC0-1.0
+
+libuvcgadget = dependency('uvcgadget')
+
+gadget = executable('uvc-gadget', 'main.c',
+                    dependencies : [
+                        libuvcgadget,
+                    ],
+                    include_directories : includes,
+                    install : true)
-- 
cgit v1.2.3