diff options
author | Adrian Freihofer <adrian.freihofer@gmail.com> | 2023-12-07 21:52:49 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2023-12-13 11:58:15 +0000 |
commit | 2df986746b4010a24def7369f356b50e52bda50d (patch) | |
tree | cfd49ffcb07ac7060eebbec53e571e74fa35107a /meta-selftest | |
parent | d7c05e4eed4ed436f2ce961df46f9654af83ffec (diff) | |
download | poky-2df986746b4010a24def7369f356b50e52bda50d.tar.gz |
oe-selftest: add a cpp-example recipe
This simple C++ project supports compilation with CMake and Meson.
(Autotool support could be added later on.)
It's supposed to be used with oe-selftest.
An artificial project has several advantages over compiling a normal
CMake or Meson based project for testing purposes:
- It is much faster because it can be kept minimalistic
- It can cover multiple odd corner cases
- No one will change it in an unpredictable way
- It can support multiple build tools with only one C++ codebase
(From OE-Core rev: 4904e772470b0d6e5d98ef0344b3f2bf54214661)
Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta-selftest')
-rw-r--r-- | meta-selftest/recipes-test/cpp/.gitignore | 1 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/cmake-example.bb | 25 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/cmake-example/run-ptest | 10 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/cpp-example.inc | 24 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/CMakeLists.txt | 61 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/cpp-example-lib.cpp | 33 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/cpp-example-lib.hpp | 21 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/cpp-example.cpp | 18 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/meson.build | 38 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/meson.options | 3 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/files/test-cpp-example.cpp | 25 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/meson-example.bb | 27 | ||||
-rw-r--r-- | meta-selftest/recipes-test/cpp/meson-example/run-ptest | 10 |
13 files changed, 296 insertions, 0 deletions
diff --git a/meta-selftest/recipes-test/cpp/.gitignore b/meta-selftest/recipes-test/cpp/.gitignore new file mode 100644 index 0000000000..30d388a12b --- /dev/null +++ b/meta-selftest/recipes-test/cpp/.gitignore | |||
@@ -0,0 +1 @@ | |||
build* \ No newline at end of file | |||
diff --git a/meta-selftest/recipes-test/cpp/cmake-example.bb b/meta-selftest/recipes-test/cpp/cmake-example.bb new file mode 100644 index 0000000000..aecfcf780a --- /dev/null +++ b/meta-selftest/recipes-test/cpp/cmake-example.bb | |||
@@ -0,0 +1,25 @@ | |||
1 | # | ||
2 | # Copyright OpenEmbedded Contributors | ||
3 | # | ||
4 | # SPDX-License-Identifier: MIT | ||
5 | # | ||
6 | |||
7 | SUMMARY = "A C++ example compiled with cmake." | ||
8 | |||
9 | require cpp-example.inc | ||
10 | |||
11 | SRC_URI += "file://CMakeLists.txt" | ||
12 | |||
13 | inherit cmake-qemu | ||
14 | |||
15 | PACKAGECONFIG[failing_test] = "-DFAILING_TEST=ON" | ||
16 | |||
17 | FILES:${PN}-ptest += "${bindir}/test-cmake-example" | ||
18 | |||
19 | do_run_tests () { | ||
20 | bbnote ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' --target test -- ${EXTRA_OECMAKE_BUILD} | ||
21 | eval ${DESTDIR:+DESTDIR=${DESTDIR} }${CMAKE_VERBOSE} cmake --build '${B}' --target test -- ${EXTRA_OECMAKE_BUILD} | ||
22 | } | ||
23 | do_run_tests[doc] = "Run cmake --target=test using qemu-user" | ||
24 | |||
25 | addtask do_run_tests after do_compile | ||
diff --git a/meta-selftest/recipes-test/cpp/cmake-example/run-ptest b/meta-selftest/recipes-test/cpp/cmake-example/run-ptest new file mode 100644 index 0000000000..94b620a198 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/cmake-example/run-ptest | |||
@@ -0,0 +1,10 @@ | |||
1 | #!/bin/sh | ||
2 | # | ||
3 | # Copyright OpenEmbedded Contributors | ||
4 | # | ||
5 | # SPDX-License-Identifier: MIT | ||
6 | # | ||
7 | |||
8 | test-cmake-example | ||
9 | |||
10 | # Note: run-ptests exits with exit value from test-cmake-example | ||
diff --git a/meta-selftest/recipes-test/cpp/cpp-example.inc b/meta-selftest/recipes-test/cpp/cpp-example.inc new file mode 100644 index 0000000000..ad374be9d0 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/cpp-example.inc | |||
@@ -0,0 +1,24 @@ | |||
1 | # | ||
2 | # Copyright OpenEmbedded Contributors | ||
3 | # | ||
4 | # SPDX-License-Identifier: MIT | ||
5 | # | ||
6 | |||
7 | LICENSE = "MIT" | ||
8 | LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" | ||
9 | |||
10 | DEPENDS += "json-c" | ||
11 | |||
12 | PV = "1.0" | ||
13 | |||
14 | SRC_URI = "\ | ||
15 | file://cpp-example.cpp \ | ||
16 | file://cpp-example-lib.hpp \ | ||
17 | file://cpp-example-lib.cpp \ | ||
18 | file://test-cpp-example.cpp \ | ||
19 | file://run-ptest \ | ||
20 | " | ||
21 | |||
22 | S = "${WORKDIR}" | ||
23 | |||
24 | inherit ptest | ||
diff --git a/meta-selftest/recipes-test/cpp/files/CMakeLists.txt b/meta-selftest/recipes-test/cpp/files/CMakeLists.txt new file mode 100644 index 0000000000..6fa6917d89 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/CMakeLists.txt | |||
@@ -0,0 +1,61 @@ | |||
1 | # | ||
2 | # Copyright OpenEmbedded Contributors | ||
3 | # | ||
4 | # SPDX-License-Identifier: MIT | ||
5 | # | ||
6 | |||
7 | cmake_minimum_required(VERSION 3.22) | ||
8 | |||
9 | project(cmake-example | ||
10 | VERSION 1.0.0 | ||
11 | LANGUAGES CXX | ||
12 | ) | ||
13 | |||
14 | option(BUILD_SHARED_LIBS "Build using shared libraries" ON) | ||
15 | option(FAILING_TEST "Compile a failing unit test to test the test infrastructure" OFF) | ||
16 | |||
17 | set(CMAKE_CXX_STANDARD 17) | ||
18 | set(CMAKE_CXX_STANDARD_REQUIRED On) | ||
19 | set(CMAKE_CXX_EXTENSIONS Off) | ||
20 | |||
21 | include(GNUInstallDirs) | ||
22 | |||
23 | # Linking a small library makes the example more useful for testing. | ||
24 | find_package(json-c) | ||
25 | |||
26 | # A simple library linking json-c library found by pkgconfig | ||
27 | add_library(cmake-example-lib cpp-example-lib.cpp cpp-example-lib.hpp) | ||
28 | set_target_properties(cmake-example-lib PROPERTIES | ||
29 | VERSION ${PROJECT_VERSION} | ||
30 | SOVERSION ${PROJECT_VERSION_MAJOR} | ||
31 | ) | ||
32 | target_link_libraries(cmake-example-lib PRIVATE json-c::json-c) | ||
33 | |||
34 | install(TARGETS cmake-example-lib | ||
35 | INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} | ||
36 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
37 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
38 | ) | ||
39 | |||
40 | # A simple executable linking the library | ||
41 | add_executable(cmake-example cpp-example.cpp) | ||
42 | target_link_libraries(cmake-example PRIVATE cmake-example-lib) | ||
43 | |||
44 | install(TARGETS cmake-example | ||
45 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
46 | ) | ||
47 | |||
48 | # A simple test executable for testing the library | ||
49 | add_executable(test-cmake-example test-cpp-example.cpp) | ||
50 | target_link_libraries(test-cmake-example PRIVATE cmake-example-lib) | ||
51 | |||
52 | if (FAILING_TEST) | ||
53 | target_compile_definitions(test-cmake-example PRIVATE FAIL_COMPARISON_STR="foo") | ||
54 | endif(FAILING_TEST) | ||
55 | |||
56 | install(TARGETS test-cmake-example | ||
57 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
58 | ) | ||
59 | |||
60 | include(CTest) | ||
61 | add_test(NAME test-cmake-example COMMAND test-cmake-example) | ||
diff --git a/meta-selftest/recipes-test/cpp/files/cpp-example-lib.cpp b/meta-selftest/recipes-test/cpp/files/cpp-example-lib.cpp new file mode 100644 index 0000000000..d3dc976864 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/cpp-example-lib.cpp | |||
@@ -0,0 +1,33 @@ | |||
1 | /* | ||
2 | * Copyright OpenEmbedded Contributors | ||
3 | * | ||
4 | * SPDX-License-Identifier: MIT | ||
5 | */ | ||
6 | |||
7 | #include <iostream> | ||
8 | #include <string> | ||
9 | #include <json-c/json.h> | ||
10 | #include "cpp-example-lib.hpp" | ||
11 | |||
12 | const std::string &CppExample::get_string() | ||
13 | { | ||
14 | return test_string; | ||
15 | } | ||
16 | |||
17 | const char *CppExample::get_json_c_version() | ||
18 | { | ||
19 | return json_c_version(); | ||
20 | } | ||
21 | |||
22 | void CppExample::print_json() | ||
23 | { | ||
24 | struct json_object *jobj; | ||
25 | const int flag = JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY; | ||
26 | |||
27 | jobj = json_object_new_object(); | ||
28 | json_object_object_add(jobj, "test_string", json_object_new_string(test_string.c_str())); | ||
29 | |||
30 | std::cout << json_object_to_json_string_ext(jobj, flag) << std::endl; | ||
31 | |||
32 | json_object_put(jobj); // Delete the json object | ||
33 | } | ||
diff --git a/meta-selftest/recipes-test/cpp/files/cpp-example-lib.hpp b/meta-selftest/recipes-test/cpp/files/cpp-example-lib.hpp new file mode 100644 index 0000000000..0ad9e7b7b2 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/cpp-example-lib.hpp | |||
@@ -0,0 +1,21 @@ | |||
1 | /* | ||
2 | * Copyright OpenEmbedded Contributors | ||
3 | * | ||
4 | * SPDX-License-Identifier: MIT | ||
5 | */ | ||
6 | |||
7 | #pragma once | ||
8 | |||
9 | #include <string> | ||
10 | |||
11 | struct CppExample | ||
12 | { | ||
13 | inline static const std::string test_string = "cpp-example-lib Magic: 123456789"; | ||
14 | |||
15 | /* Retrieve a constant string */ | ||
16 | const std::string &get_string(); | ||
17 | /* Retrieve a constant string from a library */ | ||
18 | const char *get_json_c_version(); | ||
19 | /* Call a more advanced function from a library */ | ||
20 | void print_json(); | ||
21 | }; | ||
diff --git a/meta-selftest/recipes-test/cpp/files/cpp-example.cpp b/meta-selftest/recipes-test/cpp/files/cpp-example.cpp new file mode 100644 index 0000000000..9889554e0c --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/cpp-example.cpp | |||
@@ -0,0 +1,18 @@ | |||
1 | /* | ||
2 | * Copyright OpenEmbedded Contributors | ||
3 | * | ||
4 | * SPDX-License-Identifier: MIT | ||
5 | */ | ||
6 | |||
7 | #include "cpp-example-lib.hpp" | ||
8 | |||
9 | #include <iostream> | ||
10 | |||
11 | int main() | ||
12 | { | ||
13 | auto cpp_example = CppExample(); | ||
14 | std::cout << "C++ example linking " << cpp_example.get_string() << std::endl; | ||
15 | std::cout << "Linking json-c version " << cpp_example.get_json_c_version() << std::endl; | ||
16 | cpp_example.print_json(); | ||
17 | return 0; | ||
18 | } | ||
diff --git a/meta-selftest/recipes-test/cpp/files/meson.build b/meta-selftest/recipes-test/cpp/files/meson.build new file mode 100644 index 0000000000..0e2b55f3a2 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/meson.build | |||
@@ -0,0 +1,38 @@ | |||
1 | # | ||
2 | # Copyright OpenEmbedded Contributors | ||
3 | # | ||
4 | # SPDX-License-Identifier: MIT | ||
5 | # | ||
6 | |||
7 | project('meson-example', 'cpp', | ||
8 | version: '1.0.0', | ||
9 | default_options: ['cpp_std=c++17'] | ||
10 | ) | ||
11 | |||
12 | jsoncdep = dependency('json-c') | ||
13 | |||
14 | if get_option('FAILING_TEST').enabled() | ||
15 | add_project_arguments('-DFAIL_COMPARISON_STR=foo', language: 'cpp') | ||
16 | endif | ||
17 | |||
18 | mesonexlib = shared_library('mesonexlib', | ||
19 | 'cpp-example-lib.cpp', 'cpp-example-lib.hpp', | ||
20 | version: meson.project_version(), | ||
21 | soversion: meson.project_version().split('.')[0], | ||
22 | dependencies : jsoncdep, | ||
23 | install : true | ||
24 | ) | ||
25 | |||
26 | executable('mesonex', | ||
27 | 'cpp-example.cpp', | ||
28 | link_with : mesonexlib, | ||
29 | install : true | ||
30 | ) | ||
31 | |||
32 | test_mesonex = executable('test-mesonex', | ||
33 | 'test-cpp-example.cpp', | ||
34 | link_with : mesonexlib, | ||
35 | install : true | ||
36 | ) | ||
37 | |||
38 | test('meson example test', test_mesonex) | ||
diff --git a/meta-selftest/recipes-test/cpp/files/meson.options b/meta-selftest/recipes-test/cpp/files/meson.options new file mode 100644 index 0000000000..58a0bf9e61 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/meson.options | |||
@@ -0,0 +1,3 @@ | |||
1 | |||
2 | option('FAILING_TEST', type : 'feature', value : 'disabled', | ||
3 | description : 'Compile a failing unit test to test the test infrastructure') | ||
diff --git a/meta-selftest/recipes-test/cpp/files/test-cpp-example.cpp b/meta-selftest/recipes-test/cpp/files/test-cpp-example.cpp new file mode 100644 index 0000000000..83c9bfa844 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/files/test-cpp-example.cpp | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * Copyright OpenEmbedded Contributors | ||
3 | * | ||
4 | * SPDX-License-Identifier: MIT | ||
5 | */ | ||
6 | |||
7 | #include "cpp-example-lib.hpp" | ||
8 | |||
9 | #include <iostream> | ||
10 | |||
11 | /* This is for creating a failing test for testing the test infrastructure */ | ||
12 | #ifndef FAIL_COMPARISON_STR | ||
13 | #define FAIL_COMPARISON_STR "" | ||
14 | #endif | ||
15 | |||
16 | int main() { | ||
17 | auto cpp_example = CppExample(); | ||
18 | auto ret_string = cpp_example.get_string(); | ||
19 | if(0 == ret_string.compare(CppExample::test_string + FAIL_COMPARISON_STR)) { | ||
20 | std::cout << "PASS: " << ret_string << " = " << CppExample::test_string << std::endl; | ||
21 | } else { | ||
22 | std::cout << "FAIL: " << ret_string << " != " << CppExample::test_string << std::endl; | ||
23 | return 1; | ||
24 | } | ||
25 | } | ||
diff --git a/meta-selftest/recipes-test/cpp/meson-example.bb b/meta-selftest/recipes-test/cpp/meson-example.bb new file mode 100644 index 0000000000..14a7ca8dc9 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/meson-example.bb | |||
@@ -0,0 +1,27 @@ | |||
1 | # | ||
2 | # Copyright OpenEmbedded Contributors | ||
3 | # | ||
4 | # SPDX-License-Identifier: MIT | ||
5 | # | ||
6 | |||
7 | SUMMARY = "A C++ example compiled with meson." | ||
8 | |||
9 | require cpp-example.inc | ||
10 | |||
11 | SRC_URI += "\ | ||
12 | file://meson.build \ | ||
13 | file://meson.options \ | ||
14 | " | ||
15 | |||
16 | inherit pkgconfig meson | ||
17 | |||
18 | PACKAGECONFIG[failing_test] = "-DFAILING_TEST=enabled" | ||
19 | |||
20 | FILES:${PN}-ptest += "${bindir}/test-mesonex" | ||
21 | |||
22 | do_run_tests () { | ||
23 | meson test -C "${B}" --no-rebuild | ||
24 | } | ||
25 | do_run_tests[doc] = "Run meson test using qemu-user" | ||
26 | |||
27 | addtask do_run_tests after do_compile | ||
diff --git a/meta-selftest/recipes-test/cpp/meson-example/run-ptest b/meta-selftest/recipes-test/cpp/meson-example/run-ptest new file mode 100644 index 0000000000..b1804f0096 --- /dev/null +++ b/meta-selftest/recipes-test/cpp/meson-example/run-ptest | |||
@@ -0,0 +1,10 @@ | |||
1 | #!/bin/sh | ||
2 | # | ||
3 | # Copyright OpenEmbedded Contributors | ||
4 | # | ||
5 | # SPDX-License-Identifier: MIT | ||
6 | # | ||
7 | |||
8 | test-mesonex | ||
9 | |||
10 | # Note: run-ptests exits with exit value from test-mesonex | ||