From feaa0f9e7ffa7228903a78a5efbce3bfd3dcfa25 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 4 Mar 2024 15:48:42 -0800 Subject: [PATCH 1/2] meson: Add a Meson based build system This is primarily intended to be useful for projects using Meson wishing to consume cxxopts as a subproject. By hosting the files upstream users get the benefit of automatic parity with the CMake based install provided by an OS vendor or distribution. The implementation attempts to mirror the CMake build as much as possible, with the exception of generating the cmake-config files, which Meson doesn't currently support. It uses a small python script to parse the cxxopts.hpp header to extract the version, due to a concious design decision of Meson to leave such complex logic to external scripting languages like Python. --- meson.build | 97 +++++++++++++++++++++++++++++++++++++++++++++++ meson/version.py | 47 +++++++++++++++++++++++ meson_options.txt | 41 ++++++++++++++++++++ test/meson.build | 55 +++++++++++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 meson.build create mode 100755 meson/version.py create mode 100644 meson_options.txt create mode 100644 test/meson.build diff --git a/meson.build b/meson.build new file mode 100644 index 00000000..e9740420 --- /dev/null +++ b/meson.build @@ -0,0 +1,97 @@ +# Copyright © 2024 Dylan Baker +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +project( + 'cxxopts', + 'cpp', + version : run_command( + 'meson/version.py', files('include/cxxopts.hpp'), capture : true, check : true).stdout().strip(), + meson_version : '>= 0.64', + license : 'MIT', + default_options : ['cpp_std=c++11', 'warning_level=2'], +) + +cpp = meson.get_compiler('cpp') + +with_warnings = get_option('warnings').disable_auto_if(meson.is_subproject()).allowed() +if with_warnings + add_project_arguments( + cpp.get_supported_arguments( + '-Wsuggest-override', + '-Wshadow', + '-Weffc++', + '-Wsign-compare', + '-Wshadow', + '-Wwrite-strings', + '-Wpointer-arith', + '-Winit-self', + '-Wconversion', + '-Wno-sign-conversion', + ), + language : 'cpp', + ) +endif + +install_headers('include/cxxopts.hpp') + +dep_icu = dependency('icu-uc', required : get_option('icu')) +if dep_icu.found() + add_project_arguments('-DCXXOPTS_USE_UNICODE', language : 'cpp') +endif + +with_examples = get_option('examples').disable_auto_if(meson.is_subproject()).allowed() +if with_examples + executable( + 'example', + 'src/example.cpp', + include_directories : 'include', + dependencies : dep_icu, + override_options : ['cpp_std=c++17'], + ) +endif + +with_tests = get_option('tests').disable_auto_if(meson.is_subproject()).allowed() +if with_tests + subdir('test') +endif + +extra_cflags = [] +if dep_icu.found() + extra_cflags += ['-DCXXOPTS_USE_UNICODE'] +endif + +dep_cxxopts = declare_dependency( + include_directories : 'include', + dependencies : dep_icu, + compile_args : extra_cflags, +) + +meson.override_dependency('cxxopts', dep_cxxopts) + +pkg = import('pkgconfig') + +pkg.generate( + name : 'cxxopts', + description : 'A header-only lightweight C++ command line option parser', + url : 'https://github.com/jarro2783/cxxopts', + extra_cflags : extra_cflags, + requires : dep_icu, + dataonly : not dep_icu.found(), +) diff --git a/meson/version.py b/meson/version.py new file mode 100755 index 00000000..7317d0cb --- /dev/null +++ b/meson/version.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# Copyright © 2024 Dylan Baker +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +"""Parse the cxxopts.hpp header to get the version.""" + +import sys + +def main(): + versions = [None, None, None] + with open(sys.argv[1], 'r', encoding='ascii') as f: + for line in f: + if line.startswith('#define CXXOPTS__VERSION_'): + ver = line.rstrip().rsplit(' ', 1)[-1] + if 'MAJOR' in line: + versions[0] = ver + elif 'MINOR' in line: + versions[1] = ver + elif 'PATCH' in line: + versions[2] = ver + if None not in versions: + break + + assert None not in versions, \ + "Did not find all of the expected version strings in cxxopts.hpp" + print('.'.join(versions)) + + +if __name__ == "__main__": + main() diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 00000000..5de1c77f --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,41 @@ +# Copyright © 2024 Dylan Baker +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +option( + 'examples', + type : 'feature', + description : 'Whether to build examples. Defaults to enabled when not built as a subproject, otherwise disabled.', +) +option( + 'tests', + type : 'feature', + description : 'Whether to build tests. Defaults to enabled when not built as a subproject, otherwise disabled.', +) +option( + 'warnings', + type : 'feature', + description : 'Whether to add additional warnings. Defaults to enabled when not built as a subproject, otherwise disabled.', +) +option( + 'icu', + type : 'feature', + value : 'disabled', + description : 'use ICU Unicode library.', +) \ No newline at end of file diff --git a/test/meson.build b/test/meson.build new file mode 100644 index 00000000..5662f3de --- /dev/null +++ b/test/meson.build @@ -0,0 +1,55 @@ +# Copyright © 2024 Dylan Baker +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +inc = include_directories('../include') + +test( + 'link', + executable( + 'link_test', + 'link_a.cpp', 'link_b.cpp', + dependencies : dep_icu, + include_directories : inc, + ) +) + +test( + 'options', + executable( + 'options_test', + 'main.cpp', 'options.cpp', + dependencies : dep_icu, + include_directories : inc, + ) +) + +if cpp.get_id() == 'clang' and host_machine.system() == 'linux' + executable( + 'fuzzer', + 'fuzz.cpp', + cpp_args : ['-fsanitize=fuzzer'], + link_args : ['-fsanitize=fuzzer'], + dependencies : dep_icu, + include_directories : inc, + ) +endif + +# Meson can generate basic cmake-configs files, but not when targets are used, +# so these tests don't make sense \ No newline at end of file From f226222bdac6fb91a8d1fcd48ca402f57007e7ef Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 13 Mar 2024 14:03:08 -0700 Subject: [PATCH 2/2] CI: add basic Meson testing I've tried to be a bit more minimal here than the CMake tests are, since there's already a good cross section of testing there. For Meson, I just want to touch test each of the major platforms to ensure that it works --- .github/workflows/meson.yml | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/meson.yml diff --git a/.github/workflows/meson.yml b/.github/workflows/meson.yml new file mode 100644 index 00000000..b803aa44 --- /dev/null +++ b/.github/workflows/meson.yml @@ -0,0 +1,46 @@ +name: Meson + +on: + push: + branches: [ master, main ] + pull_request: + branches: [ master, main ] + workflow_dispatch: + +jobs: + build-ubuntu: + name: Build and Test on Ubuntu + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v5 + - uses: BSFishy/meson-build@v1.0.3 + with: + action: test + meson-version: 0.64.1 + + build-macos: + name: Build and Test on MacOS + runs-on: macos-12 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v5 + - uses: BSFishy/meson-build@v1.0.3 + with: + action: test + meson-version: 0.64.1 + + build-windows: + name: Build and Test on Windows + runs-on: windows-latest + env: + CXX: clang-cl + steps: + - uses: ilammy/msvc-dev-cmd@v1 + - uses: actions/checkout@v3 + - uses: actions/setup-python@v5 + - uses: BSFishy/meson-build@v1.0.3 + with: + action: test + setup-options: -Dcpp_std=c++14 # Clang-CL doesn't actually support C++11, hide the warning + meson-version: 0.64.1