1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 12:16:40 +08:00

cmSourceFile: internally track CMake-managed source files

These source file types don't necessarily show up in the CMake code, but
are side effects of other behaviors in CMake. Support tracking them
specially so that heuristics are not required to figure out if a given
`cmSourceFile` is one of them.
This commit is contained in:
Ben Boeckel
2025-09-03 12:59:16 -04:00
parent 388923818d
commit bbdc2fd908
9 changed files with 66 additions and 3 deletions

View File

@@ -3141,6 +3141,7 @@ std::string cmGeneratorTarget::GetPchFileObject(std::string const& config,
auto* pchSf = this->Makefile->GetOrCreateSource(
pchSource, false, cmSourceFileLocationKind::Known);
pchSf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::PchSource);
pchSf->ResolveFullPath();
filename = cmStrCat(this->GetObjectDirectory(config), '/',
this->GetObjectName(pchSf));
@@ -3188,6 +3189,8 @@ std::string cmGeneratorTarget::GetPchFile(std::string const& config,
auto pchSource = this->GetPchSource(config, language, arch);
auto* pchSf = this->Makefile->GetOrCreateSource(
pchSource, false, cmSourceFileLocationKind::Known);
pchSf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::PchSource);
pchSf->ResolveFullPath();
std::string cfgSubdir;
if (this->GetGlobalGenerator()->IsMultiConfig()) {
@@ -5798,6 +5801,8 @@ std::string cmGeneratorTarget::GenerateHeaderSetVerificationFile(
this->LocalGenerator->GetCurrentBinaryDirectory(), '/', this->GetName(),
"_verify_interface_header_sets/", headerFilename, extension);
auto* verificationSource = this->Makefile->GetOrCreateSource(filename);
source.SetSpecialSourceType(
cmSourceFile::SpecialSourceType::HeaderSetVerificationSource);
verificationSource->SetProperty("LANGUAGE", language);
cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(filename));

View File

@@ -740,7 +740,9 @@ void cmGlobalXCodeGenerator::AddExtraTargets(
this->CurrentMakefile->GetSafeDefinition("CMAKE_XCODE_XCCONFIG"),
this->CurrentLocalGenerator, config);
if (!xcconfig.empty()) {
allbuild->AddSource(xcconfig);
auto* xcconfig_sf = allbuild->AddSource(xcconfig);
xcconfig_sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::XcodeXCConfigFile);
}
}
@@ -1161,6 +1163,8 @@ void cmGlobalXCodeGenerator::AddXCodeProjBuildRule(
target->GetLocalGenerator()->GetCurrentSourceDirectory());
cmSourceFile* srcCMakeLists = target->Makefile->GetOrCreateSource(
listfile, false, cmSourceFileLocationKind::Known);
srcCMakeLists->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::CMakeLists);
if (!cm::contains(sources, srcCMakeLists)) {
sources.push_back(srcCMakeLists);
}
@@ -1538,6 +1542,7 @@ bool cmGlobalXCodeGenerator::CreateXCodeTarget(
std::string plist = this->ComputeInfoPListLocation(gtgt);
cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
plist, true, cmSourceFileLocationKind::Known);
sf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::BundleInfoPlist);
commonSourceFiles.push_back(sf);
}
@@ -1811,6 +1816,8 @@ void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmGeneratorTarget* gtgt)
fout << '\n';
}
if (cmSourceFile* sf = mf->GetOrCreateSource(fname)) {
sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::XcodeForceLinkerSource);
sf->SetProperty("LANGUAGE", llang);
sf->SetProperty("CXX_SCAN_FOR_MODULES", "0");
gtgt->AddSource(fname);
@@ -4519,6 +4526,7 @@ bool cmGlobalXCodeGenerator::CreateGroups(
gtgt->GetLocalGenerator()->GetCurrentSourceDirectory());
cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
listfile, false, cmSourceFileLocationKind::Known);
sf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::CMakeLists);
addSourceToGroup(sf->ResolveFullPath());
}
@@ -4527,6 +4535,8 @@ bool cmGlobalXCodeGenerator::CreateGroups(
std::string plist = this->ComputeInfoPListLocation(gtgt.get());
cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
plist, true, cmSourceFileLocationKind::Known);
sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::BundleInfoPlist);
addSourceToGroup(sf->ResolveFullPath());
}
}

View File

