diff options
Diffstat (limited to 'Source')
27 files changed, 1513 insertions, 52 deletions
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index c8498a9..c125378 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -261,6 +261,8 @@ set(SRCS cmFileLockResult.h cmFilePathChecksum.cxx cmFilePathChecksum.h + cmFileSet.cxx + cmFileSet.h cmFileTime.cxx cmFileTime.h cmFileTimeCache.cxx @@ -316,6 +318,8 @@ set(SRCS cmInstallExportGenerator.cxx cmInstalledFile.h cmInstalledFile.cxx + cmInstallFileSetGenerator.h + cmInstallFileSetGenerator.cxx cmInstallFilesGenerator.h cmInstallFilesGenerator.cxx cmInstallImportedRuntimeArtifactsGenerator.h diff --git a/Source/cmExportBuildFileGenerator.cxx b/Source/cmExportBuildFileGenerator.cxx index aa968dc..a47f1e5 100644 --- a/Source/cmExportBuildFileGenerator.cxx +++ b/Source/cmExportBuildFileGenerator.cxx @@ -11,12 +11,15 @@ #include <cmext/algorithm> #include "cmExportSet.h" +#include "cmFileSet.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" +#include "cmListFileCache.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" #include "cmMessageType.h" +#include "cmOutputConverter.h" #include "cmPolicies.h" #include "cmStateTypes.h" #include "cmStringAlgorithms.h" @@ -135,6 +138,8 @@ bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os) this->PopulateCompatibleInterfaceProperties(gte, properties); this->GenerateInterfaceProperties(gte, os, properties); + + this->GenerateTargetFileSets(gte, os); } // Generate import file content for each configuration. @@ -356,3 +361,17 @@ std::string cmExportBuildFileGenerator::InstallNameDir( return install_name_dir; } + +std::string cmExportBuildFileGenerator::GetFileSetDirectories( + cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport* /*te*/) +{ + return cmOutputConverter::EscapeForCMake( + cmJoin(fileSet->GetDirectoryEntries(), ";")); +} + +std::string cmExportBuildFileGenerator::GetFileSetFiles( + cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport* /*te*/) +{ + return cmOutputConverter::EscapeForCMake( + cmJoin(fileSet->GetFileEntries(), ";")); +} diff --git a/Source/cmExportBuildFileGenerator.h b/Source/cmExportBuildFileGenerator.h index 244f526..a7985c7 100644 --- a/Source/cmExportBuildFileGenerator.h +++ b/Source/cmExportBuildFileGenerator.h @@ -15,9 +15,11 @@ #include "cmStateTypes.h" class cmExportSet; +class cmFileSet; class cmGeneratorTarget; class cmGlobalGenerator; class cmLocalGenerator; +class cmTargetExport; /** \class cmExportBuildFileGenerator * \brief Generate a file exporting targets from a build tree. @@ -76,6 +78,11 @@ protected: std::string InstallNameDir(cmGeneratorTarget const* target, const std::string& config) override; + std::string GetFileSetDirectories(cmGeneratorTarget* gte, cmFileSet* fileSet, + cmTargetExport* te) override; + std::string GetFileSetFiles(cmGeneratorTarget* gte, cmFileSet* fileSet, + cmTargetExport* te) override; + std::pair<std::vector<std::string>, std::string> FindBuildExportInfo( cmGlobalGenerator* gg, const std::string& name); diff --git a/Source/cmExportFileGenerator.cxx b/Source/cmExportFileGenerator.cxx index 9f38c12..896240c 100644 --- a/Source/cmExportFileGenerator.cxx +++ b/Source/cmExportFileGenerator.cxx @@ -12,6 +12,7 @@ #include "cmsys/FStream.hxx" #include "cmComputeLinkInformation.h" +#include "cmFileSet.h" #include "cmGeneratedFileStream.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" @@ -1256,3 +1257,39 @@ bool cmExportFileGenerator::PopulateExportProperties( } return true; } + +void cmExportFileGenerator::GenerateTargetFileSets(cmGeneratorTarget* gte, + std::ostream& os, + cmTargetExport* te) +{ + auto interfaceFileSets = gte->Target->GetAllInterfaceFileSets(); + if (!interfaceFileSets.empty()) { + std::string targetName = cmStrCat(this->Namespace, gte->GetExportName()); + os << "if(NOT CMAKE_VERSION VERSION_LESS \"" << DEVEL_CMAKE_VERSION(3, 23) + << "\")\n" + " target_sources(" + << targetName << "\n"; + + for (auto const& name : interfaceFileSets) { + auto* fileSet = gte->Target->GetFileSet(name); + if (!fileSet) { + gte->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("File set \"", name, + "\" is listed in interface file sets of ", gte->GetName(), + " but has not been created")); + return; + } + + os << " INTERFACE" + << "\n FILE_SET " << cmOutputConverter::EscapeForCMake(name) + << "\n TYPE " + << cmOutputConverter::EscapeForCMake(fileSet->GetType()) + << "\n BASE_DIRS " + << this->GetFileSetDirectories(gte, fileSet, te) << "\n FILES " + << this->GetFileSetFiles(gte, fileSet, te) << "\n"; + } + + os << " )\nendif()\n\n"; + } +} diff --git a/Source/cmExportFileGenerator.h b/Source/cmExportFileGenerator.h index 24e048b..5875247 100644 --- a/Source/cmExportFileGenerator.h +++ b/Source/cmExportFileGenerator.h @@ -15,7 +15,9 @@ #include "cmVersion.h" #include "cmVersionConfig.h" +class cmFileSet; class cmGeneratorTarget; +class cmTargetExport; #define STRINGIFY_HELPER(X) #X #define STRINGIFY(X) STRINGIFY_HELPER(X) @@ -184,6 +186,16 @@ protected: ImportPropertyMap& properties, std::string& errorMessage); + void GenerateTargetFileSets(cmGeneratorTarget* gte, std::ostream& os, + cmTargetExport* te = nullptr); + + virtual std::string GetFileSetDirectories(cmGeneratorTarget* gte, + cmFileSet* fileSet, + cmTargetExport* te) = 0; + virtual std::string GetFileSetFiles(cmGeneratorTarget* gte, + cmFileSet* fileSet, + cmTargetExport* te) = 0; + // The namespace in which the exports are placed in the generated file. std::string Namespace; diff --git a/Source/cmExportInstallFileGenerator.cxx b/Source/cmExportInstallFileGenerator.cxx index e9ac875..2dd8b8f 100644 --- a/Source/cmExportInstallFileGenerator.cxx +++ b/Source/cmExportInstallFileGenerator.cxx @@ -2,19 +2,23 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmExportInstallFileGenerator.h" +#include <algorithm> #include <memory> #include <sstream> #include <utility> #include "cmExportSet.h" +#include "cmFileSet.h" #include "cmGeneratedFileStream.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" #include "cmInstallExportGenerator.h" +#include "cmInstallFileSetGenerator.h" #include "cmInstallTargetGenerator.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" +#include "cmOutputConverter.h" #include "cmPolicies.h" #include "cmStateTypes.h" #include "cmStringAlgorithms.h" @@ -147,6 +151,8 @@ bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os) this->PopulateCompatibleInterfaceProperties(gt, properties); this->GenerateInterfaceProperties(gt, os, properties); + + this->GenerateTargetFileSets(gt, os, te); } if (require3_1_0) { @@ -534,3 +540,102 @@ std::string cmExportInstallFileGenerator::InstallNameDir( return install_name_dir; } + +namespace { +bool EntryIsContextSensitive( + const std::unique_ptr<cmCompiledGeneratorExpression>& cge) +{ + return cge->GetHadContextSensitiveCondition(); +} +} + +std::string cmExportInstallFileGenerator::GetFileSetDirectories( + cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* te) +{ + std::vector<std::string> resultVector; + + auto configs = + gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); + + cmGeneratorExpression ge; + auto cge = ge.Parse(te->FileSetGenerators.at(fileSet)->GetDestination()); + + for (auto const& config : configs) { + auto dest = cmStrCat("${_IMPORT_PREFIX}/", + cmOutputConverter::EscapeForCMake( + cge->Evaluate(gte->LocalGenerator, config, gte), + cmOutputConverter::WrapQuotes::NoWrap)); + + if (cge->GetHadContextSensitiveCondition() && configs.size() != 1) { + resultVector.push_back( + cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\"")); + } else { + resultVector.push_back(cmStrCat('"', dest, '"')); + break; + } + } + + return cmJoin(resultVector, " "); +} + +std::string cmExportInstallFileGenerator::GetFileSetFiles( + cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* te) +{ + std::vector<std::string> resultVector; + + auto configs = + gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); + + auto fileEntries = fileSet->CompileFileEntries(); + auto directoryEntries = fileSet->CompileDirectoryEntries(); + + cmGeneratorExpression destGe; + auto destCge = + destGe.Parse(te->FileSetGenerators.at(fileSet)->GetDestination()); + + for (auto const& config : configs) { + auto directories = fileSet->EvaluateDirectoryEntries( + directoryEntries, gte->LocalGenerator, config, gte); + + std::map<std::string, std::vector<std::string>> files; + for (auto const& entry : fileEntries) { + fileSet->EvaluateFileEntry(directories, files, entry, + gte->LocalGenerator, config, gte); + } + auto dest = cmStrCat("${_IMPORT_PREFIX}/", + cmOutputConverter::EscapeForCMake( + destCge->Evaluate(gte->LocalGenerator, config, gte), + cmOutputConverter::WrapQuotes::NoWrap), + '/'); + + bool const contextSensitive = destCge->GetHadContextSensitiveCondition() || + std::any_of(directoryEntries.begin(), directoryEntries.end(), + EntryIsContextSensitive) || + std::any_of(fileEntries.begin(), fileEntries.end(), + EntryIsContextSensitive); + + for (auto const& it : files) { + auto prefix = it.first.empty() ? "" : cmStrCat(it.first, '/'); + for (auto const& filename : it.second) { + auto relFile = + cmStrCat(prefix, cmSystemTools::GetFilenameName(filename)); + auto escapedFile = + cmStrCat(dest, + cmOutputConverter::EscapeForCMake( + relFile, cmOutputConverter::WrapQuotes::NoWrap)); + if (contextSensitive && configs.size() != 1) { + resultVector.push_back( + cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\"")); + } else { + resultVector.push_back(cmStrCat('"', escapedFile, '"')); + } + } + } + + if (!(contextSensitive && configs.size() != 1)) { + break; + } + } + + return cmJoin(resultVector, " "); +} diff --git a/Source/cmExportInstallFileGenerator.h b/Source/cmExportInstallFileGenerator.h index 5cec2e0..9374c6b 100644 --- a/Source/cmExportInstallFileGenerator.h +++ b/Source/cmExportInstallFileGenerator.h @@ -14,6 +14,7 @@ #include "cmExportFileGenerator.h" #include "cmStateTypes.h" +class cmFileSet; class cmGeneratorTarget; class cmGlobalGenerator; class cmInstallExportGenerator; @@ -97,6 +98,11 @@ protected: std::string InstallNameDir(cmGeneratorTarget const* target, const std::string& config) override; + std::string GetFileSetDirectories(cmGeneratorTarget* gte, cmFileSet* fileSet, + cmTargetExport* te) override; + std::string GetFileSetFiles(cmGeneratorTarget* gte, cmFileSet* fileSet, + cmTargetExport* te) override; + cmInstallExportGenerator* IEGen; // The import file generated for each configuration. diff --git a/Source/cmExportTryCompileFileGenerator.cxx b/Source/cmExportTryCompileFileGenerator.cxx index cbe3c4d..4fe92c6 100644 --- a/Source/cmExportTryCompileFileGenerator.cxx +++ b/Source/cmExportTryCompileFileGenerator.cxx @@ -7,17 +7,22 @@ #include <cm/memory> +#include "cmFileSet.h" #include "cmGeneratorExpression.h" #include "cmGeneratorExpressionDAGChecker.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" +#include "cmListFileCache.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" +#include "cmOutputConverter.h" #include "cmStateTypes.h" #include "cmStringAlgorithms.h" #include "cmTarget.h" #include "cmValue.h" +class cmTargetExport; + cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator( cmGlobalGenerator* gg, const std::vector<std::string>& targets, cmMakefile* mf, std::set<std::string> const& langs) @@ -137,3 +142,17 @@ std::string cmExportTryCompileFileGenerator::InstallNameDir( return install_name_dir; } + +std::string cmExportTryCompileFileGenerator::GetFileSetDirectories( + cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport* /*te*/) +{ + return cmOutputConverter::EscapeForCMake( + cmJoin(fileSet->GetDirectoryEntries(), ";")); +} + +std::string cmExportTryCompileFileGenerator::GetFileSetFiles( + cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport* /*te*/) +{ + return cmOutputConverter::EscapeForCMake( + cmJoin(fileSet->GetFileEntries(), ";")); +} diff --git a/Source/cmExportTryCompileFileGenerator.h b/Source/cmExportTryCompileFileGenerator.h index 127b8df..8a1fd7e 100644 --- a/Source/cmExportTryCompileFileGenerator.h +++ b/Source/cmExportTryCompileFileGenerator.h @@ -11,9 +11,11 @@ #include "cmExportFileGenerator.h" +class cmFileSet; class cmGeneratorTarget; class cmGlobalGenerator; class cmMakefile; +class cmTargetExport; class cmExportTryCompileFileGenerator : public cmExportFileGenerator { @@ -48,6 +50,13 @@ protected: std::string InstallNameDir(cmGeneratorTarget const* target, const std::string& config) override; + std::string GetFileSetDirectories(cmGeneratorTarget* target, + cmFileSet* fileSet, + cmTargetExport* te) override; + + std::string GetFileSetFiles(cmGeneratorTarget* target, cmFileSet* fileSet, + cmTargetExport* te) override; + private: std::string FindTargets(const std::string& prop, const cmGeneratorTarget* tgt, diff --git a/Source/cmFileAPI.cxx b/Source/cmFileAPI.cxx index d529f52..c1df992 100644 --- a/Source/cmFileAPI.cxx +++ b/Source/cmFileAPI.cxx @@ -686,7 +686,8 @@ std::string cmFileAPI::NoSupportedVersion( // The "codemodel" object kind. -static unsigned int const CodeModelV2Minor = 3; +// Update Help/manual/cmake-file-api.7.rst when updating this constant. +static unsigned int const CodeModelV2Minor = 4; void cmFileAPI::BuildClientRequestCodeModel( ClientRequest& r, std::vector<RequestVersion> const& versions) diff --git a/Source/cmFileAPICodemodel.cxx b/Source/cmFileAPICodemodel.cxx index 147181e..40e1d2e 100644 --- a/Source/cmFileAPICodemodel.cxx +++ b/Source/cmFileAPICodemodel.cxx @@ -23,11 +23,13 @@ #include "cmCryptoHash.h" #include "cmExportSet.h" #include "cmFileAPI.h" +#include "cmFileSet.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" #include "cmInstallDirectoryGenerator.h" #include "cmInstallExportGenerator.h" +#include "cmInstallFileSetGenerator.h" #include "cmInstallFilesGenerator.h" #include "cmInstallGenerator.h" #include "cmInstallGetRuntimeDependenciesGenerator.h" @@ -1043,6 +1045,53 @@ Json::Value DirectoryObject::DumpInstaller(cmInstallGenerator* gen) installer["runtimeDependencySetType"] = "library"; break; } + } else if (auto* installFileSet = + dynamic_cast<cmInstallFileSetGenerator*>(gen)) { + installer["type"] = "fileSet"; + installer["destination"] = installFileSet->GetDestination(this->Config); + + auto* fileSet = installFileSet->GetFileSet(); + auto* target = installFileSet->GetTarget(); + + auto dirCges = fileSet->CompileDirectoryEntries(); + auto dirs = fileSet->EvaluateDirectoryEntries( + dirCges, target->GetLocalGenerator(), this->Config, target); + + auto entryCges = fileSet->CompileFileEntries(); + std::map<std::string, std::vector<std::string>> entries; + for (auto const& entryCge : entryCges) { + fileSet->EvaluateFileEntry(dirs, entries, entryCge, + target->GetLocalGenerator(), this->Config, + target); + } + + Json::Value files = Json::arrayValue; + for (auto const& it : entries) { + auto dir = it.first; + if (!dir.empty()) { + dir += '/'; + } + for (auto const& file : it.second) { + files.append(this->DumpInstallerPath( + this->TopSource, file, + cmStrCat(dir, cmSystemTools::GetFilenameName(file)))); + } + } + installer["paths"] = std::move(files); + installer["fileSetName"] = fileSet->GetName(); + installer["fileSetType"] = fileSet->GetType(); + installer["fileSetDirectories"] = Json::arrayValue; + for (auto const& dir : dirs) { + installer["fileSetDirectories"].append( + RelativeIfUnder(this->TopSource, dir)); + } + installer["fileSetTarget"] = Json::objectValue; + installer["fileSetTarget"]["id"] = TargetId(target, this->TopBuild); + installer["fileSetTarget"]["index"] = this->TargetIndexMap[target]; + + if (installFileSet->GetOptional()) { + installer["isOptional"] = true; + } } // Add fields common to all install generators. diff --git a/Source/cmFileSet.cxx b/Source/cmFileSet.cxx new file mode 100644 index 0000000..08d56ba --- /dev/null +++ b/Source/cmFileSet.cxx @@ -0,0 +1,151 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "cmFileSet.h" + +#include <sstream> +#include <string> +#include <utility> +#include <vector> + +#include "cmGeneratorExpression.h" +#include "cmListFileCache.h" +#include "cmLocalGenerator.h" +#include "cmMessageType.h" +#include "cmStringAlgorithms.h" +#include "cmSystemTools.h" +#include "cmake.h" + +cmFileSet::cmFileSet(std::string name, std::string type) + : Name(std::move(name)) + , Type(std::move(type)) +{ +} + +void cmFileSet::ClearDirectoryEntries() +{ + this->DirectoryEntries.clear(); +} + +void cmFileSet::AddDirectoryEntry(BT<std::string> directories) +{ + this->DirectoryEntries.push_back(std::move(directories)); +} + +void cmFileSet::ClearFileEntries() +{ + this->FileEntries.clear(); +} + +void cmFileSet::AddFileEntry(BT<std::string> files) +{ + this->FileEntries.push_back(std::move(files)); +} + +std::vector<std::unique_ptr<cmCompiledGeneratorExpression>> +cmFileSet::CompileFileEntries() const +{ + std::vector<std::unique_ptr<cmCompiledGeneratorExpression>> result; + + for (auto const& entry : this->FileEntries) { + for (auto const& ex : cmExpandedList(entry.Value)) { + cmGeneratorExpression ge(entry.Backtrace); + auto cge = ge.Parse(ex); + result.push_back(std::move(cge)); + } + } + + return result; +} + +std::vector<std::unique_ptr<cmCompiledGeneratorExpression>> +cmFileSet::CompileDirectoryEntries() const +{ + std::vector<std::unique_ptr<cmCompiledGeneratorExpression>> result; + + for (auto const& entry : this->DirectoryEntries) { + for (auto const& ex : cmExpandedList(entry.Value)) { + cmGeneratorExpression ge(entry.Backtrace); + auto cge = ge.Parse(ex); + result.push_back(std::move(cge)); + } + } + + return result; +} + +std::vector<std::string> cmFileSet::EvaluateDirectoryEntries( + const std::vector<std::unique_ptr<cmCompiledGeneratorExpression>>& cges, + cmLocalGenerator* lg, const std::string& config, + const cmGeneratorTarget* target, + cmGeneratorExpressionDAGChecker* dagChecker) const +{ + std::vector<std::string> result; + for (auto const& cge : cges) { + auto entry = cge->Evaluate(lg, config, target, dagChecker); + auto dirs = cmExpandedList(entry); + for (std::string dir : dirs) { + if (!cmSystemTools::FileIsFullPath(dir)) { + dir = cmStrCat(lg->GetCurrentSourceDirectory(), '/', dir); + } + auto collapsedDir = cmSystemTools::CollapseFullPath(dir); + for (auto const& priorDir : result) { + auto collapsedPriorDir = cmSystemTools::CollapseFullPath(priorDir); + if (!cmSystemTools::SameFile(collapsedDir, collapsedPriorDir) && + (cmSystemTools::IsSubDirectory(collapsedDir, collapsedPriorDir) || + cmSystemTools::IsSubDirectory(collapsedPriorDir, collapsedDir))) { + lg->GetCMakeInstance()->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat( + "Base directories in file set cannot be subdirectories of each " + "other:\n ", + priorDir, "\n ", dir), + cge->GetBacktrace()); + return {}; + } + } + result.push_back(dir); + } + } + return result; +} + +void cmFileSet::EvaluateFileEntry( + const std::vector<std::string>& dirs, + std::map<std::string, std::vector<std::string>>& filesPerDir, + const std::unique_ptr<cmCompiledGeneratorExpression>& cge, + cmLocalGenerator* lg, const std::string& config, + const cmGeneratorTarget* target, + cmGeneratorExpressionDAGChecker* dagChecker) const +{ + auto files = cge->Evaluate(lg, config, target, dagChecker); + for (std::string file : cmExpandedList(files)) { + if (!cmSystemTools::FileIsFullPath(file)) { + file = cmStrCat(lg->GetCurrentSourceDirectory(), '/', file); + } + auto collapsedFile = cmSystemTools::CollapseFullPath(file); + bool found = false; + std::string relDir; + for (auto const& dir : dirs) { + auto collapsedDir = cmSystemTools::CollapseFullPath(dir); + if (cmSystemTools::IsSubDirectory(collapsedFile, collapsedDir)) { + found = true; + relDir = cmSystemTools::GetParentDirectory( + cmSystemTools::RelativePath(collapsedDir, collapsedFile)); + break; + } + } + if (!found) { + std::ostringstream e; + e << "File:\n " << file + << "\nmust be in one of the file set's base directories:"; + for (auto const& dir : dirs) { + e << "\n " << dir; + } + lg->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR, e.str(), + cge->GetBacktrace()); + return; + } + + filesPerDir[relDir].push_back(file); + } +} diff --git a/Source/cmFileSet.h b/Source/cmFileSet.h new file mode 100644 index 0000000..5ee4a98 --- /dev/null +++ b/Source/cmFileSet.h @@ -0,0 +1,64 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include <map> +#include <memory> +#include <string> +#include <vector> + +#include "cmListFileCache.h" + +class cmCompiledGeneratorExpression; +struct cmGeneratorExpressionDAGChecker; +class cmGeneratorTarget; +class cmLocalGenerator; + +class cmFileSet +{ +public: + cmFileSet(std::string name, std::string type); + + const std::string& GetName() const { return this->Name; } + const std::string& GetType() const { return this->Type; } + + void ClearDirectoryEntries(); + void AddDirectoryEntry(BT<std::string> directories); + const std::vector<BT<std::string>>& GetDirectoryEntries() const + { + return this->DirectoryEntries; + } + + void ClearFileEntries(); + void AddFileEntry(BT<std::string> files); + const std::vector<BT<std::string>>& GetFileEntries() const + { + return this->FileEntries; + } + + std::vector<std::unique_ptr<cmCompiledGeneratorExpression>> + CompileFileEntries() const; + + std::vector<std::unique_ptr<cmCompiledGeneratorExpression>> + CompileDirectoryEntries() const; + + std::vector<std::string> EvaluateDirectoryEntries( + const std::vector<std::unique_ptr<cmCompiledGeneratorExpression>>& cges, + cmLocalGenerator* lg, const std::string& config, + const cmGeneratorTarget* target, + cmGeneratorExpressionDAGChecker* dagChecker = nullptr) const; + + void EvaluateFileEntry( + const std::vector<std::string>& dirs, + std::map<std::string, std::vector<std::string>>& filesPerDir, + const std::unique_ptr<cmCompiledGeneratorExpression>& cge, + cmLocalGenerator* lg, const std::string& config, + const cmGeneratorTarget* target, + cmGeneratorExpressionDAGChecker* dagChecker = nullptr) const; + +private: + std::string Name; + std::string Type; + std::vector<BT<std::string>> DirectoryEntries; + std::vector<BT<std::string>> FileEntries; +}; diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index f5ea8c3..59c3e7a 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -25,6 +25,7 @@ #include "cmAlgorithms.h" #include "cmComputeLinkInformation.h" #include "cmCustomCommandGenerator.h" +#include "cmFileSet.h" #include "cmFileTimes.h" #include "cmGeneratedFileStream.h" #include "cmGeneratorExpression.h" @@ -41,6 +42,7 @@ #include "cmSourceFile.h" #include "cmSourceFileLocation.h" #include "cmSourceFileLocationKind.h" +#include "cmSourceGroup.h" #include "cmStandardLevelResolver.h" #include "cmState.h" #include "cmStringAlgorithms.h" @@ -173,6 +175,65 @@ private: BT<std::string> PropertyValue; }; +class TargetPropertyEntryFileSet + : public cmGeneratorTarget::TargetPropertyEntry +{ +public: + TargetPropertyEntryFileSet( + std::vector<std::string> dirs, bool contextSensitiveDirs, + std::unique_ptr<cmCompiledGeneratorExpression> entryCge, + const cmFileSet* fileSet, cmLinkImplItem const& item = NoLinkImplItem) + : cmGeneratorTarget::TargetPropertyEntry(item) + , BaseDirs(std::move(dirs)) + , ContextSensitiveDirs(contextSensitiveDirs) + , EntryCge(std::move(entryCge)) + , FileSet(fileSet) + { + } + + const std::string& Evaluate(cmLocalGenerator* lg, const std::string& config, + cmGeneratorTarget const* headTarget, + cmGeneratorExpressionDAGChecker* dagChecker, + std::string const& /*lang*/) const override + { + std::map<std::string, std::vector<std::string>> filesPerDir; + this->FileSet->EvaluateFileEntry(this->BaseDirs, filesPerDir, + this->EntryCge, lg, config, headTarget, + dagChecker); + + std::vector<std::string> files; + for (auto const& it : filesPerDir) { + files.insert(files.end(), it.second.begin(), it.second.end()); + } + + static std::string filesStr; + filesStr = cmJoin(files, ";"); + return filesStr; + } + + cmListFileBacktrace GetBacktrace() const override + { + return this->EntryCge->GetBacktrace(); + } + + std::string const& GetInput() const override + { + return this->EntryCge->GetInput(); + } + + bool GetHadContextSensitiveCondition() const override + { + return this->ContextSensitiveDirs || + this->EntryCge->GetHadContextSensitiveCondition(); + } + +private: + const std::vector<std::string> BaseDirs; + const bool ContextSensitiveDirs; + const std::unique_ptr<cmCompiledGeneratorExpression> EntryCge; + const cmFileSet* FileSet; +}; + std::unique_ptr< cmGeneratorTarget:: TargetPropertyEntry> static CreateTargetPropertyEntry(const BT<std:: @@ -1594,6 +1655,80 @@ void AddObjectEntries(cmGeneratorTarget const* headTarget, } } +void addFileSetEntry(cmGeneratorTarget const* headTarget, + std::string const& config, + cmGeneratorExpressionDAGChecker* dagChecker, + cmFileSet const* fileSet, + EvaluatedTargetPropertyEntries& entries) +{ + auto dirCges = fileSet->CompileDirectoryEntries(); + auto dirs = fileSet->EvaluateDirectoryEntries( + dirCges, headTarget->GetLocalGenerator(), config, headTarget, dagChecker); + bool contextSensitiveDirs = false; + for (auto const& dirCge : dirCges) { + if (dirCge->GetHadContextSensitiveCondition()) { + contextSensitiveDirs = true; + break; + } + } + cmake* cm = headTarget->GetLocalGenerator()->GetCMakeInstance(); + for (auto& entryCge : fileSet->CompileFileEntries()) { + TargetPropertyEntryFileSet tpe(dirs, contextSensitiveDirs, + std::move(entryCge), fileSet); + entries.Entries.emplace_back( + EvaluateTargetPropertyEntry(headTarget, config, "", dagChecker, tpe)); + for (auto const& file : entries.Entries.back().Values) { + auto* sf = headTarget->Makefile->GetOrCreateSource(file); + if (fileSet->GetType() == "HEADERS"_s) { + sf->SetProperty("HEADER_FILE_ONLY", "TRUE"); + } + +#ifndef CMAKE_BOOTSTRAP + std::string e; + std::string w; + auto path = sf->ResolveFullPath(&e, &w); + if (!w.empty()) { + cm->IssueMessage(MessageType::AUTHOR_WARNING, w, + headTarget->GetBacktrace()); + } + if (path.empty()) { + if (!e.empty()) { + cm->IssueMessage(MessageType::FATAL_ERROR, e, + headTarget->GetBacktrace()); + } + return; + } + bool found = false; + for (auto const& sg : headTarget->Makefile->GetSourceGroups()) { + if (sg.MatchesFiles(path)) { + found = true; + break; + } + } + if (!found) { + if (fileSet->GetType() == "HEADERS"_s) { + headTarget->Makefile->GetOrCreateSourceGroup("Header Files") + ->AddGroupFile(path); + } + } +#endif + } + } +} + +void AddFileSetEntries(cmGeneratorTarget const* headTarget, + std::string const& config, + cmGeneratorExpressionDAGChecker* dagChecker, + EvaluatedTargetPropertyEntries& entries) +{ + for (auto const& entry : headTarget->Target->GetHeaderSetsEntries()) { + for (auto const& name : cmExpandedList(entry.Value)) { + auto const* headerSet = headTarget->Target->GetFileSet(name); + addFileSetEntry(headTarget, config, dagChecker, headerSet, entries); + } + } +} + bool processSources(cmGeneratorTarget const* tgt, EvaluatedTargetPropertyEntries& entries, std::vector<BT<std::string>>& srcs, @@ -1731,10 +1866,18 @@ std::vector<BT<std::string>> cmGeneratorTarget::GetSourceFilePaths( uniqueSrcs, debugSources); } + // Collect this target's file sets. + std::vector<std::string>::size_type numFilesBefore3 = files.size(); + EvaluatedTargetPropertyEntries fileSetEntries; + AddFileSetEntries(this, config, &dagChecker, fileSetEntries); + bool contextDependentFileSets = + processSources(this, fileSetEntries, files, uniqueSrcs, debugSources); + // Determine if sources are context-dependent or not. if (!contextDependentDirectSources && !(contextDependentInterfaceSources && numFilesBefore < files.size()) && - !(contextDependentObjects && numFilesBefore2 < files.size())) { + !(contextDependentObjects && numFilesBefore2 < files.size()) && + !(contextDependentFileSets && numFilesBefore3 < files.size())) { this->SourcesAreContextDependent = Tribool::False; } else { this->SourcesAreContextDependent = Tribool::True; diff --git a/Source/cmInstallCommand.cxx b/Source/cmInstallCommand.cxx index eaf88f6..18f2f7f 100644 --- a/Source/cmInstallCommand.cxx +++ b/Source/cmInstallCommand.cxx @@ -6,11 +6,13 @@ #include <cassert> #include <cstddef> #include <iterator> +#include <map> #include <set> #include <sstream> #include <utility> #include <cm/memory> +#include <cm/string_view> #include <cmext/string_view> #include "cmsys/Glob.hxx" @@ -18,11 +20,13 @@ #include "cmArgumentParser.h" #include "cmExecutionStatus.h" #include "cmExportSet.h" +#include "cmFileSet.h" #include "cmGeneratorExpression.h" #include "cmGlobalGenerator.h" #include "cmInstallCommandArguments.h" #include "cmInstallDirectoryGenerator.h" #include "cmInstallExportGenerator.h" +#include "cmInstallFileSetGenerator.h" #include "cmInstallFilesGenerator.h" #include "cmInstallGenerator.h" #include "cmInstallGetRuntimeDependenciesGenerator.h" @@ -89,6 +93,9 @@ public: bool MakeFilesFullPath(const char* modeName, const std::vector<std::string>& relFiles, std::vector<std::string>& absFiles); + bool MakeFilesFullPath(const char* modeName, const std::string& basePath, + const std::vector<std::string>& relFiles, + std::vector<std::string>& absFiles); bool CheckCMP0006(bool& failure) const; std::string GetDestination(const cmInstallCommandArguments* args, @@ -177,6 +184,19 @@ std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator( args.GetDestination()); } +std::unique_ptr<cmInstallFileSetGenerator> CreateInstallFileSetGenerator( + Helper& helper, cmTarget& target, cmFileSet* fileSet, + const std::string& destination, const cmInstallCommandArguments& args) +{ + cmInstallGenerator::MessageLevel message = + cmInstallGenerator::SelectMessageLevel(helper.Makefile); + return cm::make_unique<cmInstallFileSetGenerator>( + target.GetName(), fileSet, destination, args.GetPermissions(), + args.GetConfigurations(), args.GetComponent(), message, + args.GetExcludeFromAll(), args.GetOptional(), + helper.Makefile->GetBacktrace()); +} + void AddInstallRuntimeDependenciesGenerator( Helper& helper, cmInstallRuntimeDependencySet* runtimeDependencySet, const cmInstallCommandArguments& runtimeArgs, @@ -390,6 +410,7 @@ bool HandleTargetsMode(std::vector<std::string> const& args, std::vector<std::string> PrivateHeader; std::vector<std::string> PublicHeader; std::vector<std::string> Resource; + std::vector<std::vector<std::string>> FileSets; }; static auto const argHelper = @@ -403,7 +424,8 @@ bool HandleTargetsMode(std::vector<std::string> const& args, .Bind("INCLUDES"_s, &ArgVectors::Includes) .Bind("PRIVATE_HEADER"_s, &ArgVectors::PrivateHeader) .Bind("PUBLIC_HEADER"_s, &ArgVectors::PublicHeader) - .Bind("RESOURCE"_s, &ArgVectors::Resource); + .Bind("RESOURCE"_s, &ArgVectors::Resource) + .Bind("FILE_SET"_s, &ArgVectors::FileSets); std::vector<std::string> genericArgVector; ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector); @@ -442,6 +464,8 @@ bool HandleTargetsMode(std::vector<std::string> const& args, cmInstallCommandArguments publicHeaderArgs(helper.DefaultComponentName); cmInstallCommandArguments resourceArgs(helper.DefaultComponentName); cmInstallCommandIncludesArgument includesArgs; + std::vector<cmInstallCommandFileSetArguments> fileSetArgs( + argVectors.FileSets.size(), { helper.DefaultComponentName }); // now parse the args for specific parts of the target (e.g. LIBRARY, // RUNTIME, ARCHIVE etc. @@ -455,6 +479,15 @@ bool HandleTargetsMode(std::vector<std::string> const& args, publicHeaderArgs.Parse(argVectors.PublicHeader, &unknownArgs); resourceArgs.Parse(argVectors.Resource, &unknownArgs); includesArgs.Parse(&argVectors.Includes, &unknownArgs); + for (std::size_t i = 0; i < argVectors.FileSets.size(); i++) { + // We have to create a separate object for the parsing because + // cmArgumentParser<void>::Bind() binds to a specific address, but the + // objects in the vector can move around. So we parse in an object with a + // fixed address and then copy the data into the vector. + cmInstallCommandFileSetArguments fileSetArg(helper.DefaultComponentName); + fileSetArg.Parse(argVectors.FileSets[i], &unknownArgs); + fileSetArgs[i] = std::move(fileSetArg); + } if (!unknownArgs.empty()) { // Unknown argument. @@ -473,6 +506,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, privateHeaderArgs.SetGenericArguments(&genericArgs); publicHeaderArgs.SetGenericArguments(&genericArgs); resourceArgs.SetGenericArguments(&genericArgs); + for (auto& fileSetArg : fileSetArgs) { + fileSetArg.SetGenericArguments(&genericArgs); + } success = success && archiveArgs.Finalize(); success = success && libraryArgs.Finalize(); @@ -483,6 +519,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, success = success && privateHeaderArgs.Finalize(); success = success && publicHeaderArgs.Finalize(); success = success && resourceArgs.Finalize(); + for (auto& fileSetArg : fileSetArgs) { + success = success && fileSetArg.Finalize(); + } if (!success) { return false; @@ -493,7 +532,10 @@ bool HandleTargetsMode(std::vector<std::string> const& args, if (archiveArgs.GetNamelinkOnly() || runtimeArgs.GetNamelinkOnly() || objectArgs.GetNamelinkOnly() || frameworkArgs.GetNamelinkOnly() || bundleArgs.GetNamelinkOnly() || privateHeaderArgs.GetNamelinkOnly() || - publicHeaderArgs.GetNamelinkOnly() || resourceArgs.GetNamelinkOnly()) { + publicHeaderArgs.GetNamelinkOnly() || resourceArgs.GetNamelinkOnly() || + std::any_of(fileSetArgs.begin(), fileSetArgs.end(), + [](const cmInstallCommandFileSetArguments& fileSetArg) + -> bool { return fileSetArg.GetNamelinkOnly(); })) { status.SetError( "TARGETS given NAMELINK_ONLY option not in LIBRARY group. " "The NAMELINK_ONLY option may be specified only following LIBRARY."); @@ -502,7 +544,10 @@ bool HandleTargetsMode(std::vector<std::string> const& args, if (archiveArgs.GetNamelinkSkip() || runtimeArgs.GetNamelinkSkip() || objectArgs.GetNamelinkSkip() || frameworkArgs.GetNamelinkSkip() || bundleArgs.GetNamelinkSkip() || privateHeaderArgs.GetNamelinkSkip() || - publicHeaderArgs.GetNamelinkSkip() || resourceArgs.GetNamelinkSkip()) { + publicHeaderArgs.GetNamelinkSkip() || resourceArgs.GetNamelinkSkip() || + std::any_of(fileSetArgs.begin(), fileSetArgs.end(), + [](const cmInstallCommandFileSetArguments& fileSetArg) + -> bool { return fileSetArg.GetNamelinkSkip(); })) { status.SetError( "TARGETS given NAMELINK_SKIP option not in LIBRARY group. " "The NAMELINK_SKIP option may be specified only following LIBRARY."); @@ -515,7 +560,10 @@ bool HandleTargetsMode(std::vector<std::string> const& args, bundleArgs.HasNamelinkComponent() || privateHeaderArgs.HasNamelinkComponent() || publicHeaderArgs.HasNamelinkComponent() || - resourceArgs.HasNamelinkComponent()) { + resourceArgs.HasNamelinkComponent() || + std::any_of(fileSetArgs.begin(), fileSetArgs.end(), + [](const cmInstallCommandFileSetArguments& fileSetArg) + -> bool { return fileSetArg.HasNamelinkComponent(); })) { status.SetError( "TARGETS given NAMELINK_COMPONENT option not in LIBRARY group. " "The NAMELINK_COMPONENT option may be specified only following " @@ -531,12 +579,21 @@ bool HandleTargetsMode(std::vector<std::string> const& args, !libraryArgs.GetType().empty() || !runtimeArgs.GetType().empty() || !objectArgs.GetType().empty() || !frameworkArgs.GetType().empty() || !bundleArgs.GetType().empty() || !privateHeaderArgs.GetType().empty() || - !publicHeaderArgs.GetType().empty() || !resourceArgs.GetType().empty()) { + !publicHeaderArgs.GetType().empty() || !resourceArgs.GetType().empty() || + std::any_of(fileSetArgs.begin(), fileSetArgs.end(), + [](const cmInstallCommandFileSetArguments& fileSetArg) + -> bool { return !fileSetArg.GetType().empty(); })) { status.SetError( "TARGETS given TYPE option. The TYPE option may only be specified in " " install(FILES) and install(DIRECTORIES)."); return false; } + if (std::any_of(fileSetArgs.begin(), fileSetArgs.end(), + [](const cmInstallCommandFileSetArguments& fileSetArg) + -> bool { return fileSetArg.GetFileSet().empty(); })) { + status.SetError("TARGETS given FILE_SET option without file set name."); + return false; + } cmInstallRuntimeDependencySet* runtimeDependencySet = nullptr; if (withRuntimeDependencies) { @@ -647,6 +704,7 @@ bool HandleTargetsMode(std::vector<std::string> const& args, bool installsPrivateHeader = false; bool installsPublicHeader = false; bool installsResource = false; + std::vector<bool> installsFileSet(fileSetArgs.size(), false); // Generate install script code to install the given targets. for (cmTarget* ti : targets) { @@ -662,6 +720,7 @@ bool HandleTargetsMode(std::vector<std::string> const& args, std::unique_ptr<cmInstallFilesGenerator> privateHeaderGenerator; std::unique_ptr<cmInstallFilesGenerator> publicHeaderGenerator; std::unique_ptr<cmInstallFilesGenerator> resourceGenerator; + std::vector<std::unique_ptr<cmInstallFileSetGenerator>> fileSetGenerators; // Avoid selecting default destinations for PUBLIC_HEADER and // PRIVATE_HEADER if any artifacts are specified. @@ -670,9 +729,24 @@ bool HandleTargetsMode(std::vector<std::string> const& args, // Track whether this is a namelink-only rule. bool namelinkOnly = false; - auto addTargetExport = [&]() { + auto addTargetExport = [&]() -> bool { // Add this install rule to an export if one was specified. if (!exports.empty()) { + auto interfaceFileSets = target.GetAllInterfaceFileSets(); + if (std::any_of( + interfaceFileSets.begin(), interfaceFileSets.end(), + [=](const std::string& name) -> bool { + return !std::any_of( + fileSetArgs.begin(), fileSetArgs.end(), + [=](const cmInstallCommandFileSetArguments& fileSetArg) + -> bool { return fileSetArg.GetFileSet() == name; }); + })) { + status.SetError(cmStrCat( + "TARGETS target ", target.GetName(), + " is exported but not all of its file sets are installed")); + return false; + } + auto te = cm::make_unique<cmTargetExport>(); te->TargetName = target.GetName(); te->ArchiveGenerator = archiveGenerator.get(); @@ -682,6 +756,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, te->LibraryGenerator = libraryGenerator.get(); te->RuntimeGenerator = runtimeGenerator.get(); te->ObjectsGenerator = objectGenerator.get(); + for (auto const& gen : fileSetGenerators) { + te->FileSetGenerators[gen->GetFileSet()] = gen.get(); + } target.AddInstallIncludeDirectories( cmMakeRange(includesArgs.GetIncludeDirs())); te->NamelinkOnly = namelinkOnly; @@ -689,6 +766,7 @@ bool HandleTargetsMode(std::vector<std::string> const& args, ->GetExportSets()[exports] .AddTargetExport(std::move(te)); } + return true; }; switch (target.GetType()) { @@ -700,7 +778,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, // When in namelink only mode skip all libraries on Windows. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) { namelinkOnly = true; - addTargetExport(); + if (!addTargetExport()) { + return false; + } continue; } @@ -736,7 +816,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, // When in namelink only mode skip frameworks. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) { namelinkOnly = true; - addTargetExport(); + if (!addTargetExport()) { + return false; + } continue; } @@ -785,7 +867,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, // When in namelink only mode skip frameworks. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) { namelinkOnly = true; - addTargetExport(); + if (!addTargetExport()) { + return false; + } continue; } @@ -991,8 +1075,39 @@ bool HandleTargetsMode(std::vector<std::string> const& args, } } + if (!namelinkOnly) { + for (std::size_t i = 0; i < fileSetArgs.size(); i++) { + auto* fileSet = target.GetFileSet(fileSetArgs[i].GetFileSet()); + auto interfaceFileSetEntries = cmExpandedList(target.GetSafeProperty( + cmTarget::GetInterfaceFileSetsPropertyName(fileSet->GetType()))); + if (fileSet && + std::find( + interfaceFileSetEntries.begin(), interfaceFileSetEntries.end(), + fileSetArgs[i].GetFileSet()) != interfaceFileSetEntries.end()) { + std::string destination; + if (fileSet->GetType() == "HEADERS"_s) { + destination = helper.GetIncludeDestination(&fileSetArgs[i]); + } else { + destination = fileSetArgs[i].GetDestination(); + if (destination.empty()) { + status.SetError( + cmStrCat("TARGETS given no FILE_SET DESTINATION for target \"", + target.GetName(), "\" file set \"", + fileSet->GetName(), "\".")); + return false; + } + } + fileSetGenerators.push_back(CreateInstallFileSetGenerator( + helper, target, fileSet, destination, fileSetArgs[i])); + installsFileSet[i] = true; + } + } + } + // Add this install rule to an export if one was specified. - addTargetExport(); + if (!addTargetExport()) { + return false; + } // Keep track of whether we're installing anything in each category installsArchive = installsArchive || archiveGenerator; @@ -1016,6 +1131,9 @@ bool HandleTargetsMode(std::vector<std::string> const& args, helper.Makefile->AddInstallGenerator(std::move(privateHeaderGenerator)); helper.Makefile->AddInstallGenerator(std::move(publicHeaderGenerator)); helper.Makefile->AddInstallGenerator(std::move(resourceGenerator)); + for (auto& gen : fileSetGenerators) { + helper.Makefile->AddInstallGenerator(std::move(gen)); + } } if (withRuntimeDependencies && !runtimeDependencySet->Empty()) { @@ -1067,6 +1185,12 @@ bool HandleTargetsMode(std::vector<std::string> const& args, helper.Makefile->GetGlobalGenerator()->AddInstallComponent( resourceArgs.GetComponent()); } + for (std::size_t i = 0; i < fileSetArgs.size(); i++) { + if (installsFileSet[i]) { + helper.Makefile->GetGlobalGenerator()->AddInstallComponent( + fileSetArgs[i].GetComponent()); + } + } return true; } @@ -2063,12 +2187,20 @@ bool Helper::MakeFilesFullPath(const char* modeName, const std::vector<std::string>& relFiles, std::vector<std::string>& absFiles) { + return this->MakeFilesFullPath( + modeName, this->Makefile->GetCurrentSourceDirectory(), relFiles, absFiles); +} + +bool Helper::MakeFilesFullPath(const char* modeName, + const std::string& basePath, + const std::vector<std::string>& relFiles, + std::vector<std::string>& absFiles) +{ for (std::string const& relFile : relFiles) { std::string file = relFile; std::string::size_type gpos = cmGeneratorExpression::Find(file); if (gpos != 0 && !cmSystemTools::FileIsFullPath(file)) { - file = - cmStrCat(this->Makefile->GetCurrentSourceDirectory(), '/', relFile); + file = cmStrCat(basePath, '/', relFile); } // Make sure the file is not a directory. diff --git a/Source/cmInstallCommandArguments.cxx b/Source/cmInstallCommandArguments.cxx index cc3df2a..7309316 100644 --- a/Source/cmInstallCommandArguments.cxx +++ b/Source/cmInstallCommandArguments.cxx @@ -152,6 +152,11 @@ const std::string& cmInstallCommandArguments::GetType() const return this->Type; } +const std::string& cmInstallCommandArguments::GetDefaultComponent() const +{ + return this->DefaultComponentName; +} + const std::vector<std::string>& cmInstallCommandArguments::GetConfigurations() const { @@ -220,3 +225,17 @@ void cmInstallCommandIncludesArgument::Parse( this->IncludeDirs.push_back(std::move(dir)); } } + +cmInstallCommandFileSetArguments::cmInstallCommandFileSetArguments( + std::string defaultComponent) + : cmInstallCommandArguments(std::move(defaultComponent)) +{ + this->Bind("FILE_SET"_s, this->FileSet); +} + +void cmInstallCommandFileSetArguments::Parse( + std::vector<std::string> args, std::vector<std::string>* unconsumedArgs) +{ + args.insert(args.begin(), "FILE_SET"); + this->cmInstallCommandArguments::Parse(args, unconsumedArgs); +} diff --git a/Source/cmInstallCommandArguments.h b/Source/cmInstallCommandArguments.h index f318a1a..79bd945 100644 --- a/Source/cmInstallCommandArguments.h +++ b/Source/cmInstallCommandArguments.h @@ -34,6 +34,8 @@ public: bool HasNamelinkComponent() const; const std::string& GetType() const; + const std::string& GetDefaultComponent() const; + static bool CheckPermissions(const std::string& onePerm, std::string& perm); private: @@ -71,3 +73,17 @@ public: private: std::vector<std::string> IncludeDirs; }; + +class cmInstallCommandFileSetArguments : public cmInstallCommandArguments +{ +public: + cmInstallCommandFileSetArguments(std::string defaultComponent); + + void Parse(std::vector<std::string> args, + std::vector<std::string>* unconsumedArgs); + + const std::string& GetFileSet() const { return this->FileSet; } + +private: + std::string FileSet; +}; diff --git a/Source/cmInstallFileSetGenerator.cxx b/Source/cmInstallFileSetGenerator.cxx new file mode 100644 index 0000000..7121ea3 --- /dev/null +++ b/Source/cmInstallFileSetGenerator.cxx @@ -0,0 +1,88 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "cmInstallFileSetGenerator.h" + +#include <map> +#include <string> +#include <utility> +#include <vector> + +#include "cmFileSet.h" +#include "cmGeneratorExpression.h" +#include "cmGlobalGenerator.h" +#include "cmInstallType.h" +#include "cmLocalGenerator.h" +#include "cmStringAlgorithms.h" + +cmInstallFileSetGenerator::cmInstallFileSetGenerator( + std::string targetName, cmFileSet* fileSet, std::string const& dest, + std::string file_permissions, std::vector<std::string> const& configurations, + std::string const& component, MessageLevel message, bool exclude_from_all, + bool optional, cmListFileBacktrace backtrace) + : cmInstallGenerator(dest, configurations, component, message, + exclude_from_all, false, std::move(backtrace)) + , TargetName(std::move(targetName)) + , FileSet(fileSet) + , FilePermissions(std::move(file_permissions)) + , Optional(optional) +{ + this->ActionsPerConfig = true; +} + +cmInstallFileSetGenerator::~cmInstallFileSetGenerator() = default; + +bool cmInstallFileSetGenerator::Compute(cmLocalGenerator* lg) +{ + this->LocalGenerator = lg; + + // Lookup this target in the current directory. + this->Target = lg->FindLocalNonAliasGeneratorTarget(this->TargetName); + if (!this->Target) { + // If no local target has been found, find it in the global scope. + this->Target = + lg->GetGlobalGenerator()->FindGeneratorTarget(this->TargetName); + } + + return true; +} + +std::string cmInstallFileSetGenerator::GetDestination( + std::string const& config) const +{ + return cmGeneratorExpression::Evaluate(this->Destination, + this->LocalGenerator, config); +} + +void cmInstallFileSetGenerator::GenerateScriptForConfig( + std::ostream& os, const std::string& config, Indent indent) +{ + for (auto const& dirEntry : this->CalculateFilesPerDir(config)) { + std::string destSub; + if (!dirEntry.first.empty()) { + destSub = cmStrCat('/', dirEntry.first); + } + this->AddInstallRule(os, cmStrCat(this->GetDestination(config), destSub), + cmInstallType_FILES, dirEntry.second, + this->GetOptional(), this->FilePermissions.c_str(), + nullptr, nullptr, nullptr, indent); + } +} + +std::map<std::string, std::vector<std::string>> +cmInstallFileSetGenerator::CalculateFilesPerDir( + const std::string& config) const +{ + std::map<std::string, std::vector<std::string>> result; + + auto dirCges = this->FileSet->CompileDirectoryEntries(); + auto dirs = this->FileSet->EvaluateDirectoryEntries( + dirCges, this->LocalGenerator, config, this->Target); + + auto fileCges = this->FileSet->CompileFileEntries(); + for (auto const& fileCge : fileCges) { + this->FileSet->EvaluateFileEntry( + dirs, result, fileCge, this->LocalGenerator, config, this->Target); + } + + return result; +} diff --git a/Source/cmInstallFileSetGenerator.h b/Source/cmInstallFileSetGenerator.h new file mode 100644 index 0000000..8d067d9 --- /dev/null +++ b/Source/cmInstallFileSetGenerator.h @@ -0,0 +1,52 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#pragma once + +#include <iosfwd> +#include <map> +#include <string> +#include <vector> + +#include "cmInstallGenerator.h" +#include "cmListFileCache.h" +#include "cmScriptGenerator.h" + +class cmGeneratorTarget; +class cmFileSet; +class cmLocalGenerator; + +class cmInstallFileSetGenerator : public cmInstallGenerator +{ +public: + cmInstallFileSetGenerator(std::string targetName, cmFileSet* fileSet, + std::string const& dest, + std::string file_permissions, + std::vector<std::string> const& configurations, + std::string const& component, MessageLevel message, + bool exclude_from_all, bool optional, + cmListFileBacktrace backtrace); + ~cmInstallFileSetGenerator() override; + + bool Compute(cmLocalGenerator* lg) override; + + std::string GetDestination(std::string const& config) const; + std::string GetDestination() const { return this->Destination; } + bool GetOptional() const { return this->Optional; } + cmFileSet* GetFileSet() const { return this->FileSet; } + cmGeneratorTarget* GetTarget() const { return this->Target; } + +protected: + void GenerateScriptForConfig(std::ostream& os, const std::string& config, + Indent indent) override; + +private: + std::string TargetName; + cmLocalGenerator* LocalGenerator; + cmFileSet* const FileSet; + std::string const FilePermissions; + bool const Optional; + cmGeneratorTarget* Target; + + std::map<std::string, std::vector<std::string>> CalculateFilesPerDir( + const std::string& config) const; +}; diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index 2b785e1..02b4821 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -219,10 +219,11 @@ std::string cmOutputConverter::EscapeForShell( return Shell_GetArgument(str, flags); } -std::string cmOutputConverter::EscapeForCMake(cm::string_view str) +std::string cmOutputConverter::EscapeForCMake(cm::string_view str, + WrapQuotes wrapQuotes) { // Always double-quote the argument to take care of most escapes. - std::string result = "\""; + std::string result = (wrapQuotes == WrapQuotes::Wrap) ? "\"" : ""; for (const char c : str) { if (c == '"') { // Escape the double quote to avoid ending the argument. @@ -238,7 +239,9 @@ std::string cmOutputConverter::EscapeForCMake(cm::string_view str) result += c; } } - result += "\""; + if (wrapQuotes == WrapQuotes::Wrap) { + result += "\""; + } return result; } diff --git a/Source/cmOutputConverter.h b/Source/cmOutputConverter.h index 865df71..53ec247 100644 --- a/Source/cmOutputConverter.h +++ b/Source/cmOutputConverter.h @@ -94,7 +94,13 @@ public: bool forEcho = false, bool useWatcomQuote = false, bool unescapeNinjaConfiguration = false) const; - static std::string EscapeForCMake(cm::string_view str); + enum class WrapQuotes + { + Wrap, + NoWrap, + }; + static std::string EscapeForCMake(cm::string_view str, + WrapQuotes wrapQuotes = WrapQuotes::Wrap); /** Compute an escaped version of the given argument for use in a windows shell. */ diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 97d60cf..4f0dc60 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -14,11 +14,13 @@ #include <cm/memory> #include <cmext/algorithm> +#include <cmext/string_view> #include "cmsys/RegularExpression.hxx" #include "cmAlgorithms.h" #include "cmCustomCommand.h" +#include "cmFileSet.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" @@ -200,8 +202,11 @@ public: std::vector<BT<std::string>> LinkOptionsEntries; std::vector<BT<std::string>> LinkDirectoriesEntries; std::vector<BT<std::string>> LinkImplementationPropertyEntries; + std::vector<BT<std::string>> HeaderSetsEntries; + std::vector<BT<std::string>> InterfaceHeaderSetsEntries; std::vector<std::pair<cmTarget::TLLSignature, cmListFileContext>> TLLCommands; + std::map<std::string, cmFileSet> FileSets; cmListFileBacktrace Backtrace; bool CheckImportedLibName(std::string const& prop, @@ -1110,6 +1115,16 @@ cmBTStringRange cmTarget::GetLinkImplementationEntries() const return cmMakeRange(this->impl->LinkImplementationPropertyEntries); } +cmBTStringRange cmTarget::GetHeaderSetsEntries() const +{ + return cmMakeRange(this->impl->HeaderSetsEntries); +} + +cmBTStringRange cmTarget::GetInterfaceHeaderSetsEntries() const +{ + return cmMakeRange(this->impl->InterfaceHeaderSetsEntries); +} + namespace { #define MAKE_PROP(PROP) const std::string prop##PROP = #PROP MAKE_PROP(C_STANDARD); @@ -1139,6 +1154,10 @@ MAKE_PROP(BINARY_DIR); MAKE_PROP(SOURCE_DIR); MAKE_PROP(FALSE); MAKE_PROP(TRUE); +MAKE_PROP(HEADER_DIRS); +MAKE_PROP(HEADER_SET); +MAKE_PROP(HEADER_SETS); +MAKE_PROP(INTERFACE_HEADER_SETS); #undef MAKE_PROP } @@ -1158,6 +1177,21 @@ std::string ConvertToString<cmValue>(cmValue value) { return std::string(*value); } + +template <typename ValueType> +bool StringIsEmpty(ValueType value); + +template <> +bool StringIsEmpty<const char*>(const char* value) +{ + return cmValue::IsEmpty(value); +} + +template <> +bool StringIsEmpty<cmValue>(cmValue value) +{ + return value.IsEmpty(); +} } template <typename ValueType> @@ -1321,6 +1355,104 @@ void cmTarget::StoreProperty(const std::string& prop, ValueType value) } else { this->impl->LanguageStandardProperties.erase(prop); } + } else if (prop == propHEADER_DIRS) { + auto* fileSet = this->GetFileSet("HEADERS"); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "The default header set has not yet been created."); + return; + } + fileSet->ClearDirectoryEntries(); + if (!StringIsEmpty(value)) { + fileSet->AddDirectoryEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } + } else if (prop == propHEADER_SET) { + auto* fileSet = this->GetFileSet("HEADERS"); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "The default header set has not yet been created."); + return; + } + fileSet->ClearFileEntries(); + if (!StringIsEmpty(value)) { + fileSet->AddFileEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } + } else if (cmHasLiteralPrefix(prop, "HEADER_DIRS_")) { + auto fileSetName = prop.substr(cmStrLen("HEADER_DIRS_")); + if (fileSetName.empty()) { + this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, + "Header set name cannot be empty."); + return; + } + auto* fileSet = this->GetFileSet(fileSetName); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", fileSetName, + "\" has not yet been created.")); + return; + } + fileSet->ClearDirectoryEntries(); + if (!StringIsEmpty(value)) { + fileSet->AddDirectoryEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } + } else if (cmHasLiteralPrefix(prop, "HEADER_SET_")) { + auto fileSetName = prop.substr(cmStrLen("HEADER_SET_")); + if (fileSetName.empty()) { + this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, + "Header set name cannot be empty."); + return; + } + auto* fileSet = this->GetFileSet(fileSetName); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", fileSetName, + "\" has not yet been created.")); + return; + } + fileSet->ClearFileEntries(); + if (!StringIsEmpty(value)) { + fileSet->AddFileEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } + } else if (prop == propHEADER_SETS) { + if (value) { + for (auto const& name : cmExpandedList(value)) { + if (!this->GetFileSet(name)) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", name, "\" has not yet been created.")); + return; + } + } + } + this->impl->HeaderSetsEntries.clear(); + if (!StringIsEmpty(value)) { + this->impl->HeaderSetsEntries.emplace_back( + value, this->impl->Makefile->GetBacktrace()); + } + } else if (prop == propINTERFACE_HEADER_SETS) { + if (value) { + for (auto const& name : cmExpandedList(value)) { + if (!this->GetFileSet(name)) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", name, "\" has not yet been created.")); + return; + } + } + } + this->impl->InterfaceHeaderSetsEntries.clear(); + if (!StringIsEmpty(value)) { + this->impl->InterfaceHeaderSetsEntries.emplace_back( + value, this->impl->Makefile->GetBacktrace()); + } } else { this->impl->Properties.SetProperty(prop, value); } @@ -1415,6 +1547,82 @@ void cmTarget::AppendProperty(const std::string& prop, prop == "OBJC_STANDARD" || prop == "OBJCXX_STANDARD") { this->impl->Makefile->IssueMessage( MessageType::FATAL_ERROR, prop + " property may not be appended."); + } else if (prop == "HEADER_DIRS") { + auto* fileSet = this->GetFileSet("HEADERS"); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "The default header set has not yet been created."); + return; + } + fileSet->AddDirectoryEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } else if (cmHasLiteralPrefix(prop, "HEADER_DIRS_")) { + auto fileSetName = prop.substr(cmStrLen("HEADER_DIRS_")); + if (fileSetName.empty()) { + this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, + "Header set name cannot be empty."); + return; + } + auto* fileSet = this->GetFileSet(fileSetName); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", fileSetName, + "\" has not yet been created.")); + return; + } + fileSet->AddDirectoryEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } else if (prop == "HEADER_SET") { + auto* fileSet = this->GetFileSet("HEADERS"); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "The default header set has not yet been created."); + return; + } + fileSet->AddFileEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } else if (cmHasLiteralPrefix(prop, "HEADER_SET_")) { + auto fileSetName = prop.substr(cmStrLen("HEADER_SET_")); + if (fileSetName.empty()) { + this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, + "Header set name cannot be empty."); + return; + } + auto* fileSet = this->GetFileSet(fileSetName); + if (!fileSet) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", fileSetName, + "\" has not yet been created.")); + return; + } + fileSet->AddFileEntry( + BT<std::string>(value, this->impl->Makefile->GetBacktrace())); + } else if (prop == "HEADER_SETS") { + for (auto const& name : cmExpandedList(value)) { + if (!this->GetFileSet(name)) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", name, "\" has not yet been created.")); + return; + } + } + this->impl->HeaderSetsEntries.emplace_back( + value, this->impl->Makefile->GetBacktrace()); + } else if (prop == "INTERFACE_HEADER_SETS") { + for (auto const& name : cmExpandedList(value)) { + if (!this->GetFileSet(name)) { + this->impl->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("Header set \"", name, "\" has not yet been created.")); + return; + } + } + this->impl->InterfaceHeaderSetsEntries.emplace_back( + value, this->impl->Makefile->GetBacktrace()); } else { this->impl->Properties.AppendProperty(prop, value, asString); } @@ -1633,7 +1841,11 @@ cmValue cmTarget::GetProperty(const std::string& prop) const propNAME, propBINARY_DIR, propSOURCE_DIR, - propSOURCES + propSOURCES, + propHEADER_DIRS, + propHEADER_SET, + propHEADER_SETS, + propINTERFACE_HEADER_SETS, }; if (specialProps.count(prop)) { if (prop == propC_STANDARD || prop == propCXX_STANDARD || @@ -1759,6 +1971,60 @@ cmValue cmTarget::GetProperty(const std::string& prop) const .GetDirectory() .GetCurrentSource()); } + if (prop == propHEADER_DIRS) { + auto const* fileSet = this->GetFileSet("HEADERS"); + if (!fileSet) { + return nullptr; + } + static std::string output; + output = cmJoin(fileSet->GetDirectoryEntries(), ";"_s); + return cmValue(output); + } + if (prop == propHEADER_SET) { + auto const* fileSet = this->GetFileSet("HEADERS"); + if (!fileSet) { + return nullptr; + } + static std::string output; + output = cmJoin(fileSet->GetFileEntries(), ";"_s); + return cmValue(output); + } + if (prop == propHEADER_SETS) { + static std::string output; + output = cmJoin(this->impl->HeaderSetsEntries, ";"_s); + return cmValue(output); + } + if (prop == propINTERFACE_HEADER_SETS) { + static std::string output; + output = cmJoin(this->impl->InterfaceHeaderSetsEntries, ";"_s); + return cmValue(output); + } + } + if (cmHasLiteralPrefix(prop, "HEADER_DIRS_")) { + std::string fileSetName = prop.substr(cmStrLen("HEADER_DIRS_")); + if (fileSetName.empty()) { + return nullptr; + } + auto const* fileSet = this->GetFileSet(fileSetName); + if (!fileSet) { + return nullptr; + } + static std::string output; + output = cmJoin(fileSet->GetDirectoryEntries(), ";"_s); + return cmValue(output); + } + if (cmHasLiteralPrefix(prop, "HEADER_SET_")) { + std::string fileSetName = prop.substr(cmStrLen("HEADER_SET_")); + if (fileSetName.empty()) { + return nullptr; + } + auto const* fileSet = this->GetFileSet(fileSetName); + if (!fileSet) { + return nullptr; + } + static std::string output; + output = cmJoin(fileSet->GetFileEntries(), ";"_s); + return cmValue(output); } cmValue retVal = this->impl->Properties.GetPropertyValue(prop); @@ -2015,6 +2281,59 @@ std::string cmTarget::ImportedGetFullPath( return result; } +const cmFileSet* cmTarget::GetFileSet(const std::string& name) const +{ + auto it = this->impl->FileSets.find(name); + return it == this->impl->FileSets.end() ? nullptr : &it->second; +} + +cmFileSet* cmTarget::GetFileSet(const std::string& name) +{ + auto it = this->impl->FileSets.find(name); + return it == this->impl->FileSets.end() ? nullptr : &it->second; +} + +std::pair<cmFileSet*, bool> cmTarget::GetOrCreateFileSet( + const std::string& name, const std::string& type) +{ + auto result = + this->impl->FileSets.emplace(std::make_pair(name, cmFileSet(name, type))); + return std::make_pair(&result.first->second, result.second); +} + +std::string cmTarget::GetFileSetsPropertyName(const std::string& type) +{ + if (type == "HEADERS") { + return "HEADER_SETS"; + } + return ""; +} + +std::string cmTarget::GetInterfaceFileSetsPropertyName(const std::string& type) +{ + if (type == "HEADERS") { + return "INTERFACE_HEADER_SETS"; + } + return ""; +} + +std::vector<std::string> cmTarget::GetAllInterfaceFileSets() const +{ + std::vector<std::string> result; + auto inserter = std::back_inserter(result); + + auto appendEntries = [=](const std::vector<BT<std::string>>& entries) { + for (auto const& entry : entries) { + auto expanded = cmExpandedList(entry.Value); + std::copy(expanded.begin(), expanded.end(), inserter); + } + }; + + appendEntries(this->impl->InterfaceHeaderSetsEntries); + + return result; +} + bool cmTargetInternals::CheckImportedLibName(std::string const& prop, std::string const& value) const { diff --git a/Source/cmTarget.h b/Source/cmTarget.h index 3cf6942..27b325a 100644 --- a/Source/cmTarget.h +++ b/Source/cmTarget.h @@ -20,6 +20,7 @@ #include "cmValue.h" class cmCustomCommand; +class cmFileSet; class cmGlobalGenerator; class cmInstallTargetGenerator; class cmMakefile; @@ -260,6 +261,10 @@ public: cmBTStringRange GetLinkImplementationEntries() const; + cmBTStringRange GetHeaderSetsEntries() const; + + cmBTStringRange GetInterfaceHeaderSetsEntries() const; + std::string ImportedGetFullPath(const std::string& config, cmStateEnums::ArtifactType artifact) const; @@ -268,6 +273,16 @@ public: bool operator()(cmTarget const* t1, cmTarget const* t2) const; }; + const cmFileSet* GetFileSet(const std::string& name) const; + cmFileSet* GetFileSet(const std::string& name); + std::pair<cmFileSet*, bool> GetOrCreateFileSet(const std::string& name, + const std::string& type); + + std::vector<std::string> GetAllInterfaceFileSets() const; + + static std::string GetFileSetsPropertyName(const std::string& type); + static std::string GetInterfaceFileSetsPropertyName(const std::string& type); + private: template <typename ValueType> void StoreProperty(const std::string& prop, ValueType value); diff --git a/Source/cmTargetExport.h b/Source/cmTargetExport.h index 19fc931..885ac74 100644 --- a/Source/cmTargetExport.h +++ b/Source/cmTargetExport.h @@ -6,7 +6,9 @@ #include <string> +class cmFileSet; class cmGeneratorTarget; +class cmInstallFileSetGenerator; class cmInstallFilesGenerator; class cmInstallTargetGenerator; @@ -29,6 +31,7 @@ public: cmInstallTargetGenerator* FrameworkGenerator; cmInstallTargetGenerator* BundleGenerator; cmInstallFilesGenerator* HeaderGenerator; + std::map<cmFileSet*, cmInstallFileSetGenerator*> FileSetGenerators; ///@} bool NamelinkOnly = false; diff --git a/Source/cmTargetPropCommandBase.cxx b/Source/cmTargetPropCommandBase.cxx index 3bd1ea3..391b954 100644 --- a/Source/cmTargetPropCommandBase.cxx +++ b/Source/cmTargetPropCommandBase.cxx @@ -155,10 +155,10 @@ bool cmTargetPropCommandBase::ProcessContentArgs( return false; } } - return this->PopulateTargetProperies(scope, content, prepend, system); + return this->PopulateTargetProperties(scope, content, prepend, system); } -bool cmTargetPropCommandBase::PopulateTargetProperies( +bool cmTargetPropCommandBase::PopulateTargetProperties( const std::string& scope, const std::vector<std::string>& content, bool prepend, bool system) { diff --git a/Source/cmTargetPropCommandBase.h b/Source/cmTargetPropCommandBase.h index fc24fe8..6bf7c3c 100644 --- a/Source/cmTargetPropCommandBase.h +++ b/Source/cmTargetPropCommandBase.h @@ -40,6 +40,9 @@ protected: virtual void HandleInterfaceContent(cmTarget* tgt, const std::vector<std::string>& content, bool prepend, bool system); + virtual bool PopulateTargetProperties( + const std::string& scope, const std::vector<std::string>& content, + bool prepend, bool system); private: virtual void HandleMissingTarget(const std::string& name) = 0; @@ -52,9 +55,6 @@ private: bool ProcessContentArgs(std::vector<std::string> const& args, unsigned int& argIndex, bool prepend, bool system); - bool PopulateTargetProperies(const std::string& scope, - const std::vector<std::string>& content, - bool prepend, bool system); cmExecutionStatus& Status; }; diff --git a/Source/cmTargetSourcesCommand.cxx b/Source/cmTargetSourcesCommand.cxx index 26282ef..818e271 100644 --- a/Source/cmTargetSourcesCommand.cxx +++ b/Source/cmTargetSourcesCommand.cxx @@ -2,9 +2,17 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmTargetSourcesCommand.h" +#include <algorithm> #include <sstream> +#include <utility> +#include <cm/string_view> +#include <cmext/string_view> + +#include "cmArgumentParser.h" +#include "cmFileSet.h" #include "cmGeneratorExpression.h" +#include "cmListFileCache.h" #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" @@ -15,6 +23,20 @@ namespace { +struct FileSetArgs +{ + std::string Type; + std::string FileSet; + std::vector<std::string> BaseDirs; + std::vector<std::string> Files; +}; + +auto const FileSetArgsParser = cmArgumentParser<FileSetArgs>() + .Bind("TYPE"_s, &FileSetArgs::Type) + .Bind("FILE_SET"_s, &FileSetArgs::FileSet) + .Bind("BASE_DIRS"_s, &FileSetArgs::BaseDirs) + .Bind("FILES"_s, &FileSetArgs::Files); + class TargetSourcesImpl : public cmTargetPropCommandBase { public: @@ -26,8 +48,10 @@ protected: bool prepend, bool system) override { this->cmTargetPropCommandBase::HandleInterfaceContent( - tgt, this->ConvertToAbsoluteContent(tgt, content, true), prepend, - system); + tgt, + this->ConvertToAbsoluteContent(tgt, content, IsInterface::Yes, + CheckCMP0076::Yes), + prepend, system); } private: @@ -43,29 +67,55 @@ private: const std::vector<std::string>& content, bool /*prepend*/, bool /*system*/) override { - tgt->AppendProperty( - "SOURCES", - this->Join(this->ConvertToAbsoluteContent(tgt, content, false))); + tgt->AppendProperty("SOURCES", + this->Join(this->ConvertToAbsoluteContent( + tgt, content, IsInterface::No, CheckCMP0076::Yes))); return true; // Successfully handled. } + bool PopulateTargetProperties(const std::string& scope, + const std::vector<std::string>& content, + bool prepend, bool system) override + { + if (!content.empty() && content.front() == "FILE_SET"_s) { + return this->HandleFileSetMode(scope, content, prepend, system); + } + return this->cmTargetPropCommandBase::PopulateTargetProperties( + scope, content, prepend, system); + } + std::string Join(const std::vector<std::string>& content) override { return cmJoin(content, ";"); } + enum class IsInterface + { + Yes, + No, + }; + enum class CheckCMP0076 + { + Yes, + No, + }; std::vector<std::string> ConvertToAbsoluteContent( cmTarget* tgt, const std::vector<std::string>& content, - bool isInterfaceContent); + IsInterface isInterfaceContent, CheckCMP0076 checkCmp0076); + + bool HandleFileSetMode(const std::string& scope, + const std::vector<std::string>& content, bool prepend, + bool system); }; std::vector<std::string> TargetSourcesImpl::ConvertToAbsoluteContent( cmTarget* tgt, const std::vector<std::string>& content, - bool isInterfaceContent) + IsInterface isInterfaceContent, CheckCMP0076 checkCmp0076) { // Skip conversion in case old behavior has been explicitly requested - if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076) == - cmPolicies::OLD) { + if (checkCmp0076 == CheckCMP0076::Yes && + this->Makefile->GetPolicyStatus(cmPolicies::CMP0076) == + cmPolicies::OLD) { return content; } @@ -76,7 +126,7 @@ std::vector<std::string> TargetSourcesImpl::ConvertToAbsoluteContent( std::string absoluteSrc; if (cmSystemTools::FileIsFullPath(src) || cmGeneratorExpression::Find(src) == 0 || - (!isInterfaceContent && + (isInterfaceContent == IsInterface::No && (this->Makefile->GetCurrentSourceDirectory() == tgt->GetMakefile()->GetCurrentSourceDirectory()))) { absoluteSrc = src; @@ -95,28 +145,33 @@ std::vector<std::string> TargetSourcesImpl::ConvertToAbsoluteContent( bool issueMessage = true; bool useAbsoluteContent = false; std::ostringstream e; - switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076)) { - case cmPolicies::WARN: - e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0076) << "\n"; - break; - case cmPolicies::OLD: - issueMessage = false; - break; - case cmPolicies::REQUIRED_ALWAYS: - case cmPolicies::REQUIRED_IF_USED: - this->Makefile->IssueMessage( - MessageType::FATAL_ERROR, - cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0076)); - break; - case cmPolicies::NEW: { - issueMessage = false; - useAbsoluteContent = true; - break; + if (checkCmp0076 == CheckCMP0076::Yes) { + switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076)) { + case cmPolicies::WARN: + e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0076) << "\n"; + break; + case cmPolicies::OLD: + issueMessage = false; + break; + case cmPolicies::REQUIRED_ALWAYS: + case cmPolicies::REQUIRED_IF_USED: + this->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0076)); + break; + case cmPolicies::NEW: { + issueMessage = false; + useAbsoluteContent = true; + break; + } } + } else { + issueMessage = false; + useAbsoluteContent = true; } if (issueMessage) { - if (isInterfaceContent) { + if (isInterfaceContent == IsInterface::Yes) { e << "An interface source of target \"" << tgt->GetName() << "\" has a relative path."; } else { @@ -129,6 +184,133 @@ std::vector<std::string> TargetSourcesImpl::ConvertToAbsoluteContent( return useAbsoluteContent ? absoluteContent : content; } +bool TargetSourcesImpl::HandleFileSetMode( + const std::string& scope, const std::vector<std::string>& content, + bool /*prepend*/, bool /*system*/) +{ + std::vector<std::string> unparsed; + auto args = FileSetArgsParser.Parse(content, &unparsed); + + if (!unparsed.empty()) { + this->SetError( + cmStrCat("Unrecognized keyword: \"", unparsed.front(), "\"")); + return false; + } + + if (args.FileSet.empty()) { + this->SetError("FILE_SET must not be empty"); + return false; + } + + bool const isDefault = args.Type == args.FileSet || + (args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z'); + std::string type = isDefault ? args.FileSet : args.Type; + + auto fileSet = this->Target->GetOrCreateFileSet(args.FileSet, type); + if (fileSet.second) { + if (!isDefault) { + if (args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z') { + this->SetError( + "Non-default file set name must not start with a capital letter"); + return false; + } + } + if (type.empty()) { + this->SetError("Must specify a TYPE when creating file set"); + return false; + } + if (type != "HEADERS"_s) { + this->SetError("File set TYPE may only be \"HEADERS\""); + return false; + } + + if (args.BaseDirs.empty()) { + args.BaseDirs.emplace_back(this->Makefile->GetCurrentSourceDirectory()); + } + + if (scope == "PRIVATE"_s || scope == "PUBLIC"_s) { + this->Target->AppendProperty(cmTarget::GetFileSetsPropertyName(type), + args.FileSet); + } + if (scope == "INTERFACE"_s || scope == "PUBLIC"_s) { + this->Target->AppendProperty( + cmTarget::GetInterfaceFileSetsPropertyName(type), args.FileSet); + } + } else { + type = fileSet.first->GetType(); + if (!args.Type.empty() && args.Type != type) { + this->SetError(cmStrCat( + "Type \"", args.Type, "\" for file set \"", fileSet.first->GetName(), + "\" does not match original type \"", type, "\"")); + return false; + } + + std::string existingScope = "PRIVATE"; + + auto const fileSetsProperty = cmTarget::GetFileSetsPropertyName(type); + auto const interfaceFileSetsProperty = + cmTarget::GetInterfaceFileSetsPropertyName(type); + std::vector<std::string> fileSets; + std::vector<std::string> interfaceFileSets; + cmExpandList(this->Target->GetSafeProperty(fileSetsProperty), fileSets); + cmExpandList(this->Target->GetSafeProperty(interfaceFileSetsProperty), + interfaceFileSets); + + if (std::find(interfaceFileSets.begin(), interfaceFileSets.end(), + args.FileSet) != interfaceFileSets.end()) { + existingScope = "INTERFACE"; + } + if (std::find(fileSets.begin(), fileSets.end(), args.FileSet) != + fileSets.end()) { + if (existingScope == "INTERFACE"_s) { + existingScope = "PUBLIC"; + } + } else if (existingScope != "INTERFACE"_s) { + this->SetError(cmStrCat("File set \"", args.FileSet, "\" is not in ", + fileSetsProperty, " or ", + interfaceFileSetsProperty)); + return false; + } + + if (scope != existingScope) { + this->SetError( + cmStrCat("Scope ", scope, " for file set \"", args.FileSet, + "\" does not match original scope ", existingScope)); + return false; + } + } + + auto files = this->Join(this->ConvertToAbsoluteContent( + this->Target, args.Files, IsInterface::Yes, CheckCMP0076::No)); + if (!files.empty()) { + fileSet.first->AddFileEntry( + BT<std::string>(files, this->Makefile->GetBacktrace())); + } + + auto baseDirectories = this->Join(this->ConvertToAbsoluteContent( + this->Target, args.BaseDirs, IsInterface::Yes, CheckCMP0076::No)); + if (!baseDirectories.empty()) { + fileSet.first->AddDirectoryEntry( + BT<std::string>(baseDirectories, this->Makefile->GetBacktrace())); + if (type == "HEADERS"_s) { + for (auto const& dir : cmExpandedList(baseDirectories)) { + auto interfaceDirectoriesGenex = + cmStrCat("$<BUILD_INTERFACE:", dir, ">"); + if (scope == "PRIVATE"_s || scope == "PUBLIC"_s) { + this->Target->AppendProperty("INCLUDE_DIRECTORIES", + interfaceDirectoriesGenex); + } + if (scope == "INTERFACE"_s || scope == "PUBLIC"_s) { + this->Target->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES", + interfaceDirectoriesGenex); + } + } + } + } + + return true; +} + } // namespace bool cmTargetSourcesCommand(std::vector<std::string> const& args, |