cmCPackGenerator.cxx 56 KB

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