summaryrefslogtreecommitdiffstats
path: root/meta-selftest/recipes-test/cpp/files
diff options
context:
space:
mode:
Diffstat (limited to 'meta-selftest/recipes-test/cpp/files')
-rw-r--r--meta-selftest/recipes-test/cpp/files/CMakeLists.txt61
-rw-r--r--meta-selftest/recipes-test/cpp/files/cpp-example-lib.cpp33
-rw-r--r--meta-selftest/recipes-test/cpp/files/cpp-example-lib.hpp21
-rw-r--r--meta-selftest/recipes-test/cpp/files/cpp-example.cpp18
-rw-r--r--meta-selftest/recipes-test/cpp/files/meson.build38
-rw-r--r--meta-selftest/recipes-test/cpp/files/meson.options3
-rw-r--r--meta-selftest/recipes-test/cpp/files/test-cpp-example.cpp25
7 files changed, 199 insertions, 0 deletions
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
7cmake_minimum_required(VERSION 3.22)
8
9project(cmake-example
10 VERSION 1.0.0
11 LANGUAGES CXX
12)
13
14option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
15option(FAILING_TEST "Compile a failing unit test to test the test infrastructure" OFF)
16
17set(CMAKE_CXX_STANDARD 17)
18set(CMAKE_CXX_STANDARD_REQUIRED On)
19set(CMAKE_CXX_EXTENSIONS Off)
20
21include(GNUInstallDirs)
22
23# Linking a small library makes the example more useful for testing.
24find_package(json-c)
25
26# A simple library linking json-c library found by pkgconfig
27add_library(cmake-example-lib cpp-example-lib.cpp cpp-example-lib.hpp)
28set_target_properties(cmake-example-lib PROPERTIES
29 VERSION ${PROJECT_VERSION}
30 SOVERSION ${PROJECT_VERSION_MAJOR}
31)
32target_link_libraries(cmake-example-lib PRIVATE json-c::json-c)
33
34install(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
41add_executable(cmake-example cpp-example.cpp)
42target_link_libraries(cmake-example PRIVATE cmake-example-lib)
43
44install(TARGETS cmake-example
45 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
46)
47
48# A simple test executable for testing the library
49add_executable(test-cmake-example test-cpp-example.cpp)
50target_link_libraries(test-cmake-example PRIVATE cmake-example-lib)
51
52if (FAILING_TEST)
53 target_compile_definitions(test-cmake-example PRIVATE FAIL_COMPARISON_STR="foo")
54endif(FAILING_TEST)
55
56install(TARGETS test-cmake-example
57 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
58)
59
60include(CTest)
61add_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
12const std::string &CppExample::get_string()
13{
14 return test_string;
15}
16
17const char *CppExample::get_json_c_version()
18{
19 return json_c_version();
20}
21
22void 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
11struct 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
11int 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
7project('meson-example', 'cpp',
8 version: '1.0.0',
9 default_options: ['cpp_std=c++17']
10 )
11
12jsoncdep = dependency('json-c')
13
14if get_option('FAILING_TEST').enabled()
15 add_project_arguments('-DFAIL_COMPARISON_STR=foo', language: 'cpp')
16endif
17
18mesonexlib = 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
26executable('mesonex',
27 'cpp-example.cpp',
28 link_with : mesonexlib,
29 install : true
30 )
31
32test_mesonex = executable('test-mesonex',
33 'test-cpp-example.cpp',
34 link_with : mesonexlib,
35 install : true
36)
37
38test('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
2option('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
16int 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}