From 92658df3794c52f27f3a6f680b9692a86bd41cf8 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Sat, 9 Jul 2022 16:25:29 +0300 Subject: [PATCH] In case of the top project, build examples and tests by default --- CMakeLists.txt | 64 +++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4660420..0aa0d83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,22 +3,26 @@ cmake_minimum_required(VERSION 3.10) # Detect if we are the top level CMakeLists.txt or are we included in some # other project if(NOT DEFINED PROJECT_NAME) - set(IS_TOP_PROJECT TRUE) + set(IS_TOP_PROJECT TRUE) endif() -# Turn this on in order to build elfio examples -option(ELFIO_BUILD_EXAMPLES "Build ELFIO examples" OFF) +if(IS_TOP_PROJECT) + # Turn this on in order to build elfio examples + option(ELFIO_BUILD_EXAMPLES "Build ELFIO examples" ON) -# Turns this on in order to build tests -option(ELFIO_BUILD_TESTS "Build ELFIO tests" OFF) + # Turns this on in order to build tests + option(ELFIO_BUILD_TESTS "Build ELFIO tests" ON) +endif() # Read version from header file set(version_header "elfio/elfio_version.hpp") file(READ ${version_header} ver) string(REGEX MATCH "#define ELFIO_VERSION \"([0-9\.]+)\"" _ ${ver}) -if (NOT CMAKE_MATCH_1) + +if(NOT CMAKE_MATCH_1) message(FATAL_ERROR "Unable to parse version from ${version_header}") endif() + set(version ${CMAKE_MATCH_1}) # Use configure_file to make configure step depend on elfio_version.hpp @@ -35,22 +39,22 @@ add_library(elfio::elfio ALIAS elfio) target_include_directories( elfio INTERFACE - $ - $) + $ + $) # If this is the top level project, add in logic to install elfio -if (IS_TOP_PROJECT) +if(IS_TOP_PROJECT) # Enable C++11 for examples and tests - if (ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS) + if(ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS) set(CMAKE_CXX_STANDARD 11) endif() - if (ELFIO_BUILD_EXAMPLES) + if(ELFIO_BUILD_EXAMPLES) # set (CMAKE_CXX_FLAGS "-Wall") add_subdirectory(examples) endif() - if (ELFIO_BUILD_TESTS AND IS_TOP_PROJECT) + if(ELFIO_BUILD_TESTS AND IS_TOP_PROJECT) # set (CMAKE_CXX_FLAGS "-Wall") enable_testing() add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} USES_TERMINAL) @@ -64,32 +68,32 @@ if (IS_TOP_PROJECT) write_basic_package_version_file( "${PROJECT_NAME}ConfigVersion.cmake" VERSION - ${PROJECT_VERSION} + ${PROJECT_VERSION} COMPATIBILITY - SameMajorVersion) - + SameMajorVersion) + # Create the default ${PROJECT_NAME}Config.cmake file which will be # installed and found by calls to `find_package(elfio)`. configure_package_config_file( "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION - ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) + ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) # Install the previously generated "config" and "version" files install( FILES - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION - ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) + ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) # Install the entire local `elfio` directory to the include directory install( DIRECTORY - elfio + elfio DESTINATION - ${CMAKE_INSTALL_INCLUDEDIR}) + ${CMAKE_INSTALL_INCLUDEDIR}) # Create a ${PROJECT_NAME}Targets.cmake file that is referenced by the # ${PROJECT_NAME}Config.cmake file and includes the target information @@ -97,27 +101,29 @@ if (IS_TOP_PROJECT) # ${PROJECT_NAME}_Targets export install( EXPORT - ${PROJECT_NAME}_Targets + ${PROJECT_NAME}_Targets FILE - ${PROJECT_NAME}Targets.cmake + ${PROJECT_NAME}Targets.cmake NAMESPACE - ${PROJECT_NAME}:: + ${PROJECT_NAME}:: DESTINATION - ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) + ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) # Add the elfio target to the ${PROJECT_NAME}_Targets export install( TARGETS - elfio + elfio EXPORT - ${PROJECT_NAME}_Targets) + ${PROJECT_NAME}_Targets) # cmake -B build; cmake --build build; cpack --config build/CPackSourceConfig.cmake # cmake -B build -D ELFIO_BUILD_EXAMPLES=ON -DELFIO_BUILD_TESTS=ON -G Ninja; cmake --build build; cpack --config build/CPackSourceConfig.cmake set(_fmt TGZ) + if(WIN32) - set(_fmt ZIP) + set(_fmt ZIP) endif() + set(CPACK_GENERATOR ${_fmt}) set(CPACK_SOURCE_GENERATOR ${_fmt}) set(CPACK_PACKAGE_VENDOR "ELFIO") @@ -134,7 +140,7 @@ if (IS_TOP_PROJECT) set(CPACK_SOURCE_IGNORE_FILES "/.git/;/.github/;/.vscode/;/build;/.clang-format;/.gitignore;/.travis.yml;/doc/images/;/doc/site;/doc/elfio.docx;/examples/sudo_gdb.sh;/tests") install(FILES ${CPACK_RESOURCE_FILE_README} ${CPACK_RESOURCE_FILE_LICENSE} - DESTINATION share/docs/${PROJECT_NAME}) + DESTINATION share/docs/${PROJECT_NAME}) include(CPack) endif()