cmCPackGenerator.cxx 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackGenerator.h"
  4. #include "cmsys/FStream.hxx"
  5. #include "cmsys/Glob.hxx"
  6. #include "cmsys/RegularExpression.hxx"
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <memory> // IWYU pragma: keep
  10. #include <utility>
  11. #include "cmCPackComponentGroup.h"
  12. #include "cmCPackLog.h"
  13. #include "cmCryptoHash.h"
  14. #include "cmDuration.h"
  15. #include "cmFSPermissions.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmState.h"
  20. #include "cmStateSnapshot.h"
  21. #include "cmVersion.h"
  22. #include "cmWorkingDirectory.h"
  23. #include "cmXMLSafe.h"
  24. #include "cmake.h"
  25. #if defined(__HAIKU__)
  26. # include <FindDirectory.h>
  27. # include <StorageDefs.h>
  28. #endif
  29. cmCPackGenerator::cmCPackGenerator()
  30. {
  31. this->GeneratorVerbose = cmSystemTools::OUTPUT_NONE;
  32. this->MakefileMap = nullptr;
  33. this->Logger = nullptr;
  34. this->componentPackageMethod = ONE_PACKAGE_PER_GROUP;
  35. }
  36. cmCPackGenerator::~cmCPackGenerator()
  37. {
  38. this->MakefileMap = nullptr;
  39. }
  40. void cmCPackGeneratorProgress(const char* msg, float prog, void* ptr)
  41. {
  42. cmCPackGenerator* self = static_cast<cmCPackGenerator*>(ptr);
  43. self->DisplayVerboseOutput(msg, prog);
  44. }
  45. void cmCPackGenerator::DisplayVerboseOutput(const char* msg, float progress)
  46. {
  47. (void)progress;
  48. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "" << msg << std::endl);
  49. }
  50. int cmCPackGenerator::PrepareNames()
  51. {
  52. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Create temp directory." << std::endl);
  53. // checks CPACK_SET_DESTDIR support
  54. if (IsOn("CPACK_SET_DESTDIR")) {
  55. if (SETDESTDIR_UNSUPPORTED == SupportsSetDestdir()) {
  56. cmCPackLogger(cmCPackLog::LOG_ERROR,
  57. "CPACK_SET_DESTDIR is set to ON but the '"
  58. << Name << "' generator does NOT support it."
  59. << std::endl);
  60. return 0;
  61. }
  62. if (SETDESTDIR_SHOULD_NOT_BE_USED == SupportsSetDestdir()) {
  63. cmCPackLogger(cmCPackLog::LOG_WARNING,
  64. "CPACK_SET_DESTDIR is set to ON but it is "
  65. << "usually a bad idea to do that with '" << Name
  66. << "' generator. Use at your own risk." << std::endl);
  67. }
  68. }
  69. std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  70. tempDirectory += "/_CPack_Packages/";
  71. const char* toplevelTag = this->GetOption("CPACK_TOPLEVEL_TAG");
  72. if (toplevelTag) {
  73. tempDirectory += toplevelTag;
  74. tempDirectory += "/";
  75. }
  76. tempDirectory += this->GetOption("CPACK_GENERATOR");
  77. std::string topDirectory = tempDirectory;
  78. const char* pfname = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  79. if (!pfname) {
  80. cmCPackLogger(cmCPackLog::LOG_ERROR,
  81. "CPACK_PACKAGE_FILE_NAME not specified" << std::endl);
  82. return 0;
  83. }
  84. std::string outName = pfname;
  85. tempDirectory += "/" + outName;
  86. if (!this->GetOutputExtension()) {
  87. cmCPackLogger(cmCPackLog::LOG_ERROR,
  88. "No output extension specified" << std::endl);
  89. return 0;
  90. }
  91. outName += this->GetOutputExtension();
  92. const char* pdir = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  93. if (!pdir) {
  94. cmCPackLogger(cmCPackLog::LOG_ERROR,
  95. "CPACK_PACKAGE_DIRECTORY not specified" << std::endl);
  96. return 0;
  97. }
  98. std::string destFile = pdir;
  99. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PREFIX", destFile.c_str());
  100. destFile += "/" + outName;
  101. std::string outFile = topDirectory + "/" + outName;
  102. this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
  103. this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
  104. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName.c_str());
  105. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile.c_str());
  106. this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  107. outFile.c_str());
  108. this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
  109. this->SetOptionIfNotSet(
  110. "CPACK_NATIVE_INSTALL_DIRECTORY",
  111. cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
  112. this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
  113. tempDirectory.c_str());
  114. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  115. "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
  116. const char* descFileName = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  117. if (descFileName) {
  118. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  119. "Look for: " << descFileName << std::endl);
  120. if (!cmSystemTools::FileExists(descFileName)) {
  121. cmCPackLogger(cmCPackLog::LOG_ERROR,
  122. "Cannot find description file name: ["
  123. << descFileName << "]" << std::endl);
  124. return 0;
  125. }
  126. cmsys::ifstream ifs(descFileName);
  127. if (!ifs) {
  128. cmCPackLogger(cmCPackLog::LOG_ERROR,
  129. "Cannot open description file name: " << descFileName
  130. << std::endl);
  131. return 0;
  132. }
  133. std::ostringstream ostr;
  134. std::string line;
  135. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  136. "Read description file: " << descFileName << std::endl);
  137. while (ifs && cmSystemTools::GetLineFromStream(ifs, line)) {
  138. ostr << cmXMLSafe(line) << std::endl;
  139. }
  140. this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
  141. }
  142. if (!this->GetOption("CPACK_PACKAGE_DESCRIPTION")) {
  143. cmCPackLogger(
  144. cmCPackLog::LOG_ERROR,
  145. "Project description not specified. Please specify "
  146. "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
  147. << std::endl);
  148. return 0;
  149. }
  150. const char* algoSignature = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  151. if (algoSignature) {
  152. if (!cmCryptoHash::New(algoSignature)) {
  153. cmCPackLogger(cmCPackLog::LOG_ERROR,
  154. "Cannot recognize algorithm: " << algoSignature
  155. << std::endl);
  156. return 0;
  157. }
  158. }
  159. this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "1");
  160. return 1;
  161. }
  162. int cmCPackGenerator::InstallProject()
  163. {
  164. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install projects" << std::endl);
  165. this->CleanTemporaryDirectory();
  166. std::string bareTempInstallDirectory =
  167. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  168. std::string tempInstallDirectoryStr = bareTempInstallDirectory;
  169. bool setDestDir = cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR")) |
  170. cmSystemTools::IsInternallyOn(this->GetOption("CPACK_SET_DESTDIR"));
  171. if (!setDestDir) {
  172. tempInstallDirectoryStr += this->GetPackagingInstallPrefix();
  173. }
  174. const char* tempInstallDirectory = tempInstallDirectoryStr.c_str();
  175. int res = 1;
  176. if (!cmsys::SystemTools::MakeDirectory(bareTempInstallDirectory)) {
  177. cmCPackLogger(cmCPackLog::LOG_ERROR,
  178. "Problem creating temporary directory: "
  179. << (tempInstallDirectory ? tempInstallDirectory : "(NULL)")
  180. << std::endl);
  181. return 0;
  182. }
  183. if (setDestDir) {
  184. std::string destDir = "DESTDIR=";
  185. destDir += tempInstallDirectory;
  186. cmSystemTools::PutEnv(destDir);
  187. } else {
  188. // Make sure there is no destdir
  189. cmSystemTools::PutEnv("DESTDIR=");
  190. }
  191. // prepare default created directory permissions
  192. mode_t default_dir_mode_v = 0;
  193. mode_t* default_dir_mode = nullptr;
  194. const char* default_dir_install_permissions =
  195. this->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
  196. if (default_dir_install_permissions && *default_dir_install_permissions) {
  197. std::vector<std::string> items;
  198. cmSystemTools::ExpandListArgument(default_dir_install_permissions, items);
  199. for (const auto& arg : items) {
  200. if (!cmFSPermissions::stringToModeT(arg, default_dir_mode_v)) {
  201. cmCPackLogger(cmCPackLog::LOG_ERROR,
  202. "Invalid permission value '"
  203. << arg
  204. << "'."
  205. " CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS "
  206. "value is invalid."
  207. << std::endl);
  208. return 0;
  209. }
  210. }
  211. default_dir_mode = &default_dir_mode_v;
  212. }
  213. // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
  214. // as listed
  215. if (!this->InstallProjectViaInstallCommands(setDestDir,
  216. tempInstallDirectory)) {
  217. return 0;
  218. }
  219. // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
  220. // as listed
  221. if (!this->InstallProjectViaInstallScript(setDestDir,
  222. tempInstallDirectory)) {
  223. return 0;
  224. }
  225. // If the CPackConfig file sets CPACK_INSTALLED_DIRECTORIES
  226. // then glob it and copy it to CPACK_TEMPORARY_DIRECTORY
  227. // This is used in Source packaging
  228. if (!this->InstallProjectViaInstalledDirectories(
  229. setDestDir, tempInstallDirectory, default_dir_mode)) {
  230. return 0;
  231. }
  232. // If the project is a CMAKE project then run pre-install
  233. // and then read the cmake_install script to run it
  234. if (!this->InstallProjectViaInstallCMakeProjects(
  235. setDestDir, bareTempInstallDirectory, default_dir_mode)) {
  236. return 0;
  237. }
  238. if (setDestDir) {
  239. cmSystemTools::PutEnv("DESTDIR=");
  240. }
  241. return res;
  242. }
  243. int cmCPackGenerator::InstallProjectViaInstallCommands(
  244. bool setDestDir, const std::string& tempInstallDirectory)
  245. {
  246. (void)setDestDir;
  247. const char* installCommands = this->GetOption("CPACK_INSTALL_COMMANDS");
  248. if (installCommands && *installCommands) {
  249. std::string tempInstallDirectoryEnv = "CMAKE_INSTALL_PREFIX=";
  250. tempInstallDirectoryEnv += tempInstallDirectory;
  251. cmSystemTools::PutEnv(tempInstallDirectoryEnv);
  252. std::vector<std::string> installCommandsVector;
  253. cmSystemTools::ExpandListArgument(installCommands, installCommandsVector);
  254. for (std::string const& ic : installCommandsVector) {
  255. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << ic << std::endl);
  256. std::string output;
  257. int retVal = 1;
  258. bool resB = cmSystemTools::RunSingleCommand(
  259. ic.c_str(), &output, &output, &retVal, nullptr, this->GeneratorVerbose,
  260. cmDuration::zero());
  261. if (!resB || retVal) {
  262. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  263. tmpFile += "/InstallOutput.log";
  264. cmGeneratedFileStream ofs(tmpFile);
  265. ofs << "# Run command: " << ic << std::endl
  266. << "# Output:" << std::endl
  267. << output << std::endl;
  268. cmCPackLogger(cmCPackLog::LOG_ERROR,
  269. "Problem running install command: "
  270. << ic << std::endl
  271. << "Please check " << tmpFile << " for errors"
  272. << std::endl);
  273. return 0;
  274. }
  275. }
  276. }
  277. return 1;
  278. }
  279. int cmCPackGenerator::InstallProjectViaInstalledDirectories(
  280. bool setDestDir, const std::string& tempInstallDirectory,
  281. const mode_t* default_dir_mode)
  282. {
  283. (void)setDestDir;
  284. (void)tempInstallDirectory;
  285. std::vector<cmsys::RegularExpression> ignoreFilesRegex;
  286. const char* cpackIgnoreFiles = this->GetOption("CPACK_IGNORE_FILES");
  287. if (cpackIgnoreFiles) {
  288. std::vector<std::string> ignoreFilesRegexString;
  289. cmSystemTools::ExpandListArgument(cpackIgnoreFiles,
  290. ignoreFilesRegexString);
  291. for (std::string const& ifr : ignoreFilesRegexString) {
  292. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  293. "Create ignore files regex for: " << ifr << std::endl);
  294. ignoreFilesRegex.emplace_back(ifr);
  295. }
  296. }
  297. const char* installDirectories =
  298. this->GetOption("CPACK_INSTALLED_DIRECTORIES");
  299. if (installDirectories && *installDirectories) {
  300. std::vector<std::string> installDirectoriesVector;
  301. cmSystemTools::ExpandListArgument(installDirectories,
  302. installDirectoriesVector);
  303. if (installDirectoriesVector.size() % 2 != 0) {
  304. cmCPackLogger(
  305. cmCPackLog::LOG_ERROR,
  306. "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
  307. "<subdirectory>. The <subdirectory> can be '.' to be installed in "
  308. "the toplevel directory of installation."
  309. << std::endl);
  310. return 0;
  311. }
  312. std::vector<std::string>::iterator it;
  313. const std::string& tempDir = tempInstallDirectory;
  314. for (it = installDirectoriesVector.begin();
  315. it != installDirectoriesVector.end(); ++it) {
  316. std::vector<std::pair<std::string, std::string>> symlinkedFiles;
  317. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  318. cmsys::Glob gl;
  319. std::string top = *it;
  320. it++;
  321. std::string subdir = *it;
  322. std::string findExpr = top;
  323. findExpr += "/*";
  324. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  325. "- Install directory: " << top << std::endl);
  326. gl.RecurseOn();
  327. gl.SetRecurseListDirs(true);
  328. if (!gl.FindFiles(findExpr)) {
  329. cmCPackLogger(cmCPackLog::LOG_ERROR,
  330. "Cannot find any files in the installed directory"
  331. << std::endl);
  332. return 0;
  333. }
  334. files = gl.GetFiles();
  335. for (std::string const& gf : files) {
  336. bool skip = false;
  337. std::string inFile = gf;
  338. if (cmSystemTools::FileIsDirectory(gf)) {
  339. inFile += '/';
  340. }
  341. for (cmsys::RegularExpression& reg : ignoreFilesRegex) {
  342. if (reg.find(inFile)) {
  343. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  344. "Ignore file: " << inFile << std::endl);
  345. skip = true;
  346. }
  347. }
  348. if (skip) {
  349. continue;
  350. }
  351. std::string filePath = tempDir;
  352. filePath += "/" + subdir + "/" + cmSystemTools::RelativePath(top, gf);
  353. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  354. "Copy file: " << inFile << " -> " << filePath
  355. << std::endl);
  356. /* If the file is a symlink we will have to re-create it */
  357. if (cmSystemTools::FileIsSymlink(inFile)) {
  358. std::string targetFile;
  359. std::string inFileRelative =
  360. cmSystemTools::RelativePath(top, inFile);
  361. cmSystemTools::ReadSymlink(inFile, targetFile);
  362. symlinkedFiles.emplace_back(std::move(targetFile),
  363. std::move(inFileRelative));
  364. }
  365. /* If it is not a symlink then do a plain copy */
  366. else if (!(cmSystemTools::CopyFileIfDifferent(inFile.c_str(),
  367. filePath.c_str()) &&
  368. cmSystemTools::CopyFileTime(inFile.c_str(),
  369. filePath.c_str()))) {
  370. cmCPackLogger(cmCPackLog::LOG_ERROR,
  371. "Problem copying file: " << inFile << " -> "
  372. << filePath << std::endl);
  373. return 0;
  374. }
  375. }
  376. /* rebuild symlinks in the installed tree */
  377. if (!symlinkedFiles.empty()) {
  378. std::string curDir = cmSystemTools::GetCurrentWorkingDirectory();
  379. std::string goToDir = tempDir;
  380. goToDir += "/" + subdir;
  381. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  382. "Change dir to: " << goToDir << std::endl);
  383. cmWorkingDirectory workdir(goToDir);
  384. if (workdir.Failed()) {
  385. cmCPackLogger(cmCPackLog::LOG_ERROR,
  386. "Failed to change working directory to "
  387. << goToDir << " : "
  388. << std::strerror(workdir.GetLastResult())
  389. << std::endl);
  390. return 0;
  391. }
  392. for (auto const& symlinked : symlinkedFiles) {
  393. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  394. "Will create a symlink: " << symlinked.second << "--> "
  395. << symlinked.first
  396. << std::endl);
  397. // make sure directory exists for symlink
  398. std::string destDir =
  399. cmSystemTools::GetFilenamePath(symlinked.second);
  400. if (!destDir.empty() &&
  401. !cmSystemTools::MakeDirectory(destDir, default_dir_mode)) {
  402. cmCPackLogger(cmCPackLog::LOG_ERROR,
  403. "Cannot create dir: "
  404. << destDir << "\nTrying to create symlink: "
  405. << symlinked.second << "--> " << symlinked.first
  406. << std::endl);
  407. }
  408. if (!cmSystemTools::CreateSymlink(symlinked.first,
  409. symlinked.second)) {
  410. cmCPackLogger(cmCPackLog::LOG_ERROR,
  411. "Cannot create symlink: "
  412. << symlinked.second << "--> " << symlinked.first
  413. << std::endl);
  414. return 0;
  415. }
  416. }
  417. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  418. "Going back to: " << curDir << std::endl);
  419. }
  420. }
  421. }
  422. return 1;
  423. }
  424. int cmCPackGenerator::InstallProjectViaInstallScript(
  425. bool setDestDir, const std::string& tempInstallDirectory)
  426. {
  427. const char* cmakeScripts = this->GetOption("CPACK_INSTALL_SCRIPT");
  428. if (cmakeScripts && *cmakeScripts) {
  429. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  430. "- Install scripts: " << cmakeScripts << std::endl);
  431. std::vector<std::string> cmakeScriptsVector;
  432. cmSystemTools::ExpandListArgument(cmakeScripts, cmakeScriptsVector);
  433. for (std::string const& installScript : cmakeScriptsVector) {
  434. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  435. "- Install script: " << installScript << std::endl);
  436. if (setDestDir) {
  437. // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
  438. // underneath the tempInstallDirectory. The value of the project's
  439. // CMAKE_INSTALL_PREFIX is sent in here as the value of the
  440. // CPACK_INSTALL_PREFIX variable.
  441. std::string dir;
  442. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  443. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  444. }
  445. this->SetOption("CMAKE_INSTALL_PREFIX", dir.c_str());
  446. cmCPackLogger(
  447. cmCPackLog::LOG_DEBUG,
  448. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (this->SetOption)"
  449. << std::endl);
  450. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  451. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  452. << std::endl);
  453. } else {
  454. this->SetOption("CMAKE_INSTALL_PREFIX", tempInstallDirectory.c_str());
  455. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  456. "- Using non-DESTDIR install... (this->SetOption)"
  457. << std::endl);
  458. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  459. "- Setting CMAKE_INSTALL_PREFIX to '"
  460. << tempInstallDirectory << "'" << std::endl);
  461. }
  462. this->SetOptionIfNotSet("CMAKE_CURRENT_BINARY_DIR",
  463. tempInstallDirectory.c_str());
  464. this->SetOptionIfNotSet("CMAKE_CURRENT_SOURCE_DIR",
  465. tempInstallDirectory.c_str());
  466. int res = this->MakefileMap->ReadListFile(installScript.c_str());
  467. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  468. return 0;
  469. }
  470. }
  471. }
  472. return 1;
  473. }
  474. int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
  475. bool setDestDir, const std::string& baseTempInstallDirectory,
  476. const mode_t* default_dir_mode)
  477. {
  478. const char* cmakeProjects = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
  479. const char* cmakeGenerator = this->GetOption("CPACK_CMAKE_GENERATOR");
  480. std::string absoluteDestFiles;
  481. if (cmakeProjects && *cmakeProjects) {
  482. if (!cmakeGenerator) {
  483. cmCPackLogger(cmCPackLog::LOG_ERROR,
  484. "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
  485. "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
  486. "is required to install the project."
  487. << std::endl);
  488. return 0;
  489. }
  490. std::vector<std::string> cmakeProjectsVector;
  491. cmSystemTools::ExpandListArgument(cmakeProjects, cmakeProjectsVector);
  492. std::vector<std::string>::iterator it;
  493. for (it = cmakeProjectsVector.begin(); it != cmakeProjectsVector.end();
  494. ++it) {
  495. if (it + 1 == cmakeProjectsVector.end() ||
  496. it + 2 == cmakeProjectsVector.end() ||
  497. it + 3 == cmakeProjectsVector.end()) {
  498. cmCPackLogger(
  499. cmCPackLog::LOG_ERROR,
  500. "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
  501. "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
  502. "directory, install project name, install component, and install "
  503. "subdirectory."
  504. << std::endl);
  505. return 0;
  506. }
  507. std::string installDirectory = *it;
  508. ++it;
  509. std::string installProjectName = *it;
  510. ++it;
  511. cmCPackInstallCMakeProject project;
  512. project.Directory = installDirectory;
  513. project.ProjectName = installProjectName;
  514. project.Component = *it;
  515. ++it;
  516. project.SubDirectory = *it;
  517. std::vector<std::string> componentsVector;
  518. bool componentInstall = false;
  519. /*
  520. * We do a component install iff
  521. * - the CPack generator support component
  522. * - the user did not request Monolithic install
  523. * (this works at CPack time too)
  524. */
  525. if (this->SupportsComponentInstallation() &&
  526. !(this->IsOn("CPACK_MONOLITHIC_INSTALL"))) {
  527. // Determine the installation types for this project (if provided).
  528. std::string installTypesVar = "CPACK_" +
  529. cmSystemTools::UpperCase(project.Component) + "_INSTALL_TYPES";
  530. const char* installTypes = this->GetOption(installTypesVar);
  531. if (installTypes && *installTypes) {
  532. std::vector<std::string> installTypesVector;
  533. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  534. for (std::string const& installType : installTypesVector) {
  535. project.InstallationTypes.push_back(
  536. this->GetInstallationType(project.ProjectName, installType));
  537. }
  538. }
  539. // Determine the set of components that will be used in this project
  540. std::string componentsVar =
  541. "CPACK_COMPONENTS_" + cmSystemTools::UpperCase(project.Component);
  542. const char* components = this->GetOption(componentsVar);
  543. if (components && *components) {
  544. cmSystemTools::ExpandListArgument(components, componentsVector);
  545. for (std::string const& comp : componentsVector) {
  546. project.Components.push_back(
  547. this->GetComponent(project.ProjectName, comp));
  548. }
  549. componentInstall = true;
  550. }
  551. }
  552. if (componentsVector.empty()) {
  553. componentsVector.push_back(project.Component);
  554. }
  555. const char* buildConfigCstr = this->GetOption("CPACK_BUILD_CONFIG");
  556. std::string buildConfig = buildConfigCstr ? buildConfigCstr : "";
  557. cmGlobalGenerator* globalGenerator =
  558. this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
  559. cmakeGenerator);
  560. if (!globalGenerator) {
  561. cmCPackLogger(cmCPackLog::LOG_ERROR,
  562. "Specified package generator not found. "
  563. "CPACK_CMAKE_GENERATOR value is invalid."
  564. << std::endl);
  565. return 0;
  566. }
  567. // set the global flag for unix style paths on cmSystemTools as
  568. // soon as the generator is set. This allows gmake to be used
  569. // on windows.
  570. cmSystemTools::SetForceUnixPaths(globalGenerator->GetForceUnixPaths());
  571. if (!this->RunPreinstallTarget(project.ProjectName, project.Directory,
  572. globalGenerator, buildConfig)) {
  573. return 0;
  574. }
  575. delete globalGenerator;
  576. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  577. "- Install project: " << project.ProjectName << std::endl);
  578. // Run the installation for each component
  579. for (std::string const& component : componentsVector) {
  580. if (!this->InstallCMakeProject(
  581. setDestDir, project.Directory, baseTempInstallDirectory,
  582. default_dir_mode, component, componentInstall,
  583. project.SubDirectory, buildConfig, absoluteDestFiles)) {
  584. return 0;
  585. }
  586. }
  587. this->CMakeProjects.push_back(project);
  588. }
  589. }
  590. this->SetOption("CPACK_ABSOLUTE_DESTINATION_FILES",
  591. absoluteDestFiles.c_str());
  592. return 1;
  593. }
  594. int cmCPackGenerator::RunPreinstallTarget(
  595. const std::string& installProjectName, const std::string& installDirectory,
  596. cmGlobalGenerator* globalGenerator, const std::string& buildConfig)
  597. {
  598. // Does this generator require pre-install?
  599. if (const char* preinstall = globalGenerator->GetPreinstallTargetName()) {
  600. std::string buildCommand = globalGenerator->GenerateCMakeBuildCommand(
  601. preinstall, buildConfig, "", false);
  602. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  603. "- Install command: " << buildCommand << std::endl);
  604. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  605. "- Run preinstall target for: " << installProjectName
  606. << std::endl);
  607. std::string output;
  608. int retVal = 1;
  609. bool resB = cmSystemTools::RunSingleCommand(
  610. buildCommand.c_str(), &output, &output, &retVal,
  611. installDirectory.c_str(), this->GeneratorVerbose, cmDuration::zero());
  612. if (!resB || retVal) {
  613. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  614. tmpFile += "/PreinstallOutput.log";
  615. cmGeneratedFileStream ofs(tmpFile);
  616. ofs << "# Run command: " << buildCommand << std::endl
  617. << "# Directory: " << installDirectory << std::endl
  618. << "# Output:" << std::endl
  619. << output << std::endl;
  620. cmCPackLogger(cmCPackLog::LOG_ERROR,
  621. "Problem running install command: "
  622. << buildCommand << std::endl
  623. << "Please check " << tmpFile << " for errors"
  624. << std::endl);
  625. return 0;
  626. }
  627. }
  628. return 1;
  629. }
  630. int cmCPackGenerator::InstallCMakeProject(
  631. bool setDestDir, const std::string& installDirectory,
  632. const std::string& baseTempInstallDirectory, const mode_t* default_dir_mode,
  633. const std::string& component, bool componentInstall,
  634. const std::string& installSubDirectory, const std::string& buildConfig,
  635. std::string& absoluteDestFiles)
  636. {
  637. std::string tempInstallDirectory = baseTempInstallDirectory;
  638. std::string installFile = installDirectory + "/cmake_install.cmake";
  639. if (componentInstall) {
  640. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  641. "- Install component: " << component << std::endl);
  642. }
  643. cmake cm(cmake::RoleScript, cmState::CPack);
  644. cm.SetHomeDirectory("");
  645. cm.SetHomeOutputDirectory("");
  646. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  647. cm.AddCMakePaths();
  648. cm.SetProgressCallback(cmCPackGeneratorProgress, this);
  649. cm.SetTrace(this->Trace);
  650. cm.SetTraceExpand(this->TraceExpand);
  651. cmGlobalGenerator gg(&cm);
  652. cmMakefile mf(&gg, cm.GetCurrentSnapshot());
  653. if (!installSubDirectory.empty() && installSubDirectory != "/" &&
  654. installSubDirectory != ".") {
  655. tempInstallDirectory += installSubDirectory;
  656. }
  657. if (componentInstall) {
  658. tempInstallDirectory += "/";
  659. // Some CPack generators would rather chose
  660. // the local installation directory suffix.
  661. // Some (e.g. RPM) use
  662. // one install directory for each component **GROUP**
  663. // instead of the default
  664. // one install directory for each component.
  665. tempInstallDirectory += GetComponentInstallDirNameSuffix(component);
  666. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  667. tempInstallDirectory += "/";
  668. tempInstallDirectory += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  669. }
  670. }
  671. const char* default_dir_inst_permissions =
  672. this->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
  673. if (default_dir_inst_permissions && *default_dir_inst_permissions) {
  674. mf.AddDefinition("CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS",
  675. default_dir_inst_permissions);
  676. }
  677. if (!setDestDir) {
  678. tempInstallDirectory += this->GetPackagingInstallPrefix();
  679. }
  680. if (setDestDir) {
  681. // For DESTDIR based packaging, use the *project*
  682. // CMAKE_INSTALL_PREFIX underneath the tempInstallDirectory. The
  683. // value of the project's CMAKE_INSTALL_PREFIX is sent in here as
  684. // the value of the CPACK_INSTALL_PREFIX variable.
  685. //
  686. // If DESTDIR has been 'internally set ON' this means that
  687. // the underlying CPack specific generator did ask for that
  688. // In this case we may override CPACK_INSTALL_PREFIX with
  689. // CPACK_PACKAGING_INSTALL_PREFIX
  690. // I know this is tricky and awkward but it's the price for
  691. // CPACK_SET_DESTDIR backward compatibility.
  692. if (cmSystemTools::IsInternallyOn(this->GetOption("CPACK_SET_DESTDIR"))) {
  693. this->SetOption("CPACK_INSTALL_PREFIX",
  694. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX"));
  695. }
  696. std::string dir;
  697. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  698. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  699. }
  700. mf.AddDefinition("CMAKE_INSTALL_PREFIX", dir.c_str());
  701. cmCPackLogger(
  702. cmCPackLog::LOG_DEBUG,
  703. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf.AddDefinition)"
  704. << std::endl);
  705. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  706. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  707. << std::endl);
  708. // Make sure that DESTDIR + CPACK_INSTALL_PREFIX directory
  709. // exists:
  710. //
  711. if (cmSystemTools::StringStartsWith(dir.c_str(), "/")) {
  712. dir = tempInstallDirectory + dir;
  713. } else {
  714. dir = tempInstallDirectory + "/" + dir;
  715. }
  716. /*
  717. * We must re-set DESTDIR for each component
  718. * We must not add the CPACK_INSTALL_PREFIX part because
  719. * it will be added using the override of CMAKE_INSTALL_PREFIX
  720. * The main reason for this awkward trick is that
  721. * are using DESTDIR for 2 different reasons:
  722. * - Because it was asked by the CPack Generator or the user
  723. * using CPACK_SET_DESTDIR
  724. * - Because it was already used for component install
  725. * in order to put things in subdirs...
  726. */
  727. cmSystemTools::PutEnv(std::string("DESTDIR=") + tempInstallDirectory);
  728. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  729. "- Creating directory: '" << dir << "'" << std::endl);
  730. if (!cmsys::SystemTools::MakeDirectory(dir, default_dir_mode)) {
  731. cmCPackLogger(cmCPackLog::LOG_ERROR,
  732. "Problem creating temporary directory: " << dir
  733. << std::endl);
  734. return 0;
  735. }
  736. } else {
  737. mf.AddDefinition("CMAKE_INSTALL_PREFIX", tempInstallDirectory.c_str());
  738. if (!cmsys::SystemTools::MakeDirectory(tempInstallDirectory,
  739. default_dir_mode)) {
  740. cmCPackLogger(cmCPackLog::LOG_ERROR,
  741. "Problem creating temporary directory: "
  742. << tempInstallDirectory << std::endl);
  743. return 0;
  744. }
  745. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  746. "- Using non-DESTDIR install... (mf.AddDefinition)"
  747. << std::endl);
  748. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  749. "- Setting CMAKE_INSTALL_PREFIX to '" << tempInstallDirectory
  750. << "'" << std::endl);
  751. }
  752. if (!buildConfig.empty()) {
  753. mf.AddDefinition("BUILD_TYPE", buildConfig.c_str());
  754. }
  755. std::string installComponentLowerCase = cmSystemTools::LowerCase(component);
  756. if (installComponentLowerCase != "all") {
  757. mf.AddDefinition("CMAKE_INSTALL_COMPONENT", component.c_str());
  758. }
  759. // strip on TRUE, ON, 1, one or several file names, but not on
  760. // FALSE, OFF, 0 and an empty string
  761. if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES"))) {
  762. mf.AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
  763. }
  764. // Remember the list of files before installation
  765. // of the current component (if we are in component install)
  766. std::string const& InstallPrefix = tempInstallDirectory;
  767. std::vector<std::string> filesBefore;
  768. std::string findExpr = tempInstallDirectory;
  769. if (componentInstall) {
  770. cmsys::Glob glB;
  771. findExpr += "/*";
  772. glB.RecurseOn();
  773. glB.SetRecurseListDirs(true);
  774. glB.FindFiles(findExpr);
  775. filesBefore = glB.GetFiles();
  776. std::sort(filesBefore.begin(), filesBefore.end());
  777. }
  778. // If CPack was asked to warn on ABSOLUTE INSTALL DESTINATION
  779. // then forward request to cmake_install.cmake script
  780. if (this->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  781. mf.AddDefinition("CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  782. }
  783. // If current CPack generator does support
  784. // ABSOLUTE INSTALL DESTINATION or CPack has been asked for
  785. // then ask cmake_install.cmake script to error out
  786. // as soon as it occurs (before installing file)
  787. if (!SupportsAbsoluteDestination() ||
  788. this->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  789. mf.AddDefinition("CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  790. }
  791. // do installation
  792. int res = mf.ReadListFile(installFile.c_str());
  793. // forward definition of CMAKE_ABSOLUTE_DESTINATION_FILES
  794. // to CPack (may be used by generators like CPack RPM or DEB)
  795. // in order to transparently handle ABSOLUTE PATH
  796. if (mf.GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES")) {
  797. mf.AddDefinition("CPACK_ABSOLUTE_DESTINATION_FILES",
  798. mf.GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES"));
  799. }
  800. // Now rebuild the list of files after installation
  801. // of the current component (if we are in component install)
  802. if (componentInstall) {
  803. cmsys::Glob glA;
  804. glA.RecurseOn();
  805. glA.SetRecurseListDirs(true);
  806. glA.SetRecurseThroughSymlinks(false);
  807. glA.FindFiles(findExpr);
  808. std::vector<std::string> filesAfter = glA.GetFiles();
  809. std::sort(filesAfter.begin(), filesAfter.end());
  810. std::vector<std::string>::iterator diff;
  811. std::vector<std::string> result(filesAfter.size());
  812. diff = std::set_difference(filesAfter.begin(), filesAfter.end(),
  813. filesBefore.begin(), filesBefore.end(),
  814. result.begin());
  815. std::vector<std::string>::iterator fit;
  816. std::string localFileName;
  817. // Populate the File field of each component
  818. for (fit = result.begin(); fit != diff; ++fit) {
  819. localFileName = cmSystemTools::RelativePath(InstallPrefix, *fit);
  820. localFileName =
  821. localFileName.substr(localFileName.find_first_not_of('/'));
  822. Components[component].Files.push_back(localFileName);
  823. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  824. "Adding file <" << localFileName << "> to component <"
  825. << component << ">" << std::endl);
  826. }
  827. }
  828. if (nullptr != mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES")) {
  829. if (!absoluteDestFiles.empty()) {
  830. absoluteDestFiles += ";";
  831. }
  832. absoluteDestFiles += mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  833. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  834. "Got some ABSOLUTE DESTINATION FILES: " << absoluteDestFiles
  835. << std::endl);
  836. // define component specific var
  837. if (componentInstall) {
  838. std::string absoluteDestFileComponent =
  839. std::string("CPACK_ABSOLUTE_DESTINATION_FILES") + "_" +
  840. GetComponentInstallDirNameSuffix(component);
  841. if (nullptr != this->GetOption(absoluteDestFileComponent)) {
  842. std::string absoluteDestFilesListComponent =
  843. this->GetOption(absoluteDestFileComponent);
  844. absoluteDestFilesListComponent += ";";
  845. absoluteDestFilesListComponent +=
  846. mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  847. this->SetOption(absoluteDestFileComponent,
  848. absoluteDestFilesListComponent.c_str());
  849. } else {
  850. this->SetOption(absoluteDestFileComponent,
  851. mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES"));
  852. }
  853. }
  854. }
  855. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  856. return 0;
  857. }
  858. return 1;
  859. }
  860. bool cmCPackGenerator::ReadListFile(const char* moduleName)
  861. {
  862. bool retval;
  863. std::string fullPath = this->MakefileMap->GetModulesFile(moduleName);
  864. retval = this->MakefileMap->ReadListFile(fullPath.c_str());
  865. // include FATAL_ERROR and ERROR in the return status
  866. retval = retval && (!cmSystemTools::GetErrorOccuredFlag());
  867. return retval;
  868. }
  869. void cmCPackGenerator::SetOptionIfNotSet(const std::string& op,
  870. const char* value)
  871. {
  872. const char* def = this->MakefileMap->GetDefinition(op);
  873. if (def && *def) {
  874. return;
  875. }
  876. this->SetOption(op, value);
  877. }
  878. void cmCPackGenerator::SetOption(const std::string& op, const char* value)
  879. {
  880. if (!value) {
  881. this->MakefileMap->RemoveDefinition(op);
  882. return;
  883. }
  884. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  885. this->GetNameOfClass() << "::SetOption(" << op << ", " << value
  886. << ")" << std::endl);
  887. this->MakefileMap->AddDefinition(op, value);
  888. }
  889. int cmCPackGenerator::DoPackage()
  890. {
  891. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  892. "Create package using " << this->Name << std::endl);
  893. // Prepare CPack internal name and check
  894. // values for many CPACK_xxx vars
  895. if (!this->PrepareNames()) {
  896. return 0;
  897. }
  898. // Digest Component grouping specification
  899. if (!this->PrepareGroupingKind()) {
  900. return 0;
  901. }
  902. if (cmSystemTools::IsOn(
  903. this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY"))) {
  904. const char* toplevelDirectory =
  905. this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  906. if (cmSystemTools::FileExists(toplevelDirectory)) {
  907. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  908. "Remove toplevel directory: " << toplevelDirectory
  909. << std::endl);
  910. if (!cmSystemTools::RepeatedRemoveDirectory(toplevelDirectory)) {
  911. cmCPackLogger(cmCPackLog::LOG_ERROR,
  912. "Problem removing toplevel directory: "
  913. << toplevelDirectory << std::endl);
  914. return 0;
  915. }
  916. }
  917. }
  918. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  919. "About to install project " << std::endl);
  920. if (!this->InstallProject()) {
  921. return 0;
  922. }
  923. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Done install project " << std::endl);
  924. const char* tempPackageFileName =
  925. this->GetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  926. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  927. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  928. cmsys::Glob gl;
  929. std::string findExpr = tempDirectory;
  930. findExpr += "/*";
  931. gl.RecurseOn();
  932. gl.SetRecurseListDirs(true);
  933. gl.SetRecurseThroughSymlinks(false);
  934. if (!gl.FindFiles(findExpr)) {
  935. cmCPackLogger(cmCPackLog::LOG_ERROR,
  936. "Cannot find any files in the packaging tree" << std::endl);
  937. return 0;
  938. }
  939. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package" << std::endl);
  940. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  941. "Package files to: "
  942. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  943. << std::endl);
  944. if (cmSystemTools::FileExists(tempPackageFileName)) {
  945. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  946. "Remove old package file" << std::endl);
  947. cmSystemTools::RemoveFile(tempPackageFileName);
  948. }
  949. if (cmSystemTools::IsOn(
  950. this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  951. tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  952. }
  953. // The files to be installed
  954. files = gl.GetFiles();
  955. packageFileNames.clear();
  956. /* Put at least one file name into the list of
  957. * wanted packageFileNames. The specific generator
  958. * may update this during PackageFiles.
  959. * (either putting several names or updating the provided one)
  960. */
  961. packageFileNames.emplace_back(tempPackageFileName ? tempPackageFileName
  962. : "");
  963. toplevel = tempDirectory;
  964. { // scope that enables package generators to run internal scripts with
  965. // latest CMake policies enabled
  966. cmMakefile::ScopePushPop pp{ this->MakefileMap };
  967. this->MakefileMap->SetPolicyVersion(cmVersion::GetCMakeVersion(),
  968. std::string());
  969. if (!this->PackageFiles() || cmSystemTools::GetErrorOccuredFlag()) {
  970. cmCPackLogger(cmCPackLog::LOG_ERROR,
  971. "Problem compressing the directory" << std::endl);
  972. return 0;
  973. }
  974. }
  975. /* Prepare checksum algorithm*/
  976. const char* algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  977. std::unique_ptr<cmCryptoHash> crypto = cmCryptoHash::New(algo ? algo : "");
  978. /*
  979. * Copy the generated packages to final destination
  980. * - there may be several of them
  981. * - the initially provided name may have changed
  982. * (because the specific generator did 'normalize' it)
  983. */
  984. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  985. "Copying final package(s) [" << packageFileNames.size()
  986. << "]:" << std::endl);
  987. /* now copy package one by one */
  988. for (std::string const& pkgFileName : packageFileNames) {
  989. std::string tmpPF(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  990. std::string filename(cmSystemTools::GetFilenameName(pkgFileName));
  991. tempPackageFileName = pkgFileName.c_str();
  992. tmpPF += "/" + filename;
  993. const char* packageFileName = tmpPF.c_str();
  994. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  995. "Copy final package(s): "
  996. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  997. << " to " << (packageFileName ? packageFileName : "(NULL)")
  998. << std::endl);
  999. if (!cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
  1000. packageFileName)) {
  1001. cmCPackLogger(
  1002. cmCPackLog::LOG_ERROR,
  1003. "Problem copying the package: "
  1004. << (tempPackageFileName ? tempPackageFileName : "(NULL)") << " to "
  1005. << (packageFileName ? packageFileName : "(NULL)") << std::endl);
  1006. return 0;
  1007. }
  1008. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1009. "- package: " << packageFileName << " generated."
  1010. << std::endl);
  1011. /* Generate checksum file */
  1012. if (crypto) {
  1013. std::string hashFile(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  1014. hashFile += "/" + filename;
  1015. hashFile += "." + cmSystemTools::LowerCase(algo);
  1016. cmsys::ofstream outF(hashFile.c_str());
  1017. if (!outF) {
  1018. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1019. "Cannot create checksum file: " << hashFile
  1020. << std::endl);
  1021. return 0;
  1022. }
  1023. outF << crypto->HashFile(packageFileName) << " " << filename << "\n";
  1024. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1025. "- checksum file: " << hashFile << " generated."
  1026. << std::endl);
  1027. }
  1028. }
  1029. return 1;
  1030. }
  1031. int cmCPackGenerator::Initialize(const std::string& name, cmMakefile* mf)
  1032. {
  1033. this->MakefileMap = mf;
  1034. this->Name = name;
  1035. // set the running generator name
  1036. this->SetOption("CPACK_GENERATOR", this->Name.c_str());
  1037. // Load the project specific config file
  1038. const char* config = this->GetOption("CPACK_PROJECT_CONFIG_FILE");
  1039. if (config) {
  1040. mf->ReadListFile(config);
  1041. }
  1042. int result = this->InitializeInternal();
  1043. if (cmSystemTools::GetErrorOccuredFlag()) {
  1044. return 0;
  1045. }
  1046. // If a generator subclass did not already set this option in its
  1047. // InitializeInternal implementation, and the project did not already set
  1048. // it, the default value should be:
  1049. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
  1050. return result;
  1051. }
  1052. int cmCPackGenerator::InitializeInternal()
  1053. {
  1054. return 1;
  1055. }
  1056. bool cmCPackGenerator::IsSet(const std::string& name) const
  1057. {
  1058. return this->MakefileMap->IsSet(name);
  1059. }
  1060. bool cmCPackGenerator::IsOn(const std::string& name) const
  1061. {
  1062. return cmSystemTools::IsOn(GetOption(name));
  1063. }
  1064. bool cmCPackGenerator::IsSetToOff(const std::string& op) const
  1065. {
  1066. const char* ret = this->MakefileMap->GetDefinition(op);
  1067. if (ret && *ret) {
  1068. return cmSystemTools::IsOff(ret);
  1069. }
  1070. return false;
  1071. }
  1072. bool cmCPackGenerator::IsSetToEmpty(const std::string& op) const
  1073. {
  1074. const char* ret = this->MakefileMap->GetDefinition(op);
  1075. if (ret) {
  1076. return !*ret;
  1077. }
  1078. return false;
  1079. }
  1080. const char* cmCPackGenerator::GetOption(const std::string& op) const
  1081. {
  1082. const char* ret = this->MakefileMap->GetDefinition(op);
  1083. if (!ret) {
  1084. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1085. "Warning, GetOption return NULL for: " << op << std::endl);
  1086. }
  1087. return ret;
  1088. }
  1089. std::vector<std::string> cmCPackGenerator::GetOptions() const
  1090. {
  1091. return this->MakefileMap->GetDefinitions();
  1092. }
  1093. int cmCPackGenerator::PackageFiles()
  1094. {
  1095. return 0;
  1096. }
  1097. const char* cmCPackGenerator::GetInstallPath()
  1098. {
  1099. if (!this->InstallPath.empty()) {
  1100. return this->InstallPath.c_str();
  1101. }
  1102. #if defined(_WIN32) && !defined(__CYGWIN__)
  1103. std::string prgfiles;
  1104. std::string sysDrive;
  1105. if (cmsys::SystemTools::GetEnv("ProgramFiles", prgfiles)) {
  1106. this->InstallPath = prgfiles;
  1107. } else if (cmsys::SystemTools::GetEnv("SystemDrive", sysDrive)) {
  1108. this->InstallPath = sysDrive;
  1109. this->InstallPath += "/Program Files";
  1110. } else {
  1111. this->InstallPath = "c:/Program Files";
  1112. }
  1113. this->InstallPath += "/";
  1114. this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  1115. this->InstallPath += "-";
  1116. this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  1117. #elif defined(__HAIKU__)
  1118. char dir[B_PATH_NAME_LENGTH];
  1119. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1120. B_OK) {
  1121. this->InstallPath = dir;
  1122. } else {
  1123. this->InstallPath = "/boot/system";
  1124. }
  1125. #else
  1126. this->InstallPath = "/usr/local/";
  1127. #endif
  1128. return this->InstallPath.c_str();
  1129. }
  1130. const char* cmCPackGenerator::GetPackagingInstallPrefix()
  1131. {
  1132. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1133. "GetPackagingInstallPrefix: '"
  1134. << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'"
  1135. << std::endl);
  1136. return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  1137. }
  1138. std::string cmCPackGenerator::FindTemplate(const char* name)
  1139. {
  1140. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1141. "Look for template: " << (name ? name : "(NULL)")
  1142. << std::endl);
  1143. std::string ffile = this->MakefileMap->GetModulesFile(name);
  1144. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  1145. "Found template: " << ffile << std::endl);
  1146. return ffile;
  1147. }
  1148. bool cmCPackGenerator::ConfigureString(const std::string& inString,
  1149. std::string& outString)
  1150. {
  1151. this->MakefileMap->ConfigureString(inString, outString, true, false);
  1152. return true;
  1153. }
  1154. bool cmCPackGenerator::ConfigureFile(const char* inName, const char* outName,
  1155. bool copyOnly /* = false */)
  1156. {
  1157. return this->MakefileMap->ConfigureFile(inName, outName, copyOnly, true,
  1158. false) == 1;
  1159. }
  1160. int cmCPackGenerator::CleanTemporaryDirectory()
  1161. {
  1162. std::string tempInstallDirectoryWithPostfix =
  1163. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  1164. const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
  1165. if (cmsys::SystemTools::FileExists(tempInstallDirectory)) {
  1166. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1167. "- Clean temporary : " << tempInstallDirectory << std::endl);
  1168. if (!cmSystemTools::RepeatedRemoveDirectory(tempInstallDirectory)) {
  1169. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1170. "Problem removing temporary directory: "
  1171. << tempInstallDirectory << std::endl);
  1172. return 0;
  1173. }
  1174. }
  1175. return 1;
  1176. }
  1177. cmInstalledFile const* cmCPackGenerator::GetInstalledFile(
  1178. std::string const& name) const
  1179. {
  1180. cmake const* cm = this->MakefileMap->GetCMakeInstance();
  1181. return cm->GetInstalledFile(name);
  1182. }
  1183. int cmCPackGenerator::PrepareGroupingKind()
  1184. {
  1185. // find a component package method specified by the user
  1186. ComponentPackageMethod method = UNKNOWN_COMPONENT_PACKAGE_METHOD;
  1187. if (this->GetOption("CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE")) {
  1188. method = ONE_PACKAGE;
  1189. }
  1190. if (this->GetOption("CPACK_COMPONENTS_IGNORE_GROUPS")) {
  1191. method = ONE_PACKAGE_PER_COMPONENT;
  1192. }
  1193. if (this->GetOption("CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP")) {
  1194. method = ONE_PACKAGE_PER_GROUP;
  1195. }
  1196. std::string groupingType;
  1197. // Second way to specify grouping
  1198. if (nullptr != this->GetOption("CPACK_COMPONENTS_GROUPING")) {
  1199. groupingType = this->GetOption("CPACK_COMPONENTS_GROUPING");
  1200. }
  1201. if (!groupingType.empty()) {
  1202. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  1203. "[" << this->Name << "]"
  1204. << " requested component grouping = " << groupingType
  1205. << std::endl);
  1206. if (groupingType == "ALL_COMPONENTS_IN_ONE") {
  1207. method = ONE_PACKAGE;
  1208. } else if (groupingType == "IGNORE") {
  1209. method = ONE_PACKAGE_PER_COMPONENT;
  1210. } else if (groupingType == "ONE_PER_GROUP") {
  1211. method = ONE_PACKAGE_PER_GROUP;
  1212. } else {
  1213. cmCPackLogger(
  1214. cmCPackLog::LOG_WARNING,
  1215. "[" << this->Name << "]"
  1216. << " requested component grouping type <" << groupingType
  1217. << "> UNKNOWN not in (ALL_COMPONENTS_IN_ONE,IGNORE,ONE_PER_GROUP)"
  1218. << std::endl);
  1219. }
  1220. }
  1221. // Some components were defined but NO group
  1222. // fallback to default if not group based
  1223. if (method == ONE_PACKAGE_PER_GROUP && this->ComponentGroups.empty() &&
  1224. !this->Components.empty()) {
  1225. if (componentPackageMethod == ONE_PACKAGE) {
  1226. method = ONE_PACKAGE;
  1227. } else {
  1228. method = ONE_PACKAGE_PER_COMPONENT;
  1229. }
  1230. cmCPackLogger(
  1231. cmCPackLog::LOG_WARNING,
  1232. "[" << this->Name << "]"
  1233. << " One package per component group requested, "
  1234. << "but NO component groups exist: Ignoring component group."
  1235. << std::endl);
  1236. }
  1237. // if user specified packaging method, override the default packaging method
  1238. if (method != UNKNOWN_COMPONENT_PACKAGE_METHOD) {
  1239. componentPackageMethod = method;
  1240. }
  1241. const char* method_names[] = { "ALL_COMPONENTS_IN_ONE", "IGNORE_GROUPS",
  1242. "ONE_PER_GROUP" };
  1243. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  1244. "[" << this->Name << "]"
  1245. << " requested component grouping = "
  1246. << method_names[componentPackageMethod] << std::endl);
  1247. return 1;
  1248. }
  1249. std::string cmCPackGenerator::GetComponentInstallDirNameSuffix(
  1250. const std::string& componentName)
  1251. {
  1252. return componentName;
  1253. }
  1254. std::string cmCPackGenerator::GetComponentPackageFileName(
  1255. const std::string& initialPackageFileName,
  1256. const std::string& groupOrComponentName, bool isGroupName)
  1257. {
  1258. /*
  1259. * the default behavior is to use the
  1260. * component [group] name as a suffix
  1261. */
  1262. std::string suffix = "-" + groupOrComponentName;
  1263. /* check if we should use DISPLAY name */
  1264. std::string dispNameVar = "CPACK_" + Name + "_USE_DISPLAY_NAME_IN_FILENAME";
  1265. if (IsOn(dispNameVar)) {
  1266. /* the component Group case */
  1267. if (isGroupName) {
  1268. std::string groupDispVar = "CPACK_COMPONENT_GROUP_" +
  1269. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1270. const char* groupDispName = GetOption(groupDispVar);
  1271. if (groupDispName) {
  1272. suffix = "-" + std::string(groupDispName);
  1273. }
  1274. }
  1275. /* the [single] component case */
  1276. else {
  1277. std::string dispVar = "CPACK_COMPONENT_" +
  1278. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1279. const char* dispName = GetOption(dispVar);
  1280. if (dispName) {
  1281. suffix = "-" + std::string(dispName);
  1282. }
  1283. }
  1284. }
  1285. return initialPackageFileName + suffix;
  1286. }
  1287. enum cmCPackGenerator::CPackSetDestdirSupport
  1288. cmCPackGenerator::SupportsSetDestdir() const
  1289. {
  1290. return cmCPackGenerator::SETDESTDIR_SUPPORTED;
  1291. }
  1292. bool cmCPackGenerator::SupportsAbsoluteDestination() const
  1293. {
  1294. return true;
  1295. }
  1296. bool cmCPackGenerator::SupportsComponentInstallation() const
  1297. {
  1298. return false;
  1299. }
  1300. bool cmCPackGenerator::WantsComponentInstallation() const
  1301. {
  1302. return (!IsOn("CPACK_MONOLITHIC_INSTALL") &&
  1303. SupportsComponentInstallation()
  1304. // check that we have at least one group or component
  1305. && (!this->ComponentGroups.empty() || !this->Components.empty()));
  1306. }
  1307. cmCPackInstallationType* cmCPackGenerator::GetInstallationType(
  1308. const std::string& projectName, const std::string& name)
  1309. {
  1310. (void)projectName;
  1311. bool hasInstallationType = this->InstallationTypes.count(name) != 0;
  1312. cmCPackInstallationType* installType = &this->InstallationTypes[name];
  1313. if (!hasInstallationType) {
  1314. // Define the installation type
  1315. std::string macroPrefix =
  1316. "CPACK_INSTALL_TYPE_" + cmsys::SystemTools::UpperCase(name);
  1317. installType->Name = name;
  1318. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1319. if (displayName && *displayName) {
  1320. installType->DisplayName = displayName;
  1321. } else {
  1322. installType->DisplayName = installType->Name;
  1323. }
  1324. installType->Index = static_cast<unsigned>(this->InstallationTypes.size());
  1325. }
  1326. return installType;
  1327. }
  1328. cmCPackComponent* cmCPackGenerator::GetComponent(
  1329. const std::string& projectName, const std::string& name)
  1330. {
  1331. bool hasComponent = this->Components.count(name) != 0;
  1332. cmCPackComponent* component = &this->Components[name];
  1333. if (!hasComponent) {
  1334. // Define the component
  1335. std::string macroPrefix =
  1336. "CPACK_COMPONENT_" + cmsys::SystemTools::UpperCase(name);
  1337. component->Name = name;
  1338. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1339. if (displayName && *displayName) {
  1340. component->DisplayName = displayName;
  1341. } else {
  1342. component->DisplayName = component->Name;
  1343. }
  1344. component->IsHidden = this->IsOn(macroPrefix + "_HIDDEN");
  1345. component->IsRequired = this->IsOn(macroPrefix + "_REQUIRED");
  1346. component->IsDisabledByDefault = this->IsOn(macroPrefix + "_DISABLED");
  1347. component->IsDownloaded = this->IsOn(macroPrefix + "_DOWNLOADED") ||
  1348. cmSystemTools::IsOn(this->GetOption("CPACK_DOWNLOAD_ALL"));
  1349. const char* archiveFile = this->GetOption(macroPrefix + "_ARCHIVE_FILE");
  1350. if (archiveFile && *archiveFile) {
  1351. component->ArchiveFile = archiveFile;
  1352. }
  1353. const char* plist = this->GetOption(macroPrefix + "_PLIST");
  1354. if (plist && *plist) {
  1355. component->Plist = plist;
  1356. }
  1357. const char* groupName = this->GetOption(macroPrefix + "_GROUP");
  1358. if (groupName && *groupName) {
  1359. component->Group = GetComponentGroup(projectName, groupName);
  1360. component->Group->Components.push_back(component);
  1361. } else {
  1362. component->Group = nullptr;
  1363. }
  1364. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1365. if (description && *description) {
  1366. component->Description = description;
  1367. }
  1368. // Determine the installation types.
  1369. const char* installTypes = this->GetOption(macroPrefix + "_INSTALL_TYPES");
  1370. if (installTypes && *installTypes) {
  1371. std::vector<std::string> installTypesVector;
  1372. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  1373. for (std::string const& installType : installTypesVector) {
  1374. component->InstallationTypes.push_back(
  1375. this->GetInstallationType(projectName, installType));
  1376. }
  1377. }
  1378. // Determine the component dependencies.
  1379. const char* depends = this->GetOption(macroPrefix + "_DEPENDS");
  1380. if (depends && *depends) {
  1381. std::vector<std::string> dependsVector;
  1382. cmSystemTools::ExpandListArgument(depends, dependsVector);
  1383. for (std::string const& depend : dependsVector) {
  1384. cmCPackComponent* child = GetComponent(projectName, depend);
  1385. component->Dependencies.push_back(child);
  1386. child->ReverseDependencies.push_back(component);
  1387. }
  1388. }
  1389. }
  1390. return component;
  1391. }
  1392. cmCPackComponentGroup* cmCPackGenerator::GetComponentGroup(
  1393. const std::string& projectName, const std::string& name)
  1394. {
  1395. (void)projectName;
  1396. std::string macroPrefix =
  1397. "CPACK_COMPONENT_GROUP_" + cmsys::SystemTools::UpperCase(name);
  1398. bool hasGroup = this->ComponentGroups.count(name) != 0;
  1399. cmCPackComponentGroup* group = &this->ComponentGroups[name];
  1400. if (!hasGroup) {
  1401. // Define the group
  1402. group->Name = name;
  1403. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1404. if (displayName && *displayName) {
  1405. group->DisplayName = displayName;
  1406. } else {
  1407. group->DisplayName = group->Name;
  1408. }
  1409. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1410. if (description && *description) {
  1411. group->Description = description;
  1412. }
  1413. group->IsBold = this->IsOn(macroPrefix + "_BOLD_TITLE");
  1414. group->IsExpandedByDefault = this->IsOn(macroPrefix + "_EXPANDED");
  1415. const char* parentGroupName =
  1416. this->GetOption(macroPrefix + "_PARENT_GROUP");
  1417. if (parentGroupName && *parentGroupName) {
  1418. group->ParentGroup = GetComponentGroup(projectName, parentGroupName);
  1419. group->ParentGroup->Subgroups.push_back(group);
  1420. } else {
  1421. group->ParentGroup = nullptr;
  1422. }
  1423. }
  1424. return group;
  1425. }