cmGlobalUnixMakefileGenerator3.cxx 37 KB

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