Merge topic 'cxx-module-metadata'

fa10dc6c22 Experimental/CXXModules: Implement EcoStd Module Metadata parser

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !11422
This commit is contained in:
Brad King
2025-11-19 13:53:13 +00:00
committed by Kitware Robot
25 changed files with 805 additions and 466 deletions

View File

@@ -165,6 +165,8 @@ add_library(
cmCustomCommandTypes.h
cmCxxModuleMapper.cxx
cmCxxModuleMapper.h
cmCxxModuleMetadata.cxx
cmCxxModuleMetadata.h
cmCxxModuleUsageEffects.cxx
cmCxxModuleUsageEffects.h
cmDefinitions.cxx

View File

@@ -0,0 +1,460 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCxxModuleMetadata.h"
#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <cmext/string_view>
#include <cm3p/json/value.h>
#include <cm3p/json/writer.h>
#include "cmsys/FStream.hxx"
#include "cmFileSet.h"
#include "cmJSONState.h"
#include "cmListFileCache.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
namespace {
bool JsonIsStringArray(Json::Value const& v)
{
return v.isArray() &&
std::all_of(v.begin(), v.end(),
[](Json::Value const& it) { return it.isString(); });
}
bool ParsePreprocessorDefine(Json::Value& dval,
cmCxxModuleMetadata::PreprocessorDefineData& out,
cmJSONState* state)
{
if (!dval.isObject()) {
state->AddErrorAtValue("each entry in 'definitions' must be an object",
&dval);
return false;
}
if (!dval.isMember("name") || !dval["name"].isString() ||
dval["name"].asString().empty()) {
state->AddErrorAtValue(
"preprocessor definition requires a non-empty 'name'", &dval["name"]);
return false;
}
out.Name = dval["name"].asString();
if (dval.isMember("value")) {
if (dval["value"].isString()) {
out.Value = dval["value"].asString();
} else if (!dval["value"].isNull()) {
state->AddErrorAtValue(
"'value' in preprocessor definition must be string or null",
&dval["value"]);
return false;
}
}
if (dval.isMember("undef")) {
if (!dval["undef"].isBool()) {
state->AddErrorAtValue(
"'undef' in preprocessor definition must be boolean", &dval["undef"]);
return false;
}
out.Undef = dval["undef"].asBool();
}
if (dval.isMember("vendor")) {
out.Vendor = std::move(dval["vendor"]);
}
return true;
}
bool ParseLocalArguments(Json::Value& lav,
cmCxxModuleMetadata::LocalArgumentsData& out,
cmJSONState* state)
{
if (!lav.isObject()) {
state->AddErrorAtValue("'local-arguments' must be an object", &lav);
return false;
}
if (lav.isMember("include-directories")) {
if (!JsonIsStringArray(lav["include-directories"])) {
state->AddErrorAtValue(
"'include-directories' must be an array of strings",
&lav["include-directories"]);
return false;
}
for (auto const& s : lav["include-directories"]) {
out.IncludeDirectories.push_back(s.asString());
}
}
if (lav.isMember("system-include-directories")) {
if (!JsonIsStringArray(lav["system-include-directories"])) {
state->AddErrorAtValue(
"'system-include-directories' must be an array of strings",
&lav["system-include-directories"]);
return false;
}
for (auto const& s : lav["system-include-directories"]) {
out.SystemIncludeDirectories.push_back(s.asString());
}
}
if (lav.isMember("definitions")) {
if (!lav["definitions"].isArray()) {
state->AddErrorAtValue("'definitions' must be an array",
&lav["definitions"]);
return false;
}
for (Json::Value& dval : lav["definitions"]) {
out.Definitions.emplace_back();
if (!ParsePreprocessorDefine(dval, out.Definitions.back(), state)) {
return false;
}
}
}
if (lav.isMember("vendor")) {
out.Vendor = std::move(lav["vendor"]);
}
return true;
}
bool ParseModule(Json::Value& mval, cmCxxModuleMetadata::ModuleData& mod,
cmJSONState* state)
{
if (!mval.isObject()) {
state->AddErrorAtValue("each entry in 'modules' must be an object", &mval);
return false;
}
if (!mval.isMember("logical-name") || !mval["logical-name"].isString() ||
mval["logical-name"].asString().empty()) {
state->AddErrorAtValue(
"module entries require a non-empty 'logical-name' string",
&mval["logical-name"]);
return false;
}
mod.LogicalName = mval["logical-name"].asString();
if (!mval.isMember("source-path") || !mval["source-path"].isString() ||
mval["source-path"].asString().empty()) {
state->AddErrorAtValue(
"module entries require a non-empty 'source-path' string",
&mval["source-path"]);
return false;
}
mod.SourcePath = mval["source-path"].asString();
if (mval.isMember("is-interface")) {
if (!mval["is-interface"].isBool()) {
state->AddErrorAtValue("'is-interface' must be boolean",
&mval["is-interface"]);
return false;
}
mod.IsInterface = mval["is-interface"].asBool();
} else {
mod.IsInterface = true;
}
if (mval.isMember("is-std-library")) {
if (!mval["is-std-library"].isBool()) {
state->AddErrorAtValue("'is-std-library' must be boolean",
&mval["is-std-library"]);
return false;
}
mod.IsStdLibrary = mval["is-std-library"].asBool();
} else {
mod.IsStdLibrary = false;
}
if (mval.isMember("local-arguments")) {
mod.LocalArguments.emplace();
if (!ParseLocalArguments(mval["local-arguments"], *mod.LocalArguments,
state)) {
return false;
}
}
if (mval.isMember("vendor")) {
mod.Vendor = std::move(mval["vendor"]);
}
return true;
}
bool ParseRoot(Json::Value& root, cmCxxModuleMetadata& meta,
cmJSONState* state)
{
if (!root.isMember("version") || !root["version"].isInt()) {
state->AddErrorAtValue(
"Top-level member 'version' is required and must be an integer", &root);
return false;
}
meta.Version = root["version"].asInt();
if (root.isMember("revision")) {
if (!root["revision"].isInt()) {
state->AddErrorAtValue("'revision' must be an integer",
&root["revision"]);
return false;
}
meta.Revision = root["revision"].asInt();
}
if (root.isMember("modules")) {
if (!root["modules"].isArray()) {
state->AddErrorAtValue("'modules' must be an array", &root["modules"]);
return false;
}
for (Json::Value& mval : root["modules"]) {
meta.Modules.emplace_back();
if (!ParseModule(mval, meta.Modules.back(), state)) {
return false;
}
}
}
for (std::string& key : root.getMemberNames()) {
if (key == "version" || key == "revision" || key == "modules") {
continue;
}
meta.Extensions.emplace(std::move(key), std::move(root[key]));
}
return true;
}
} // namespace
cmCxxModuleMetadata::ParseResult cmCxxModuleMetadata::LoadFromFile(
std::string const& path)
{
ParseResult res;
Json::Value root;
cmJSONState parseState(path, &root);
if (!parseState.errors.empty()) {
res.Error = parseState.GetErrorMessage();
return res;
}
cmCxxModuleMetadata meta;
if (!ParseRoot(root, meta, &parseState)) {
res.Error = parseState.GetErrorMessage();
return res;
}
meta.MetadataFilePath = path;
res.Meta = std::move(meta);
return res;
}
namespace {
Json::Value SerializePreprocessorDefine(
cmCxxModuleMetadata::PreprocessorDefineData const& d)
{
Json::Value dv(Json::objectValue);
dv["name"] = d.Name;
if (d.Value) {
dv["value"] = *d.Value;
} else {
dv["value"] = Json::Value::null;
}
dv["undef"] = d.Undef;
if (d.Vendor) {
dv["vendor"] = *d.Vendor;
}
return dv;
}
Json::Value SerializeLocalArguments(
cmCxxModuleMetadata::LocalArgumentsData const& la)
{
Json::Value lav(Json::objectValue);
if (!la.IncludeDirectories.empty()) {
Json::Value& inc = lav["include-directories"] = Json::arrayValue;
for (auto const& s : la.IncludeDirectories) {
inc.append(s);
}
}
if (!la.SystemIncludeDirectories.empty()) {
Json::Value& sinc = lav["system-include-directories"] = Json::arrayValue;
for (auto const& s : la.SystemIncludeDirectories) {
sinc.append(s);
}
}
if (!la.Definitions.empty()) {
Json::Value& defs = lav["definitions"] = Json::arrayValue;
for (auto const& d : la.Definitions) {
defs.append(SerializePreprocessorDefine(d));
}
}
if (la.Vendor) {
lav["vendor"] = *la.Vendor;
}
return lav;
}
Json::Value SerializeModule(cmCxxModuleMetadata::ModuleData const& m)
{
Json::Value mv(Json::objectValue);
mv["logical-name"] = m.LogicalName;
mv["source-path"] = m.SourcePath;
mv["is-interface"] = m.IsInterface;
mv["is-std-library"] = m.IsStdLibrary;
if (m.LocalArguments) {
mv["local-arguments"] = SerializeLocalArguments(*m.LocalArguments);
}
if (m.Vendor) {
mv["vendor"] = *m.Vendor;
}
return mv;
}
} // namespace
Json::Value cmCxxModuleMetadata::ToJsonValue(cmCxxModuleMetadata const& meta)
{
Json::Value root(Json::objectValue);
root["version"] = meta.Version;
root["revision"] = meta.Revision;
Json::Value& modules = root["modules"] = Json::arrayValue;
for (auto const& m : meta.Modules) {
modules.append(SerializeModule(m));
}
for (auto const& kv : meta.Extensions) {
root[kv.first] = kv.second;
}
return root;
}
cmCxxModuleMetadata::SaveResult cmCxxModuleMetadata::SaveToFile(
std::string const& path, cmCxxModuleMetadata const& meta)
{
SaveResult st;
cmsys::ofstream ofs(path.c_str());
if (!ofs.is_open()) {
st.Error = cmStrCat("Unable to open file for writing: "_s, path);
return st;
}
Json::StreamWriterBuilder wbuilder;
wbuilder["indentation"] = " ";
ofs << Json::writeString(wbuilder, ToJsonValue(meta));
if (!ofs.good()) {
st.Error = cmStrCat("Write failed for file: "_s, path);
return st;
}
st.Ok = true;
return st;
}
void cmCxxModuleMetadata::PopulateTarget(
cmTarget& target, cmCxxModuleMetadata const& meta,
std::vector<std::string> const& configs)
{
std::vector<cm::string_view> allIncludeDirectories;
std::vector<std::string> allCompileDefinitions;
std::set<std::string> baseDirs;
std::string metadataDir =
cmSystemTools::GetFilenamePath(meta.MetadataFilePath);
auto fileSet = target.GetOrCreateFileSet("CXX_MODULES", "CXX_MODULES",
cmFileSetVisibility::Interface);
for (auto const& module : meta.Modules) {
std::string sourcePath = module.SourcePath;
if (!cmSystemTools::FileIsFullPath(sourcePath)) {
sourcePath = cmStrCat(metadataDir, '/', sourcePath);
}
// Module metadata files can reference files in different roots,
// just use the immediate parent directory as a base directory
baseDirs.insert(cmSystemTools::GetFilenamePath(sourcePath));
fileSet.first->AddFileEntry(sourcePath);
if (module.LocalArguments) {
for (auto const& incDir : module.LocalArguments->IncludeDirectories) {
allIncludeDirectories.push_back(incDir);
}
for (auto const& sysIncDir :
module.LocalArguments->SystemIncludeDirectories) {
allIncludeDirectories.push_back(sysIncDir);
}
for (auto const& def : module.LocalArguments->Definitions) {
if (!def.Undef) {
if (def.Value) {
allCompileDefinitions.push_back(
cmStrCat(def.Name, "="_s, *def.Value));
} else {
allCompileDefinitions.push_back(def.Name);
}
}
}
}
}
for (auto const& baseDir : baseDirs) {
fileSet.first->AddDirectoryEntry(baseDir);
}
if (!allIncludeDirectories.empty()) {
target.SetProperty("IMPORTED_CXX_MODULES_INCLUDE_DIRECTORIES",
cmJoin(allIncludeDirectories, ";"));
}
if (!allCompileDefinitions.empty()) {
target.SetProperty("IMPORTED_CXX_MODULES_COMPILE_DEFINITIONS",
cmJoin(allCompileDefinitions, ";"));
}
for (auto const& config : configs) {
std::vector<std::string> moduleList;
for (auto const& module : meta.Modules) {
if (module.IsInterface) {
std::string sourcePath = module.SourcePath;
if (!cmSystemTools::FileIsFullPath(sourcePath)) {
sourcePath = cmStrCat(metadataDir, '/', sourcePath);
}
moduleList.push_back(cmStrCat(module.LogicalName, "="_s, sourcePath));
}
}
if (!moduleList.empty()) {
std::string upperConfig = cmSystemTools::UpperCase(config);
std::string propertyName =
cmStrCat("IMPORTED_CXX_MODULES_"_s, upperConfig);
target.SetProperty(propertyName, cmJoin(moduleList, ";"));
}
}
}

View File

@@ -0,0 +1,84 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <cm/optional>
#include <cm/string_view>
#include <cm3p/json/value.h>
class cmTarget;
class cmCxxModuleMetadata
{
public:
struct PreprocessorDefineData
{
std::string Name;
cm::optional<std::string> Value;
bool Undef = false;
cm::optional<Json::Value> Vendor;
};
struct LocalArgumentsData
{
std::vector<std::string> IncludeDirectories;
std::vector<std::string> SystemIncludeDirectories;
std::vector<PreprocessorDefineData> Definitions;
cm::optional<Json::Value> Vendor;
};
struct ModuleData
{
std::string LogicalName;
std::string SourcePath;
bool IsInterface = true;
bool IsStdLibrary = false;
cm::optional<LocalArgumentsData> LocalArguments;
cm::optional<Json::Value> Vendor;
};
int Version = 0;
int Revision = 0;
std::vector<ModuleData> Modules;
std::string MetadataFilePath;
std::unordered_map<std::string, Json::Value> Extensions;
struct ParseResult;
struct SaveResult
{
bool Ok = false;
std::string Error;
explicit operator bool() const { return Ok; }
};
static ParseResult LoadFromFile(std::string const& path);
static Json::Value ToJsonValue(cmCxxModuleMetadata const& meta);
static SaveResult SaveToFile(std::string const& path,
cmCxxModuleMetadata const& meta);
static void PopulateTarget(cmTarget& target, cmCxxModuleMetadata const& meta,
std::vector<std::string> const& configs);
static void PopulateTarget(cmTarget& target, cmCxxModuleMetadata const& meta,
cm::string_view config)
{
PopulateTarget(target, meta,
std::vector<std::string>{ std::string(config) });
}
static void PopulateTarget(cmTarget& target, cmCxxModuleMetadata const& meta)
{
PopulateTarget(target, meta, "NOCONFIG");
}
};
struct cmCxxModuleMetadata::ParseResult
{
cm::optional<cmCxxModuleMetadata> Meta;
std::string Error;
explicit operator bool() const { return static_cast<bool>(Meta); }
};

View File

@@ -22,6 +22,7 @@
#include "cmAlgorithms.h"
#include "cmComputeLinkInformation.h" // IWYU pragma: keep
#include "cmCryptoHash.h"
#include "cmCxxModuleMetadata.h"
#include "cmCxxModuleUsageEffects.h"
#include "cmExperimental.h"
#include "cmFileSet.h"
@@ -5126,6 +5127,70 @@ bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
return cm::contains(this->LinkImplicitNullProperties, p);
}
namespace {
bool CreateCxxStdlibTarget(cmMakefile* makefile, cmLocalGenerator* lg,
std::string const& targetName,
std::string const& cxxTargetName,
std::string const& stdLevel,
std::vector<std::string> const& configs)
{
#ifndef CMAKE_BOOTSTRAP
static cm::optional<cmCxxModuleMetadata> metadata;
// Load metadata only when we need to create a target
if (!metadata) {
auto errorMessage =
makefile->GetDefinition("CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE");
if (!errorMessage.IsEmpty()) {
makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat(R"(The "CXX_MODULE_STD" property on target ")", targetName,
"\" requires toolchain support, but it was not provided. "
"Reason:\n ",
*errorMessage));
return false;
}
auto metadataPath =
makefile->GetDefinition("CMAKE_CXX_STDLIB_MODULES_JSON");
if (metadataPath.IsEmpty()) {
makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat(
R"("The "CXX_MODULE_STD" property on target ")", targetName,
"\" requires CMAKE_CXX_STDLIB_MODULES_JSON be set, but it was not "
"provided by the toolchain."));
return false;
}
auto parseResult = cmCxxModuleMetadata::LoadFromFile(*metadataPath);
if (!parseResult) {
makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("Failed to load C++ standard library modules metadata "
"from \"",
*metadataPath, "\": ", parseResult.Error));
return false;
}
metadata = std::move(*parseResult.Meta);
}
auto* stdlibTgt = makefile->AddImportedTarget(
cxxTargetName, cmStateEnums::INTERFACE_LIBRARY, true);
cmCxxModuleMetadata::PopulateTarget(*stdlibTgt, *metadata, configs);
stdlibTgt->AppendProperty("IMPORTED_CXX_MODULES_COMPILE_FEATURES",
cmStrCat("cxx_std_", stdLevel));
lg->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(stdlibTgt, lg));
#endif // CMAKE_BOOTSTRAP
return true;
}
} // namespace
bool cmGeneratorTarget::ApplyCXXStdTargets()
{
cmStandardLevelResolver standardResolver(this->Makefile);
@@ -5207,35 +5272,33 @@ bool cmGeneratorTarget::ApplyCXXStdTargets()
auto const stdLevel =
standardResolver.GetLevelString("CXX", *explicitLevel);
auto const targetName = cmStrCat("__CMAKE::CXX", stdLevel);
if (!this->Makefile->FindTargetToUse(targetName)) {
auto basicReason = this->Makefile->GetDefinition(cmStrCat(
"CMAKE_CXX", stdLevel, "_COMPILER_IMPORT_STD_NOT_FOUND_MESSAGE"));
std::string reason;
if (!basicReason.IsEmpty()) {
reason = cmStrCat(" Reason:\n ", basicReason);
}
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat(R"(The "CXX_MODULE_STD" property on the target ")",
this->GetName(), "\" requires that the \"", targetName,
"\" target exist, but it was not provided by the toolchain.",
reason));
break;
}
auto const cxxTargetName = cmStrCat("__CMAKE::CXX", stdLevel);
// Check the experimental feature here as well. A toolchain may have
// provided the target and skipped the check in the toolchain preparation
// logic.
if (!cmExperimental::HasSupportEnabled(
*this->Makefile, cmExperimental::Feature::CxxImportStd)) {
break;
// Create the __CMAKE::CXX## IMPORTED interface target if it doesn't
// already exist
if (!this->Makefile->FindTargetToUse(cxxTargetName) &&
!CreateCxxStdlibTarget(this->Makefile, this->LocalGenerator,
this->GetName(), cxxTargetName, stdLevel,
configs)) {
return false;
}
this->Target->AppendProperty(
"LINK_LIBRARIES",
cmStrCat("$<BUILD_LOCAL_INTERFACE:$<$<CONFIG:", config, ">:", targetName,
">>"));
cmStrCat("$<BUILD_LOCAL_INTERFACE:$<$<CONFIG:", config,
">:", cxxTargetName, ">>"));
}
// Check the experimental feature here. A toolchain may have
// skipped the check in the toolchain preparation logic.
if (!cmExperimental::HasSupportEnabled(
*this->Makefile, cmExperimental::Feature::CxxImportStd)) {
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
"Experimental `import std` support not enabled when detecting "
"toolchain; it must be set before `CXX` is enabled (usually a "
"`project()` call).");
return false;
}
return true;
@@ -5335,7 +5398,9 @@ bool cmGeneratorTarget::DiscoverSyntheticTargets(cmSyntheticTargetCache& cache,
}
// See `cmGlobalGenerator::ApplyCXXStdTargets` in
// `cmGlobalGenerator::Compute` for non-synthetic target resolutions.
gtp->ApplyCXXStdTargets();
if (!gtp->ApplyCXXStdTargets()) {
return false;
}
gtp->DiscoverSyntheticTargets(cache, config);

View File

@@ -1787,7 +1787,17 @@ void cmGlobalGenerator::ComputeTargetOrder(cmGeneratorTarget const* gt,
bool cmGlobalGenerator::ApplyCXXStdTargets()
{
for (auto const& gen : this->LocalGenerators) {
for (auto const& tgt : gen->GetGeneratorTargets()) {
// tgt->ApplyCXXStd can create targets itself, so we need iterators which
// won't be invalidated by that target creation
auto const& genTgts = gen->GetGeneratorTargets();
std::vector<cmGeneratorTarget*> existingTgts;
existingTgts.reserve(genTgts.size());
for (auto const& tgt : genTgts) {
existingTgts.push_back(tgt.get());
}
for (auto const& tgt : existingTgts) {
if (!tgt->ApplyCXXStdTargets()) {
return false;
}