cmGlobalUnixMakefileGenerator3.cxx 37 KB

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