cxxmodules: scan C++ sources for imports by default

Existing projects are not using C++ modules in their sources,
so introduce policy CMP0155 to enable scanning by default.
This commit is contained in:
Ben Boeckel
2023-10-02 10:17:31 -04:00
parent 3cddd11649
commit 437280b127
50 changed files with 129 additions and 41 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ following queries. The first query that provides a yes/no answer is used.
- If the :prop_tgt:`CXX_SCAN_FOR_MODULES` target property is set, its value
will be used. Set the :variable:`CMAKE_CXX_SCAN_FOR_MODULES` variable
to initialize this property on all targets as they are created.
- Otherwise the source will be scanned.
- Otherwise, the source file will be scanned. See policy :policy:`CMP0155`.
Compiler Support
================
+1
View File
@@ -57,6 +57,7 @@ Policies Introduced by CMake 3.28
.. toctree::
:maxdepth: 1
CMP0155: C++ sources in targets with at least C++20 are scanned for imports. </policy/CMP0155>
CMP0154: Generated files are private by default in targets using file sets. </policy/CMP0154>
CMP0153: The exec_program command should not be called. </policy/CMP0153>
CMP0152: file(REAL_PATH) resolves symlinks before collapsing ../ components. </policy/CMP0152>
+26
View File
@@ -0,0 +1,26 @@
CMP0155
-------
.. versionadded:: 3.28
C++ sources in targets with at least C++20 are scanned for imports.
CMake 3.27 and below assume that C++ sources do not ``import`` modules.
CMake 3.28 and above prefer to assume that C++ sources in targets using C++20
or higher might ``import`` modules, and must be scanned before compiling,
unless explicitly disabled. This policy provides compatibility for projects
that use C++20 or higher, without modules, that have not been updated to turn
off scanning, e.g., via the :variable:`CMAKE_CXX_SCAN_FOR_MODULES` variable.
See the :manual:`cmake-cxxmodules(7)` manual for more details on C++ module
support.
The ``OLD`` behavior for this policy is to assume that C++ 20 and newer
sources do not import modules. The ``NEW`` behavior for this policy is to
assume that C++ 20 and newer files may import modules, and need to be scanned.
This policy was introduced in CMake version 3.28. 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
+7
View File
@@ -871,6 +871,13 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
? "NEW"
: "OLD");
/* Set the appropriate policy information for C++ module support */
fprintf(fout, "cmake_policy(SET CMP0155 %s)\n",
this->Makefile->GetPolicyStatus(cmPolicies::CMP0155) ==
cmPolicies::NEW
? "NEW"
: "OLD");
// Workaround for -Wl,-headerpad_max_install_names issue until we can avoid
// adding that flag in the platform and compiler language files
fprintf(fout,
+16 -1
View File
@@ -9261,7 +9261,22 @@ bool cmGeneratorTarget::NeedDyndepForSource(std::string const& lang,
if (tgtProp.IsSet()) {
return tgtProp.IsOn();
}
return true;
bool policyAnswer = false;
switch (this->GetPolicyStatusCMP0155()) {
case cmPolicies::WARN:
case cmPolicies::OLD:
// The OLD behavior is to not scan the source.
policyAnswer = false;
break;
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::NEW:
// The NEW behavior is to scan the source.
policyAnswer = true;
break;
}
return policyAnswer;
}
void cmGeneratorTarget::BuildFileSetInfoCache(std::string const& config) const
+6 -1
View File
@@ -469,6 +469,10 @@ class cmMakefile;
SELECT( \
POLICY, CMP0154, \
"Generated files are private by default in targets using file sets.", 3, \
28, 0, cmPolicies::WARN) \
SELECT( \
POLICY, CMP0155, \
"C++ sources in targets with at least C++20 are scanned for imports", 3, \
28, 0, cmPolicies::WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
@@ -508,7 +512,8 @@ class cmMakefile;
F(CMP0119) \
F(CMP0131) \
F(CMP0142) \
F(CMP0154)
F(CMP0154) \
F(CMP0155)
#define CM_FOR_EACH_CUSTOM_COMMAND_POLICY(F) \
F(CMP0116) \
@@ -0,0 +1 @@
1
@@ -0,0 +1,8 @@
(CMake Error in CMakeLists.txt:
( The target named "cmp0155-new" has C\+\+ sources that use modules but the
compiler does not provide a way to discover the import graph dependencies
| The target named "cmp0155-new" contains C\+\+ sources that use modules which
is not supported by the generator
)
)*
CMake Generate step failed. Build files cannot be regenerated correctly.
@@ -0,0 +1,11 @@
enable_language(CXX)
unset(CMAKE_CXX_SCANDEP_SOURCE)
cmake_policy(SET CMP0155 NEW)
add_executable(cmp0155-new
sources/module-use.cxx)
set_target_properties(cmp0155-new
PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON)
@@ -0,0 +1,11 @@
enable_language(CXX)
unset(CMAKE_CXX_SCANDEP_SOURCE)
cmake_policy(SET CMP0155 OLD)
add_executable(cmp0155-old
sources/module-use.cxx)
set_target_properties(cmp0155-old
PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON)
@@ -17,6 +17,8 @@ if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
run_cmake(NoScanningSourceFileProperty)
run_cmake(NoScanningTargetProperty)
run_cmake(CMP0155-OLD)
run_cmake(CMP0155-NEW)
endif ()
if (RunCMake_GENERATOR MATCHES "Ninja")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_circular CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_deep_chain CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_duplicate CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_bmi_and_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_bmi_and_interfaces REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_bmi_and_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_bmi_and_interfaces REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_compile_commands CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_include_directories CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_include_directories CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "ac01f462-0f5f-432a-86aa-acef252918a6")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_interfaces REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_interfaces REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_interfaces_no_properties CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_interfaces_no_properties REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_interfaces_no_properties REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_usage CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_usage REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_export_usage CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library NONE)
find_package(export_usage REQUIRED)
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_generated CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_import_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_install_bmi_and_interfaces CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_install_bmi CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_internal_partitions CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_objlib CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_partitions CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_public_req_private CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.26)
cmake_minimum_required(VERSION 3.26...3.28)
project(req_private_other_target CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.27)
cmake_minimum_required(VERSION 3.27...3.28)
project(cxx_modules_same_src_name CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(scan_properties CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_simple CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_try_compile CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.24)
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_try_run CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
@@ -38,6 +38,7 @@
\* CMP0131
\* CMP0142
\* CMP0154
\* CMP0155
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)