@@ -2786,6 +2786,8 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
auto* pch_sf = this->Makefile->GetOrCreateSource(
pchSource, false, cmSourceFileLocationKind::Known);
pch_sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::PchSource);
// PCH sources should never be scanned as they cannot contain C++
// module references.
pch_sf->SetProperty("CXX_SCAN_FOR_MODULES", "0");
@@ -2881,6 +2883,8 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
// be grouped as "Precompile Header File"
auto* pchHeader_sf = this->Makefile->GetOrCreateSource(
pchHeader, false, cmSourceFileLocationKind::Known);
pchHeader_sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::PchHeader);
std::string err;
pchHeader_sf->ResolveFullPath(&err);
if (!err.empty()) {
@@ -2989,7 +2993,9 @@ void cmLocalGenerator::CopyPchCompilePdb(
cmSourceFile* copy_rule = this->AddCustomCommandToOutput(std::move(cc));
if (copy_rule) {
copy_rule->SetProperty("CXX_SCAN_FOR_MODULES", "0");
target->AddSource(copy_rule->ResolveFullPath());
auto* pch_pdb_sf = target->AddSource(copy_rule->ResolveFullPath());
pch_pdb_sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::PchPdbReuseSource);
}
}
}
@@ -3338,6 +3344,8 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
for (UnitySource const& file : unity_files) {
auto* unity = this->GetMakefile()->GetOrCreateSource(file.Path);
unity->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::UnitySource);
target->AddSource(file.Path, true);
unity->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "ON");
unity->SetProperty("UNITY_SOURCE_FILE", file.Path);

View File

@@ -158,7 +158,8 @@ void cmLocalXCodeGenerator::AddXCConfigSources(cmGeneratorTarget* target)
*xcconfig,
this, config);
if (!file.empty()) {
target->AddSource(file);
auto* xcconfig_sf = target->AddSource(file);
xcconfig_sf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::XcodeXCConfigFile);
}
}
}

View File

@@ -3147,6 +3147,7 @@ void cmMakefile::AddTargetObject(std::string const& tgtName,
{
cmSourceFile* sf =
this->GetOrCreateSource(objFile, true, cmSourceFileLocationKind::Known);
sf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::Object);
sf->SetObjectLibrary(tgtName);
sf->SetProperty("EXTERNAL_OBJECT", "1");
// TODO: Compute a language for this object based on the associated source

View File

@@ -43,6 +43,8 @@ bool cmQTWrapCPPCommand(std::vector<std::string> const& args,
std::string newName =
cmStrCat(mf.GetCurrentBinaryDirectory(), "/moc_", srcName, ".cxx");
cmSourceFile* sf = mf.GetOrCreateSource(newName, true);
sf->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::QtWrapCppSource);
if (curr) {
sf->SetProperty("ABSTRACT", curr->GetProperty("ABSTRACT"));
}

View File

@@ -2105,6 +2105,8 @@ cmSourceFile* cmQtAutoGenInitializer::RegisterGeneratedSource(
std::string const& filename)
{
cmSourceFile* gFile = this->Makefile->GetOrCreateSource(filename, true);
gFile->SetSpecialSourceType(
cmSourceFile::SpecialSourceType::QtAutogenSource);
gFile->MarkAsGenerated();
gFile->SetProperty("SKIP_AUTOGEN", "1");
gFile->SetProperty("SKIP_LINTING", "ON");

View File

@@ -29,6 +29,11 @@ cmSourceFile::cmSourceFile(cmMakefile* mf, std::string const& name,
}
}
void cmSourceFile::SetSpecialSourceType(cmSourceFile::SpecialSourceType type)
{
this->SpecialSource = type;
}
std::string const& cmSourceFile::GetExtension() const
{
return this->Extension;

View File

@@ -40,6 +40,34 @@ public:
cmCustomCommand* GetCustomCommand() const;
void SetCustomCommand(std::unique_ptr<cmCustomCommand> cc);
enum class SpecialSourceType
{
// Sources for user-provided sources.
User,
// Source files for object files.
Object,
// Sources representing `CMakeLists.txt` files.
CMakeLists,
// Xcode `Info.plist` files for bundle targets.
BundleInfoPlist,
// Xcode source to force a link to occur with an appropriate language.
XcodeForceLinkerSource,
// Xcode `
XcodeXCConfigFile,
// Header set verification files.
HeaderSetVerificationSource,
// PCH files.
PchHeader,
PchSource,
PchPdbReuseSource,
// Unity sources.
UnitySource,
// Qt support sources.
QtWrapCppSource,
QtAutogenSource,
};
void SetSpecialSourceType(SpecialSourceType type);
//! Set/Get a property of this source file
void SetProperty(std::string const& prop, cmValue value);
void RemoveProperty(std::string const& prop)
@@ -166,6 +194,7 @@ private:
std::vector<BT<std::string>> IncludeDirectories;
bool FindFullPathFailed = false;
bool IsGenerated = false;
SpecialSourceType SpecialSource = SpecialSourceType::User;
bool FindFullPath(std::string* error, std::string* cmp0115Warning);
void CheckExtension();