cmCPackGenerator.cxx 56 KB

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