cmGlobalUnixMakefileGenerator3.cxx 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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::UTILITY))
  368. {
  369. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  370. tname += "/DependInfo.cmake";
  371. cmSystemTools::ConvertToUnixSlashes(tname);
  372. cmakefileStream << " \"" << tname.c_str() << "\"\n";
  373. }
  374. }
  375. }
  376. cmakefileStream << " )\n";
  377. }
  378. //----------------------------------------------------------------------------
  379. void
  380. cmGlobalUnixMakefileGenerator3
  381. ::WriteDirectoryRule2(std::ostream& ruleFileStream,
  382. cmLocalUnixMakefileGenerator3* lg,
  383. const char* pass, bool check_all,
  384. bool check_relink)
  385. {
  386. // Get the relative path to the subdirectory from the top.
  387. std::string makeTarget = lg->GetMakefile()->GetStartOutputDirectory();
  388. makeTarget += "/";
  389. makeTarget += pass;
  390. // The directory-level rule should depend on the target-level rules
  391. // for all targets in the directory.
  392. std::vector<std::string> depends;
  393. for(cmTargets::iterator l = lg->GetMakefile()->GetTargets().begin();
  394. l != lg->GetMakefile()->GetTargets().end(); ++l)
  395. {
  396. if((l->second.GetType() == cmTarget::EXECUTABLE) ||
  397. (l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  398. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  399. (l->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  400. (l->second.GetType() == cmTarget::UTILITY))
  401. {
  402. // Add this to the list of depends rules in this directory.
  403. if((!check_all || !l->second.GetPropertyAsBool("EXCLUDE_FROM_ALL")) &&
  404. (!check_relink ||
  405. l->second.NeedRelinkBeforeInstall(lg->ConfigurationName.c_str())))
  406. {
  407. std::string tname = lg->GetRelativeTargetDirectory(l->second);
  408. tname += "/";
  409. tname += pass;
  410. depends.push_back(tname);
  411. }
  412. }
  413. }
  414. // The directory-level rule should depend on the directory-level
  415. // rules of the subdirectories.
  416. for(std::vector<cmLocalGenerator*>::iterator sdi =
  417. lg->GetChildren().begin(); sdi != lg->GetChildren().end(); ++sdi)
  418. {
  419. cmLocalUnixMakefileGenerator3* slg =
  420. static_cast<cmLocalUnixMakefileGenerator3*>(*sdi);
  421. std::string subdir = slg->GetMakefile()->GetStartOutputDirectory();
  422. subdir += "/";
  423. subdir += pass;
  424. depends.push_back(subdir);
  425. }
  426. // Work-around for makes that drop rules that have no dependencies
  427. // or commands.
  428. if(depends.empty() && this->EmptyRuleHackDepends != "")
  429. {
  430. depends.push_back(this->EmptyRuleHackDepends);
  431. }
  432. // Write the rule.
  433. std::string doc = "Convenience name for \"";
  434. doc += pass;
  435. doc += "\" pass in the directory.";
  436. std::vector<std::string> no_commands;
  437. lg->WriteMakeRule(ruleFileStream, doc.c_str(),
  438. makeTarget.c_str(), depends, no_commands, true);
  439. }
  440. //----------------------------------------------------------------------------
  441. void
  442. cmGlobalUnixMakefileGenerator3
  443. ::WriteDirectoryRules2(std::ostream& ruleFileStream,
  444. cmLocalUnixMakefileGenerator3* lg)
  445. {
  446. // Only subdirectories need these rules.
  447. if(!lg->GetParent())
  448. {
  449. return;
  450. }
  451. // Begin the directory-level rules section.
  452. std::string dir = lg->GetMakefile()->GetStartOutputDirectory();
  453. dir = lg->Convert(dir.c_str(), cmLocalGenerator::HOME_OUTPUT,
  454. cmLocalGenerator::MAKEFILE);
  455. lg->WriteDivider(ruleFileStream);
  456. ruleFileStream
  457. << "# Directory level rules for directory "
  458. << dir << "\n\n";
  459. // Write directory-level rules for "all".
  460. this->WriteDirectoryRule2(ruleFileStream, lg, "all", true, false);
  461. // Write directory-level rules for "clean".
  462. this->WriteDirectoryRule2(ruleFileStream, lg, "clean", false, false);
  463. // Write directory-level rules for "preinstall".
  464. this->WriteDirectoryRule2(ruleFileStream, lg, "preinstall", true, true);
  465. }
  466. std::string cmGlobalUnixMakefileGenerator3
  467. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  468. const char* additionalOptions, const char *targetName,
  469. const char* config, bool ignoreErrors, bool fast)
  470. {
  471. // Project name and config are not used yet.
  472. (void)projectName;
  473. (void)config;
  474. std::string makeCommand =
  475. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  476. // Since we have full control over the invocation of nmake, let us
  477. // make it quiet.
  478. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  479. {
  480. makeCommand += " /NOLOGO ";
  481. }
  482. if ( ignoreErrors )
  483. {
  484. makeCommand += " -i";
  485. }
  486. if ( additionalOptions )
  487. {
  488. makeCommand += " ";
  489. makeCommand += additionalOptions;
  490. }
  491. if ( targetName && strlen(targetName))
  492. {
  493. cmLocalUnixMakefileGenerator3 *lg;
  494. if (this->LocalGenerators.size())
  495. {
  496. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  497. (this->LocalGenerators[0]);
  498. }
  499. else
  500. {
  501. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  502. (this->CreateLocalGenerator());
  503. // set the Start directories
  504. lg->GetMakefile()->SetStartDirectory
  505. (this->CMakeInstance->GetStartDirectory());
  506. lg->GetMakefile()->SetStartOutputDirectory
  507. (this->CMakeInstance->GetStartOutputDirectory());
  508. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  509. }
  510. makeCommand += " \"";
  511. std::string tname = targetName;
  512. if(fast)
  513. {
  514. tname += "/fast";
  515. }
  516. tname = lg->Convert(tname.c_str(),cmLocalGenerator::HOME_OUTPUT,
  517. cmLocalGenerator::MAKEFILE);
  518. makeCommand += tname.c_str();
  519. makeCommand += "\"";
  520. if (!this->LocalGenerators.size())
  521. {
  522. delete lg;
  523. }
  524. }
  525. return makeCommand;
  526. }
  527. //----------------------------------------------------------------------------
  528. void
  529. cmGlobalUnixMakefileGenerator3
  530. ::WriteConvenienceRules(std::ostream& ruleFileStream,
  531. std::set<cmStdString> &emitted)
  532. {
  533. std::vector<std::string> depends;
  534. std::vector<std::string> commands;
  535. depends.push_back("cmake_check_build_system");
  536. // write the target convenience rules
  537. unsigned int i;
  538. cmLocalUnixMakefileGenerator3 *lg;
  539. for (i = 0; i < this->LocalGenerators.size(); ++i)
  540. {
  541. lg = static_cast<cmLocalUnixMakefileGenerator3 *>
  542. (this->LocalGenerators[i]);
  543. // for each target Generate the rule files for each target.
  544. cmTargets& targets = lg->GetMakefile()->GetTargets();
  545. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  546. {
  547. // Don't emit the same rule twice (e.g. two targets with the same
  548. // simple name)
  549. if(t->second.GetName() &&
  550. strlen(t->second.GetName()) &&
  551. emitted.insert(t->second.GetName()).second &&
  552. // Handle user targets here. Global targets are handled in
  553. // the local generator on a per-directory basis.
  554. ((t->second.GetType() == cmTarget::EXECUTABLE) ||
  555. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  556. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  557. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  558. (t->second.GetType() == cmTarget::UTILITY)))
  559. {
  560. // Add a rule to build the target by name.
  561. lg->WriteDivider(ruleFileStream);
  562. ruleFileStream
  563. << "# Target rules for targets named "
  564. << t->second.GetName() << "\n\n";
  565. // Write the rule.
  566. commands.clear();
  567. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  568. tmp += "Makefile2";
  569. commands.push_back(lg->GetRecursiveMakeCall
  570. (tmp.c_str(),t->second.GetName()));
  571. depends.clear();
  572. depends.push_back("cmake_check_build_system");
  573. lg->WriteMakeRule(ruleFileStream,
  574. "Build rule for target.",
  575. t->second.GetName(), depends, commands,
  576. true);
  577. // Add a fast rule to build the target
  578. std::string localName = lg->GetRelativeTargetDirectory(t->second);
  579. std::string makefileName;
  580. makefileName = localName;
  581. makefileName += "/build.make";
  582. depends.clear();
  583. commands.clear();
  584. std::string makeTargetName = localName;
  585. makeTargetName += "/build";
  586. localName = t->second.GetName();
  587. localName += "/fast";
  588. commands.push_back(lg->GetRecursiveMakeCall
  589. (makefileName.c_str(), makeTargetName.c_str()));
  590. lg->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  591. localName.c_str(), depends, commands, true);
  592. // Add a local name for the rule to relink the target before
  593. // installation.
  594. if(t->second.NeedRelinkBeforeInstall(lg->ConfigurationName.c_str()))
  595. {
  596. makeTargetName = lg->GetRelativeTargetDirectory(t->second);
  597. makeTargetName += "/preinstall";
  598. localName = t->second.GetName();
  599. localName += "/preinstall";
  600. depends.clear();
  601. commands.clear();
  602. commands.push_back(lg->GetRecursiveMakeCall
  603. (makefileName.c_str(), makeTargetName.c_str()));
  604. lg->WriteMakeRule(ruleFileStream,
  605. "Manual pre-install relink rule for target.",
  606. localName.c_str(), depends, commands, true);
  607. }
  608. }
  609. }
  610. }
  611. }
  612. //----------------------------------------------------------------------------
  613. void
  614. cmGlobalUnixMakefileGenerator3
  615. ::WriteConvenienceRules2(std::ostream& ruleFileStream,
  616. cmLocalUnixMakefileGenerator3 *lg)
  617. {
  618. std::vector<std::string> depends;
  619. std::vector<std::string> commands;
  620. std::string localName;
  621. std::string makeTargetName;
  622. // write the directory level rules for this local gen
  623. this->WriteDirectoryRules2(ruleFileStream,lg);
  624. depends.push_back("cmake_check_build_system");
  625. // for each target Generate the rule files for each target.
  626. cmTargets& targets = lg->GetMakefile()->GetTargets();
  627. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  628. {
  629. if (t->second.GetName()
  630. && strlen(t->second.GetName())
  631. && ((t->second.GetType() == cmTarget::EXECUTABLE)
  632. || (t->second.GetType() == cmTarget::STATIC_LIBRARY)
  633. || (t->second.GetType() == cmTarget::SHARED_LIBRARY)
  634. || (t->second.GetType() == cmTarget::MODULE_LIBRARY)
  635. || (t->second.GetType() == cmTarget::UTILITY)))
  636. {
  637. std::string makefileName;
  638. // Add a rule to build the target by name.
  639. localName = lg->GetRelativeTargetDirectory(t->second);
  640. makefileName = localName;
  641. makefileName += "/build.make";
  642. bool needRequiresStep = this->NeedRequiresStep(t->second);
  643. lg->WriteDivider(ruleFileStream);
  644. ruleFileStream
  645. << "# Target rules for target "
  646. << localName << "\n\n";
  647. commands.clear();
  648. makeTargetName = localName;
  649. makeTargetName += "/depend";
  650. commands.push_back(lg->GetRecursiveMakeCall
  651. (makefileName.c_str(),makeTargetName.c_str()));
  652. // add requires if we need it for this generator
  653. if (needRequiresStep)
  654. {
  655. makeTargetName = localName;
  656. makeTargetName += "/requires";
  657. commands.push_back(lg->GetRecursiveMakeCall
  658. (makefileName.c_str(),makeTargetName.c_str()));
  659. }
  660. makeTargetName = localName;
  661. makeTargetName += "/build";
  662. commands.push_back(lg->GetRecursiveMakeCall
  663. (makefileName.c_str(),makeTargetName.c_str()));
  664. // Write the rule.
  665. localName += "/all";
  666. depends.clear();
  667. std::string progressDir =
  668. lg->GetMakefile()->GetHomeOutputDirectory();
  669. progressDir += cmake::GetCMakeFilesDirectory();
  670. {
  671. cmOStringStream progCmd;
  672. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  673. // all target counts
  674. progCmd << lg->Convert(progressDir.c_str(),
  675. cmLocalGenerator::FULL,
  676. cmLocalGenerator::SHELL);
  677. progCmd << " ";
  678. std::vector<unsigned long>& progFiles =
  679. this->ProgressMap[&t->second].Marks;
  680. for (std::vector<unsigned long>::iterator i = progFiles.begin();
  681. i != progFiles.end(); ++i)
  682. {
  683. progCmd << " " << *i;
  684. }
  685. commands.push_back(progCmd.str());
  686. }
  687. progressDir = "Built target ";
  688. progressDir += t->first;
  689. lg->AppendEcho(commands,progressDir.c_str());
  690. this->AppendGlobalTargetDepends(depends,t->second);
  691. lg->WriteMakeRule(ruleFileStream, "All Build rule for target.",
  692. localName.c_str(), depends, commands, true);
  693. // add the all/all dependency
  694. if(!this->IsExcluded(this->LocalGenerators[0], t->second))
  695. {
  696. depends.clear();
  697. depends.push_back(localName);
  698. commands.clear();
  699. lg->WriteMakeRule(ruleFileStream, "Include target in all.",
  700. "all", depends, commands, true);
  701. }
  702. // Write the rule.
  703. commands.clear();
  704. progressDir = lg->GetMakefile()->GetHomeOutputDirectory();
  705. progressDir += cmake::GetCMakeFilesDirectory();
  706. {
  707. // TODO: Convert the total progress count to a make variable.
  708. cmOStringStream progCmd;
  709. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start ";
  710. // # in target
  711. progCmd << lg->Convert(progressDir.c_str(),
  712. cmLocalGenerator::FULL,
  713. cmLocalGenerator::SHELL);
  714. //
  715. std::set<cmTarget *> emitted;
  716. progCmd << " "
  717. << this->CountProgressMarksInTarget(&t->second, emitted);
  718. commands.push_back(progCmd.str());
  719. }
  720. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  721. tmp += "Makefile2";
  722. commands.push_back(lg->GetRecursiveMakeCall
  723. (tmp.c_str(),localName.c_str()));
  724. {
  725. cmOStringStream progCmd;
  726. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  727. progCmd << lg->Convert(progressDir.c_str(),
  728. cmLocalGenerator::FULL,
  729. cmLocalGenerator::SHELL);
  730. progCmd << " 0";
  731. commands.push_back(progCmd.str());
  732. }
  733. depends.clear();
  734. depends.push_back("cmake_check_build_system");
  735. localName = lg->GetRelativeTargetDirectory(t->second);
  736. localName += "/rule";
  737. lg->WriteMakeRule(ruleFileStream,
  738. "Build rule for subdir invocation for target.",
  739. localName.c_str(), depends, commands, true);
  740. // Add a target with the canonical name (no prefix, suffix or path).
  741. commands.clear();
  742. depends.clear();
  743. depends.push_back(localName);
  744. lg->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  745. t->second.GetName(), depends, commands, true);
  746. // Add rules to prepare the target for installation.
  747. if(t->second.NeedRelinkBeforeInstall(lg->ConfigurationName.c_str()))
  748. {
  749. localName = lg->GetRelativeTargetDirectory(t->second);
  750. localName += "/preinstall";
  751. depends.clear();
  752. commands.clear();
  753. commands.push_back(lg->GetRecursiveMakeCall
  754. (makefileName.c_str(), localName.c_str()));
  755. lg->WriteMakeRule(ruleFileStream,
  756. "Pre-install relink rule for target.",
  757. localName.c_str(), depends, commands, true);
  758. if(!this->IsExcluded(this->LocalGenerators[0], t->second))
  759. {
  760. depends.clear();
  761. depends.push_back(localName);
  762. commands.clear();
  763. lg->WriteMakeRule(ruleFileStream, "Prepare target for install.",
  764. "preinstall", depends, commands, true);
  765. }
  766. }
  767. // add the clean rule
  768. localName = lg->GetRelativeTargetDirectory(t->second);
  769. makeTargetName = localName;
  770. makeTargetName += "/clean";
  771. depends.clear();
  772. commands.clear();
  773. commands.push_back(lg->GetRecursiveMakeCall
  774. (makefileName.c_str(), makeTargetName.c_str()));
  775. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  776. makeTargetName.c_str(), depends, commands, true);
  777. commands.clear();
  778. depends.push_back(makeTargetName);
  779. lg->WriteMakeRule(ruleFileStream, "clean rule for target.",
  780. "clean", depends, commands, true);
  781. }
  782. }
  783. }
  784. //----------------------------------------------------------------------------
  785. size_t
  786. cmGlobalUnixMakefileGenerator3
  787. ::CountProgressMarksInTarget(cmTarget* target,
  788. std::set<cmTarget*>& emitted)
  789. {
  790. size_t count = 0;
  791. if(emitted.insert(target).second)
  792. {
  793. count = this->ProgressMap[target].Marks.size();
  794. TargetDependSet const& depends = this->GetTargetDirectDepends(*target);
  795. for(TargetDependSet::const_iterator di = depends.begin();
  796. di != depends.end(); ++di)
  797. {
  798. count += this->CountProgressMarksInTarget(*di, emitted);
  799. }
  800. }
  801. return count;
  802. }
  803. //----------------------------------------------------------------------------
  804. size_t
  805. cmGlobalUnixMakefileGenerator3
  806. ::CountProgressMarksInAll(cmLocalUnixMakefileGenerator3* lg)
  807. {
  808. size_t count = 0;
  809. std::set<cmTarget*> emitted;
  810. std::set<cmTarget*> const& targets = this->LocalGeneratorToTargetMap[lg];
  811. for(std::set<cmTarget*>::const_iterator t = targets.begin();
  812. t != targets.end(); ++t)
  813. {
  814. count += this->CountProgressMarksInTarget(*t, emitted);
  815. }
  816. return count;
  817. }
  818. //----------------------------------------------------------------------------
  819. void
  820. cmGlobalUnixMakefileGenerator3::RecordTargetProgress(
  821. cmMakefileTargetGenerator* tg)
  822. {
  823. TargetProgress& tp = this->ProgressMap[tg->GetTarget()];
  824. tp.NumberOfActions = tg->GetNumberOfProgressActions();
  825. tp.VariableFile = tg->GetProgressFileNameFull();
  826. }
  827. //----------------------------------------------------------------------------
  828. bool
  829. cmGlobalUnixMakefileGenerator3::ProgressMapCompare
  830. ::operator()(cmTarget* l, cmTarget* r) const
  831. {
  832. // Order by target name.
  833. if(int c = strcmp(l->GetName(), r->GetName()))
  834. {
  835. return c < 0;
  836. }
  837. // Order duplicate targets by binary directory.
  838. return strcmp(l->GetMakefile()->GetCurrentOutputDirectory(),
  839. r->GetMakefile()->GetCurrentOutputDirectory()) < 0;
  840. }
  841. //----------------------------------------------------------------------------
  842. void
  843. cmGlobalUnixMakefileGenerator3::TargetProgress
  844. ::WriteProgressVariables(unsigned long total, unsigned long &current)
  845. {
  846. cmGeneratedFileStream fout(this->VariableFile.c_str());
  847. for(unsigned long i = 1; i <= this->NumberOfActions; ++i)
  848. {
  849. fout << "CMAKE_PROGRESS_" << i << " = ";
  850. if (total <= 100)
  851. {
  852. unsigned long num = i + current;
  853. fout << num;
  854. this->Marks.push_back(num);
  855. }
  856. else if (((i+current)*100)/total > ((i-1+current)*100)/total)
  857. {
  858. unsigned long num = ((i+current)*100)/total;
  859. fout << num;
  860. this->Marks.push_back(num);
  861. }
  862. fout << "\n";
  863. }
  864. fout << "\n";
  865. current += this->NumberOfActions;
  866. }
  867. //----------------------------------------------------------------------------
  868. void
  869. cmGlobalUnixMakefileGenerator3
  870. ::AppendGlobalTargetDepends(std::vector<std::string>& depends,
  871. cmTarget& target)
  872. {
  873. TargetDependSet const& depends_set = this->GetTargetDirectDepends(target);
  874. for(TargetDependSet::const_iterator i = depends_set.begin();
  875. i != depends_set.end(); ++i)
  876. {
  877. // Create the target-level dependency.
  878. cmTarget const* dep = *i;
  879. cmLocalUnixMakefileGenerator3* lg3 =
  880. static_cast<cmLocalUnixMakefileGenerator3*>
  881. (dep->GetMakefile()->GetLocalGenerator());
  882. std::string tgtName = lg3->GetRelativeTargetDirectory(*dep);
  883. tgtName += "/all";
  884. depends.push_back(tgtName);
  885. }
  886. }
  887. //----------------------------------------------------------------------------
  888. void cmGlobalUnixMakefileGenerator3::WriteHelpRule
  889. (std::ostream& ruleFileStream, cmLocalUnixMakefileGenerator3 *lg)
  890. {
  891. // add the help target
  892. std::string path;
  893. std::vector<std::string> no_depends;
  894. std::vector<std::string> commands;
  895. lg->AppendEcho(commands,"The following are some of the valid targets "
  896. "for this Makefile:");
  897. lg->AppendEcho(commands,"... all (the default if no target is provided)");
  898. lg->AppendEcho(commands,"... clean");
  899. lg->AppendEcho(commands,"... depend");
  900. // Keep track of targets already listed.
  901. std::set<cmStdString> emittedTargets;
  902. // for each local generator
  903. unsigned int i;
  904. cmLocalUnixMakefileGenerator3 *lg2;
  905. for (i = 0; i < this->LocalGenerators.size(); ++i)
  906. {
  907. lg2 =
  908. static_cast<cmLocalUnixMakefileGenerator3 *>(this->LocalGenerators[i]);
  909. // for the passed in makefile or if this is the top Makefile wripte out
  910. // the targets
  911. if (lg2 == lg || !lg->GetParent())
  912. {
  913. // for each target Generate the rule files for each target.
  914. cmTargets& targets = lg2->GetMakefile()->GetTargets();
  915. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  916. {
  917. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  918. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  919. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  920. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  921. (t->second.GetType() == cmTarget::GLOBAL_TARGET) ||
  922. (t->second.GetType() == cmTarget::UTILITY))
  923. {
  924. if(emittedTargets.insert(t->second.GetName()).second)
  925. {
  926. path = "... ";
  927. path += t->second.GetName();
  928. lg->AppendEcho(commands,path.c_str());
  929. }
  930. }
  931. }
  932. }
  933. }
  934. std::vector<cmStdString> const& localHelp = lg->GetLocalHelp();
  935. for(std::vector<cmStdString>::const_iterator o = localHelp.begin();
  936. o != localHelp.end(); ++o)
  937. {
  938. path = "... ";
  939. path += *o;
  940. lg->AppendEcho(commands, path.c_str());
  941. }
  942. lg->WriteMakeRule(ruleFileStream, "Help Target",
  943. "help",
  944. no_depends, commands, true);
  945. ruleFileStream << "\n\n";
  946. }
  947. bool cmGlobalUnixMakefileGenerator3
  948. ::NeedRequiresStep(cmTarget const& target)
  949. {
  950. std::set<cmStdString> languages;
  951. target.GetLanguages(languages);
  952. for(std::set<cmStdString>::const_iterator l = languages.begin();
  953. l != languages.end(); ++l)
  954. {
  955. std::string var = "CMAKE_NEEDS_REQUIRES_STEP_";
  956. var += *l;
  957. var += "_FLAG";
  958. if(target.GetMakefile()->GetDefinition(var.c_str()))
  959. {
  960. return true;
  961. }
  962. }
  963. return false;
  964. }