• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.12)
2
3# Detect if we are the top level CMakeLists.txt or are we included in some
4# other project
5if(NOT DEFINED PROJECT_NAME)
6  set(IS_TOP_PROJECT TRUE)
7endif()
8
9# Turn this on in order to build elfio examples
10option(ELFIO_BUILD_EXAMPLES "Build ELFIO examples" OFF)
11
12# Turns this on in order to build tests
13option(ELFIO_BUILD_TESTS    "Build ELFIO tests" OFF)
14
15# Read version from header file
16set(version_header "elfio/elfio_version.hpp")
17file(READ ${version_header} ver)
18string(REGEX MATCH "#define ELFIO_VERSION \"([0-9\.]+)\"" _ ${ver})
19if (NOT CMAKE_MATCH_1)
20    message(FATAL_ERROR "Unable to parse version from ${version_header}")
21endif()
22set(version ${CMAKE_MATCH_1})
23
24# Use configure_file to make configure step depend on elfio_version.hpp
25configure_file(${version_header} ${CMAKE_CURRENT_BINARY_DIR}/elfio_version.hpp.copy COPYONLY)
26
27project(elfio VERSION ${version} LANGUAGES C CXX)
28
29include(GNUInstallDirs)
30
31# Create a header only CMake target for elfio
32add_library(elfio INTERFACE)
33add_library(elfio::elfio ALIAS elfio)
34
35target_include_directories(
36    elfio
37    INTERFACE
38        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
39        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
40
41# Enable C++11 for examples and tests
42if (ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS)
43    set(CMAKE_CXX_STANDARD 11)
44endif()
45
46if (ELFIO_BUILD_EXAMPLES)
47    add_subdirectory(examples)
48endif()
49
50if (ELFIO_BUILD_TESTS AND IS_TOP_PROJECT)
51    enable_testing()
52    add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} USES_TERMINAL)
53    add_subdirectory(tests)
54endif()
55
56# If this is the top level project, add in logic to install elfio
57if (IS_TOP_PROJECT)
58    include(CMakePackageConfigHelpers)
59
60    # Create a file that includes the current project version. This will be
61    # installed with the elfio CMake package.
62    write_basic_package_version_file(
63        "${PROJECT_NAME}ConfigVersion.cmake"
64        VERSION
65            ${PROJECT_VERSION}
66        COMPATIBILITY
67            SameMajorVersion)
68
69    # Create the default ${PROJECT_NAME}Config.cmake file which will be
70    # installed and found by calls to `find_package(elfio)`.
71    configure_package_config_file(
72        "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
73        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
74        INSTALL_DESTINATION
75            ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
76
77    # Install the previously generated "config" and "version" files
78    install(
79        FILES
80            "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
81            "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
82        DESTINATION
83            ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
84
85    # Install the entire local `elfio` directory to the include directory
86    install(
87        DIRECTORY
88            elfio
89        DESTINATION
90            ${CMAKE_INSTALL_INCLUDEDIR})
91
92    # Create a ${PROJECT_NAME}Targets.cmake file that is referenced by the
93    # ${PROJECT_NAME}Config.cmake file and includes the target information
94    # needed to compile/link against all targets exported under the
95    # ${PROJECT_NAME}_Targets export
96    install(
97        EXPORT
98            ${PROJECT_NAME}_Targets
99        FILE
100            ${PROJECT_NAME}Targets.cmake
101        NAMESPACE
102            ${PROJECT_NAME}::
103        DESTINATION
104            ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
105
106    # Add the elfio target to the ${PROJECT_NAME}_Targets export
107    install(
108        TARGETS
109            elfio
110        EXPORT
111            ${PROJECT_NAME}_Targets)
112endif()
113