Merge topic 'autogen-system-include'

7bf4e30090 Autogen: Default AUTOGEN_USE_SYSTEM_INCLUDE to ON if it is not set
033dc7ee2f Autogen: Add AUTOGEN_USE_SYSTEM_INCLUDE target property
8ba16db163 Tests/RunCMake: Add option for dynamic expected output

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !8400
This commit is contained in:
Brad King
2023-05-04 13:00:51 +00:00
committed by Kitware Robot
25 changed files with 264 additions and 1 deletions

View File

@@ -73,6 +73,7 @@ syn keyword cmakeProperty contained
\ AUTOGEN_ORIGIN_DEPENDS
\ AUTOGEN_PARALLEL
\ AUTOGEN_SOURCE_GROUP
\ AUTOGEN_USE_SYSTEM_INCLUDE
\ AUTOGEN_TARGETS_FOLDER
\ AUTOGEN_TARGET_DEPENDS
\ AUTOMOC
@@ -683,6 +684,7 @@ syn keyword cmakeVariable contained
\ CMAKE_ASM_VISIBILITY_PRESET
\ CMAKE_AUTOGEN_ORIGIN_DEPENDS
\ CMAKE_AUTOGEN_PARALLEL
\ CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE
\ CMAKE_AUTOGEN_VERBOSE
\ CMAKE_AUTOMOC
\ CMAKE_AUTOMOC_COMPILER_PREDEFINES

View File

@@ -57,6 +57,7 @@ Policies Introduced by CMake 3.27
.. toctree::
:maxdepth: 1
CMP0151: AUTOMOC include directory is a system include directory by default. </policy/CMP0151>
CMP0150: ExternalProject_Add and FetchContent_Declare treat relative git repository paths as being relative to parent project's remote. </policy/CMP0150>
CMP0149: Visual Studio generators select latest Windows SDK by default. </policy/CMP0149>
CMP0148: The FindPythonInterp and FindPythonLibs modules are removed. </policy/CMP0148>

View File

@@ -133,6 +133,7 @@ Properties on Targets
/prop_tgt/AUTOGEN_ORIGIN_DEPENDS
/prop_tgt/AUTOGEN_PARALLEL
/prop_tgt/AUTOGEN_TARGET_DEPENDS
/prop_tgt/AUTOGEN_USE_SYSTEM_INCLUDE
/prop_tgt/AUTOMOC
/prop_tgt/AUTOMOC_COMPILER_PREDEFINES
/prop_tgt/AUTOMOC_DEPEND_FILTERS

View File

@@ -397,6 +397,7 @@ Variables that Control the Build
/variable/CMAKE_ARCHIVE_OUTPUT_DIRECTORY_CONFIG
/variable/CMAKE_AUTOGEN_ORIGIN_DEPENDS
/variable/CMAKE_AUTOGEN_PARALLEL
/variable/CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE
/variable/CMAKE_AUTOGEN_VERBOSE
/variable/CMAKE_AUTOMOC
/variable/CMAKE_AUTOMOC_COMPILER_PREDEFINES

28
Help/policy/CMP0151.rst Normal file
View File

@@ -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

View File

@@ -0,0 +1,17 @@
AUTOGEN_USE_SYSTEM_INCLUDE
--------------------------
``AUTOGEN_USE_SYSTEM_INCLUDE`` is a boolean property that can be set
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.
This property is initialized by the
:variable:`CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE` variable if it is set when
a target is created.

View File

@@ -0,0 +1,7 @@
autogen-system-include
----------------------
* The :prop_tgt:`AUTOGEN_USE_SYSTEM_INCLUDE` target property and
corresponding :variable:`CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE` were
added to explicitly control whether autogen headers are
considered system headers.

View File

@@ -0,0 +1,10 @@
CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE
--------------------------------
.. versionadded:: 3.27
This variable is used to initialize the :prop_tgt:`AUTOGEN_USE_SYSTEM_INCLUDE`
property on all targets as they are created. See that target property for
additional information.
By default ``CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE`` is unset.

