summaryrefslogtreecommitdiffstats
path: root/meta-selftest/recipes-test/cpp/files/CMakeLists.txt
diff options
context:
space:
mode:
Diffstat (limited to 'meta-selftest/recipes-test/cpp/files/CMakeLists.txt')
-rw-r--r--meta-selftest/recipes-test/cpp/files/CMakeLists.txt61
1 files changed, 61 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)