From 3ad691586e48954ff9ebef5040b0ff56c9b4a6d4 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Mon, 6 Jan 2025 17:14:40 +0100 Subject: [PATCH] Add `_qt_internal_set_source_file_generated` function Create a function `_qt_internal_set_source_file_generated` that sets the source file property `GENERATED` along with Qt relevant properties: - `SKIP_LINTING` if CMake>=3.27 and `QT_FEATURE_lint_generated_code` Task-number: QTBUG-125077 Change-Id: I0ef5f7901f502366aaf2d020554c72e4845101b6 Reviewed-by: Alexey Edelev (cherry picked from commit f654519c7bd142d5cc9f81e0c826dfd31956791d) Reviewed-by: Qt Cherry-pick Bot --- cmake/QtPublicTargetHelpers.cmake | 90 +++++++++++++++++++++++++++++++ configure.cmake | 4 ++ 2 files changed, 94 insertions(+) diff --git a/cmake/QtPublicTargetHelpers.cmake b/cmake/QtPublicTargetHelpers.cmake index a534d5e8752..b31732f56bd 100644 --- a/cmake/QtPublicTargetHelpers.cmake +++ b/cmake/QtPublicTargetHelpers.cmake @@ -424,3 +424,93 @@ function(_qt_internal_warn_about_example_add_subdirectory) ) endif() endfunction() + +# Mark source files as generated. +# +# This sets `GENERATED` property to TRUE, along with other Qt relevant properties, +# e.g. `SKIP_LINTING`. +# +# Synopsis +# +# _qt_internal_set_source_file_generated(SOURCE ... +# [CONFIGURE_GENERATED] +# [SKIP_AUTOGEN] +# [DIRECTORY ...] +# [TARGET_DIRECTORY ...] +# ) +# +# Arguments +# +# `SOURCES` +# Source files that are generated. +# +# Equivalent to `set_source_files_properties()`. +# +# `DIRECTORY` +# Equivalent to `set_source_files_properties(DIRECTORY)`. +# +# `TARGET_DIRECTORY` +# Equivalent to `set_source_files_properties(TARGET_DIRECTORY)`. +# +# `SKIP_AUTOGEN` +# Set SKIP_AUTOGEN property to True as well. +# +# `CONFIGURE_GENERATED` +# Files are generated with `configure_file`. +# Does not set `GENERATED TRUE` property. This is needed to avoid removing the file when +# running the clean target. +function(_qt_internal_set_source_file_generated) + set(option_args + SKIP_AUTOGEN + CONFIGURE_GENERATED + ) + set(single_args "") + set(multi_args + SOURCES + DIRECTORY + TARGET_DIRECTORY + ) + + cmake_parse_arguments(PARSE_ARGV 0 arg + "${option_args}" "${single_args}" "${multi_args}" + ) + # Parse required variables + if(NOT arg_SOURCES AND QT_FEATURE_developer_build) + message(WARNING + "Unexpected call _qt_internal_set_source_file_generated with empty `SOURCES`." + ) + endif() + # Prepend again the appropriate keywords to pass to `set_source_files_properties` + if(arg_DIRECTORY) + list(PREPEND arg_DIRECTORY DIRECTORY) + endif() + if(arg_TARGET_DIRECTORY) + list(PREPEND arg_TARGET_DIRECTORY TARGET_DIRECTORY) + endif() + + # Construct the properties list + set(properties "") + if(NOT arg_CONFIGURE_GENERATED) + list(APPEND properties + GENERATED TRUE + ) + endif() + if(arg_SKIP_AUTOGEN) + list(APPEND properties + SKIP_AUTOGEN TRUE + ) + endif() + # Add SKIP_LINTING if possible. We do not add it unconditionally here to avoid + # confusion when CMake ignores this variable. + if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.27" AND NOT QT_FEATURE_lint_generated_code) + list(APPEND properties + SKIP_LINTING TRUE + ) + endif() + + set_source_files_properties(${arg_SOURCES} + ${arg_DIRECTORY} + ${arg_TARGET_DIRECTORY} + PROPERTIES ${properties} + ) +endfunction() diff --git a/configure.cmake b/configure.cmake index 0e86ed743d7..59cef3c9b93 100644 --- a/configure.cmake +++ b/configure.cmake @@ -580,6 +580,10 @@ qt_feature("no-prefix" AUTODETECT NOT QT_WILL_INSTALL CONDITION NOT QT_WILL_INSTALL ) +qt_feature("lint_generated_code" + LABEL "Lint qt-generated code" + AUTODETECT QT_FEATURE_developer_build +) qt_feature("private_tests" PRIVATE LABEL "Developer build: private_tests" CONDITION QT_FEATURE_developer_build