cmake/Source/CPack/cmCPackBundleGenerator.cxx

263 lines
7.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCPackBundleGenerator.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <vector>
#include "cmCPackLog.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2019-11-11 23:01:05 +01:00
cmCPackBundleGenerator::cmCPackBundleGenerator() = default;
2019-11-11 23:01:05 +01:00
cmCPackBundleGenerator::~cmCPackBundleGenerator() = default;
int cmCPackBundleGenerator::InitializeInternal()
{
2021-11-20 13:41:27 +01:00
cmValue name = this->GetOption("CPACK_BUNDLE_NAME");
if (!name) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"CPACK_BUNDLE_NAME must be set to use the Bundle generator."
<< std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (this->GetOption("CPACK_BUNDLE_APPLE_CERT_APP")) {
const std::string codesign_path = cmSystemTools::FindProgram(
"codesign", std::vector<std::string>(), false);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (codesign_path.empty()) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot locate codesign command" << std::endl);
2015-04-27 22:25:09 +02:00
return 0;
}
2021-11-20 13:41:27 +01:00
this->SetOptionIfNotSet("CPACK_COMMAND_CODESIGN", codesign_path);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return this->Superclass::InitializeInternal();
}
const char* cmCPackBundleGenerator::GetPackagingInstallPrefix()
{
2020-02-01 23:06:01 +01:00
this->InstallPrefix = cmStrCat('/', this->GetOption("CPACK_BUNDLE_NAME"),
".app/Contents/Resources");
return this->InstallPrefix.c_str();
}
2015-04-27 22:25:09 +02:00
int cmCPackBundleGenerator::ConstructBundle()
{
// Get required arguments ...
2021-11-20 13:41:27 +01:00
cmValue cpack_bundle_name = this->GetOption("CPACK_BUNDLE_NAME");
if (cpack_bundle_name->empty()) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_BUNDLE_NAME must be set." << std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
cmValue cpack_bundle_plist = this->GetOption("CPACK_BUNDLE_PLIST");
if (cpack_bundle_plist->empty()) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_BUNDLE_PLIST must be set." << std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
cmValue cpack_bundle_icon = this->GetOption("CPACK_BUNDLE_ICON");
if (cpack_bundle_icon->empty()) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_BUNDLE_ICON must be set." << std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
// Get optional arguments ...
2021-11-20 13:41:27 +01:00
cmValue cpack_bundle_startup_command =
this->GetOption("CPACK_BUNDLE_STARTUP_COMMAND");
// The staging directory contains everything that will end-up inside the
// final disk image ...
2020-02-01 23:06:01 +01:00
std::string const staging = toplevel;
2015-04-27 22:25:09 +02:00
std::ostringstream contents;
2023-12-07 09:12:54 +01:00
contents << staging << "/" << cpack_bundle_name
<< ".app/"
"Contents";
2015-04-27 22:25:09 +02:00
std::ostringstream application;
2023-12-07 09:12:54 +01:00
application << contents.str()
<< "/"
"MacOS";
2015-04-27 22:25:09 +02:00
std::ostringstream resources;
2023-12-07 09:12:54 +01:00
resources << contents.str()
<< "/"
"Resources";
// Install a required, user-provided bundle metadata file ...
2015-04-27 22:25:09 +02:00
std::ostringstream plist_source;
plist_source << cpack_bundle_plist;
2015-04-27 22:25:09 +02:00
std::ostringstream plist_target;
2023-12-07 09:12:54 +01:00
plist_target << contents.str()
<< "/"
"Info.plist";
2016-07-09 11:21:54 +02:00
if (!this->CopyFile(plist_source, plist_target)) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"Error copying plist. Check the value of CPACK_BUNDLE_PLIST."
2016-07-09 11:21:54 +02:00
<< std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
// Install a user-provided bundle icon ...
2015-04-27 22:25:09 +02:00
std::ostringstream icon_source;
icon_source << cpack_bundle_icon;
2015-04-27 22:25:09 +02:00
std::ostringstream icon_target;
icon_target << resources.str() << "/" << cpack_bundle_name << ".icns";
2016-07-09 11:21:54 +02:00
if (!this->CopyFile(icon_source, icon_target)) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"Error copying bundle icon. Check the value of CPACK_BUNDLE_ICON."
2016-07-09 11:21:54 +02:00
<< std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
// Optionally a user-provided startup command (could be an
// executable or a script) ...
2021-11-20 13:41:27 +01:00
if (!cpack_bundle_startup_command->empty()) {
2015-04-27 22:25:09 +02:00
std::ostringstream command_source;
command_source << cpack_bundle_startup_command;
2015-04-27 22:25:09 +02:00
std::ostringstream command_target;
command_target << application.str() << "/" << cpack_bundle_name;
2016-07-09 11:21:54 +02:00
if (!this->CopyFile(command_source, command_target)) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error copying startup command. "
" Check the value of CPACK_BUNDLE_STARTUP_COMMAND."
2016-07-09 11:21:54 +02:00
<< std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
cmSystemTools::SetPermissions(command_target.str().c_str(), 0777);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return 1;
}
int cmCPackBundleGenerator::PackageFiles()
{
2016-07-09 11:21:54 +02:00
if (!this->ConstructBundle()) {
2015-04-27 22:25:09 +02:00
return 0;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!this->SignBundle(toplevel)) {
2015-04-27 22:25:09 +02:00
return 0;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2011-06-19 15:41:06 +03:00
return this->CreateDMG(toplevel, packageFileNames[0]);
}
bool cmCPackBundleGenerator::SupportsComponentInstallation() const
{
return false;
}
2015-04-27 22:25:09 +02:00
int cmCPackBundleGenerator::SignBundle(const std::string& src_dir)
{
2021-11-20 13:41:27 +01:00
cmValue cpack_apple_cert_app =
this->GetOption("CPACK_BUNDLE_APPLE_CERT_APP");
2015-04-27 22:25:09 +02:00
// codesign the application.
2021-11-20 13:41:27 +01:00
if (!cpack_apple_cert_app->empty()) {
2015-08-17 11:37:30 +02:00
std::string output;
2015-04-27 22:25:09 +02:00
std::string bundle_path;
2020-02-01 23:06:01 +01:00
bundle_path =
cmStrCat(src_dir, '/', this->GetOption("CPACK_BUNDLE_NAME"), ".app");
2015-04-27 22:25:09 +02:00
// A list of additional files to sign, ie. frameworks and plugins.
2015-08-17 11:37:30 +02:00
const std::string sign_parameter =
this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_PARAMETER")
2021-11-20 13:41:27 +01:00
? *this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_PARAMETER")
2015-08-17 11:37:30 +02:00
: "--deep -f";
2021-11-20 13:41:27 +01:00
cmValue sign_files = this->GetOption("CPACK_BUNDLE_APPLE_CODESIGN_FILES");
2015-04-27 22:25:09 +02:00
2023-07-02 19:51:09 +02:00
cmList relFiles{ sign_files };
2015-04-27 22:25:09 +02:00
// sign the files supplied by the user, ie. frameworks.
2018-01-26 17:06:56 +01:00
for (auto const& file : relFiles) {
2023-12-07 09:12:54 +01:00
auto temp_sign_file_cmd =
cmStrCat(this->GetOption("CPACK_COMMAND_CODESIGN"), ' ',
sign_parameter, " -s \"", cpack_apple_cert_app, "\" -i ",
this->GetOption("CPACK_APPLE_BUNDLE_ID"), " \"", bundle_path,
file, '"');
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!this->RunCommand(temp_sign_file_cmd, &output)) {
2016-10-30 18:24:19 +01:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
2018-01-26 17:06:56 +01:00
"Error signing file:" << bundle_path << file << std::endl
2016-10-30 18:24:19 +01:00
<< output << std::endl);
2015-04-27 22:25:09 +02:00
return 0;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// sign main binary
2023-12-07 09:12:54 +01:00
auto temp_sign_binary_cmd =
cmStrCat(this->GetOption("CPACK_COMMAND_CODESIGN"), ' ', sign_parameter,
" -s \"", cpack_apple_cert_app, "\" \"", bundle_path, '"');
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!this->RunCommand(temp_sign_binary_cmd, &output)) {
2015-04-27 22:25:09 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Error signing the application binary." << std::endl
<< output
<< std::endl);
2015-04-27 22:25:09 +02:00
return 0;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// sign app bundle
2023-12-07 09:12:54 +01:00
auto temp_codesign_cmd =
cmStrCat(this->GetOption("CPACK_COMMAND_CODESIGN"), ' ', sign_parameter,
" -s \"", cpack_apple_cert_app, "\"");
2016-07-09 11:21:54 +02:00
if (this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS")) {
2023-12-07 09:12:54 +01:00
temp_codesign_cmd +=
cmStrCat(" --entitlements ",
this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS"));
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
temp_codesign_cmd += cmStrCat(" \"", bundle_path, '"');
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!this->RunCommand(temp_codesign_cmd, &output)) {
2015-04-27 22:25:09 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Error signing the application package." << std::endl
<< output
<< std::endl);
2015-04-27 22:25:09 +02:00
return 0;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"- Application has been codesigned" << std::endl);
2015-04-27 22:25:09 +02:00
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
2016-07-09 11:21:54 +02:00
(this->GetOption("CPACK_BUNDLE_APPLE_ENTITLEMENTS")
? "with entitlement sandboxing"
: "without entitlement sandboxing")
<< std::endl);
2015-04-27 22:25:09 +02:00
}
return 1;
}