1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-19 02:17:27 +08:00

target_sources: Improve error message for CXX_MODULES on INTERFACE libraries

We support non-compiled `SOURCES` on `INTERFACE` libraries, and also
support `CXX_MODULES` on *imported* `INTERFACE` libraries (via synthetic
targets that compile module interface units).  However, we do not
support `CXX_MODULES` on non-imported `INTERFACE` libraries because
there is no place to hold module interface unit's object files for their
module initializers.  Previously this was not explicitly rejected, and
so was diagnosed only by "CMake Internal Error" messages due to
assumption violations in the implementation.

Fixes: #26524
Co-authored-by: Ben Boeckel <ben.boeckel@kitware.com>
This commit is contained in:
Brad King
2024-12-11 11:53:26 -05:00
parent cd179e7560
commit 854eba0c53
11 changed files with 61 additions and 0 deletions

View File

@@ -265,6 +265,18 @@ bool TargetSourcesImpl::HandleOneFileSet(
return false;
}
if (cmFileSetVisibilityIsForSelf(visibility) &&
this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
!this->Target->IsImported()) {
if (type == "CXX_MODULES"_s) {
this->SetError(R"(File set TYPE "CXX_MODULES" may not have "PUBLIC" )"
R"(or "PRIVATE" visibility on INTERFACE libraries.)");
return false;
}
}
// FIXME(https://wg21.link/P3470): This condition can go
// away when interface-only module units are a thing.
if (cmFileSetVisibilityIsForInterface(visibility) &&
!cmFileSetVisibilityIsForSelf(visibility) &&
!this->Target->IsImported()) {

View File

@@ -0,0 +1,5 @@
CMake Error at FileSetModulesInterfaceOnInterface.cmake:[0-9]+ \(target_sources\):
target_sources File set TYPE "CXX_MODULES" may not have "INTERFACE"
visibility
Call Stack \(most recent call first\):
CMakeLists.txt:[0-9]+ \(include\)

View File

@@ -0,0 +1,8 @@
add_library(module INTERFACE)
target_sources(module
INTERFACE
FILE_SET fs TYPE CXX_MODULES FILES
sources/module.cxx)
target_compile_features(module
INTERFACE
cxx_std_20)

View File

@@ -0,0 +1,5 @@
^CMake Error at FileSetModulesPrivateOnInterface\.cmake:[0-9]+ \(target_sources\):
target_sources File set TYPE "CXX_MODULES" may not have "PUBLIC" or
"PRIVATE" visibility on INTERFACE libraries\.
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -0,0 +1,11 @@
enable_language(CXX)
set(CMAKE_CXX_SCANDEP_SOURCE "")
add_library(module INTERFACE)
target_sources(module
PRIVATE
FILE_SET fs TYPE CXX_MODULES FILES
sources/module.cxx)
target_compile_features(module
INTERFACE
cxx_std_20)

View File

@@ -0,0 +1,5 @@
^CMake Error at FileSetModulesPublicOnInterface\.cmake:[0-9]+ \(target_sources\):
target_sources File set TYPE "CXX_MODULES" may not have "PUBLIC" or
"PRIVATE" visibility on INTERFACE libraries\.
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -0,0 +1,11 @@
enable_language(CXX)
set(CMAKE_CXX_SCANDEP_SOURCE "")
add_library(module INTERFACE)
target_sources(module
PUBLIC
FILE_SET fs TYPE CXX_MODULES FILES
sources/module.cxx)
target_compile_features(module
INTERFACE
cxx_std_20)

View File

@@ -72,6 +72,7 @@ set(scopes
Private
Public)
set(target_types
Interface
Static
)
foreach (fileset_type IN LISTS fileset_types)