introducing-cmake/exercises/CMakeLists.txt

146 lines
4.8 KiB
CMake
Raw Normal View History

2019-07-27 18:28:47 +02:00
# cmake_minimum_required(VERSION 3.11)
cmake_minimum_required(VERSION 3.11)
## Declare a project named "cmake-exercise"
project(cmake-exercise VERSION 0.90.0)
if(CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "Don't build in the source directory, pretty please.")
endif()
## Includes
include(GNUInstallDirs)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)
## Find necessary packages
find_package(OpenMP)
find_package(Threads REQUIRED)
## Artifacts to create
add_library(obj OBJECT)
add_library(static STATIC)
add_library(shared SHARED)
add_library(interface INTERFACE)
add_executable(tests)
## Generate other files
generate_export_header(shared
BASE_NAME DSO
EXPORT_FILE_NAME include/cmake-exercise/config.hh)
## Specify source files
target_sources(obj
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cmake-exercise/a.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/a.hh>
PRIVATE src/a.cc)
target_sources(shared
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cmake-exercise/b.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/b.hh>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/cmake-exercise/config.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/config.hh>
PRIVATE src/b.cc
$<TARGET_PROPERTY:obj,INTERFACE_SOURCES>
$<TARGET_OBJECTS:obj>)
target_sources(interface
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cmake-exercise/c.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/c.hh>)
target_sources(tests
PRIVATE test/test.cc)
## Add include folders
target_include_directories(interface
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_include_directories(obj
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_include_directories(shared
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
## Set target properties for visibility and PIC
set_property(TARGET obj PROPERTY POSITION_INDEPENDENT_CODE TRUE)
set_property(TARGET obj shared PROPERTY CXX_VISIBILITY_PRESET hidden)
set_property(TARGET obj shared PROPERTY VISIBILITY_INLINES_HIDDEN TRUE)
## Specify C++ version to use
target_compile_features(interface
INTERFACE cxx_std_11)
target_compile_features(obj
INTERFACE cxx_std_11
PRIVATE cxx_std_14)
## Link to other libraries, and inherit their settings
target_link_libraries(obj
PUBLIC Threads::Threads)
target_link_libraries(static
PRIVATE interface
PUBLIC obj)
target_link_libraries(shared
PRIVATE interface obj)
target_link_libraries(tests
PRIVATE static)
if(TARGET OpenMP::OpenMP_CXX)
target_compile_definitions(tests PRIVATE HAVE_OPENMP)
target_link_libraries(tests PRIVATE OpenMP::OpenMP_CXX)
endif()
## Install all needed files for packaging
install(FILES $<TARGET_PROPERTY:shared,INTERFACE_SOURCES>
$<TARGET_PROPERTY:interface,INTERFACE_SOURCES>
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT devel)
install(TARGETS shared
EXPORT cmake-exercise
DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS obj interface
EXPORT cmake-exercise
COMPONENT devel
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS tests
DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake-exercise/test
COMPONENT test)
## Add a test
enable_testing()
add_test(NAME not-really-a-test
COMMAND tests --ignored-param)
## Generate the export sets to be able to import this project
## in other projects via find_package (the config file is not complete though)
set(CMAKE_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/cmake-exercise)
export(EXPORT cmake-exercise
NAMESPACE cmake-exercise::
FILE cmake-exercise-targets.cmake)
install(EXPORT cmake-exercise
NAMESPACE cmake-exercise::
DESTINATION ${CMAKE_INSTALL_CMAKEDIR}
FILE cmake-exercise-targets.cmake
COMPONENT devel)
configure_package_config_file(cmake-exercise-config.cmake.in
cmake-exercise-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_CMAKEDIR})
write_basic_package_version_file(cmake-exercise-version.cmake
COMPATIBILITY SameMajorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake-exercise-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake-exercise-version.cmake
DESTINATION ${CMAKE_INSTALL_CMAKEDIR}
COMPONENT devel)
## Trigger packaging
set(CPACK_PACKAGE_FILE_NAME package)
set(CPACK_GENERATOR TXZ)
set(CPACK_ARCHIVE_COMPONENT_INSTALL TRUE)
include(CPack)