From: Martin Belanger Date: Thu, 7 Oct 2021 00:39:55 +0000 (-0400) Subject: build: Add support for meson build system X-Git-Tag: v1.0-rc0~92^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8aefabff6c06de7ec519bacaada17fdd66f6da2e;p=users%2Fsagi%2Flibnvme.git build: Add support for meson build system [dwagner: updated README.md] Signed-off-by: Daniel Wagner --- diff --git a/.gitignore b/.gitignore index cbf80394..29a202e3 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ config-host.mak config.log cscope.* + +.build diff --git a/README b/README deleted file mode 100644 index 1b40be2e..00000000 --- a/README +++ /dev/null @@ -1,26 +0,0 @@ -libnvme -------- - -This is the libnvme development C library. libnvme provides type -defintions for NVMe specification structures, enumerations, and bit -fields, helper functions to construct, dispatch, and decode commands -and payloads, and utilities to connect, scan, and manage nvme devices -on a Linux system. - -The public specification is the authority to resolve any protocol -discrepencies with this library. For more info on NVM Express, please see: - - http://nvmexpress.org - -Subscribe to linux-nvme@lists.infradead.org for linux-nvme related discussions -and development for both kernel and userspace. The list is archived here: - - http://lists.infradead.org/mailman/listinfo/linux-nvme - -License -------- - -Except where otherwise stated, all software contained within this repo is -currently licensed LGPL, see COPYING for more information. - -Keith Busch 2020-02-06 diff --git a/README.md b/README.md new file mode 100644 index 00000000..2613a528 --- /dev/null +++ b/README.md @@ -0,0 +1,173 @@ +# libnvme + +This is the libnvme development C library. libnvme provides type +defintions for NVMe specification structures, enumerations, and bit +fields, helper functions to construct, dispatch, and decode commands +and payloads, and utilities to connect, scan, and manage nvme devices +on a Linux system. + +The public specification is the authority to resolve any protocol +discrepencies with this library. For more info on NVM Express, please +see: + + http://nvmexpress.org + +Subscribe to linux-nvme@lists.infradead.org for linux-nvme related +discussions and development for both kernel and userspace. The list is +archived here: + + http://lists.infradead.org/mailman/listinfo/linux-nvme + +# License + +Except where otherwise stated, all software contained within this repo +is currently licensed LGPL, see COPYING for more information. + +Keith Busch 2020-02-06 + +------ + +# Building with meson + +## What is the meson build system? + +Here's an excerpt from the meson web site: *Meson is **an open source +build system** meant to be both extremely fast, and, even more +importantly, as user friendly as possible. The main design point of +Meson is that every moment a developer spends writing or debugging +build definitions is a second wasted.* + +Several well-known projects such as `systemd` and `Gnome` use meson as +their build system. A summary of projects using meson can be found +[here](https://mesonbuild.com/Users.html). For more info on meson, +please consult the following sites: + +**Wiki page**: https://en.wikipedia.org/wiki/Meson_(software) + +**meson documentation**: https://mesonbuild.com/ + +**meson repo**: https://github.com/mesonbuild/meson + +## Prerequisite + +First, install meson. + +**Debian / Ubuntu**: + +```bash +sudo apt-get install meson +``` + +**Fedora / Red Hat**: + +```bash +sudo dnf install meson +``` + +## To compile libnvme + +Using meson is similar to projects that use a `configure` script before running `make`. + +To `configure` the project: + +``` +meson .build +``` + +One nice feature of meson is that it doesn't mix build artifacts +(e.g. `*.o`, `*.so`, etc.) with source code. In the above example, +"`.build`" is the name of the directory where the build configuration +as well as all the build artifacts will be saved. This directory can +be named anything as long as it's not an existing source directory. To +completely "clean" all the build artifacts, one need only delete the +`.build` directory. + +To compile: + +``` +cd .build +ninja +``` + +Or: + +``` +ninja -C .build +``` + +## To install libnvme + +To install `libnvme`: + +``` +cd .build +meson install +``` + +## To run unit tests + +To run unit tests: + +``` +cd .build +meson test +``` + +## To clean after a build + +To perform the equivalent of a `make clean` without deleting the build configuration. + +``` +cd .build +ninja -t clean +``` + +Or: + +``` +ninja -C .build -t clean +``` + +## To purge everything + +To completely clean all build artifacts, including the build configuration. + +``` +rm -rf .build +``` + +## Supported build options + +A few build options can be specified on the command line when invoking meson. + +| Option | Values [default] | Description | +| ------- | ------------------- | ------------------------------------------------------------ | +| systemd | [auto], true, false | Whether to link libsystemd to libnvme. When set to `auto`, the default, meson will check for the presence of library and will only link to it if it is found. When set to `true`, meson will make this library a mandatory dependency. When set to `false`, meson will not link the library to libnvme, even if the library is available. | + +### Changing the build options from the command-line (i.e. w/o modifying any files) + +Here's an example where we tell meson that we do not want to link +against the `systemd` library: + +```bash +meson .build -Dsystemd=false +``` + +To configure a build for debugging purposes (i.e. optimization turned +off and debug symbols enabled): + +```bash +meson .build -Dbuildtype=debug +``` + +To enable address sanitizer (advanced debugging of memory issues): + +```bash +meson .build -Db_sanitize=address +``` + +To list configuration options that are available and possible values: + +```bash +meson configure .build +``` diff --git a/ccan/meson.build b/ccan/meson.build new file mode 100644 index 00000000..f24b94f5 --- /dev/null +++ b/ccan/meson.build @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# +configurator = executable( + 'configurator', + ['tools/configurator/configurator.c'], + c_args: ['-D_GNU_SOURCE'], +) + +config_h = custom_target( + 'config.h', + output: 'config.h', + capture: true, + command: [configurator, ] +) + + diff --git a/doc/meson.build b/doc/meson.build new file mode 100644 index 00000000..d4bbf566 --- /dev/null +++ b/doc/meson.build @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: BSD-3 +# +# This file is part of STorage Appliance Services (STAS). +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# + +# Requires python3-sphinx and python3-sphinx-rtd-theme + +sphinx_sources = [ + 'conf.py', + 'libnvme.rst', + 'index.rst' +] + +man_pages = [ + 'libnvme.1' +] + +mandir1 = join_paths(get_option('mandir'), 'man1') + +if get_option('man') + sphinx_build = find_program('sphinx-build-3', 'sphinx-build') + + custom_target( + 'man', + command: [sphinx_build, + '-b', 'man', + meson.current_source_dir(), + meson.current_build_dir()], + input: sphinx_sources, + output: man_pages, + install: true, + install_dir: mandir1, + ) +endif diff --git a/examples/meson.build b/examples/meson.build new file mode 100644 index 00000000..0ed97d00 --- /dev/null +++ b/examples/meson.build @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# +executable( + 'telemetry-listen', + ['telemetry-listen.c'], + link_with: libnvme_static, + include_directories: incdir) + +executable( + 'display-columnar', + ['display-columnar.c'], + link_with: libnvme_static, + include_directories: incdir) + +executable( + 'discover-loop', + ['discover-loop.c'], + link_with: libnvme_static, + include_directories: incdir) + diff --git a/libnvme.spec.in b/libnvme.spec.in new file mode 100644 index 00000000..e2dbd139 --- /dev/null +++ b/libnvme.spec.in @@ -0,0 +1,53 @@ +Name: @NAME@ +Version: @VERSION@ +Release: 0 +Summary: Linux-native nvme device management library +License: @LICENSE@ +Source: %{name}-%{version}.tar.gz +BuildRoot: %{_tmppath}/%{name}-root +URL: http://github.com/linux-nvme/libnvme +BuildRequires: gcc + +%description +Provides library functions for accessing and managing nvme devices on a Linux +system. + +%package devel +Summary: Development files for Linux-native nvme +Requires: libnvme +Provides: libnvme.so.1 + +%description devel +This package provides header files to include and libraries to link with +for Linux-native nvme device maangement. + +%prep +%autosetup -c + +%build +%meson +%meson_build + +%install +%meson_install + +%check +%meson_test + +%files +%defattr(-,root,root) +%attr(0755,root,root) %{_libdir}/libnvme.so.* +%doc COPYING + +%files devel +%defattr(-,root,root) +%attr(-,root,root) %{_includedir}/nvme/ +%attr(0644,root,root) %{_includedir}/libnvme.h +%attr(0755,root,root) %{_libdir}/libnvme.so +%attr(0644,root,root) %{_libdir}/libnvme.a +%attr(0644,root,root) %{_libdir}/pkgconfig/* +%attr(0644,root,root) %{_mandir}/man2/* + +%changelog +* Thu Dec 12 2019 Keith Busch - 0.1 +- Initial version diff --git a/meson.build b/meson.build new file mode 100644 index 00000000..1da4c299 --- /dev/null +++ b/meson.build @@ -0,0 +1,200 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# + +################################################################################ +# Building libnvme using meson and ninja: +# meson .build +# ninja -C .build +# +# Installing the code after building: +# cd .build +# sudo meson install +# +# Running unit tests: +# cd .build +# meson test +# +# In these examples, ".build" is the name of the directory where the build +# artifacts are saved. The directory need not be called ".build", but it must +# be a unique (non-existing) directory. +# +# Changing build options from the command line: +# Build options can be changed at the command line without modifying the +# "meson.build" files. This is particularly useful during debugging. For +# example, the "buildtype" option allows to disable optimization to +# facilitate debugging. This option can be specified on the command line as +# follows: +# +# meson .build -Dbuildtype=debug +# +# Doing so overrides the value found in the meson.build, which is set to +# "buildtype=release" below. The buildtype option can take any of the +# following values. +# +# plain: no extra build flags are used, even for compiler warnings, +# useful for distro packagers and other cases where you need +# to specify all arguments by yourself +# +# debug: debug info is generated but the result is not optimized, +# this is the default +# +# debugoptimized: debug info is generated and the code is optimized (on most +# compilers this means -g -O2) +# +# release: full optimization, no debug info +# +# default_options: https://mesonbuild.com/Builtin-options.html#compiler-options +# +# Examples: meson .build -Dbuildtype=debug +# meson .build -Db_sanitize=address +# meson .build -Djson-c=true +# +# References: https://mesonbuild.com/ +# https://ninja-build.org/ +# +################################################################################ +project( + 'libnvme', ['c', 'cpp'], + meson_version: '>= 0.47.0', + version: '0.1', + license: 'LGPLv2+', + default_options: [ + 'buildtype=release', + 'prefix=/usr', + ] +) + +################################################################################ +cc = meson.get_compiler('c') + +prefixdir = get_option('prefix') +libdir = join_paths(prefixdir, get_option('libdir')) +includedir = join_paths(prefixdir, get_option('includedir')) +datadir = join_paths(prefixdir, get_option('datadir')) +mandir = join_paths(prefixdir, get_option('mandir')) +bindir = join_paths(prefixdir, get_option('bindir')) + +pkgconfiglibdir = get_option('pkgconfiglibdir') == '' ? join_paths(libdir, 'pkgconfig') : get_option('pkgconfiglibdir') + +################################################################################ +conf = configuration_data() + +# Check for libuuid availability +libuuid = dependency('uuid', required: true) +conf.set('CONFIG_LIBUUID', libuuid.found(), description: 'Is libuuid required?') + +# Check for libjson-c availability +libjson = dependency('json-c', required: false) +if not libjson.found() + libjson = cc.find_library('json-c', required: true) +endif +conf.set('CONFIG_JSONC', libjson.found(), description: 'Is json-c required?') + +# Check for libsystemd availability +want_systemd = get_option('systemd') +if want_systemd != 'false' + libsystemd = dependency('libsystemd', required: want_systemd == 'true') + have = libsystemd.found() +else + libsystemd = [] + have = false +endif +conf.set('CONFIG_SYSTEMD', have, description: 'Is libsystemd required?') + +################################################################################ +# The following commented-out text is the beginning of an effort to replace +# ccan/tools/configurator. It is not complete yet. Eventually we would like +# to replace everything that ccan/tools/configurator does with meson. +# To be continued... +################################################################################ +#args = ['-g3', '-ggdb', '-Wall', '-Wundef', '-Wmissing-prototypes', '-Wmissing-declarations', '-Wstrict-prototypes', '-Wold-style-definition'] +#conf.set10( +# 'HAVE_STRUCT_TIMESPEC', +# cc.compiles( +# ''' +# #include +# static void func(void) { +# struct timespec ts; +# ts.tv_sec = ts.tv_nsec = 1; +# } +# int main(int argc, char *argv[]) { +# (void)func(); +# return 0; +# } +# ''', +# no_builtin_args: true, +# args: args, +# name: 'struct timespec' +# ), +# description: 'Is struct timespec defined?' +#) +#conf.set10( +# 'HAVE_ASPRINTF', +# cc.compiles( +# ''' +# #define _GNU_SOURCE +# #include +# static char *func(int x) { +# char *p; +# if (asprintf(&p, "%u", x) == -1) +# p = NULL; +# return p; +# } +# int main(int argc, char *argv[]) { +# (void)func(1000); +# return 0; +# } +# ''', +# no_builtin_args: true, +# args: args, +# name: 'asprintf()' +# ), +# description: 'Is asprintf() supported?' +#) + +configure_file( + output: 'config-host.h', + configuration: conf +) + +################################################################################ +substs = configuration_data() +substs.set('NAME', meson.project_name()) +substs.set('VERSION', meson.project_version()) +substs.set('LICENSE', meson.project_license()[0]) +configure_file( + input: 'libnvme.spec.in', + output: 'libnvme.spec', + configuration: substs, +) + +################################################################################ +pkg = import('pkgconfig') +pkg.generate( + filebase: meson.project_name(), + name: meson.project_name(), + version: meson.project_version(), + description: 'Manage "libnvme" subsystem devices (Non-volatile Memory Express)', + url: 'http://github.com/linux-nvme/libnvme/', + libraries: ['-L${libdir}', '-lnvme'], + requires: [libuuid, libjson, libsystemd], +) + +################################################################################ +add_project_arguments('-include', 'config-host.h', language : 'c') +add_global_arguments(['-fomit-frame-pointer', '-D_GNU_SOURCE'], language : 'c') +incdir = include_directories(['ccan', 'src']) + +################################################################################ +subdir('ccan') +subdir('src') +subdir('pynvme') +subdir('test') +subdir('examples') +subdir('doc') + diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 00000000..f08b27fb --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,7 @@ +# -*- mode: meson -*- + +option('pkgconfiglibdir', type : 'string', value : '', description : 'directory for standard pkg-config files') + +option('man', type : 'boolean', value : false, description : 'build and install man pages (requires sphinx-build)') + +option('systemd', type : 'combo', choices : ['auto', 'true', 'false'], description : 'libsystemd support') diff --git a/pynvme/__init__.py b/pynvme/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pynvme/meson.build b/pynvme/meson.build new file mode 100644 index 00000000..15d5cc26 --- /dev/null +++ b/pynvme/meson.build @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# +python3 = import('python').find_installation('python3') +swig = find_program('swig', required: true) +pymod_swig = custom_target( + 'nvme.py', + input: ['nvme.i', config_h], + output: ['nvme.py', 'nvme_wrap.c'], + command: [swig, '-python', '-py3', '-o', '@OUTPUT1@', '@INPUT0@'], + install: true, + install_dir: [python3.get_install_dir(pure: false, subdir: 'libnvme'), false], +) + +pynvme_clib = python3.extension_module( + '_nvme', + pymod_swig[1], + dependencies : python3.dependency(), + include_directories: incdir, + link_with: libnvme_static, + install: true, + subdir: 'libnvme', +) + +python3.install_sources( + ['__init__.py', ], + pure: false, + subdir: 'libnvme', +) diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 00000000..db03e041 --- /dev/null +++ b/src/meson.build @@ -0,0 +1,71 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# +sources = [ + 'nvme/cleanup.c', + 'nvme/fabrics.c', + 'nvme/filters.c', + 'nvme/ioctl.c', + 'nvme/log.c', + 'nvme/tree.c', + 'nvme/util.c', + config_h, +] + +if conf.get('CONFIG_JSONC') + sources += 'nvme/json.c' +endif + +deps = [ + libuuid, + libsystemd, + libjson, +] + +source_dir = meson.current_source_dir() +mapfile = 'libnvme.map' +version_script_arg = join_paths(source_dir, mapfile) + +libnvme_shared = shared_library( + 'nvme', # produces libnvme.so + sources, + version: meson.project_version(), + soversion: '1', + link_args: ['-Wl,--version-script=' + version_script_arg], + dependencies: deps, + link_depends: mapfile, + include_directories: incdir, + install: true, +) + +libnvme_static = static_library( + 'nvme', # produces libnvme.a + sources, + link_args: ['-Wl,--version-script=' + version_script_arg], + dependencies: deps, + link_depends: mapfile, + include_directories: incdir, + install : false +) + +mode = ['rw-r--r--', 0, 0] +install_headers('libnvme.h', install_mode: mode) +install_headers([ + 'nvme/fabrics.h', + 'nvme/filters.h', + 'nvme/ioctl.h', + 'nvme/log.h', + 'nvme/tree.h', + 'nvme/types.h', + 'nvme/util.h', + ], + subdir: 'nvme', + install_mode: mode, +) + + + diff --git a/test/meson.build b/test/meson.build new file mode 100644 index 00000000..61be2ccf --- /dev/null +++ b/test/meson.build @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# +main = executable( + 'main-test', + ['test.c'], + link_with: libnvme_static, + include_directories: incdir +) + +cpp = executable( + 'test-cpp', + ['cpp.cc'], + link_with: libnvme_static, + include_directories: incdir +) + +register = executable( + 'test-register', + ['register.c'], + link_with: libnvme_static, + include_directories: incdir +) + +zns = executable( + 'test-zns', + ['zns.c'], + link_with: libnvme_static, + include_directories: incdir +) + +test('main', main) +test('main', main, args: ['nvme10']) +test('cpp', main) +test('register', main, args: ['nvme10']) +test('zns', main)