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

0001 # SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0002 #
0003 # SPDX-License-Identifier: BSD-3-Clause
0004 
0005 #[=======================================================================[.rst:
0006 ECMDeprecationSettings
0007 -----------------------
0008 
0009 This module provides the ``ecm_set_disabled_deprecation_versions`` function setting the excluding
0010 deprecated API for Qt and KF projects.
0011 
0012 This method expects pairs of the identifier and deprecation version.
0013 For the identifier ``QT`` this functions adds the definition ``QT_DISABLE_DEPRECATED_BEFORE`` with the given version in a hexadecimal format.
0014 Otherwise the name for the definition is generated using ``${IDENTIFIER}_DISABLE_DEPRECATED_BEFORE_AND_AT``,
0015 following the naming of the generated code in :module:`ECMGenerateExportHeader`.
0016 The version for the definition can be overwritten, by passing definition name and the deprecation version
0017 as a CMake definition. This allows one to exclude deprecations without having to edit the CMakeLists.txt file.
0018 
0019 This module provides the following function:
0020 
0021 ::
0022 
0023   ecm_set_disabled_deprecation_versions(
0024       [DISABLE_NEWER_WARNINGS] # since 5.96
0025       [<identifier> <deprecation_version>]
0026       [<identifier2> <deprecation_version2>]
0027   )
0028 
0029 ``DISABLE_NEWER_WARNINGS`` disables additionally the compiler warnings for API deprecated in newer versions
0030 of the same major version.
0031 
0032 
0033 Example usage:
0034 
0035 .. code-block:: cmake
0036 
0037   set(QT_MIN_VERSION "5.15.2")
0038   set(KF5_MIN_VERSION "5.90")
0039 
0040   ecm_set_disabled_deprecation_versions(
0041     QT ${QT_MIN_VERSION}
0042     KF ${KF5_MIN_VERSION}
0043     KCOREADDONS 5.89.0 # In case we depend on deprecated KCoreAddons API
0044   )
0045 
0046 
0047 
0048 Since 5.91
0049 #]=======================================================================]
0050 
0051 function (ecm_set_disabled_deprecation_versions)
0052     cmake_parse_arguments(ARGS "SHOW_DEPRECATIONS;DISABLE_NEWER_WARNINGS" "" "" ${ARGN})
0053 
0054     # support legacy initial flag to opt-in to warnings
0055     if (ARGS_SHOW_DEPRECATIONS)
0056         message(DEPRECATION "SHOW_DEPRECATIONS is deprecated, since 5.96 warnings are enabled by default.")
0057     endif()
0058     if (ARGS_SHOW_DEPRECATIONS AND ARGS_DISABLE_NEWER_WARNINGS)
0059         message(FATAL_ERROR "SHOW_DEPRECATIONS && DISABLE_NEWER_WARNINGS cannot be set both.")
0060     endif()
0061     set(show_newer_warnings TRUE)
0062     if (ARGS_DISABLE_NEWER_WARNINGS)
0063         set(show_newer_warnings FALSE)
0064     endif()
0065 
0066     list(LENGTH ARGS_UNPARSED_ARGUMENTS PAIR_COUNT)
0067     math(EXPR is_even_number "${PAIR_COUNT} % 2")
0068     if (NOT is_even_number EQUAL 0)
0069         message(FATAL_ERROR "Expected number of arguments is an even number of identifiers and version")
0070     endif()
0071     math(EXPR number_pairs "(${PAIR_COUNT} / 2) - 1")
0072     foreach (it RANGE ${number_pairs})
0073         # get values
0074         math(EXPR current_index "${it} * 2")
0075         list(GET ARGS_UNPARSED_ARGUMENTS ${current_index} DEPRECATION_NAME)
0076         math(EXPR next_index "(${it} *2) + 1")
0077         list(GET ARGS_UNPARSED_ARGUMENTS ${next_index} DEPRECATION_VERSION)
0078 
0079         # get the string identifier for the target definition
0080         string(COMPARE EQUAL ${DEPRECATION_NAME} "QT" IS_QT_DEPRECATION)
0081         if (IS_QT_DEPRECATION)
0082             set(DEPRECATION_DEFINITION_NAME QT_DISABLE_DEPRECATED_BEFORE)
0083         else()
0084             set(DEPRECATION_DEFINITION_NAME ${DEPRECATION_NAME}_DISABLE_DEPRECATED_BEFORE_AND_AT)
0085         endif()
0086         # we want to be able to set this version without being forced to edit the CMakeLists.txt file
0087         if (${${DEPRECATION_DEFINITION_NAME}})
0088             set(DEPRECATION_VERSION "${${DEPRECATION_DEFINITION_NAME}}")
0089         endif()
0090 
0091         # make a sanity check to make sure we do not get malformed versions
0092         _ecm_version_triple_sanity_check("${DEPRECATION_VERSION}")
0093 
0094         # add the actual compile definition with the given hex value
0095         _ecm_geh_generate_hex_number_from_version(DEPRECATION_HEX_VERSION ${DEPRECATION_VERSION})
0096         add_definitions(-D${DEPRECATION_DEFINITION_NAME}=${DEPRECATION_HEX_VERSION})
0097 
0098         # Set the version for the deprecation warnings
0099         if (show_newer_warnings)
0100             string(REGEX MATCH "([0-9]+)\\." _ ${DEPRECATION_VERSION})
0101             if (NOT CMAKE_MATCH_1)
0102                 message(FATAL_ERROR "Failed to get major version from ${DEPRECATION_VERSION}")
0103             endif()
0104             # Add 1 to the major version and store it as a hex value
0105             math(EXPR next_major_version "(${CMAKE_MATCH_1} + 1) * 65536 " OUTPUT_FORMAT HEXADECIMAL)
0106             add_definitions(-D${DEPRECATION_NAME}_DEPRECATED_WARNINGS_SINCE=${next_major_version})
0107         endif()
0108 
0109     endforeach()
0110 endfunction()
0111 
0112 # helper method
0113 function(_ecm_geh_generate_hex_number_from_version _var_name _version)
0114     set(_hexnumber 0)
0115 
0116     string(REGEX MATCH "^([0-9]+)\\.([0-9]+)(\\.([0-9]+))?$" _ ${_version})
0117 
0118     # Set the patch version to 0, if none is specified by the regex.
0119     # This is the case for min. versions that don't specify the patch level, like in the snipped of the method docs.
0120     if (NOT CMAKE_MATCH_4)
0121         set(CMAKE_MATCH_4 "0")
0122     endif()
0123 
0124     math(EXPR _hexnumber "${CMAKE_MATCH_1}*65536 + ${CMAKE_MATCH_2}*256 + ${CMAKE_MATCH_4}" OUTPUT_FORMAT HEXADECIMAL)
0125     set(${_var_name} ${_hexnumber} PARENT_SCOPE)
0126 endfunction()
0127 
0128 function (_ecm_version_triple_sanity_check unchecked_version)
0129     # helper string
0130     set(_version_triple_regexp "^([0-9]+)\\.([0-9]+)(\\.([0-9]+))?$")
0131     # args sanity check
0132     if (NOT unchecked_version)
0133         message(FATAL_ERROR "No VERSION passed when calling ecm_set_deprecation_versions().")
0134     elseif(NOT unchecked_version MATCHES ${_version_triple_regexp})
0135         message(FATAL_ERROR "VERSION ${unchecked_version} expected to be in x.y.z format when calling ecm_set_deprecation_versions().")
0136     endif()
0137 endfunction()