Files
poky/meta-selftest/recipes-test/cpp/files/CMakeLists.txt
Adrian Freihofer 2df986746b 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>
2023-12-13 11:58:15 +00:00

62 lines
1.7 KiB
CMake

#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#
cmake_minimum_required(VERSION 3.22)
project(cmake-example
VERSION 1.0.0
LANGUAGES CXX
)
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
option(FAILING_TEST "Compile a failing unit test to test the test infrastructure" OFF)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED On)
set(CMAKE_CXX_EXTENSIONS Off)
include(GNUInstallDirs)
# Linking a small library makes the example more useful for testing.
find_package(json-c)
# A simple library linking json-c library found by pkgconfig
add_library(cmake-example-lib cpp-example-lib.cpp cpp-example-lib.hpp)
set_target_properties(cmake-example-lib PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
target_link_libraries(cmake-example-lib PRIVATE json-c::json-c)
install(TARGETS cmake-example-lib
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# A simple executable linking the library
add_executable(cmake-example cpp-example.cpp)
target_link_libraries(cmake-example PRIVATE cmake-example-lib)
install(TARGETS cmake-example
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# A simple test executable for testing the library
add_executable(test-cmake-example test-cpp-example.cpp)
target_link_libraries(test-cmake-example PRIVATE cmake-example-lib)
if (FAILING_TEST)
target_compile_definitions(test-cmake-example PRIVATE FAIL_COMPARISON_STR="foo")
endif(FAILING_TEST)
install(TARGETS test-cmake-example
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
include(CTest)
add_test(NAME test-cmake-example COMMAND test-cmake-example)