Warning, /frameworks/extra-cmake-modules/modules/ECMCheckOutboundLicense.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2020 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0002 #
0003 # SPDX-License-Identifier: BSD-3-Clause
0004 
0005 #[=======================================================================[.rst:
0006 ECMCheckOutboundLicense
0007 -----------------------
0008 
0009 Assert that source file licenses are compatible with a desired outbound license
0010 of a compiled binary artifact (e.g., library, plugin or application).
0011 
0012 This module provides the ``ecm_check_outbound_license`` function that
0013 generates unit tests for checking the compatibility of license statements.
0014 The license statements in all tested files are required to be added by using
0015 the SPDX marker ``SPDX-License-Identifier``.
0016 
0017 During the CMake configuration of the project, a temporary license bill of
0018 materials (BOM) in SPDX format is generated by calling the REUSE tool
0019 (see <https://reuse.software>). That BOM is parsed and license computations
0020 based on an internal compatibility matrix are performed.
0021 
0022 Preconditions for using this module:
0023     * All tested input source files must contain the SPDX-License-Identifier tag.
0024     * Python3 must be available.
0025     * The REUSE tool must be available, which generates the bill-of-materials
0026       by running ``reuse spdx`` on the tested directory.
0027 
0028 When this module is included, a ``SKIP_LICENSE_TESTS`` option is added (default
0029 ``OFF``). Turning this option on skips the generation of license tests, which might
0030 be convenient if licenses shall not be tested in all build configurations.
0031 
0032 ::
0033 
0034   ecm_check_outbound_license(LICENSES <outbound-licenses>
0035                              FILES <source-files>
0036                              [TEST_NAME <name>]
0037                              [WILL_FAIL])
0038 
0039 This method adds a custom unit test to ensure the specified outbound license to be
0040 compatible with the specified license headers. Note that a convenient way is to use
0041 the CMake ``GLOB`` argument of the ``FILE`` function.
0042 
0043 ``LICENSES``  : List of one or multiple outbound license regarding which the compatibility of the source code files shall be tested.
0044                 Currently, the following values are supported (values are SPDX registry identifiers):
0045                    * MIT
0046                    * BSD-2-Clause
0047                    * BSD-3-Clause
0048                    * LGPL-2.0-only
0049                    * LGPL-2.1-only
0050                    * LGPL-3.0-only
0051                    * GPL-2.0-only
0052                    * GPL-3.0-only
0053 
0054 ``FILES:``    : List of source files that contain valid SPDX-License-Identifier markers.
0055                 The paths can be relative to the CMake file that generates the test case
0056                 or be absolute paths.
0057 
0058 ``TEST_NAME`` : Optional parameter that defines the name of the generated test case.
0059                 If no name is defined, the relative path to the test directory with appended
0060                 license name is used. Every test has ``licensecheck_`` as prefix.
0061 
0062 ``WILL_FAIL`` : Optional parameter that inverts the test result. This parameter is usually only
0063                 used for tests of the module.
0064 
0065 Since 5.75.0
0066 #]=======================================================================]
0067 
0068 include(FeatureSummary)
0069 
0070 option(SKIP_LICENSE_TESTS "Skip outbound license tests" OFF)
0071 
0072 find_package(Python3)
0073 set_package_properties(Python3 PROPERTIES
0074     PURPOSE "Required to run tests of module ECMCheckOutboundLicense"
0075     TYPE OPTIONAL
0076 )
0077 
0078 find_package(ReuseTool)
0079 set_package_properties(ReuseTool PROPERTIES
0080     PURPOSE "Required to run tests of module ECMCheckOutboundLicense"
0081     TYPE OPTIONAL
0082 )
0083 
0084 if (NOT SKIP_LICENSE_TESTS AND NOT REUSETOOL_FOUND)
0085     add_feature_info(SPDX_LICENSE_TESTING FALSE "Automatic license testing based on SPDX definitions (requires reuse tool)")
0086     message(WARNING "Reuse tool not found, skipping test generation")
0087 else()
0088     add_feature_info(SPDX_LICENSE_TESTING TRUE "Automatic license testing based on SPDX definitions (requires reuse tool)")
0089 endif()
0090 
0091 set(SPDX_BOM_OUTPUT "${CMAKE_BINARY_DIR}/spdx.txt")
0092 
0093 # test fixture for generating SPDX bill of materials
0094 if(SKIP_LICENSE_TESTS OR NOT REUSETOOL_FOUND)
0095     message(STATUS "Skipping execution of outbound license tests.")
0096 else()
0097     add_test(
0098         NAME generate_spdx_bom
0099         COMMAND reuse spdx -o ${SPDX_BOM_OUTPUT}
0100         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
0101     )
0102     set_tests_properties(generate_spdx_bom PROPERTIES FIXTURES_SETUP SPDX_BOM)
0103 endif()
0104 
0105 function(ecm_check_outbound_license)
0106     if(SKIP_LICENSE_TESTS OR NOT REUSETOOL_FOUND)
0107         return()
0108     endif()
0109 
0110     set(_options WILL_FAIL)
0111     set(_oneValueArgs TEST_NAME)
0112     set(_multiValueArgs LICENSES FILES)
0113     cmake_parse_arguments(ARG "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN} )
0114 
0115     if(NOT ARG_LICENSES)
0116         message(FATAL_ERROR "No LICENSES argument given to ecm_check_outbound_license")
0117     endif()
0118 
0119     if(NOT ARG_FILES)
0120         message(WARNING "No FILES argument given to ecm_check_outbound_license")
0121         return()
0122     endif()
0123 
0124     if(NOT ARG_TEST_NAME)
0125         # compute test name based on licensecheck_<relative-path>_<licence> if not name given
0126         string(REPLACE "${CMAKE_SOURCE_DIR}/" "" TEMP_TEST_NAME "${CMAKE_CURRENT_SOURCE_DIR}_${ARG_LICENSE}")
0127         string(MAKE_C_IDENTIFIER ${TEMP_TEST_NAME} ARG_TEST_NAME)
0128     endif()
0129 
0130     # generate file with list of relative file paths
0131     string(REPLACE "${CMAKE_BINARY_DIR}/" "" RELATIVE_PREFIX_PATH ${CMAKE_CURRENT_BINARY_DIR})
0132     set(OUTPUT_FILE ${CMAKE_BINARY_DIR}/licensecheck_${ARG_TEST_NAME}.txt)
0133     file(REMOVE ${OUTPUT_FILE})
0134     foreach(_file ${ARG_FILES})
0135         # check script expects files to start with "./", which must be relative to CMAKE_SOURCE_DIR
0136         if (IS_ABSOLUTE ${_file})
0137             string(REPLACE ${CMAKE_SOURCE_DIR} "." TEMPORARY_PATH ${_file})
0138             file(APPEND ${OUTPUT_FILE} "${TEMPORARY_PATH}\n")
0139         else()
0140             file(APPEND ${OUTPUT_FILE} "./${RELATIVE_PREFIX_PATH}/${_file}\n")
0141         endif()
0142     endforeach()
0143 
0144     file(COPY ${ECM_MODULE_DIR}/check-outbound-license.py DESTINATION ${CMAKE_BINARY_DIR})
0145 
0146     foreach(_license ${ARG_LICENSES})
0147         string(MAKE_C_IDENTIFIER ${_license} LICENSE_ID)
0148         add_test(
0149             NAME licensecheck_${ARG_TEST_NAME}_${LICENSE_ID}
0150             COMMAND python3 ${CMAKE_BINARY_DIR}/check-outbound-license.py -l ${_license} -s ${SPDX_BOM_OUTPUT} -i ${OUTPUT_FILE}
0151         )
0152         set_tests_properties(licensecheck_${ARG_TEST_NAME}_${LICENSE_ID} PROPERTIES FIXTURES_REQUIRED SPDX_BOM)
0153         set_tests_properties(licensecheck_${ARG_TEST_NAME}_${LICENSE_ID} PROPERTIES WILL_FAIL ${ARG_WILL_FAIL})
0154     endforeach()
0155 endfunction()