diff options
author | Ben Boeckel <ben.boeckel@kitware.com> | 2023-05-14 21:24:02 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2023-05-18 12:34:31 (GMT) |
commit | 18f87c87f86473dd2106b6d8ab7acc9def99b9b1 (patch) | |
tree | aab3ae4f9f7487eabccd2ffaa679e8ff18669c8f /Source/cmCxxModuleMapper.cxx | |
parent | 56f7d6f827290dd7d4d5c6edf0fe12568d4a819e (diff) | |
download | CMake-18f87c87f86473dd2106b6d8ab7acc9def99b9b1.zip CMake-18f87c87f86473dd2106b6d8ab7acc9def99b9b1.tar.gz CMake-18f87c87f86473dd2106b6d8ab7acc9def99b9b1.tar.bz2 |
cmCxxModuleMapper: track whether modules are private or not
This allows collation to give a useful error message when it finds usage
of a private module rather than collation just not informing the
compilation and the compiler erroring out about not being able to import
unknown modules (which exists, but it was not told about due to
visibility).
Fixes: #24652
Diffstat (limited to 'Source/cmCxxModuleMapper.cxx')
-rw-r--r-- | Source/cmCxxModuleMapper.cxx | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/Source/cmCxxModuleMapper.cxx b/Source/cmCxxModuleMapper.cxx index e320f54..e836a2a 100644 --- a/Source/cmCxxModuleMapper.cxx +++ b/Source/cmCxxModuleMapper.cxx @@ -61,13 +61,15 @@ std::string const& CxxBmiLocation::Location() const return empty; } -cm::optional<std::string> CxxModuleLocations::BmiGeneratorPathForModule( +CxxBmiLocation CxxModuleLocations::BmiGeneratorPathForModule( std::string const& logical_name) const { - if (auto l = this->BmiLocationForModule(logical_name)) { - return this->PathForGenerator(std::move(*l)); + auto bmi_loc = this->BmiLocationForModule(logical_name); + if (bmi_loc.IsKnown() && !bmi_loc.IsPrivate()) { + bmi_loc = + CxxBmiLocation::Known(this->PathForGenerator(bmi_loc.Location())); } - return {}; + return bmi_loc; } namespace { @@ -86,18 +88,21 @@ std::string CxxModuleMapContentClang(CxxModuleLocations const& loc, // A series of flags which tell the compiler where to look for modules. for (auto const& p : obj.Provides) { - if (auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName)) { + auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName); + if (bmi_loc.IsKnown()) { // Force the TU to be considered a C++ module source file regardless of // extension. mm << "-x c++-module\n"; - mm << "-fmodule-output=" << *bmi_loc << '\n'; + mm << "-fmodule-output=" << bmi_loc.Location() << '\n'; break; } } for (auto const& r : obj.Requires) { - if (auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName)) { - mm << "-fmodule-file=" << r.LogicalName << "=" << *bmi_loc << '\n'; + auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName); + if (bmi_loc.IsKnown()) { + mm << "-fmodule-file=" << r.LogicalName << "=" << bmi_loc.Location() + << '\n'; } } @@ -120,13 +125,15 @@ std::string CxxModuleMapContentGcc(CxxModuleLocations const& loc, mm << "$root " << loc.RootDirectory << "\n"; for (auto const& p : obj.Provides) { - if (auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName)) { - mm << p.LogicalName << ' ' << *bmi_loc << '\n'; + auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName); + if (bmi_loc.IsKnown()) { + mm << p.LogicalName << ' ' << bmi_loc.Location() << '\n'; } } for (auto const& r : obj.Requires) { - if (auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName)) { - mm << r.LogicalName << ' ' << *bmi_loc << '\n'; + auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName); + if (bmi_loc.IsKnown()) { + mm << r.LogicalName << ' ' << bmi_loc.Location() << '\n'; } } @@ -167,8 +174,9 @@ std::string CxxModuleMapContentMsvc(CxxModuleLocations const& loc, mm << "-internalPartition\n"; } - if (auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName)) { - mm << "-ifcOutput " << *bmi_loc << '\n'; + auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName); + if (bmi_loc.IsKnown()) { + mm << "-ifcOutput " << bmi_loc.Location() << '\n'; } } @@ -176,10 +184,11 @@ std::string CxxModuleMapContentMsvc(CxxModuleLocations const& loc, std::set<std::string> transitive_usage_names; for (auto const& r : obj.Requires) { - if (auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName)) { + auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName); + if (bmi_loc.IsKnown()) { auto flag = flag_for_method(r.Method); - mm << flag << ' ' << r.LogicalName << '=' << *bmi_loc << "\n"; + mm << flag << ' ' << r.LogicalName << '=' << bmi_loc.Location() << "\n"; transitive_usage_directs.insert(r.LogicalName); // Insert transitive usages. @@ -281,18 +290,28 @@ std::set<std::string> CxxModuleUsageSeed( for (cmScanDepInfo const& object : objects) { // Add references for each of the provided modules. for (auto const& p : object.Provides) { - if (auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName)) { + auto bmi_loc = loc.BmiGeneratorPathForModule(p.LogicalName); + if (bmi_loc.IsKnown()) { // XXX(cxx-modules): How to support header units? - usages.AddReference(p.LogicalName, *bmi_loc, LookupMethod::ByName); + usages.AddReference(p.LogicalName, bmi_loc.Location(), + LookupMethod::ByName); } } // For each requires, pull in what is required. for (auto const& r : object.Requires) { - // Find transitive usages. - auto transitive_usages = usages.Usage.find(r.LogicalName); // Find the required name in the current target. auto bmi_loc = loc.BmiGeneratorPathForModule(r.LogicalName); + if (bmi_loc.IsPrivate()) { + cmSystemTools::Error( + cmStrCat("Unable to use module '", r.LogicalName, + "' as it is 'PRIVATE' and therefore not accessible outside " + "of its owning target.")); + continue; + } + + // Find transitive usages. + auto transitive_usages = usages.Usage.find(r.LogicalName); for (auto const& p : object.Provides) { auto& this_usages = usages.Usage[p.LogicalName]; @@ -304,14 +323,14 @@ std::set<std::string> CxxModuleUsageSeed( if (transitive_usages != usages.Usage.end()) { this_usages.insert(transitive_usages->second.begin(), transitive_usages->second.end()); - } else if (bmi_loc) { + } else if (bmi_loc.IsKnown()) { // Mark that we need to update transitive usages later. internal_usages[p.LogicalName].insert(r.LogicalName); } } - if (bmi_loc) { - usages.AddReference(r.LogicalName, *bmi_loc, r.Method); + if (bmi_loc.IsKnown()) { + usages.AddReference(r.LogicalName, bmi_loc.Location(), r.Method); } } } |