diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst index ff573906b2..7c488060b9 100644 --- a/Help/manual/cmake-policies.7.rst +++ b/Help/manual/cmake-policies.7.rst @@ -57,6 +57,7 @@ Policies Introduced by CMake 3.27 .. toctree:: :maxdepth: 1 + CMP0151: AUTOMOC include directory is a system include directory by default. CMP0150: ExternalProject_Add and FetchContent_Declare treat relative git repository paths as being relative to parent project's remote. CMP0149: Visual Studio generators select latest Windows SDK by default. CMP0148: The FindPythonInterp and FindPythonLibs modules are removed. diff --git a/Help/policy/CMP0151.rst b/Help/policy/CMP0151.rst new file mode 100644 index 0000000000..c12f595f16 --- /dev/null +++ b/Help/policy/CMP0151.rst @@ -0,0 +1,28 @@ +CMP0151 +------- + +.. versionadded:: 3.27 + +AUTOMOC include directory is a system include directory by default. + +Headers generated for :ref:`Qt AUTOMOC` are placed in target-specific include +directories. CMake 3.26 and older added these as normal include directories. +CMake 3.27 and newer prefer to add them as system include directories. +This policy provides compatibility for projects that have not been updated +to expect this. + +If the :prop_tgt:`AUTOGEN_USE_SYSTEM_INCLUDE` target property is set, +perhaps via the :variable:`CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE` variable, +then its value is used regardless of the setting of this policy. + +The ``OLD`` behavior for this policy is to add autogen include directory to +the target's include directories. +The ``NEW`` behavior for this policy is to add autogen include directory to +the target's system include directories. + +This policy was introduced in CMake version 3.27. Use the +:command:`cmake_policy` command to set it to ``OLD`` or ``NEW`` explicitly. +Unlike many policies, CMake version |release| does *not* warn +when this policy is not set and simply uses ``OLD`` behavior. + +.. include:: DEPRECATED.txt diff --git a/Help/prop_tgt/AUTOGEN_USE_SYSTEM_INCLUDE.rst b/Help/prop_tgt/AUTOGEN_USE_SYSTEM_INCLUDE.rst index fd21070137..84212c861f 100644 --- a/Help/prop_tgt/AUTOGEN_USE_SYSTEM_INCLUDE.rst +++ b/Help/prop_tgt/AUTOGEN_USE_SYSTEM_INCLUDE.rst @@ -6,6 +6,9 @@ on a target to indicate that the autogen target include directory should be added as a system include directory or normal include directory to the target. +If this property is not set, the autogen target include directory is added +as a system include directory by default. See policy :policy:`CMP0151`. + See the :manual:`cmake-qt(7)` manual for more information on using CMake with Qt. diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 23e50a9715..a0030d3dc1 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -455,6 +455,10 @@ class cmMakefile; "ExternalProject_Add and FetchContent_Declare commands " \ "treat relative GIT_REPOSITORY paths as being relative " \ "to the parent project's remote.", \ + 3, 27, 0, cmPolicies::WARN) \ + SELECT(POLICY, CMP0151, \ + "AUTOMOC include directory is a system include directory by " \ + "default.", \ 3, 27, 0, cmPolicies::WARN) #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1) diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index deb2c04a97..544e1efc0e 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -587,7 +587,18 @@ bool cmQtAutoGenInitializer::InitCustomTargets() addBefore = true; } } else { - addBefore = true; + switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0151)) { + case cmPolicies::WARN: + case cmPolicies::OLD: + addBefore = true; + break; + case cmPolicies::REQUIRED_IF_USED: + case cmPolicies::REQUIRED_ALWAYS: + case cmPolicies::NEW: + this->GenTarget->AddSystemIncludeDirectory(this->Dir.IncludeGenExp, + "CXX"); + break; + } } this->GenTarget->AddIncludeDirectory(this->Dir.IncludeGenExp, addBefore); } diff --git a/Tests/RunCMake/Autogen/CMP0151-common.cmake b/Tests/RunCMake/Autogen/CMP0151-common.cmake new file mode 100644 index 0000000000..bbefd5f55b --- /dev/null +++ b/Tests/RunCMake/Autogen/CMP0151-common.cmake @@ -0,0 +1,10 @@ +enable_language(CXX) + +find_package(Qt${with_qt_version} REQUIRED COMPONENTS Core Widgets Gui) + +set(CMAKE_AUTOMOC ON) + +add_library(dummy SHARED empty.cpp) +target_link_libraries(dummy Qt${with_qt_version}::Core + Qt${with_qt_version}::Widgets + Qt${with_qt_version}::Gui) diff --git a/Tests/RunCMake/Autogen/CMP0151-new.cmake b/Tests/RunCMake/Autogen/CMP0151-new.cmake new file mode 100644 index 0000000000..9c77e586f9 --- /dev/null +++ b/Tests/RunCMake/Autogen/CMP0151-new.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/CMP0151-common.cmake") diff --git a/Tests/RunCMake/Autogen/CMP0151-old.cmake b/Tests/RunCMake/Autogen/CMP0151-old.cmake new file mode 100644 index 0000000000..9c77e586f9 --- /dev/null +++ b/Tests/RunCMake/Autogen/CMP0151-old.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/CMP0151-common.cmake") diff --git a/Tests/RunCMake/Autogen/RunCMakeTest.cmake b/Tests/RunCMake/Autogen/RunCMakeTest.cmake index 2f2b645cdf..45051326cb 100644 --- a/Tests/RunCMake/Autogen/RunCMakeTest.cmake +++ b/Tests/RunCMake/Autogen/RunCMakeTest.cmake @@ -40,9 +40,18 @@ if (DEFINED with_qt_version) string(APPEND test_expect_stdout "_Debug") endif() + block() + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0151-new-build) + run_cmake_with_options(CMP0151-new ${RunCMake_TEST_OPTIONS} -DCMAKE_POLICY_DEFAULT_CMP0151=NEW) + set(RunCMake_TEST_NO_CLEAN 1) + set(RunCMake_TEST_EXPECT_stdout "${test_expect_stdout}") + message(STATUS "RunCMake_TEST_EXPECT_stdout: ${RunCMake_TEST_EXPECT_stdout}") + run_cmake_command(CMP0151-new-build ${CMAKE_COMMAND} --build . --config Debug --verbose) + endblock() + block() set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/AutogenUseSystemIncludeOn-build) - run_cmake(AutogenUseSystemIncludeOn) + run_cmake_with_options(AutogenUseSystemIncludeOn ${RunCMake_TEST_OPTIONS} -DCMAKE_POLICY_DEFAULT_CMP0151=NEW) set(RunCMake_TEST_NO_CLEAN 1) set(RunCMake_TEST_EXPECT_stdout "${test_expect_stdout}") message(STATUS "RunCMake_TEST_EXPECT_stdout: ${RunCMake_TEST_EXPECT_stdout}") @@ -61,9 +70,18 @@ if (DEFINED with_qt_version) string(APPEND test_expect_stdout "_Debug") endif() + block() + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0151-old-build) + run_cmake_with_options(CMP0151-old ${RunCMake_TEST_OPTIONS} -DCMAKE_POLICY_DEFAULT_CMP0151=OLD) + set(RunCMake_TEST_NO_CLEAN 1) + set(RunCMake_TEST_EXPECT_stdout "${test_expect_stdout}") + message(STATUS "RunCMake_TEST_EXPECT_stdout: ${RunCMake_TEST_EXPECT_stdout}") + run_cmake_command(CMP0151-old-build ${CMAKE_COMMAND} --build . --config Debug --verbose) + endblock() + block() set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/AutogenUseSystemIncludeOff-build) - run_cmake(AutogenUseSystemIncludeOff) + run_cmake_with_options(AutogenUseSystemIncludeOff ${RunCMake_TEST_OPTIONS} -DCMAKE_POLICY_DEFAULT_CMP0151=NEW) set(RunCMake_TEST_NO_CLEAN 1) set(RunCMake_TEST_EXPECT_stdout "${test_expect_stdout}") message(STATUS "RunCMake_TEST_EXPECT_stdout: ${RunCMake_TEST_EXPECT_stdout}")