1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmExportInstallPackageInfoGenerator.h"
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
#include <cm/optional>
#include <cm/string_view>
#include <cmext/algorithm>
#include <cmext/string_view>
#include <cm3p/json/value.h>
#include "cmAlgorithms.h"
#include "cmExportSet.h"
#include "cmFileSet.h"
#include "cmGeneratorExpression.h"
#include "cmGeneratorTarget.h"
#include "cmInstallExportGenerator.h"
#include "cmInstallFileSetGenerator.h"
#include "cmList.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmOutputConverter.h"
#include "cmPackageInfoArguments.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
#include "cmTargetExport.h"
cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator(
cmInstallExportGenerator* iegen, cmPackageInfoArguments arguments)
: cmExportPackageInfoGenerator(std::move(arguments))
, cmExportInstallFileGenerator(iegen)
{
}
std::string cmExportInstallPackageInfoGenerator::GetConfigImportFileGlob()
const
{
std::string glob = cmStrCat(this->FileBase, "@*", this->FileExt);
return glob;
}
std::string const& cmExportInstallPackageInfoGenerator::GetExportName() const
{
return this->GetPackageName();
}
bool cmExportInstallPackageInfoGenerator::GenerateMainFile(std::ostream& os)
{
std::vector<cmTargetExport const*> allTargets;
{
auto visitor = [&](cmTargetExport const* te) { allTargets.push_back(te); };
if (!this->CollectExports(visitor)) {
return false;
}
}
if (!this->CheckDefaultTargets()) {
return false;
}
Json::Value root = this->GeneratePackageInfo();
Json::Value& components = root["components"];
// Compute the relative import prefix for the file
std::string const& packagePath = this->GenerateImportPrefix();
if (packagePath.empty()) {
return false;
}
root["cps_path"] = packagePath;
// Create all the imported targets.
for (cmTargetExport const* te : allTargets) {
cmGeneratorTarget* gt = te->Target;
cmStateEnums::TargetType targetType = this->GetExportTargetType(te);
Json::Value* const component =
this->GenerateImportTarget(components, gt, targetType);
if (!component) {
return false;
}
ImportPropertyMap properties;
if (!this->PopulateInterfaceProperties(te, properties)) {
return false;
}
this->PopulateInterfaceLinkLibrariesProperty(
gt, cmGeneratorExpression::InstallInterface, properties);
if (targetType != cmStateEnums::INTERFACE_LIBRARY) {
this->RequiresConfigFiles = true;
}
// De-duplicate include directories prior to generation.
auto it = properties.find("INTERFACE_INCLUDE_DIRECTORIES");
if (it != properties.end()) {
auto list = cmList{ it->second };
list = cmList{ list.begin(), cmRemoveDuplicates(list) };
properties["INTERFACE_INCLUDE_DIRECTORIES"] = list.to_string();
}
// Set configuration-agnostic properties for component.
this->GenerateInterfaceProperties(*component, gt, properties);
if (!this->GenerateFileSetProperties(*component, gt, te)) {
return false;
}
}
this->GeneratePackageRequires(root);
// Write the primary packing information file.
this->WritePackageInfo(root, os);
bool result = true;
// Generate an import file for each configuration.
if (this->RequiresConfigFiles) {
for (std::string const& c : this->Configurations) {
if (!this->GenerateImportFileConfig(c)) {
result = false;
}
}
}
return result;
}
void cmExportInstallPackageInfoGenerator::GenerateImportTargetsConfig(
std::ostream& os, std::string const& config, std::string const& suffix)
{
Json::Value root;
root["name"] = this->GetPackageName();
root["configuration"] = (config.empty() ? "noconfig" : config);
Json::Value& components = root["components"];
for (auto const& te : this->GetExportSet()->GetTargetExports()) {
// Collect import properties for this target.
ImportPropertyMap properties;
std::set<std::string> importedLocations;
if (this->GetExportTargetType(te.get()) !=
cmStateEnums::INTERFACE_LIBRARY) {
this->PopulateImportProperties(config, suffix, te.get(), properties,
importedLocations);
}
Json::Value component =
this->GenerateInterfaceConfigProperties(suffix, properties);
this->GenerateFileSetProperties(component, te->Target, te.get(), config);
if (!component.empty()) {
components[te->Target->GetExportName()] = std::move(component);
}
}
this->WritePackageInfo(root, os);
}
std::string cmExportInstallPackageInfoGenerator::GenerateImportPrefix() const
{
std::string expDest = this->IEGen->GetDestination();
if (cmSystemTools::FileIsFullPath(expDest)) {
std::string const& installPrefix =
this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition(
"CMAKE_INSTALL_PREFIX");
if (cmHasPrefix(expDest, installPrefix)) {
auto n = installPrefix.length();
while (n < expDest.length() && expDest[n] == '/') {
++n;
}
expDest = expDest.substr(n);
} else {
this->ReportError(
cmStrCat("install(PACKAGE_INFO \"", this->GetExportName(),
"\" ...) specifies DESTINATION \"", expDest,
"\" which is not a subdirectory of the install prefix."));
return {};
}
}
if (expDest.empty()) {
return this->GetInstallPrefix();
}
return cmStrCat(this->GetImportPrefixWithSlash(), expDest);
}
std::string cmExportInstallPackageInfoGenerator::InstallNameDir(
cmGeneratorTarget const* target, std::string const& config)
{
std::string install_name_dir;
cmMakefile* mf = target->Target->GetMakefile();
if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
install_name_dir =
target->GetInstallNameDirForInstallTree(config, "@prefix@");
}
return install_name_dir;
}
std::string cmExportInstallPackageInfoGenerator::GetCxxModulesDirectory() const
{
// TODO: Implement a not-CMake-specific mechanism for providing module
// information.
// return IEGen->GetCxxModuleDirectory();
return {};
}
cm::optional<std::string>
cmExportInstallPackageInfoGenerator::GetFileSetDirectory(
cmGeneratorTarget* gte, cmTargetExport const* te, cmFileSet* fileSet,
cm::optional<std::string> const& config)
{
cmGeneratorExpression ge(*gte->Makefile->GetCMakeInstance());
auto cge =
ge.Parse(te->FileSetGenerators.at(fileSet->GetName())->GetDestination());
std::string const unescapedDest =
cge->Evaluate(gte->LocalGenerator, config.value_or(""), gte);
bool const isConfigDependent = cge->GetHadContextSensitiveCondition();
if (config && !isConfigDependent) {
return {};
}
if (!config && isConfigDependent) {
this->RequiresConfigFiles = true;
return {};
}
std::string const& type = fileSet->GetType();
if (config && (type == "CXX_MODULES"_s)) {
// C++ modules do not support interface file sets which are dependent
// upon the configuration.
cmMakefile* mf = gte->LocalGenerator->GetMakefile();
std::ostringstream e;
e << "The \"" << gte->GetName() << "\" target's interface file set \""
<< fileSet->GetName() << "\" of type \"" << type
<< "\" contains context-sensitive base file entries which is not "
"supported.";
mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
return {};
}
cm::optional<std::string> dest = cmOutputConverter::EscapeForCMake(
unescapedDest, cmOutputConverter::WrapQuotes::NoWrap);
if (!cmSystemTools::FileIsFullPath(unescapedDest)) {
dest = cmStrCat("@prefix@/"_s, *dest);
}
return dest;
}
bool cmExportInstallPackageInfoGenerator::GenerateFileSetProperties(
Json::Value& component, cmGeneratorTarget* gte, cmTargetExport const* te,
cm::optional<std::string> config)
{
std::set<std::string> seenIncludeDirectories;
for (auto const& name : gte->Target->GetAllInterfaceFileSets()) {
cmFileSet* 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 false;
}
cm::optional<std::string> const& fileSetDirectory =
this->GetFileSetDirectory(gte, te, fileSet, config);
if (fileSet->GetType() == "HEADERS"_s) {
if (fileSetDirectory &&
!cm::contains(seenIncludeDirectories, *fileSetDirectory)) {
component["includes"].append(*fileSetDirectory);
seenIncludeDirectories.insert(*fileSetDirectory);
}
} else if (fileSet->GetType() == "CXX_MODULES"_s) {
/* TODO: Handle the CXX_MODULE directory */
}
}
return true;
}
|