cmGlobalUnixMakefileGenerator3.cxx 37 KB

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