Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/meson.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Meson

on:
push:
branches: [ master, main ]
pull_request:
branches: [ master, main ]
workflow_dispatch:

jobs:
build-ubuntu:
strategy:
matrix:
icu: [enabled, disabled]
name: Build and Test on Ubuntu
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v5
- uses: BSFishy/[email protected]
with:
action: build
setup-options: -Dicu=${{ matrix.icu }}
meson-version: 0.64.1
directory: builddir
- name: Run Tests
run: meson test -C builddir
- name: Run examples
run: builddir/example

build-macos:
name: Build and Test on MacOS
runs-on: macos-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v5
- uses: BSFishy/[email protected]
with:
action: build
meson-version: 1.10.0
directory: builddir
- name: Run Tests
run: meson test -C builddir

build-windows:
name: Build and Test on Windows
runs-on: windows-latest
steps:
- uses: ilammy/msvc-dev-cmd@v1
- uses: actions/checkout@v6
- uses: actions/setup-python@v5
- uses: BSFishy/[email protected]
with:
action: test
setup-options: -Dcpp_std=c++14 # MSVC doesn't actually support C++11, hide the warning
meson-version: 0.64.1
directory: builddir
- name: Run Tests
run: meson test -C builddir
97 changes: 97 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright © 2024-2026 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(),
)
47 changes: 47 additions & 0 deletions meson/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# Copyright © 2024-2026 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()
41 changes: 41 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright © 2024-2026 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.',
)
Comment on lines +21 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all options like examples, and warning aren't covered in build workflows, it would be nice that you could provide logs from your system that they can successfully execute now. Something like results of meson run --option=example.

This will provide a proof that all the meson build instructions you have written, works as expected.
And will act as a proof that the options worked in the past at one place, if build breaks at some point in the future.

55 changes: 55 additions & 0 deletions test/meson.build
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcbaker It looks to me that we are not running the tests find-package-test and add-subdirectory-test that targets the src/example.cpp . Is that not a problem? Are those tests on for something cmake specific? Or it would benefit to add them later ( don't have to be this PR, we can track that in a different PR)

cmake output from github workflow -

Run ctest -C $BUILD_TYPE --output-on-failure
Test project /home/runner/work/cxxopts/cxxopts/build/test
    Start 1: options
1/3 Test #1: options ..........................   Passed    0.00 sec
    Start 2: find-package-test
2/3 Test #2: find-package-test ................   Passed    6.45 sec
    Start 3: add-subdirectory-test
3/3 Test #3: add-subdirectory-test ............   Passed    6.26 sec

100% tests passed, 0 tests failed out of 3

Total Test time (real) =  12.73 sec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The find-package test relies on having the cmake config files. Meson can generate very simple cmake files, but not using targets like cxxopts does, but we haven't put effort into generating more complex ones because CMake itself is migrating away from that toward CPS, and we are also working on CPS support, so there isn't really a good way for me to implement it.

The add-subdirectory test doesn't make a lot of sense for Meson either, as Meson has always had a feature more like CMake's ExternalProject, and doesn't allow external projects to be added as a subdirectory. I could potentially add a test to use cxxopts via a subproject call (roughly equivalent to ExternalProject), if you'd like.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, these are cmake specific and it sounds like meson doesn't have an equivalent anyway.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright © 2024-2026 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
Loading