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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCPackPKGGenerator.h"
#include <vector>
#include <cmext/string_view>
#include "cmCPackComponentGroup.h"
#include "cmCPackGenerator.h"
#include "cmCPackLog.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmValue.h"
#include "cmXMLWriter.h"
cmCPackPKGGenerator::cmCPackPKGGenerator()
{
this->componentPackageMethod = ONE_PACKAGE;
}
cmCPackPKGGenerator::~cmCPackPKGGenerator() = default;
bool cmCPackPKGGenerator::SupportsComponentInstallation() const
{
return true;
}
int cmCPackPKGGenerator::InitializeInternal()
{
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"cmCPackPKGGenerator::Initialize()" << std::endl);
return this->Superclass::InitializeInternal();
}
std::string cmCPackPKGGenerator::GetPackageName(
cmCPackComponent const& component)
{
if (component.ArchiveFile.empty()) {
std::string packagesDir =
cmStrCat(this->GetOption("CPACK_TEMPORARY_DIRECTORY"), ".dummy");
return cmStrCat(
cmSystemTools::GetFilenameWithoutLastExtension(packagesDir), '-',
component.Name, ".pkg");
}
return cmStrCat(component.ArchiveFile, ".pkg");
}
void cmCPackPKGGenerator::CreateBackground(char const* themeName,
char const* metapackageFile,
cm::string_view genName,
cmXMLWriter& xout)
{
std::string paramSuffix =
themeName ? cmSystemTools::UpperCase(themeName) : "";
std::string opt = themeName
? cmStrCat("CPACK_", genName, "_BACKGROUND_", paramSuffix)
: cmStrCat("CPACK_", genName, "_BACKGROUND");
cmValue bgFileName = this->GetOption(opt);
if (!bgFileName) {
return;
}
std::string bgFilePath =
cmStrCat(metapackageFile, "/Contents/", *bgFileName);
if (!cmSystemTools::FileExists(bgFilePath)) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Background image doesn't exist in the resource directory: "
<< *bgFileName << std::endl);
return;
}
if (themeName) {
xout.StartElement(cmStrCat("background-", themeName));
} else {
xout.StartElement("background");
}
xout.Attribute("file", *bgFileName);
cmValue param = this->GetOption(cmStrCat(opt, "_ALIGNMENT"));
if (param) {
xout.Attribute("alignment", *param);
}
param = this->GetOption(cmStrCat(opt, "_SCALING"));
if (param) {
xout.Attribute("scaling", *param);
}
// Apple docs say that you must provide either mime-type or uti
// attribute for the background, but I've seen examples that
// doesn't have them, so don't make them mandatory.
param = this->GetOption(cmStrCat(opt, "_MIME_TYPE"));
if (param) {
xout.Attribute("mime-type", *param);
}
param = this->GetOption(cmStrCat(opt, "_UTI"));
if (param) {
xout.Attribute("uti", *param);
}
xout.EndElement();
}
void cmCPackPKGGenerator::WriteDistributionFile(char const* metapackageFile,
char const* genName)
{
std::string distributionTemplate =
this->FindTemplate("CPack.distribution.dist.in");
if (distributionTemplate.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot find input file: " << distributionTemplate
<< std::endl);
return;
}
std::string distributionFile =
cmStrCat(metapackageFile, "/Contents/distribution.dist");
std::ostringstream xContents;
cmXMLWriter xout(xContents, 1);
// Installer-wide options and domains. These need to be separate from the
// choices and background elements added further below so that we can
// preserve backward compatibility.
xout.StartElement("options");
xout.Attribute("allow-external-scripts", "no");
xout.Attribute("customize", "allow");
if (cmIsOff(this->GetOption("CPACK_PRODUCTBUILD_DOMAINS"))) {
xout.Attribute("rootVolumeOnly", "false");
}
xout.EndElement();
this->CreateDomains(xout);
// In order to preserve backward compatibility, all elements added below
// here need to be made available in a variable named
// CPACK_PACKAGEMAKER_CHOICES. The above elements are new and only appear
// in the CPACK_APPLE_PKG_INSTALLER_CONTENT variable, which is a superset
// of what CPACK_PACKAGEMAKER_CHOICES used to provide. The renaming reflects
// the fact that CMake has deprecated the PackageMaker generator.
// Create the choice outline, which provides a tree-based view of
// the components in their groups.
std::ostringstream choiceOut;
cmXMLWriter xChoiceOut(choiceOut, 1);
xChoiceOut.StartElement("choices-outline");
// Emit the outline for the groups
for (auto const& group : this->ComponentGroups) {
if (!group.second.ParentGroup) {
CreateChoiceOutline(group.second, xChoiceOut);
}
}
// Emit the outline for the non-grouped components
for (auto const& comp : this->Components) {
if (!comp.second.Group) {
xChoiceOut.StartElement("line");
xChoiceOut.Attribute("choice", cmStrCat(comp.first, "Choice"));
xChoiceOut.Content(""); // Avoid self-closing tag.
xChoiceOut.EndElement();
}
}
if (!this->PostFlightComponent.Name.empty()) {
xChoiceOut.StartElement("line");
xChoiceOut.Attribute("choice",
cmStrCat(PostFlightComponent.Name, "Choice"));
xChoiceOut.Content(""); // Avoid self-closing tag.
xChoiceOut.EndElement();
}
xChoiceOut.EndElement(); // choices-outline>
// Create the actual choices
for (auto const& group : this->ComponentGroups) {
CreateChoice(group.second, xChoiceOut);
}
for (auto const& comp : this->Components) {
CreateChoice(comp.second, xChoiceOut);
}
if (!this->PostFlightComponent.Name.empty()) {
CreateChoice(PostFlightComponent, xChoiceOut);
}
// default background. These are not strictly part of the choices, but they
// must be included in CPACK_PACKAGEMAKER_CHOICES to preserve backward
// compatibility.
this->CreateBackground(nullptr, metapackageFile, genName, xChoiceOut);
// Dark Aqua
this->CreateBackground("darkAqua", metapackageFile, genName, xChoiceOut);
// Provide the content for substitution into the template. Support both the
// old and new variables.
this->SetOption("CPACK_PACKAGEMAKER_CHOICES", choiceOut.str());
this->SetOption("CPACK_APPLE_PKG_INSTALLER_CONTENT",
cmStrCat(xContents.str(), " ", choiceOut.str()));
// Create the distribution.dist file in the metapackage to turn it
// into a distribution package.
this->ConfigureFile(distributionTemplate, distributionFile);
}
void cmCPackPKGGenerator::CreateChoiceOutline(
cmCPackComponentGroup const& group, cmXMLWriter& xout)
{
xout.StartElement("line");
xout.Attribute("choice", cmStrCat(group.Name, "Choice"));
for (cmCPackComponentGroup* subgroup : group.Subgroups) {
CreateChoiceOutline(*subgroup, xout);
}
for (cmCPackComponent* comp : group.Components) {
xout.StartElement("line");
xout.Attribute("choice", cmStrCat(comp->Name, "Choice"));
xout.Content(""); // Avoid self-closing tag.
xout.EndElement();
}
xout.EndElement();
}
void cmCPackPKGGenerator::CreateChoice(cmCPackComponentGroup const& group,
cmXMLWriter& xout)
{
xout.StartElement("choice");
xout.Attribute("id", cmStrCat(group.Name, "Choice"));
xout.Attribute("title", group.DisplayName);
xout.Attribute("start_selected", "true");
xout.Attribute("start_enabled", "true");
xout.Attribute("start_visible", "true");
if (!group.Description.empty()) {
xout.Attribute("description", group.Description);
}
xout.EndElement();
}
void cmCPackPKGGenerator::CreateChoice(cmCPackComponent const& component,
cmXMLWriter& xout)
{
std::string packageId;
if (cmValue i = this->GetOption("CPACK_PRODUCTBUILD_IDENTIFIER")) {
packageId = cmStrCat(i, '.', component.Name);
} else {
packageId =
cmStrCat("com.", this->GetOption("CPACK_PACKAGE_VENDOR"), '.',
this->GetOption("CPACK_PACKAGE_NAME"), '.', component.Name);
}
xout.StartElement("choice");
xout.Attribute("id", cmStrCat(component.Name, "Choice"));
xout.Attribute("title", component.DisplayName);
xout.Attribute(
"start_selected",
component.IsDisabledByDefault && !component.IsRequired ? "false" : "true");
xout.Attribute("start_enabled", component.IsRequired ? "false" : "true");
xout.Attribute("start_visible", component.IsHidden ? "false" : "true");
if (!component.Description.empty()) {
xout.Attribute("description", component.Description);
}
if (!component.Dependencies.empty() ||
!component.ReverseDependencies.empty()) {
// The "selected" expression is evaluated each time any choice is
// selected, for all choices *except* the one that the user
// selected. A component is marked selected if it has been
// selected (my.choice.selected in Javascript) and all of the
// components it depends on have been selected (transitively) or
// if any of the components that depend on it have been selected
// (transitively). Assume that we have components A, B, C, D, and
// E, where each component depends on the previous component (B
// depends on A, C depends on B, D depends on C, and E depends on
// D). The expression we build for the component C will be
// my.choice.selected && B && A || D || E
// This way, selecting C will automatically select everything it depends
// on (B and A), while selecting something that depends on C--either D
// or E--will automatically cause C to get selected.
std::ostringstream selected("my.choice.selected", std::ios_base::ate);
std::set<cmCPackComponent const*> visited;
AddDependencyAttributes(component, visited, selected);
visited.clear();
AddReverseDependencyAttributes(component, visited, selected);
xout.Attribute("selected", selected.str());
}
xout.StartElement("pkg-ref");
xout.Attribute("id", packageId);
xout.EndElement(); // pkg-ref
xout.EndElement(); // choice
// Create a description of the package associated with this
// component.
std::string relativePackageLocation =
cmStrCat("Contents/Packages/", this->GetPackageName(component));
// Determine the installed size of the package.
std::string dirName =
cmStrCat(this->GetOption("CPACK_TEMPORARY_DIRECTORY"), '/', component.Name,
this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX"));
unsigned long installedSize = component.GetInstalledSizeInKbytes(dirName);
xout.StartElement("pkg-ref");
xout.Attribute("id", packageId);
xout.Attribute("version", this->GetOption("CPACK_PACKAGE_VERSION"));
xout.Attribute("installKBytes", installedSize);
// The auth attribute is deprecated in favor of the domains element
if (cmIsOff(this->GetOption("CPACK_PRODUCTBUILD_DOMAINS"))) {
xout.Attribute("auth", "Admin");
}
xout.Attribute("onConclusion", "None");
if (component.IsDownloaded) {
xout.Content(this->GetOption("CPACK_DOWNLOAD_SITE"));
xout.Content(this->GetPackageName(component));
} else {
xout.Content("file:./");
xout.Content(cmSystemTools::EncodeURL(relativePackageLocation,
/*escapeSlashes=*/false));
}
xout.EndElement(); // pkg-ref
}
void cmCPackPKGGenerator::CreateDomains(cmXMLWriter& xout)
{
if (cmIsOff(this->GetOption("CPACK_PRODUCTBUILD_DOMAINS"))) {
return;
}
xout.StartElement("domains");
// Product can be installed at the root of any volume by default
// unless specified
cmValue param = this->GetOption("CPACK_PRODUCTBUILD_DOMAINS_ANYWHERE");
xout.Attribute("enable_anywhere",
(param && cmIsOff(param)) ? "false" : "true");
// Product cannot be installed into the current user's home directory
// by default unless specified
param = this->GetOption("CPACK_PRODUCTBUILD_DOMAINS_USER");
xout.Attribute("enable_currentUserHome",
(param && cmIsOn(param)) ? "true" : "false");
// Product can be installed into the root directory by default
// unless specified
param = this->GetOption("CPACK_PRODUCTBUILD_DOMAINS_ROOT");
xout.Attribute("enable_localSystem",
(param && cmIsOff(param)) ? "false" : "true");
xout.EndElement();
}
void cmCPackPKGGenerator::AddDependencyAttributes(
cmCPackComponent const& component,
std::set<cmCPackComponent const*>& visited, std::ostringstream& out)
{
if (visited.find(&component) != visited.end()) {
return;
}
visited.insert(&component);
for (cmCPackComponent* depend : component.Dependencies) {
out << " && choices['" << depend->Name << "Choice'].selected";
AddDependencyAttributes(*depend, visited, out);
}
}
void cmCPackPKGGenerator::AddReverseDependencyAttributes(
cmCPackComponent const& component,
std::set<cmCPackComponent const*>& visited, std::ostringstream& out)
{
if (visited.find(&component) != visited.end()) {
return;
}
visited.insert(&component);
for (cmCPackComponent* depend : component.ReverseDependencies) {
out << " || choices['" << depend->Name << "Choice'].selected";
AddReverseDependencyAttributes(*depend, visited, out);
}
}
bool cmCPackPKGGenerator::CopyCreateResourceFile(std::string const& name,
std::string const& dirName)
{
std::string uname = cmSystemTools::UpperCase(name);
std::string cpackVar = cmStrCat("CPACK_RESOURCE_FILE_", uname);
cmValue inFileName = this->GetOption(cpackVar);
if (!inFileName) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPack option: "
<< cpackVar << " not specified. It should point to "
<< (!name.empty() ? name : "<empty>") << ".rtf, " << name
<< ".html, or " << name << ".txt file" << std::endl);
return false;
}
if (!cmSystemTools::FileExists(inFileName)) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot find " << (!name.empty() ? name : "<empty>")
<< " resource file: " << inFileName
<< std::endl);
return false;
}
std::string ext = cmSystemTools::GetFilenameLastExtension(inFileName);
if (ext != ".rtfd"_s && ext != ".rtf"_s && ext != ".html"_s &&
ext != ".txt"_s) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"Bad file extension specified: "
<< ext
<< ". Currently only .rtfd, .rtf, .html, and .txt files allowed."
<< std::endl);
return false;
}
std::string destFileName = cmStrCat(dirName, '/', name, ext);
// Set this so that distribution.dist gets the right name (without
// the path).
this->SetOption(cmStrCat("CPACK_RESOURCE_FILE_", uname, "_NOPATH"),
cmStrCat(name, ext));
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Configure file: " << (inFileName ? *inFileName : "(NULL)")
<< " to " << destFileName << std::endl);
this->ConfigureFile(inFileName, destFileName);
return true;
}
bool cmCPackPKGGenerator::CopyResourcePlistFile(std::string const& name,
char const* outName)
{
if (!outName) {
outName = name.c_str();
}
std::string inFName = cmStrCat("CPack.", name, ".in");
std::string inFileName = this->FindTemplate(inFName);
if (inFileName.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot find input file: " << inFName << std::endl);
return false;
}
std::string destFileName =
cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), '/', outName);
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Configure file: " << inFileName << " to " << destFileName
<< std::endl);
this->ConfigureFile(inFileName, destFileName);
return true;
}
int cmCPackPKGGenerator::CopyInstallScript(std::string const& resdir,
std::string const& script,
std::string const& name)
{
std::string dst = cmStrCat(resdir, '/', name);
cmSystemTools::CopyFileAlways(script, dst);
cmSystemTools::SetPermissions(dst, 0777);
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"copy script : " << script << "\ninto " << dst << std::endl);
return 1;
}
|