165042b18Sopenharmony_cicmake_minimum_required(VERSION 3.10)
265042b18Sopenharmony_ci
365042b18Sopenharmony_ci# Detect if we are the top level CMakeLists.txt or are we included in some
465042b18Sopenharmony_ci# other project
565042b18Sopenharmony_ciif(NOT DEFINED PROJECT_NAME)
665042b18Sopenharmony_ci    set(IS_TOP_PROJECT TRUE)
765042b18Sopenharmony_ciendif()
865042b18Sopenharmony_ci
965042b18Sopenharmony_ciif(IS_TOP_PROJECT)
1065042b18Sopenharmony_ci    # Turn this on in order to build elfio examples
1165042b18Sopenharmony_ci    option(ELFIO_BUILD_EXAMPLES "Build ELFIO examples" ON)
1265042b18Sopenharmony_ci
1365042b18Sopenharmony_ci    # Turns this on in order to build tests
1465042b18Sopenharmony_ci    option(ELFIO_BUILD_TESTS "Build ELFIO tests" OFF)
1565042b18Sopenharmony_ciendif()
1665042b18Sopenharmony_ci
1765042b18Sopenharmony_ci# Read version from header file
1865042b18Sopenharmony_ciset(version_header "elfio/elfio_version.hpp")
1965042b18Sopenharmony_cifile(READ ${version_header} ver)
2065042b18Sopenharmony_cistring(REGEX MATCH "#define ELFIO_VERSION \"([0-9\.]+)\"" _ ${ver})
2165042b18Sopenharmony_ci
2265042b18Sopenharmony_ciif(NOT CMAKE_MATCH_1)
2365042b18Sopenharmony_ci    message(FATAL_ERROR "Unable to parse version from ${version_header}")
2465042b18Sopenharmony_ciendif()
2565042b18Sopenharmony_ci
2665042b18Sopenharmony_ciset(version ${CMAKE_MATCH_1})
2765042b18Sopenharmony_ci
2865042b18Sopenharmony_ci# Use configure_file to make configure step depend on elfio_version.hpp
2965042b18Sopenharmony_ciconfigure_file(${version_header} ${CMAKE_CURRENT_BINARY_DIR}/elfio_version.hpp.copy COPYONLY)
3065042b18Sopenharmony_ci
3165042b18Sopenharmony_ciproject(elfio VERSION ${version} LANGUAGES C CXX)
3265042b18Sopenharmony_ci
3365042b18Sopenharmony_ciinclude(GNUInstallDirs)
3465042b18Sopenharmony_ci
3565042b18Sopenharmony_ci# Create a header only CMake target for elfio
3665042b18Sopenharmony_ciadd_library(elfio INTERFACE)
3765042b18Sopenharmony_ciadd_library(elfio::elfio ALIAS elfio)
3865042b18Sopenharmony_ci
3965042b18Sopenharmony_citarget_include_directories(
4065042b18Sopenharmony_ci    elfio
4165042b18Sopenharmony_ci    INTERFACE
4265042b18Sopenharmony_ci    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
4365042b18Sopenharmony_ci    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
4465042b18Sopenharmony_ci
4565042b18Sopenharmony_ci# If this is the top level project, add in logic to install elfio
4665042b18Sopenharmony_ciif(IS_TOP_PROJECT)
4765042b18Sopenharmony_ci    # Enable C++11 for examples and tests
4865042b18Sopenharmony_ci    if(ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS)
4965042b18Sopenharmony_ci        set(CMAKE_CXX_STANDARD 14)
5065042b18Sopenharmony_ci    endif()
5165042b18Sopenharmony_ci
5265042b18Sopenharmony_ci    if(ELFIO_BUILD_EXAMPLES)
5365042b18Sopenharmony_ci        # set (CMAKE_CXX_FLAGS "-Wall")
5465042b18Sopenharmony_ci        add_subdirectory(examples)
5565042b18Sopenharmony_ci    endif()
5665042b18Sopenharmony_ci
5765042b18Sopenharmony_ci    if(ELFIO_BUILD_TESTS AND IS_TOP_PROJECT)
5865042b18Sopenharmony_ci        # set (CMAKE_CXX_FLAGS "-Wall")
5965042b18Sopenharmony_ci        enable_testing()
6065042b18Sopenharmony_ci        add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} USES_TERMINAL)
6165042b18Sopenharmony_ci        add_subdirectory(tests)
6265042b18Sopenharmony_ci    endif()
6365042b18Sopenharmony_ci
6465042b18Sopenharmony_ci    include(CMakePackageConfigHelpers)
6565042b18Sopenharmony_ci
6665042b18Sopenharmony_ci    # Create a file that includes the current project version. This will be
6765042b18Sopenharmony_ci    # installed with the elfio CMake package.
6865042b18Sopenharmony_ci    write_basic_package_version_file(
6965042b18Sopenharmony_ci        "${PROJECT_NAME}ConfigVersion.cmake"
7065042b18Sopenharmony_ci        VERSION
7165042b18Sopenharmony_ci        ${PROJECT_VERSION}
7265042b18Sopenharmony_ci        COMPATIBILITY
7365042b18Sopenharmony_ci        SameMajorVersion)
7465042b18Sopenharmony_ci
7565042b18Sopenharmony_ci    # Create the default ${PROJECT_NAME}Config.cmake file which will be
7665042b18Sopenharmony_ci    # installed and found by calls to `find_package(elfio)`.
7765042b18Sopenharmony_ci    configure_package_config_file(
7865042b18Sopenharmony_ci        "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
7965042b18Sopenharmony_ci        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
8065042b18Sopenharmony_ci        INSTALL_DESTINATION
8165042b18Sopenharmony_ci        ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
8265042b18Sopenharmony_ci
8365042b18Sopenharmony_ci    # Install the previously generated "config" and "version" files
8465042b18Sopenharmony_ci    install(
8565042b18Sopenharmony_ci        FILES
8665042b18Sopenharmony_ci        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
8765042b18Sopenharmony_ci        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
8865042b18Sopenharmony_ci        DESTINATION
8965042b18Sopenharmony_ci        ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
9065042b18Sopenharmony_ci
9165042b18Sopenharmony_ci    # Install the entire local `elfio` directory to the include directory
9265042b18Sopenharmony_ci    install(
9365042b18Sopenharmony_ci        DIRECTORY
9465042b18Sopenharmony_ci        elfio
9565042b18Sopenharmony_ci        DESTINATION
9665042b18Sopenharmony_ci        ${CMAKE_INSTALL_INCLUDEDIR})
9765042b18Sopenharmony_ci
9865042b18Sopenharmony_ci    # Create a ${PROJECT_NAME}Targets.cmake file that is referenced by the
9965042b18Sopenharmony_ci    # ${PROJECT_NAME}Config.cmake file and includes the target information
10065042b18Sopenharmony_ci    # needed to compile/link against all targets exported under the
10165042b18Sopenharmony_ci    # ${PROJECT_NAME}_Targets export
10265042b18Sopenharmony_ci    install(
10365042b18Sopenharmony_ci        EXPORT
10465042b18Sopenharmony_ci        ${PROJECT_NAME}_Targets
10565042b18Sopenharmony_ci        FILE
10665042b18Sopenharmony_ci        ${PROJECT_NAME}Targets.cmake
10765042b18Sopenharmony_ci        NAMESPACE
10865042b18Sopenharmony_ci        ${PROJECT_NAME}::
10965042b18Sopenharmony_ci        DESTINATION
11065042b18Sopenharmony_ci        ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
11165042b18Sopenharmony_ci
11265042b18Sopenharmony_ci    # Add the elfio target to the ${PROJECT_NAME}_Targets export
11365042b18Sopenharmony_ci    install(
11465042b18Sopenharmony_ci        TARGETS
11565042b18Sopenharmony_ci        elfio
11665042b18Sopenharmony_ci        EXPORT
11765042b18Sopenharmony_ci        ${PROJECT_NAME}_Targets)
11865042b18Sopenharmony_ci
11965042b18Sopenharmony_ci    # cmake -B build; cmake --build build; cpack --config build/CPackSourceConfig.cmake
12065042b18Sopenharmony_ci    # cmake -B build -D ELFIO_BUILD_EXAMPLES=ON -DELFIO_BUILD_TESTS=ON -G Ninja; cmake --build build; cpack --config build/CPackSourceConfig.cmake
12165042b18Sopenharmony_ci    set(_fmt TGZ)
12265042b18Sopenharmony_ci
12365042b18Sopenharmony_ci    if(WIN32)
12465042b18Sopenharmony_ci        set(_fmt ZIP)
12565042b18Sopenharmony_ci    endif()
12665042b18Sopenharmony_ci
12765042b18Sopenharmony_ci    set(CPACK_GENERATOR ${_fmt})
12865042b18Sopenharmony_ci    set(CPACK_SOURCE_GENERATOR ${_fmt})
12965042b18Sopenharmony_ci    set(CPACK_PACKAGE_VENDOR "ELFIO")
13065042b18Sopenharmony_ci    set(CPACK_PACKAGE_CONTACT "Serge Lamikhov-Center")
13165042b18Sopenharmony_ci    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.txt")
13265042b18Sopenharmony_ci    set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
13365042b18Sopenharmony_ci    set(CPACK_OUTPUT_FILE_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/package")
13465042b18Sopenharmony_ci    set(CPACK_PACKAGE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
13565042b18Sopenharmony_ci    string(TOLOWER ${CMAKE_SYSTEM_NAME} _sys)
13665042b18Sopenharmony_ci    string(TOLOWER ${PROJECT_NAME} _project_lower)
13765042b18Sopenharmony_ci    set(CPACK_PACKAGE_FILE_NAME "${_project_lower}-${_sys}")
13865042b18Sopenharmony_ci    set(CPACK_SOURCE_PACKAGE_FILE_NAME "${_project_lower}-${PROJECT_VERSION}")
13965042b18Sopenharmony_ci
14065042b18Sopenharmony_ci    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")
14165042b18Sopenharmony_ci
14265042b18Sopenharmony_ci    install(FILES ${CPACK_RESOURCE_FILE_README} ${CPACK_RESOURCE_FILE_LICENSE}
14365042b18Sopenharmony_ci        DESTINATION share/docs/${PROJECT_NAME})
14465042b18Sopenharmony_ci
14565042b18Sopenharmony_ci    include(CPack)
14665042b18Sopenharmony_ciendif()
147