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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCPackAppImageGenerator.h"
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <utility>
#include <vector>
#include <fcntl.h>
#include <sys/types.h>
#include "cmsys/FStream.hxx"
#include "cmCPackLog.h"
#include "cmELF.h"
#include "cmGeneratedFileStream.h"
#include "cmSystemTools.h"
#include "cmValue.h"
cmCPackAppImageGenerator::cmCPackAppImageGenerator() = default;
cmCPackAppImageGenerator::~cmCPackAppImageGenerator() = default;
int cmCPackAppImageGenerator::InitializeInternal()
{
this->SetOptionIfNotSet("CPACK_APPIMAGE_TOOL_EXECUTABLE", "appimagetool");
this->AppimagetoolPath = cmSystemTools::FindProgram(
*this->GetOption("CPACK_APPIMAGE_TOOL_EXECUTABLE"));
if (this->AppimagetoolPath.empty()) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"Cannot find AppImageTool: '"
<< *this->GetOption("CPACK_APPIMAGE_TOOL_EXECUTABLE")
<< "' check if it's installed, is executable, or is in your PATH"
<< std::endl);
return 0;
}
this->SetOptionIfNotSet("CPACK_APPIMAGE_PATCHELF_EXECUTABLE", "patchelf");
this->PatchElfPath = cmSystemTools::FindProgram(
*this->GetOption("CPACK_APPIMAGE_PATCHELF_EXECUTABLE"));
if (this->PatchElfPath.empty()) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"Cannot find patchelf: '"
<< *this->GetOption("CPACK_APPIMAGE_PATCHELF_EXECUTABLE")
<< "' check if it's installed, is executable, or is in your PATH"
<< std::endl);
return 0;
}
return Superclass::InitializeInternal();
}
int cmCPackAppImageGenerator::PackageFiles()
{
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"AppDir: \"" << this->toplevel << "\"" << std::endl);
// Desktop file must be in the toplevel dir
auto const desktopFile = FindDesktopFile();
if (!desktopFile) {
cmCPackLogger(cmCPackLog::LOG_WARNING,
"A desktop file is required to build an AppImage, make sure "
"it's listed for install()."
<< std::endl);
return 0;
}
{
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"Found Desktop file: \"" << desktopFile.value() << "\""
<< std::endl);
std::string desktopSymLink = this->toplevel + "/" +
cmSystemTools::GetFilenameName(desktopFile.value());
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"Desktop file destination: \"" << desktopSymLink << "\""
<< std::endl);
auto status = cmSystemTools::CreateSymlink(
cmSystemTools::RelativePath(toplevel, *desktopFile), desktopSymLink);
if (status.IsSuccess()) {
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Desktop symbolic link created successfully."
<< std::endl);
} else {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error creating symbolic link." << status.GetString()
<< std::endl);
return 0;
}
}
auto const desktopEntry = ParseDesktopFile(*desktopFile);
{
// Prepare Icon file
auto const iconValue = desktopEntry.find("Icon");
if (iconValue == desktopEntry.end()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"An Icon key is required to build an AppImage, make sure "
"the desktop file has a reference to one."
<< std::endl);
return 0;
}
auto icon = this->GetOption("CPACK_PACKAGE_ICON");
if (!icon) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_PACKAGE_ICON is required to build an AppImage."
<< std::endl);
return 0;
}
if (!cmSystemTools::StringStartsWith(*icon, iconValue->second.c_str())) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_PACKAGE_ICON must match the file name referenced "
"in the desktop file."
<< std::endl);
return 0;
}
auto const iconFile = FindFile(icon);
if (!iconFile) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Could not find the Icon referenced in the desktop file: "
<< *icon << std::endl);
return 0;
}
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"Icon file: \"" << *iconFile << "\"" << std::endl);
std::string iconSymLink =
this->toplevel + "/" + cmSystemTools::GetFilenameName(*iconFile);
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"Icon link destination: \"" << iconSymLink << "\""
<< std::endl);
auto status = cmSystemTools::CreateSymlink(
cmSystemTools::RelativePath(toplevel, *iconFile), iconSymLink);
if (status.IsSuccess()) {
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Icon symbolic link created successfully." << std::endl);
} else {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error creating symbolic link." << status.GetString()
<< std::endl);
return 0;
}
}
std::string application;
{
// Prepare executable file
auto const execValue = desktopEntry.find("Exec");
if (execValue == desktopEntry.end() || execValue->second.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"An Exec key is required to build an AppImage, make sure "
"the desktop file has a reference to one."
<< std::endl);
return 0;
}
auto const execName =
cmSystemTools::SplitString(execValue->second, ' ').front();
auto const mainExecutable = FindFile(execName);
if (!mainExecutable) {
cmCPackLogger(
cmCPackLog::LOG_ERROR,
"Could not find the Executable referenced in the desktop file: "
<< execName << std::endl);
return 0;
}
application = cmSystemTools::RelativePath(toplevel, *mainExecutable);
}
std::string const appRunFile = this->toplevel + "/AppRun";
{
// AppRun script will run our application
cmGeneratedFileStream appRun(appRunFile);
appRun << R"sh(#! /usr/bin/env bash
# autogenerated by CPack
# make sure errors in sourced scripts will cause this script to stop
set -e
this_dir="$(readlink -f "$(dirname "$0")")"
)sh" << std::endl;
appRun << R"sh(exec "$this_dir"/)sh" << application << R"sh( "$@")sh"
<< std::endl;
}
mode_t permissions;
{
auto status = cmSystemTools::GetPermissions(appRunFile, permissions);
if (!status.IsSuccess()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error getting AppRun permission: " << status.GetString()
<< std::endl);
return 0;
}
}
auto status =
cmSystemTools::SetPermissions(appRunFile, permissions | S_IXUSR);
if (!status.IsSuccess()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error changing AppRun permission: " << status.GetString()
<< std::endl);
return 0;
}
// Set RPATH to "$ORIGIN/../lib"
if (!ChangeRPath()) {
return 0;
}
// Run appimagetool
std::vector<std::string> command{
this->AppimagetoolPath,
this->toplevel,
};
command.emplace_back("../" + *this->GetOption("CPACK_PACKAGE_FILE_NAME") +
this->GetOutputExtension());
auto addOptionFlag = [&command, this](std::string const& op,
std::string commandFlag) {
auto opt = this->GetOption(op);
if (opt) {
command.emplace_back(commandFlag);
}
};
auto addOption = [&command, this](std::string const& op,
std::string commandFlag) {
auto opt = this->GetOption(op);
if (opt) {
command.emplace_back(commandFlag);
command.emplace_back(*opt);
}
};
auto addOptions = [&command, this](std::string const& op,
std::string commandFlag) {
auto opt = this->GetOption(op);
if (opt) {
auto const options = cmSystemTools::SplitString(*opt, ';');
for (auto const& mkOpt : options) {
command.emplace_back(commandFlag);
command.emplace_back(mkOpt);
}
}
};
addOption("CPACK_APPIMAGE_UPDATE_INFORMATION", "--updateinformation");
addOptionFlag("CPACK_APPIMAGE_GUESS_UPDATE_INFORMATION", "--guess");
addOption("CPACK_APPIMAGE_COMPRESSOR", "--comp");
addOptions("CPACK_APPIMAGE_MKSQUASHFS_OPTIONS", "--mksquashfs-opt");
addOptionFlag("CPACK_APPIMAGE_NO_APPSTREAM", "--no-appstream");
addOption("CPACK_APPIMAGE_EXCLUDE_FILE", "--exclude-file");
addOption("CPACK_APPIMAGE_RUNTIME_FILE", "--runtime-file");
addOptionFlag("CPACK_APPIMAGE_SIGN", "--sign");
addOption("CPACK_APPIMAGE_SIGN_KEY", "--sign-key");
cmCPackLogger(cmCPackLog::LOG_OUTPUT,
"Running AppImageTool: "
<< cmSystemTools::PrintSingleCommand(command) << std::endl);
int retVal = 1;
bool resS = cmSystemTools::RunSingleCommand(
command, nullptr, nullptr, &retVal, this->toplevel.c_str(),
cmSystemTools::OutputOption::OUTPUT_PASSTHROUGH);
if (!resS || retVal) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Problem running appimagetool: " << this->AppimagetoolPath
<< std::endl);
return 0;
}
return 1;
}
cm::optional<std::string> cmCPackAppImageGenerator::FindFile(
std::string const& filename) const
{
for (std::string const& file : this->files) {
if (cmSystemTools::GetFilenameName(file) == filename) {
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found file:" << file << std::endl);
return file;
}
}
return cm::nullopt;
}
cm::optional<std::string> cmCPackAppImageGenerator::FindDesktopFile() const
{
cmValue desktopFileOpt = GetOption("CPACK_APPIMAGE_DESKTOP_FILE");
if (desktopFileOpt) {
return FindFile(*desktopFileOpt);
}
for (std::string const& file : this->files) {
if (cmSystemTools::StringEndsWith(file, ".desktop")) {
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Found desktop file:" << file << std::endl);
return file;
}
}
return cm::nullopt;
}
namespace {
// Trim leading and trailing whitespace from a string
std::string trim(std::string const& str)
{
auto start = std::find_if_not(
str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); });
auto end = std::find_if_not(str.rbegin(), str.rend(), [](unsigned char c) {
return std::isspace(c);
}).base();
return (start < end) ? std::string(start, end) : std::string();
}
} // namespace
std::unordered_map<std::string, std::string>
cmCPackAppImageGenerator::ParseDesktopFile(std::string const& filePath) const
{
std::unordered_map<std::string, std::string> ret;
cmsys::ifstream file(filePath);
if (!file.is_open()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Failed to open desktop file:" << filePath << std::endl);
return ret;
}
bool inDesktopEntry = false;
std::string line;
while (std::getline(file, line)) {
line = trim(line);
if (line.empty() || line[0] == '#') {
// Skip empty lines or comments
continue;
}
if (line.front() == '[' && line.back() == ']') {
// We only care for [Desktop Entry] section
inDesktopEntry = (line == "[Desktop Entry]");
continue;
}
if (inDesktopEntry) {
size_t delimiter_pos = line.find('=');
if (delimiter_pos == std::string::npos) {
cmCPackLogger(cmCPackLog::LOG_WARNING,
"Invalid desktop file line format: " << line
<< std::endl);
continue;
}
std::string key = trim(line.substr(0, delimiter_pos));
std::string value = trim(line.substr(delimiter_pos + 1));
if (!key.empty()) {
ret.emplace(key, value);
}
}
}
return ret;
}
bool cmCPackAppImageGenerator::ChangeRPath()
{
// AppImages are mounted in random locations so we need RPATH to resolve to
// that location
std::string const newRPath = "$ORIGIN/../lib";
for (std::string const& file : this->files) {
cmELF elf(file.c_str());
auto const type = elf.GetFileType();
switch (type) {
case cmELF::FileType::FileTypeExecutable:
case cmELF::FileType::FileTypeSharedLibrary: {
std::string oldRPath;
auto const* rpath = elf.GetRPath();
if (rpath) {
oldRPath = rpath->Value;
} else {
auto const* runpath = elf.GetRunPath();
if (runpath) {
oldRPath = runpath->Value;
} else {
oldRPath = "";
}
}
if (cmSystemTools::StringStartsWith(oldRPath, "$ORIGIN")) {
// Skip libraries with ORIGIN RPATH set
continue;
}
if (!PatchElfSetRPath(file, newRPath)) {
return false;
}
break;
}
default:
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"ELF <" << file << "> type: " << type << std::endl);
break;
}
}
return true;
}
bool cmCPackAppImageGenerator::PatchElfSetRPath(std::string const& file,
std::string const& rpath) const
{
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Changing RPATH: " << file << " to: " << rpath << std::endl);
int retVal = 1;
bool resS = cmSystemTools::RunSingleCommand(
{
this->PatchElfPath,
"--set-rpath",
rpath,
file,
},
nullptr, nullptr, &retVal, nullptr,
cmSystemTools::OutputOption::OUTPUT_NONE);
if (!resS || retVal) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Problem running patchelf to change RPATH: " << file
<< std::endl);
return false;
}
return true;
}
|