View File

@@ -731,6 +731,29 @@ void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
BT<std::string>(src, this->Makefile->GetBacktrace()), true));
}
void cmGeneratorTarget::AddSystemIncludeDirectory(std::string const& inc,
std::string const& lang)
{
std::string config_upper;
auto const& configs =
this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
for (auto const& config : configs) {
std::string inc_with_config = inc;
if (!config.empty()) {
cmSystemTools::ReplaceString(inc_with_config, "$<CONFIG>", config);
config_upper = cmSystemTools::UpperCase(config);
}
auto const& key = cmStrCat(config_upper, "/", lang);
this->Target->AddSystemIncludeDirectories({ inc_with_config });
this->SystemIncludesCache[key].emplace_back(inc_with_config);
// SystemIncludesCache should be sorted so that binary search can be used
std::sort(this->SystemIncludesCache[key].begin(),
this->SystemIncludesCache[key].end());
}
}
std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
cmSourceFile const* sf) const
{

View File

@@ -912,6 +912,8 @@ public:
std::vector<std::string> GetGeneratedISPCObjects(
std::string const& config) const;
void AddSystemIncludeDirectory(std::string const& inc,
std::string const& lang);
bool AddHeaderSetVerification();
std::string GenerateHeaderSetVerificationFile(
cmSourceFile& source, const std::string& dir,

View File

@@ -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)

View File

@@ -574,7 +574,31 @@ bool cmQtAutoGenInitializer::InitCustomTargets()
// Add autogen include directory to the origin target INCLUDE_DIRECTORIES
if (this->MocOrUicEnabled() || (this->Rcc.Enabled && this->MultiConfig)) {
this->GenTarget->AddIncludeDirectory(this->Dir.IncludeGenExp, true);
auto addBefore = false;
auto const& value =
this->GenTarget->GetProperty("AUTOGEN_USE_SYSTEM_INCLUDE");
if (value.IsSet()) {
if (cmIsOn(value)) {
this->GenTarget->AddSystemIncludeDirectory(this->Dir.IncludeGenExp,
"CXX");
} else {
addBefore = true;
}
} else {
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);
}
// Scan files

View File

@@ -550,6 +550,7 @@ TargetProperty const StaticTargetProperties[] = {
// -- Autogen
{ "AUTOGEN_ORIGIN_DEPENDS"_s, IC::CanCompileSources },
{ "AUTOGEN_PARALLEL"_s, IC::CanCompileSources },
{ "AUTOGEN_USE_SYSTEM_INCLUDE"_s, IC::CanCompileSources },
// -- moc
{ "AUTOMOC_DEPEND_FILTERS"_s, IC::CanCompileSources },
// -- C++

View File

@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.26)
project(GlobalAutogenSystemUseInclude)
include("../AutogenCoreTest.cmake")
block()
set(test_autogen_use_system_include ON)
set(CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE ${test_autogen_use_system_include})
add_executable(autogen_test_on main.cpp)
get_target_property(target_autogen_use_system_include autogen_test_on AUTOGEN_USE_SYSTEM_INCLUDE)
if(NOT ${CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE} STREQUAL ${target_autogen_use_system_include})
message(FATAL_ERROR "CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE not set")
endif()
endblock()
block()
set(test_autogen_use_system_include OFF)
set(CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE ${test_autogen_use_system_include})
add_executable(autogen_test_off main.cpp)
get_target_property(target_autogen_use_system_include autogen_test_off AUTOGEN_USE_SYSTEM_INCLUDE)
if(NOT ${CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE} STREQUAL ${target_autogen_use_system_include})
message(FATAL_ERROR "CMAKE_AUTOGEN_USE_SYSTEM_INCLUDE not set")
endif()
endblock()

View File

@@ -0,0 +1,4 @@
int main()
{
return 0;
}

View File

@@ -3,6 +3,7 @@ ADD_AUTOGEN_TEST(AutogenOriginDependsOff autogenOriginDependsOff)
ADD_AUTOGEN_TEST(AutogenOriginDependsOn)
ADD_AUTOGEN_TEST(AutogenTargetDepends)
ADD_AUTOGEN_TEST(Complex QtAutogen)
ADD_AUTOGEN_TEST(GlobalAutogenSystemUseInclude)
ADD_AUTOGEN_TEST(GlobalAutogenTarget)
ADD_AUTOGEN_TEST(GlobalAutogenExecutable)
ADD_AUTOGEN_TEST(LowMinimumVersion lowMinimumVersion)

View File

@@ -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)

