c++modules: create synthetic targets only when needed

When the target has only private c++ modules, do not create a synthetic
target.
This commit is contained in:
Marc Chevrier
2026-04-06 16:52:18 -04:00
committed by Brad King
parent 9add46e2fb
commit 3e2765b354
9 changed files with 59 additions and 1 deletions
+5 -1
View File
@@ -5392,7 +5392,7 @@ bool cmGeneratorTarget::DiscoverSyntheticTargets(
for (auto const& entry : impl.Libraries) {
auto const* gt = entry.Target;
if (!gt || !gt->HaveCxx20ModuleSources()) {
if (!gt || !gt->HaveInterfaceCxx20ModuleSources()) {
continue;
}
@@ -5882,6 +5882,10 @@ bool cmGeneratorTarget::HaveFortranSources() const
return have_direct || have_via_target_objects;
}
bool cmGeneratorTarget::HaveInterfaceCxx20ModuleSources() const
{
return !this->GetInterfaceFileSets(cm::FileSetMetadata::CXX_MODULES).empty();
}
bool cmGeneratorTarget::HaveCxx20ModuleSources() const
{
return !this->GetFileSets(cm::FileSetMetadata::CXX_MODULES).empty() ||
+1
View File
@@ -1546,6 +1546,7 @@ public:
* This will inspect the target itself to see if C++20 module
* support is expected to work based on its sources.
*/
bool HaveInterfaceCxx20ModuleSources() const;
bool HaveCxx20ModuleSources() const;
enum class Cxx20SupportLevel
@@ -174,6 +174,9 @@ if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
run_cxx_module_test(non-trivial-collation-order-randomized)
run_cxx_module_test(duplicate)
set(RunCMake_CXXModules_NO_TEST 1)
if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION)
run_cxx_module_test(private-cxx-modules)
endif ()
run_cxx_module_test(imp-from-object)
run_cxx_module_test(circular)
run_cxx_module_test(try-compile)
@@ -0,0 +1 @@
CMake Error: Unable to use module 'importable' as it is 'PRIVATE' and therefore not accessible outside of its owning target\.
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_library CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
include(GenerateExportHeader)
add_library(library)
generate_export_header(library)
target_sources(library
PUBLIC
FILE_SET HEADERS
BASE_DIRS
"${CMAKE_CURRENT_BINARY_DIR}"
FILES
"${CMAKE_CURRENT_BINARY_DIR}/library_export.h"
PRIVATE
FILE_SET CXX_MODULES
BASE_DIRS
"${CMAKE_CURRENT_SOURCE_DIR}"
FILES
importable.ixx
PRIVATE
importable.cxx)
target_compile_features(library PUBLIC cxx_std_20)
add_executable(exe)
target_link_libraries(exe PRIVATE library)
target_sources(exe
PRIVATE
main.cxx)
@@ -0,0 +1,6 @@
module importable;
int from_import()
{
return 0;
}
@@ -0,0 +1,5 @@
export module importable;
#include "library_export.h"
export LIBRARY_EXPORT int from_import();
@@ -0,0 +1,6 @@
import importable;
int main(int argc, char* argv[])
{
return from_import();
}