# SPDX-License-Identifier: GPL-3.0-only
#
# @file package.cmake
#
# @copyright Copyright (C) 2013-2024 srcML, LLC. (www.srcML.org)
#
# CPack configuration for all installers

# The CMake variable CPACK_OUTPUT_FILE_PREFIX defines where the installers are placed.
# Choose in this order:
# 1. The preset or user defined CPACK_OUTPUT_FILE_PREFIX
# 2. The external directory "/Builds" if the file /.dockerenv exists and "/Builds" exists
# 3. The subdirectory "dist"
if(NOT DEFINED CPACK_OUTPUT_FILE_PREFIX)
    set(CPACK_OUTPUT_FILE_PREFIX "/Builds")
    if(NOT EXISTS "/.dockerenv" OR NOT EXISTS ${CPACK_OUTPUT_FILE_PREFIX})
        set(CPACK_OUTPUT_FILE_PREFIX "dist")
    endif()
endif()
# If the directory is relative, assume in the build directory
cmake_path(ABSOLUTE_PATH CPACK_OUTPUT_FILE_PREFIX BASE_DIRECTORY "${CMAKE_BINARY_DIR}" NORMALIZE)
message(STATUS "Installers will be written to: ${CPACK_OUTPUT_FILE_PREFIX}")

# especially for archives
set(CPACK_COMPONENTS_GROUPING "ONE_PER_GROUP")

# Package name
set(CPACK_PACKAGE_NAME "srcml")

# Package release number (NOT srcml or libsrcml release)
# Note: Update when package is updated, but not contents
set(SRCML_PACKAGE_RELEASE 1)

# summary
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "srcML Toolkit")

# description
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/package/welcome.html)

# vendor
set(CPACK_PACKAGE_VENDOR "srcML, LLC.")

# contact
set(CPACK_PACKAGE_CONTACT "Software Development Laboratories <srcML.org>")

# package version
# set as part of project()

# license
set(CPACK_RESOURCE_FILE_LICENSE ${PROJECT_SOURCE_DIR}/COPYING.txt)

# README
set(CPACK_RESOURCE_FILE_README ${PROJECT_SOURCE_DIR}/README.md)

# welcome message
set(CPACK_RESOURCE_FILE_WELCOME ${PROJECT_SOURCE_DIR}/package/welcome.html)

# strip executables
set(CPACK_STRIP_FILES ON)

# Generate user and development tar.gz's
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)

# Client component, SRCML, is just one component so it appears as on item
# for installers that let you pick what to install
# Development group, SRCMLDEV, is composed of multiple components
include(CPackComponent)
cpack_add_install_type(CLIENT DISPLAY_NAME "srcml cli")
cpack_add_install_type(DEVELOPER DISPLAY_NAME "development")

cpack_add_component(SRCML
                    DISPLAY_NAME "srcml"
                    DESCRIPTION "srcml cli program with required libraries"
                    REQUIRED
                    INSTALL_TYPES CLIENT DEVELOPER)

cpack_add_component(DEVLIBS
                    DISPLAY_NAME "libsrcml"
                    DESCRIPTION "Include file, development libraries, i.e.., static library, and CMake configuration"
                    INSTALL_TYPES DEVELOPER)

# Copy and rename the README file for the proper extension
configure_file(${PROJECT_SOURCE_DIR}/README.md ${CMAKE_BINARY_DIR}/README.txt COPYONLY)
set(CPACK_RESOURCE_FILE_README ${CMAKE_BINARY_DIR}/README.txt)

# Test targets for workflows
set(SRCML_TEST_TIMEOUT 5 CACHE STRING "ctest timeout in seconds")
function(add_workflow_test_targets binary_dir output_directory package_file_name)

    # Target for test_client
    add_custom_target(test_client
        WORKING_DIRECTORY ${binary_dir}
        COMMAND ${CMAKE_CTEST_COMMAND} -VV -R ^srcml --timeout ${SRCML_TEST_TIMEOUT} --output-log ${output_directory}/${package_file_name}-client.log --output-junit ${output_directory}/${package_file_name}-client.xml || true
    )

    # Target for test_libsrcml
    add_custom_target(test_libsrcml
        WORKING_DIRECTORY ${binary_dir}
        COMMAND ${CMAKE_CTEST_COMMAND} -VV -R ^libsrcml --timeout ${SRCML_TEST_TIMEOUT} --output-log ${output_directory}/${package_file_name}-libsrcml.log --output-junit ${output_directory}/${package_file_name}-libsrcml.xml || true
    )

    # Target for test_parser
    # TODO: export/convert log to junit for ingestion into circleci
    add_custom_target(test_parser
        WORKING_DIRECTORY ${binary_dir}
        COMMAND ${CMAKE_COMMAND} --build . --target run_parser_tests | tee ${output_directory}/${package_file_name}-parser.log
        DEPENDS gen_parser_tests
    )
endfunction()

# Generators are defined in the *-cpack.cmake files
set(CPACK_GENERATOR)
file(GLOB INSTALLERS "*-cpack.cmake")
foreach(INSTALLER IN ITEMS ${INSTALLERS})
    include(${INSTALLER})
endforeach()

message(STATUS "CPack generators: ${CPACK_GENERATOR}")

# Hide the LOCAL components
get_cmake_property(CPACK_COMPONENTS_ALL COMPONENTS)
list(REMOVE_ITEM CPACK_COMPONENTS_ALL "LOCAL")

message(STATUS "CPACK_GENERATOR: ${CPACK_GENERATOR}")

# needs to be last so not overwritten
include(CPack)
