cmGlobalUnixMakefileGenerator3.cxx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGlobalUnixMakefileGenerator3.h"
  11. #include "cmLocalUnixMakefileGenerator3.h"
  12. #include "cmMakefileTargetGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmake.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmSourceFile.h"
  17. #include "cmTarget.h"
  18. #include "cmGeneratorTarget.h"
  19. cmGlobalUnixMakefileGenerator3::cmGlobalUnixMakefileGenerator3()
  20. {
  21. // This type of makefile always requires unix style paths
  22. this->ForceUnixPaths = true;
  23. this->FindMakeProgramFile = "CMakeUnixFindMake.cmake";
  24. this->ToolSupportsColor = true;
  25. #if defined(_WIN32) || defined(__VMS)
  26. this->UseLinkScript = false;
  27. #else
  28. this->UseLinkScript = true;
  29. #endif
  30. this->CommandDatabase = NULL;
  31. }
  32. void cmGlobalUnixMakefileGenerator3
  33. ::EnableLanguage(std::vector<std::string>const& languages,
  34. cmMakefile *mf,
  35. bool optional)
  36. {
  37. this->cmGlobalGenerator::EnableLanguage(languages, mf, optional);
  38. for(std::vector<std::string>::const_iterator l = languages.begin();
  39. l != languages.end(); ++l)
  40. {
  41. if(*l == "NONE")
  42. {
  43. continue;
  44. }
  45. this->ResolveLanguageCompiler(*l, mf, optional);
  46. }
  47. }
  48. ///! Create a local generator appropriate to this Global Generator
  49. cmLocalGenerator *cmGlobalUnixMakefileGenerator3::CreateLocalGenerator()
  50. {
  51. cmLocalGenerator* lg = new cmLocalUnixMakefileGenerator3;
  52. lg->SetGlobalGenerator(this);
  53. return lg;
  54. }
  55. //----------------------------------------------------------------------------
  56. void cmGlobalUnixMakefileGenerator3
  57. ::GetDocumentation(cmDocumentationEntry& entry) const
  58. {
  59. entry.Name = this->GetName();
  60. entry.Brief = "Generates standard UNIX makefiles.";
  61. entry.Full =
  62. "A hierarchy of UNIX makefiles is generated into the build tree. Any "
  63. "standard UNIX-style make program can build the project through the "
  64. "default make target. A \"make install\" target is also provided.";
  65. }
  66. //----------------------------------------------------------------------------
  67. void
  68. cmGlobalUnixMakefileGenerator3
  69. ::ComputeTargetObjects(cmGeneratorTarget* gt) const
  70. {
  71. cmTarget* target = gt->Target;
  72. cmLocalUnixMakefileGenerator3* lg =
  73. static_cast<cmLocalUnixMakefileGenerator3*>(gt->LocalGenerator);
  74. // Compute full path to object file directory for this target.
  75. std::string dir_max;
  76. dir_max += gt->Makefile->GetCurrentOutputDirectory();
  77. dir_max += "/";
  78. dir_max += gt->LocalGenerator->GetTargetDirectory(*target);
  79. dir_max += "/";
  80. gt->ObjectDirectory = dir_max;
  81. // Compute the name of each object file.
  82. for(std::vector<cmSourceFile*>::iterator
  83. si = gt->ObjectSources.begin();
  84. si != gt->ObjectSources.end(); ++si)
  85. {
  86. cmSourceFile* sf = *si;
  87. bool hasSourceExtension = true;
  88. std::string objectName = gt->LocalGenerator
  89. ->GetObjectFileNameWithoutTarget(*sf, dir_max,
  90. &hasSourceExtension);
  91. gt->Objects[sf] = objectName;
  92. lg->AddLocalObjectFile(target, sf, objectName, hasSourceExtension);
  93. }
  94. }
  95. void cmGlobalUnixMakefileGenerator3::Generate()
  96. {
  97. // first do superclass method
  98. this->cmGlobalGenerator::Generate();
  99. // initialize progress
  100. unsigned long total = 0;
  101. for(ProgressMapType::const_iterator pmi = this->ProgressMap.begin();
  102. pmi != this->ProgressMap.end(); ++pmi)
  103. {
  104. total += pmi->second.NumberOfActions;
  105. }
  106. // write each target's progress.make this loop is done twice. Bascially the
  107. // Generate pass counts all the actions, the first loop below determines
  108. // how many actions have progress updates for each target and writes to
  109. // corrrect variable values for everything except the all targets. The
  110. // second loop actually writes out correct values for the all targets as
  111. // well. This is because the all targets require more information that is
  112. // computed in the first loop.
  113. unsigned long current = 0;
  114. for(ProgressMapType::iterator pmi = this->ProgressMap.begin();
  115. pmi != this->ProgressMap.end(); ++pmi)
  116. {
  117. pmi->second.WriteProgressVariables(total, current);
  118. }
  119. for(unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  120. {
  121. cmLocalUnixMakefileGenerator3 *lg =
  122. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  123. std::string markFileName = lg->GetMakefile()->GetStartOutputDirectory();
  124. markFileName += "/";
  125. markFileName += cmake::GetCMakeFilesDirectory();
  126. markFileName += "/progress.marks";
  127. cmGeneratedFileStream markFile(markFileName.c_str());
  128. markFile << this->CountProgressMarksInAll(lg) << "\n";
  129. }
  130. // write the main makefile
  131. this->WriteMainMakefile2();
  132. this->WriteMainCMakefile();
  133. if (this->CommandDatabase != NULL) {
  134. *this->CommandDatabase << std::endl << "]";
  135. delete this->CommandDatabase;
  136. this->CommandDatabase = NULL;
  137. }
  138. }
  139. void cmGlobalUnixMakefileGenerator3::AddCXXCompileCommand(
  140. const std::string &sourceFile, const std::string &workingDirectory,
  141. const std::string &compileCommand) {
  142. if (this->CommandDatabase == NULL)
  143. {
  144. std::string commandDatabaseName =
  145. std::string(this->GetCMakeInstance()->GetHomeOutputDirectory())
  146. + "/compile_commands.json";
  147. this->CommandDatabase =
  148. new cmGeneratedFileStream(commandDatabaseName.c_str());
  149. *this->CommandDatabase << "[" << std::endl;
  150. } else {
  151. *this->CommandDatabase << "," << std::endl;
  152. }
  153. *this->CommandDatabase << "{" << std::endl
  154. << " \"directory\": \""
  155. << cmGlobalGenerator::EscapeJSON(workingDirectory) << "\","
  156. << std::endl
  157. << " \"command\": \"" <<
  158. cmGlobalGenerator::EscapeJSON(compileCommand) << "\","
  159. << std::endl
  160. << " \"file\": \"" <<
  161. cmGlobalGenerator::EscapeJSON(sourceFile) << "\""
  162. << std::endl << "}";
  163. }
  164. void cmGlobalUnixMakefileGenerator3::WriteMainMakefile2()
  165. {
  166. // Open the output file. This should not be copy-if-different
  167. // because the check-build-system step compares the makefile time to
  168. // see if the build system must be regenerated.
  169. std::string makefileName =
  170. this->GetCMakeInstance()->GetHomeOutputDirectory();
  171. makefileName += cmake::GetCMakeFilesDirectory();
  172. makefileName += "/Makefile2";
  173. cmGeneratedFileStream makefileStream(makefileName.c_str());
  174. if(!makefileStream)
  175. {
  176. return;
  177. }
  178. // get a local generator for some useful methods
  179. cmLocalUnixMakefileGenerator3 *lg =
  180. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[0]);
  181. // Write the do not edit header.
  182. lg->WriteDisclaimer(makefileStream);
  183. // Write the main entry point target. This must be the VERY first
  184. // target so that make with no arguments will run it.
  185. // Just depend on the all target to drive the build.
  186. std::vector<std::string> depends;
  187. std::vector<std::string> no_commands;
  188. depends.push_back("all");
  189. // Write the rule.
  190. lg->WriteMakeRule(makefileStream,
  191. "Default target executed when no arguments are "
  192. "given to make.",
  193. "default_target",
  194. depends,
  195. no_commands, true);
  196. depends.clear();
  197. // The all and preinstall rules might never have any dependencies
  198. // added to them.
  199. if(this->EmptyRuleHackDepends != "")
  200. {
  201. depends.push_back(this->EmptyRuleHackDepends);
  202. }
  203. // Write and empty all:
  204. lg->WriteMakeRule(makefileStream,
  205. "The main recursive all target", "all",
  206. depends, no_commands, true);
  207. // Write an empty preinstall:
  208. lg->WriteMakeRule(makefileStream,
  209. "The main recursive preinstall target", "preinstall",
  210. depends, no_commands, true);
  211. // Write out the "special" stuff
  212. lg->WriteSpecialTargetsTop(makefileStream);
  213. // write the target convenience rules
  214. unsigned int i;
  215. for (i = 0; i < this->LocalGenerators.size(); ++i)
  216. {
  217. lg =
  218. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  219. this->WriteConvenienceRules2(makefileStream,lg);
  220. }
  221. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[0]);
  222. lg->WriteSpecialTargetsBottom(makefileStream);
  223. }
  224. //----------------------------------------------------------------------------
  225. void cmGlobalUnixMakefileGenerator3::WriteMainCMakefile()
  226. {
  227. // Open the output file. This should not be copy-if-different
  228. // because the check-build-system step compares the makefile time to
  229. // see if the build system must be regenerated.
  230. std::string cmakefileName =
  231. this->GetCMakeInstance()->GetHomeOutputDirectory();
  232. cmakefileName += cmake::GetCMakeFilesDirectory();
  233. cmakefileName += "/Makefile.cmake";
  234. cmGeneratedFileStream cmakefileStream(cmakefileName.c_str());
  235. if(!cmakefileStream)
  236. {
  237. return;
  238. }
  239. std::string makefileName =
  240. this->GetCMakeInstance()->GetHomeOutputDirectory();
  241. makefileName += "/Makefile";
  242. // get a local generator for some useful methods
  243. cmLocalUnixMakefileGenerator3 *lg =
  244. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[0]);
  245. // Write the do not edit header.
  246. lg->WriteDisclaimer(cmakefileStream);
  247. // Save the generator name
  248. cmakefileStream
  249. << "# The generator used is:\n"
  250. << "SET(CMAKE_DEPENDS_GENERATOR \"" << this->GetName() << "\")\n\n";
  251. // for each cmMakefile get its list of dependencies
  252. std::vector<std::string> lfiles;
  253. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  254. {
  255. lg =
  256. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  257. // Get the list of files contributing to this generation step.
  258. lfiles.insert(lfiles.end(),lg->GetMakefile()->GetListFiles().begin(),
  259. lg->GetMakefile()->GetListFiles().end());
  260. }
  261. // Sort the list and remove duplicates.
  262. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  263. #if !defined(__VMS) // The Compaq STL on VMS crashes, so accept duplicates.
  264. std::vector<std::string>::iterator new_end =
  265. std::unique(lfiles.begin(),lfiles.end());
  266. lfiles.erase(new_end, lfiles.end());
  267. #endif
  268. // reset lg to the first makefile
  269. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[0]);
  270. // Build the path to the cache file.
  271. std::string cache = this->GetCMakeInstance()->GetHomeOutputDirectory();
  272. cache += "/CMakeCache.txt";
  273. // Save the list to the cmake file.
  274. cmakefileStream
  275. << "# The top level Makefile was generated from the following files:\n"
  276. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  277. << " \""
  278. << lg->Convert(cache.c_str(),
  279. cmLocalGenerator::START_OUTPUT).c_str() << "\"\n";
  280. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  281. i != lfiles.end(); ++i)
  282. {
  283. cmakefileStream
  284. << " \""
  285. << lg->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT).c_str()
  286. << "\"\n";
  287. }
  288. cmakefileStream
  289. << " )\n\n";
  290. // Build the path to the cache check file.
  291. std::string check = this->GetCMakeInstance()->GetHomeOutputDirectory();
  292. check += cmake::GetCMakeFilesDirectory();
  293. check += "/cmake.check_cache";
  294. // Set the corresponding makefile in the cmake file.
  295. cmakefileStream
  296. << "# The corresponding makefile is:\n"
  297. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  298. << " \""
  299. << lg->Convert(makefileName.c_str(),
  300. cmLocalGenerator::START_OUTPUT).c_str() << "\"\n"
  301. << " \""
  302. << lg->Convert(check.c_str(),
  303. cmLocalGenerator::START_OUTPUT).c_str() << "\"\n";
  304. cmakefileStream << " )\n\n";
  305. // CMake must rerun if a byproduct is missing.
  306. {
  307. cmakefileStream
  308. << "# Byproducts of CMake generate step:\n"
  309. << "SET(CMAKE_MAKEFILE_PRODUCTS\n";
  310. const std::vector<std::string>& outfiles =
  311. lg->GetMakefile()->GetOutputFiles();
  312. for(std::vector<std::string>::const_iterator k = outfiles.begin();
  313. k != outfiles.end(); ++k)
  314. {
  315. cmakefileStream << " \"" <<
  316. lg->Convert(k->c_str(),cmLocalGenerator::HOME_OUTPUT).c_str()
  317. << "\"\n";
  318. }
  319. // add in all the directory information files
  320. std::string tmpStr;
  321. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  322. {
  323. lg =
  324. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  325. tmpStr = lg->GetMakefile()->GetStartOutputDirectory();
  326. tmpStr += cmake::GetCMakeFilesDirectory();
  327. tmpStr += "/CMakeDirectoryInformation.cmake";
  328. cmakefileStream << " \"" <<
  329. lg->Convert(tmpStr.c_str(),cmLocalGenerator::HOME_OUTPUT).c_str()
  330. << "\"\n";
  331. }
  332. cmakefileStream << " )\n\n";
  333. }
  334. this->WriteMainCMakefileLanguageRules(cmakefileStream,
  335. this->LocalGenerators);
  336. }
  337. void cmGlobalUnixMakefileGenerator3
  338. ::WriteMainCMakefileLanguageRules(cmGeneratedFileStream& cmakefileStream,
  339. std::vector<cmLocalGenerator *> &lGenerators
  340. )
  341. {
  342. cmLocalUnixMakefileGenerator3 *lg;
  343. // now list all the target info files
  344. cmakefileStream
  345. << "# Dependency information for all targets:\n";
  346. cmakefileStream
  347. << "SET(CMAKE_DEPEND_INFO_FILES\n";
  348. for (unsigned int i = 0; i < lGenerators.size(); ++i)
  349. {
  350. lg = static_cast<cmLocalUnixMakefileGenerator3 *>(lGenerators[i]);
  351. // for all of out targets
  352. for (cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  353. l != lg->GetMakefile()->GetTargets().end(); l++)
  354. {
  355. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  356. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  357. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  358. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  359. (l->second.GetType() == cmTarget::OBJECT_LIBRARY) ||
  360. (l->second.GetType() == cmTarget::UTILITY))
  361. {
  362. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  363. tname += "/DependInfo.cmake";
  364. cmSystemTools::ConvertToUnixSlashes(tname);
  365. cmakefileStream << " \"" << tname.c_str() << "\"\n";
  366. }
  367. }
  368. }
  369. cmakefileStream << " )\n";
  370. }
  371. //----------------------------------------------------------------------------
  372. void
  373. cmGlobalUnixMakefileGenerator3
  374. ::WriteDirectoryRule2(std::ostream& ruleFileStream,
  375. cmLocalUnixMakefileGenerator3* lg,
  376. const char* pass, bool check_all,
  377. bool check_relink)
  378. {
  379. // Get the relative path to the subdirectory from the top.
  380. std::string makeTarget = lg->GetMakefile()->GetStartOutputDirectory();
  381. makeTarget += "/";
  382. makeTarget += pass;
  383. // The directory-level rule should depend on the target-level rules
  384. // for all targets in the directory.
  385. std::vector<std::string> depends;
  386. for(cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  387. l != lg->GetMakefile()->GetTargets().end(); ++l)
  388. {
  389. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  390. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  391. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  392. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  393. (l->second.GetType() == cmTarget::OBJECT_LIBRARY) ||
  394. (l->second.GetType() == cmTarget::UTILITY))
  395. {
  396. // Add this to the list of depends rules in this directory.
  397. if((!check_all || !l->second.GetPropertyAsBool("EXCLUDE_FROM_ALL")) &&
  398. (!check_relink ||
  399. l->second.NeedRelinkBeforeInstall(lg->ConfigurationName.c_str())))
  400. {
  401. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  402. tname += "/";
  403. tname += pass;
  404. depends.push_back(tname);
  405. }
  406. }
  407. }
  408. // The directory-level rule should depend on the directory-level
  409. // rules of the subdirectories.
  410. for(std::vector<cmLocalGenerator*>::iterator sdi =
  411. lg->GetChildren().begin(); sdi != lg->GetChildren().end(); ++sdi)
  412. {
  413. cmLocalUnixMakefileGenerator3* slg =
  414. static_cast<cmLocalUnixMakefileGenerator3*>(*sdi);
  415. std::string subdir = slg->GetMakefile()->GetStartOutputDirectory();
  416. subdir += "/";
  417. subdir += pass;
  418. depends.push_back(subdir);
  419. }
  420. // Work-around for makes that drop rules that have no dependencies
  421. // or commands.
  422. if(depends.empty() && this->EmptyRuleHackDepends != "")
  423. {
  424. depends.push_back(this->EmptyRuleHackDepends);
  425. }
  426. // Write the rule.
  427. std::string doc = "Convenience name for \"";
  428. doc += pass;
  429. doc += "\" pass in the directory.";
  430. std::vector<std::string> no_commands;
  431. lg->WriteMakeRule(ruleFileStream, doc.c_str(),
  432. makeTarget.c_str(), depends, no_commands, true);
  433. }
  434. //----------------------------------------------------------------------------
  435. void
  436. cmGlobalUnixMakefileGenerator3
  437. ::WriteDirectoryRules2(std::ostream& ruleFileStream,
  438. cmLocalUnixMakefileGenerator3* lg)
  439. {
  440. // Only subdirectories need these rules.
  441. if(!lg->GetParent())
  442. {
  443. return;
  444. }
  445. // Begin the directory-level rules section.
  446. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  447. dir = lg->Convert(dir.c_str(), cmLocalGenerator::HOME_OUTPUT,
  448. cmLocalGenerator::MAKEFILE);
  449. lg->WriteDivider(ruleFileStream);
  450. ruleFileStream
  451. << "# Directory level rules for directory "
  452. << dir << "\n\n";
  453. // Write directory-level rules for "all".
  454. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  455. // Write directory-level rules for "clean".
  456. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false);
  457. // Write directory-level rules for "preinstall".
  458. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  459. }
  460. std::string cmGlobalUnixMakefileGenerator3
  461. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  462. const char* additionalOptions, const char *targetName,
  463. const char* config, bool ignoreErrors, bool fast)
  464. {
  465. // Project name and config are not used yet.
  466. (void)projectName;
  467. (void)config;
  468. std::string makeCommand =
  469. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  470. // Since we have full control over the invocation of nmake, let us
  471. // make it quiet.
  472. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  473. {
  474. makeCommand += " /NOLOGO ";
  475. }
  476. if ( ignoreErrors )
  477. {
  478. makeCommand += " -i";
  479. }
  480. if ( additionalOptions )
  481. {
  482. makeCommand += " ";
  483. makeCommand += additionalOptions;
  484. }
  485. if ( targetName && strlen(targetName))
  486. {
  487. cmLocalUnixMakefileGenerator3 *lg;
  488. if (this->LocalGenerators.size())
  489. {
  490. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  491. (this->LocalGenerators[0]);
  492. }
  493. else
  494. {
  495. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  496. (this->CreateLocalGenerator());
  497. // set the Start directories
  498. lg->GetMakefile()->SetStartDirectory
  499. (this->CMakeInstance->GetStartDirectory());
  500. lg->GetMakefile()->SetStartOutputDirectory
  501. (this->CMakeInstance->GetStartOutputDirectory());
  502. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  503. }
  504. makeCommand += " \"";
  505. std::string tname = targetName;
  506. if(fast)
  507. {
  508. tname += "/fast";
  509. }
  510. tname = lg->Convert(tname.c_str(),cmLocalGenerator::HOME_OUTPUT,
  511. cmLocalGenerator::MAKEFILE);
  512. makeCommand += tname.c_str();
  513. makeCommand += "\"";
  514. if (!this->LocalGenerators.size())
  515. {
  516. delete lg;
  517. }
  518. }
  519. return makeCommand;
  520. }
  521. //----------------------------------------------------------------------------
  522. void
  523. cmGlobalUnixMakefileGenerator3
  524. ::WriteConvenienceRules(std::ostream& ruleFileStream,
  525. std::set<cmStdString> &emitted)
  526. {
  527. std::vector<std::string> depends;
  528. std::vector<std::string> commands;
  529. depends.push_back("cmake_check_build_system");
  530. // write the target convenience rules
  531. unsigned int i;
  532. cmLocalUnixMakefileGenerator3 *lg;
  533. for (i = 0; i < this->LocalGenerators.size(); ++i)
  534. {
  535. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  536. (this->LocalGenerators[i]);
  537. // for each target Generate the rule files for each target.
  538. cmTargets& targets = lg->GetMakefile()->GetTargets();
  539. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  540. {
  541. // Don't emit the same rule twice (e.g. two targets with the same
  542. // simple name)
  543. if(t->second.GetName() &&
  544. strlen(t->second.GetName()) &&
  545. emitted.insert(t->second.GetName()).second &&
  546. // Handle user targets here. Global targets are handled in
  547. // the local generator on a per-directory basis.
  548. ((t->second.GetType() == cmTarget::EXECUTABLE) ||
  549. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  550. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  551. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  552. (t->second.GetType() == cmTarget::OBJECT_LIBRARY) ||
  553. (t->second.GetType() == cmTarget::UTILITY)))
  554. {
  555. // Add a rule to build the target by name.
  556. lg->WriteDivider(ruleFileStream);
  557. ruleFileStream
  558. << "# Target rules for targets named "
  559. << t->second.GetName() << "\n\n";
  560. // Write the rule.
  561. commands.clear();
  562. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  563. tmp += "Makefile2";
  564. commands.push_back(lg->GetRecursiveMakeCall
  565. (tmp.c_str(),t->second.GetName()));
  566. depends.clear();
  567. depends.push_back("cmake_check_build_system");
  568. lg->WriteMakeRule(ruleFileStream,
  569. "Build rule for target.",
  570. t->second.GetName(), depends, commands,
  571. true);
  572. // Add a fast rule to build the target
  573. std::string localName = lg->GetRelativeTargetDirectory(t->second);
  574. std::string makefileName;
  575. makefileName = localName;
  576. makefileName += "/build.make";
  577. depends.clear();
  578. commands.clear();
  579. std::string makeTargetName = localName;
  580. makeTargetName += "/build";
  581. localName = t->second.GetName();
  582. localName += "/fast";
  583. commands.push_back(lg->GetRecursiveMakeCall
  584. (makefileName.c_str(), makeTargetName.c_str()));
  585. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  586. localName.c_str(), depends, commands, true);
  587. // Add a local name for the rule to relink the target before
  588. // installation.
  589. if(t->second.NeedRelinkBeforeInstall(lg->ConfigurationName.c_str()))
  590. {
  591. makeTargetName = lg->GetRelativeTargetDirectory(t->second);
  592. makeTargetName += "/preinstall";
  593. localName = t->second.GetName();
  594. localName += "/preinstall";
  595. depends.clear();
  596. commands.clear();
  597. commands.push_back(lg->GetRecursiveMakeCall
  598. (makefileName.c_str(), makeTargetName.c_str()));
  599. lg->WriteMakeRule(ruleFileStream,
  600. "Manual pre-install relink rule for target.",
  601. localName.c_str(), depends, commands, true);
  602. }
  603. }
  604. }
  605. }
  606. }
  607. //----------------------------------------------------------------------------
  608. void
  609. cmGlobalUnixMakefileGenerator3
  610. ::WriteConvenienceRules2(std::ostream& ruleFileStream,
  611. cmLocalUnixMakefileGenerator3 *lg)
  612. {
  613. std::vector<std::string> depends;
  614. std::vector<std::string> commands;
  615. std::string localName;
  616. std::string makeTargetName;
  617. // write the directory level rules for this local gen
  618. this->WriteDirectoryRules2(ruleFileStream,lg);
  619. depends.push_back("cmake_check_build_system");
  620. // for each target Generate the rule files for each target.
  621. cmTargets& targets = lg->GetMakefile()->GetTargets();
  622. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  623. {
  624. if (t->second.GetName()
  625. && strlen(t->second.GetName())
  626. && ((t->second.GetType() == cmTarget::EXECUTABLE)
  627. || (t->second.GetType() == cmTarget::STATIC_LIBRARY)
  628. || (t->second.GetType() == cmTarget::SHARED_LIBRARY)
  629. || (t->second.GetType() == cmTarget::MODULE_LIBRARY)
  630. || (t->second.GetType() == cmTarget::OBJECT_LIBRARY)
  631. || (t->second.GetType() == cmTarget::UTILITY)))
  632. {
  633. std::string makefileName;
  634. // Add a rule to build the target by name.
  635. localName = lg->GetRelativeTargetDirectory(t->second);
  636. makefileName = localName;
  637. makefileName += "/build.make";
  638. bool needRequiresStep = this->NeedRequiresStep(t->second);
  639. lg->WriteDivider(ruleFileStream);
  640. ruleFileStream
  641. << "# Target rules for target "
  642. << localName << "\n\n";
  643. commands.clear();
  644. makeTargetName = localName;
  645. makeTargetName += "/depend";
  646. commands.push_back(lg->GetRecursiveMakeCall
  647. (makefileName.c_str(),makeTargetName.c_str()));
  648. // add requires if we need it for this generator
  649. if (needRequiresStep)
  650. {
  651. makeTargetName = localName;
  652. makeTargetName += "/requires";
  653. commands.push_back(lg->GetRecursiveMakeCall
  654. (makefileName.c_str(),makeTargetName.c_str()));
  655. }
  656. makeTargetName = localName;
  657. makeTargetName += "/build";
  658. commands.push_back(lg->GetRecursiveMakeCall
  659. (makefileName.c_str(),makeTargetName.c_str()));
  660. // Write the rule.
  661. localName += "/all";
  662. depends.clear();
  663. std::string progressDir =
  664. lg->GetMakefile()->GetHomeOutputDirectory();
  665. progressDir += cmake::GetCMakeFilesDirectory();
  666. {
  667. cmOStringStream progCmd;
  668. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  669. // all target counts
  670. progCmd << lg->Convert(progressDir.c_str(),
  671. cmLocalGenerator::FULL,
  672. cmLocalGenerator::SHELL);
  673. progCmd << " ";
  674. std::vector<unsigned long>& progFiles =
  675. this->ProgressMap[&t->second].Marks;
  676. for (std::vector<unsigned long>::iterator i = progFiles.begin();
  677. i != progFiles.end(); ++i)
  678. {
  679. progCmd << " " << *i;
  680. }
  681. commands.push_back(progCmd.str());
  682. }
  683. progressDir = "Built target ";
  684. progressDir += t->first;
  685. lg->AppendEcho(commands,progressDir.c_str());
  686. this->AppendGlobalTargetDepends(depends,t->second);
  687. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  688. localName.c_str(), depends, commands, true);
  689. // add the all/all dependency
  690. if(!this->IsExcluded(this->LocalGenerators[0], t->second))
  691. {
  692. depends.clear();
  693. depends.push_back(localName);
  694. commands.clear();
  695. lg->WriteMakeRule(ruleFileStream, "Include target in all.",
  696. "all", depends, commands, true);
  697. }
  698. // Write the rule.
  699. commands.clear();
  700. progressDir = lg->GetMakefile()->GetHomeOutputDirectory();
  701. progressDir += cmake::GetCMakeFilesDirectory();
  702. {
  703. // TODO: Convert the total progress count to a make variable.
  704. cmOStringStream progCmd;
  705. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  706. // # in target
  707. progCmd << lg->Convert(progressDir.c_str(),
  708. cmLocalGenerator::FULL,
  709. cmLocalGenerator::SHELL);
  710. //
  711. std::set<cmTarget *> emitted;
  712. progCmd << " "
  713. << this->CountProgressMarksInTarget(&t->second, emitted);
  714. commands.push_back(progCmd.str());
  715. }
  716. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  717. tmp += "Makefile2";
  718. commands.push_back(lg->GetRecursiveMakeCall
  719. (tmp.c_str(),localName.c_str()));
  720. {
  721. cmOStringStream progCmd;
  722. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  723. progCmd << lg->Convert(progressDir.c_str(),
  724. cmLocalGenerator::FULL,
  725. cmLocalGenerator::SHELL);
  726. progCmd << " 0";
  727. commands.push_back(progCmd.str());
  728. }
  729. depends.clear();
  730. depends.push_back("cmake_check_build_system");
  731. localName = lg->GetRelativeTargetDirectory(t->second);
  732. localName += "/rule";
  733. lg->WriteMakeRule(ruleFileStream,
  734. "Build rule for subdir invocation for target.",
  735. localName.c_str(), depends, commands, true);
  736. // Add a target with the canonical name (no prefix, suffix or path).
  737. commands.clear();
  738. depends.clear();
  739. depends.push_back(localName);
  740. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  741. t->second.GetName(), depends, commands, true);
  742. // Add rules to prepare the target for installation.
  743. if(t->second.NeedRelinkBeforeInstall(lg->ConfigurationName.c_str()))
  744. {
  745. localName = lg->GetRelativeTargetDirectory(t->second);
  746. localName += "/preinstall";
  747. depends.clear();
  748. commands.clear();
  749. commands.push_back(lg->GetRecursiveMakeCall
  750. (makefileName.c_str(), localName.c_str()));
  751. lg->WriteMakeRule(ruleFileStream,
  752. "Pre-install relink rule for target.",
  753. localName.c_str(), depends, commands, true);
  754. if(!this->IsExcluded(this->LocalGenerators[0], t->second))
  755. {
  756. depends.clear();
  757. depends.push_back(localName);
  758. commands.clear();
  759. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  760. "preinstall", depends, commands, true);
  761. }
  762. }
  763. // add the clean rule
  764. localName = lg->GetRelativeTargetDirectory(t->second);
  765. makeTargetName = localName;
  766. makeTargetName += "/clean";
  767. depends.clear();
  768. commands.clear();
  769. commands.push_back(lg->GetRecursiveMakeCall
  770. (makefileName.c_str(), makeTargetName.c_str()));
  771. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  772. makeTargetName.c_str(), depends, commands, true);
  773. commands.clear();
  774. depends.push_back(makeTargetName);
  775. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  776. "clean", depends, commands, true);
  777. }
  778. }
  779. }
  780. //----------------------------------------------------------------------------
  781. size_t
  782. cmGlobalUnixMakefileGenerator3
  783. ::CountProgressMarksInTarget(cmTarget* target,
  784. std::set<cmTarget*>& emitted)
  785. {
  786. size_t count = 0;
  787. if(emitted.insert(target).second)
  788. {
  789. count = this->ProgressMap[target].Marks.size();
  790. TargetDependSet const& depends = this->GetTargetDirectDepends(*target);
  791. for(TargetDependSet::const_iterator di = depends.begin();
  792. di != depends.end(); ++di)
  793. {
  794. count += this->CountProgressMarksInTarget(*di, emitted);
  795. }
  796. }
  797. return count;
  798. }
  799. //----------------------------------------------------------------------------
  800. size_t
  801. cmGlobalUnixMakefileGenerator3
  802. ::CountProgressMarksInAll(cmLocalUnixMakefileGenerator3* lg)
  803. {
  804. size_t count = 0;
  805. std::set<cmTarget*> emitted;
  806. std::set<cmTarget*> const& targets = this->LocalGeneratorToTargetMap[lg];
  807. for(std::set<cmTarget*>::const_iterator t = targets.begin();
  808. t != targets.end(); ++t)
  809. {
  810. count += this->CountProgressMarksInTarget(*t, emitted);
  811. }
  812. return count;
  813. }
  814. //----------------------------------------------------------------------------
  815. void
  816. cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  817. cmMakefileTargetGenerator* tg)
  818. {
  819. TargetProgress& tp = this->ProgressMap[tg->GetTarget()];
  820. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  821. tp.VariableFile = tg->GetProgressFileNameFull();
  822. }
  823. //----------------------------------------------------------------------------
  824. bool
  825. cmGlobalUnixMakefileGenerator3::ProgressMapCompare
  826. ::operator()(cmTarget* l, cmTarget* r) const
  827. {
  828. // Order by target name.
  829. if(int c = strcmp(l->GetName(), r->GetName()))
  830. {
  831. return c < 0;
  832. }
  833. // Order duplicate targets by binary directory.
  834. return strcmp(l->GetMakefile()->GetCurrentOutputDirectory(),
  835. r->GetMakefile()->GetCurrentOutputDirectory()) < 0;
  836. }
  837. //----------------------------------------------------------------------------
  838. void
  839. cmGlobalUnixMakefileGenerator3::TargetProgress
  840. ::WriteProgressVariables(unsigned long total, unsigned long &current)
  841. {
  842. cmGeneratedFileStream fout(this->VariableFile.c_str());
  843. for(unsigned long i = 1; i <= this->NumberOfActions; ++i)
  844. {
  845. fout << "CMAKE_PROGRESS_" << i << " = ";
  846. if (total <= 100)
  847. {
  848. unsigned long num = i + current;
  849. fout << num;
  850. this->Marks.push_back(num);
  851. }
  852. else if (((i+current)*100)/total > ((i-1+current)*100)/total)
  853. {
  854. unsigned long num = ((i+current)*100)/total;
  855. fout << num;
  856. this->Marks.push_back(num);
  857. }
  858. fout << "\n";
  859. }
  860. fout << "\n";
  861. current += this->NumberOfActions;
  862. }
  863. //----------------------------------------------------------------------------
  864. void
  865. cmGlobalUnixMakefileGenerator3
  866. ::AppendGlobalTargetDepends(std::vector<std::string>& depends,
  867. cmTarget& target)
  868. {
  869. TargetDependSet const& depends_set = this->GetTargetDirectDepends(target);
  870. for(TargetDependSet::const_iterator i = depends_set.begin();
  871. i != depends_set.end(); ++i)
  872. {
  873. // Create the target-level dependency.
  874. cmTarget const* dep = *i;
  875. cmLocalUnixMakefileGenerator3* lg3 =
  876. static_cast<cmLocalUnixMakefileGenerator3*>
  877. (dep->GetMakefile()->GetLocalGenerator());
  878. std::string tgtName = lg3->GetRelativeTargetDirectory(*dep);
  879. tgtName += "/all";
  880. depends.push_back(tgtName);
  881. }
  882. }
  883. //----------------------------------------------------------------------------
  884. void cmGlobalUnixMakefileGenerator3::WriteHelpRule
  885. (std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3 *lg)
  886. {
  887. // add the help target
  888. std::string path;
  889. std::vector<std::string> no_depends;
  890. std::vector<std::string> commands;
  891. lg->AppendEcho(commands,"The following are some of the valid targets "
  892. "for this Makefile:");
  893. lg->AppendEcho(commands,"... all (the default if no target is provided)");
  894. lg->AppendEcho(commands,"... clean");
  895. lg->AppendEcho(commands,"... depend");
  896. // Keep track of targets already listed.
  897. std::set<cmStdString> emittedTargets;
  898. // for each local generator
  899. unsigned int i;
  900. cmLocalUnixMakefileGenerator3 *lg2;
  901. for (i = 0; i < this->LocalGenerators.size(); ++i)
  902. {
  903. lg2 =
  904. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  905. // for the passed in makefile or if this is the top Makefile wripte out
  906. // the targets
  907. if (lg2 == lg || !lg->GetParent())
  908. {
  909. // for each target Generate the rule files for each target.
  910. cmTargets& targets = lg2->GetMakefile()->GetTargets();
  911. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  912. {
  913. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  914. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  915. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  916. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  917. (t->second.GetType() == cmTarget::OBJECT_LIBRARY) ||
  918. (t->second.GetType() == cmTarget::GLOBAL_TARGET) ||
  919. (t->second.GetType() == cmTarget::UTILITY))
  920. {
  921. if(emittedTargets.insert(t->second.GetName()).second)
  922. {
  923. path = "... ";
  924. path += t->second.GetName();
  925. lg->AppendEcho(commands,path.c_str());
  926. }
  927. }
  928. }
  929. }
  930. }
  931. std::vector<cmStdString> const& localHelp = lg->GetLocalHelp();
  932. for(std::vector<cmStdString>::const_iterator o = localHelp.begin();
  933. o != localHelp.end(); ++o)
  934. {
  935. path = "... ";
  936. path += *o;
  937. lg->AppendEcho(commands, path.c_str());
  938. }
  939. lg->WriteMakeRule(ruleFileStream, "Help Target",
  940. "help",
  941. no_depends, commands, true);
  942. ruleFileStream << "\n\n";
  943. }
  944. bool cmGlobalUnixMakefileGenerator3
  945. ::NeedRequiresStep(cmTarget const& target)
  946. {
  947. std::set<cmStdString> languages;
  948. target.GetLanguages(languages);
  949. for(std::set<cmStdString>::const_iterator l = languages.begin();
  950. l != languages.end(); ++l)
  951. {
  952. std::string var = "CMAKE_NEEDS_REQUIRES_STEP_";
  953. var += *l;
  954. var += "_FLAG";
  955. if(target.GetMakefile()->GetDefinition(var.c_str()))
  956. {
  957. return true;
  958. }
  959. }
  960. return false;
  961. }