View File

@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/AutogenUseSystemIncludeCommon.cmake")
set_target_properties(dummy PROPERTIES AUTOGEN_USE_SYSTEM_INCLUDE OFF)

View File

@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/AutogenUseSystemIncludeCommon.cmake")
set_target_properties(dummy PROPERTIES AUTOGEN_USE_SYSTEM_INCLUDE ON)

View File

@@ -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)

View File

@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/CMP0151-common.cmake")

View File

@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/CMP0151-common.cmake")

View File

@@ -0,0 +1,13 @@
enable_language(CXX)
set(info "")
foreach(var
CMAKE_INCLUDE_FLAG_CXX
CMAKE_INCLUDE_SYSTEM_FLAG_CXX
)
if(DEFINED ${var})
string(APPEND info "set(${var} \"${${var}}\")\n")
endif()
endforeach()
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/info.cmake" "${info}")

View File

@@ -22,4 +22,70 @@ if (DEFINED with_qt_version)
set(RunCMake_TEST_NO_CLEAN 1)
run_cmake_command(MocPredefs-build ${CMAKE_COMMAND} --build . --config Debug)
endblock()
# Detect information from the toolchain:
# - CMAKE_INCLUDE_FLAG_CXX
# - CMAKE_INCLUDE_SYSTEM_FLAG_CXX
run_cmake(Inspect)
include("${RunCMake_BINARY_DIR}/Inspect-build/info.cmake")
if(CMAKE_INCLUDE_SYSTEM_FLAG_CXX)
if(RunCMake_GENERATOR MATCHES "Visual Studio")
string(REGEX REPLACE "^-" "/" test_expect_stdout "${CMAKE_INCLUDE_SYSTEM_FLAG_CXX}")
else()
set(test_expect_stdout "-*${CMAKE_INCLUDE_SYSTEM_FLAG_CXX}")
endif()
string(APPEND test_expect_stdout " *(\"[^\"]*|([^ ]|\\ )*)[\\/]dummy_autogen[\\/]include")
if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
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_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}")
run_cmake_command(AutogenUseSystemIncludeOn ${CMAKE_COMMAND} --build . --config Debug --verbose)
endblock()
endif()
if(CMAKE_INCLUDE_FLAG_CXX)
if(RunCMake_GENERATOR MATCHES "Visual Studio")
string(REGEX REPLACE "^-" "/" test_expect_stdout "${CMAKE_INCLUDE_FLAG_CXX}")
else()
set(test_expect_stdout "-*${CMAKE_INCLUDE_FLAG_CXX}")
endif()
string(APPEND test_expect_stdout " *(\"[^\"]*|([^ ]|\\ )*)[\\/]dummy_autogen[\\/]include")
if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
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_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}")
run_cmake_command(AutogenUseSystemIncludeOff ${CMAKE_COMMAND} --build . --config Debug --verbose)
endblock()
endif()
endif ()

View File

@@ -47,6 +47,8 @@ function(run_cmake test)
elseif(EXISTS ${top_src}/${test}-${o}.txt)
file(READ ${top_src}/${test}-${o}.txt expect_${o})
string(REGEX REPLACE "\n+$" "" expect_${o} "${expect_${o}}")
elseif(DEFINED RunCMake_TEST_EXPECT_${o})
string(REGEX REPLACE "\n+$" "" expect_${o} "${RunCMake_TEST_EXPECT_${o}}")
else()
unset(expect_${o})
endif()