cmGlobalUnixMakefileGenerator3.cxx 37 KB

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