From 699a391c7859f5d76db20a33af5aa3191a0409af Mon Sep 17 00:00:00 2001 From: Andrew McNulty Date: Mon, 13 Feb 2023 15:55:47 +0100 Subject: [PATCH 1/5] Updates to Meson build system: 1. Use cross-platform `/` operator for path construction. 2. Use `meson.project_source_root()` for correct path resolution of generate_test_runner.rb path when used as a subproject. 3. Bump the minimum required Meson version to '0.56.0' as this is needed for the above changes. --- meson.build | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/meson.build b/meson.build index fbb9444..94138f5 100644 --- a/meson.build +++ b/meson.build @@ -5,21 +5,22 @@ # license: MIT # project('unity', 'c', - license: 'MIT', - meson_version: '>=0.37.0', - default_options: ['werror=true', 'c_std=c11']) + license: 'MIT', + # `meson.project_source_root()` introduced in 0.56.0 + meson_version: '>=0.56.0', + default_options: [ + 'werror=true', + 'c_std=c11' + ] +) subdir('src') unity_dep = declare_dependency(link_with: unity_lib, include_directories: unity_dir) - -# Get the generate_test_runner script relative to itself or the parent project if it is being used as a subproject -# NOTE: This could be (and probably is) a complete hack - but I haven't yet been able to find a better way.... -if meson.is_subproject() -gen_test_runner_path = find_program(meson.source_root() / 'subprojects/unity/auto/generate_test_runner.rb') -else -gen_test_runner_path = find_program('subprojects/unity/auto/generate_test_runner.rb') -endif - -# Create a generator that we can access from the parent project -gen_test_runner = generator(gen_test_runner_path, output: '@BASENAME@_Runner.c', arguments: ['@INPUT@', '@OUTPUT@'] ) +# Create a generator that can be used by consumers of our build system to generate +# test runners. +gen_test_runner = generator( + find_program(meson.project_source_root() / 'auto' / 'generate_test_runner.rb'), + output: '@BASENAME@_Runner.c', + arguments: ['@INPUT@', '@OUTPUT@'] +) From cd80a79db525d3456ae7a80c3252b314119536fd Mon Sep 17 00:00:00 2001 From: Andrew McNulty Date: Mon, 13 Feb 2023 16:19:39 +0100 Subject: [PATCH 2/5] Add Meson example based on Owen Torres' example. --- .gitignore | 1 + examples/example_1/meson.build | 48 +++++++++++++++++++++++ examples/example_1/readme.txt | 9 ++++- examples/example_1/subprojects/unity.wrap | 3 ++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 examples/example_1/meson.build create mode 100644 examples/example_1/subprojects/unity.wrap diff --git a/.gitignore b/.gitignore index 7500c74..211683a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ build/ builddir/ test/sandbox .DS_Store +examples/example_1/subprojects/unity examples/example_1/test1.exe examples/example_1/test2.exe examples/example_2/all_tests.exe diff --git a/examples/example_1/meson.build b/examples/example_1/meson.build new file mode 100644 index 0000000..645eeb9 --- /dev/null +++ b/examples/example_1/meson.build @@ -0,0 +1,48 @@ +project('Unity example', 'c', + license: 'MIT', + default_options: [ + 'c_std=c99', + 'warning_level=3', + ], + meson_version: '>= 0.49.0' +) + +unity_subproject = subproject('unity') +unity_dependency = unity_subproject.get_variable('unity_dep') +unity_gen_runner = unity_subproject.get_variable('gen_test_runner') + +src1 = files([ + 'src' / 'ProductionCode.c', + 'test' / 'TestProductionCode.c', +]) + +src2 = files([ + 'src' / 'ProductionCode2.c', + 'test' / 'TestProductionCode2.c', +]) + +inc = include_directories('src') + +test1 = executable('test1', + sources: [ + src1, + unity_gen_runner.process('test' / 'TestProductionCode.c') + ], + include_directories: [ inc ], + dependencies: [ unity_dependency ], +) + +test('test1', test1, + should_fail: true) + +test2 = executable('test2', + sources: [ + src2, + unity_gen_runner.process('test' / 'TestProductionCode2.c') + ], + include_directories: [ inc ], + dependencies: [ unity_dependency ], +) + +test('test2', test2) + diff --git a/examples/example_1/readme.txt b/examples/example_1/readme.txt index dfed815..ddddd36 100644 --- a/examples/example_1/readme.txt +++ b/examples/example_1/readme.txt @@ -2,4 +2,11 @@ Example 1 ========= Close to the simplest possible example of Unity, using only basic features. -Run make to build & run the example tests. \ No newline at end of file + +Build and run with Make +--- +Just run `make`. + +Build and run with Meson +--- +Run `meson setup build` to create the build directory, and then `meson test -C build` to build and run the tests. diff --git a/examples/example_1/subprojects/unity.wrap b/examples/example_1/subprojects/unity.wrap new file mode 100644 index 0000000..6df241b --- /dev/null +++ b/examples/example_1/subprojects/unity.wrap @@ -0,0 +1,3 @@ +[wrap-git] +url = https://github.com/ThrowTheSwitch/Unity.git +revision = head From 44bc9e6dbe40e92b0e9f44cc00c65938f23984b1 Mon Sep 17 00:00:00 2001 From: Andrew McNulty Date: Mon, 13 Feb 2023 17:22:52 +0100 Subject: [PATCH 3/5] Update Meson build system The following features from the CMake build have been implemented: * Library version retrieved from unity.h. * Extension support. * Library, header, and package configuration file installation. This commit is entirely based on existing work by Owen Torres. --- auto/extract_version.py | 15 +++++++++ extras/fixture/src/meson.build | 8 +++++ extras/memory/src/meson.build | 7 +++++ meson.build | 56 +++++++++++++++++++++++++++++++++- meson_options.txt | 2 ++ src/meson.build | 12 +++++--- 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100755 auto/extract_version.py create mode 100644 extras/fixture/src/meson.build create mode 100644 extras/memory/src/meson.build create mode 100644 meson_options.txt diff --git a/auto/extract_version.py b/auto/extract_version.py new file mode 100755 index 0000000..1d137e5 --- /dev/null +++ b/auto/extract_version.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 +import re +import sys + +ver_re = re.compile(r"^#define\s+UNITY_VERSION_(?:MAJOR|MINOR|BUILD)\s+(\d+)$") +version = [] + +with open(sys.argv[1], "r") as f: + for line in f: + m = ver_re.match(line) + if m: + version.append(m.group(1)) + +print(".".join(version)) + diff --git a/extras/fixture/src/meson.build b/extras/fixture/src/meson.build new file mode 100644 index 0000000..5d11470 --- /dev/null +++ b/extras/fixture/src/meson.build @@ -0,0 +1,8 @@ +unity_inc += include_directories('.') +unity_src += files('unity_fixture.c') + +install_headers( + 'unity_fixture.h', + 'unity_fixture_internals.h', + subdir: meson.project_name() +) diff --git a/extras/memory/src/meson.build b/extras/memory/src/meson.build new file mode 100644 index 0000000..53a66ca --- /dev/null +++ b/extras/memory/src/meson.build @@ -0,0 +1,7 @@ +unity_inc += include_directories('.') +unity_src += files('unity_memory.c') + +install_headers( + 'unity_memory.h', + subdir: meson.project_name() +) diff --git a/meson.build b/meson.build index 94138f5..1a56f27 100644 --- a/meson.build +++ b/meson.build @@ -6,6 +6,17 @@ # project('unity', 'c', license: 'MIT', + + # Set project version to value extracted from unity.h header + version: run_command( + [ + find_program('python', 'python3'), + 'auto' / 'extract_version.py', + 'src' / 'unity.h' + ], + check: true + ).stdout().strip(), + # `meson.project_source_root()` introduced in 0.56.0 meson_version: '>=0.56.0', default_options: [ @@ -14,8 +25,41 @@ project('unity', 'c', ] ) +build_fixture = get_option('extension_fixture').enabled() +build_memory = get_option('extension_memory').enabled() + +unity_src = [] +unity_inc = [] + subdir('src') -unity_dep = declare_dependency(link_with: unity_lib, include_directories: unity_dir) + +if build_fixture + # Building the fixture extension implies building the memory + # extension. + build_memory = true + subdir('extras/fixture/src') +endif + +if build_memory + subdir('extras/memory/src') +endif + +unity_lib = static_library(meson.project_name(), + sources: unity_src, + include_directories: unity_inc, + install: true +) + +unity_dep = declare_dependency( + link_with: unity_lib, + include_directories: unity_inc +) + +# Generate pkg-config file. +pkg = import('pkgconfig') +pkg.generate(unity_lib, + description: 'C Unit testing framework.' +) # Create a generator that can be used by consumers of our build system to generate # test runners. @@ -24,3 +68,13 @@ gen_test_runner = generator( output: '@BASENAME@_Runner.c', arguments: ['@INPUT@', '@OUTPUT@'] ) + +# Summarize. +summary( + { + 'Fixture extension': build_fixture, + 'Memory extension': build_memory, + }, + section: 'Extensions', + bool_yn: true +) diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..10b136f --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,2 @@ +option('extension_fixture', type: 'feature', value: 'disabled', description: 'Whether to use the fixture extension.') +option('extension_memory', type: 'feature', value: 'disabled', description: 'Whether to use the memory extension.') diff --git a/src/meson.build b/src/meson.build index fbe4b5b..4d7751f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -4,10 +4,12 @@ # # license: MIT # -unity_dir = include_directories('.') -unity_lib = static_library(meson.project_name(), - 'unity.c', - include_directories: unity_dir, - native: true +unity_inc += include_directories('.') +unity_src += files('unity.c') + +install_headers( + 'unity.h', + 'unity_internals.h', + subdir: meson.project_name() ) From 43378c4262a83572615ad241d0c2af0c47c0d844 Mon Sep 17 00:00:00 2001 From: Andrew McNulty Date: Tue, 14 Feb 2023 09:18:13 +0100 Subject: [PATCH 4/5] Implement review feedback for Meson updates. 1. Call the version extraction script directly instead of through a Python returned from `find_program()`. 2. We don't need to use `meson.project_source_root()` as `find_program()` will search relative to the current meson.build script. 3. Lower the required version back to `>= 0.37.0`, and modify some things to get rid of warnings with this version selected. The use of `summary()`, `dict`, and positional arguments in `pkgconfig.generate()` generate warnings with this version so remove `summary()` and dict()`, also pass keyword arguments to `pkgconfig.generate()`. --- meson.build | 29 ++++++++++------------------- meson_options.txt | 4 ++-- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/meson.build b/meson.build index 1a56f27..312c4e3 100644 --- a/meson.build +++ b/meson.build @@ -10,23 +10,21 @@ project('unity', 'c', # Set project version to value extracted from unity.h header version: run_command( [ - find_program('python', 'python3'), - 'auto' / 'extract_version.py', - 'src' / 'unity.h' + 'auto/extract_version.py', + 'src/unity.h' ], check: true ).stdout().strip(), - # `meson.project_source_root()` introduced in 0.56.0 - meson_version: '>=0.56.0', + meson_version: '>=0.37.0', default_options: [ 'werror=true', 'c_std=c11' ] ) -build_fixture = get_option('extension_fixture').enabled() -build_memory = get_option('extension_memory').enabled() +build_fixture = get_option('extension_fixture') +build_memory = get_option('extension_memory') unity_src = [] unity_inc = [] @@ -57,24 +55,17 @@ unity_dep = declare_dependency( # Generate pkg-config file. pkg = import('pkgconfig') -pkg.generate(unity_lib, +pkg.generate( + name: meson.project_name(), + version: meson.project_version(), + libraries: [ unity_lib ], description: 'C Unit testing framework.' ) # Create a generator that can be used by consumers of our build system to generate # test runners. gen_test_runner = generator( - find_program(meson.project_source_root() / 'auto' / 'generate_test_runner.rb'), + find_program('auto/generate_test_runner.rb'), output: '@BASENAME@_Runner.c', arguments: ['@INPUT@', '@OUTPUT@'] ) - -# Summarize. -summary( - { - 'Fixture extension': build_fixture, - 'Memory extension': build_memory, - }, - section: 'Extensions', - bool_yn: true -) diff --git a/meson_options.txt b/meson_options.txt index 10b136f..fbb66d7 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,2 +1,2 @@ -option('extension_fixture', type: 'feature', value: 'disabled', description: 'Whether to use the fixture extension.') -option('extension_memory', type: 'feature', value: 'disabled', description: 'Whether to use the memory extension.') +option('extension_fixture', type: 'boolean', value: 'false', description: 'Whether to enable the fixture extension.') +option('extension_memory', type: 'boolean', value: 'false', description: 'Whether to enable the memory extension.') From fba6be17c744faa71718b4abd675b42f11b471c6 Mon Sep 17 00:00:00 2001 From: Andrew McNulty Date: Tue, 14 Feb 2023 17:53:03 +0100 Subject: [PATCH 5/5] Bump meson_version to '0.47.0' The use of the check kwarg in run_command() was introduced in meson version 0.47.0 --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 312c4e3..708b295 100644 --- a/meson.build +++ b/meson.build @@ -16,7 +16,7 @@ project('unity', 'c', check: true ).stdout().strip(), - meson_version: '>=0.37.0', + meson_version: '>=0.47.0', default_options: [ 'werror=true', 'c_std=c11'