cmLocalUnixMakefileGenerator2.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmLocalUnixMakefileGenerator2.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. //----------------------------------------------------------------------------
  19. cmLocalUnixMakefileGenerator2::cmLocalUnixMakefileGenerator2()
  20. {
  21. }
  22. //----------------------------------------------------------------------------
  23. cmLocalUnixMakefileGenerator2::~cmLocalUnixMakefileGenerator2()
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmLocalUnixMakefileGenerator2::Generate(bool fromTheTop)
  28. {
  29. // TODO: Account for control-c during Makefile generation.
  30. // Generate old style for now.
  31. this->cmLocalUnixMakefileGenerator::Generate(fromTheTop);
  32. // Generate the rule files for each target.
  33. const cmTargets& targets = m_Makefile->GetTargets();
  34. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  35. {
  36. this->GenerateTargetRuleFile(t->second);
  37. }
  38. // Generate the main makefile.
  39. this->GenerateMakefile();
  40. // Generate the cmake file that keeps the makefile up to date.
  41. this->GenerateCMakefile();
  42. }
  43. //----------------------------------------------------------------------------
  44. void cmLocalUnixMakefileGenerator2::GenerateMakefile()
  45. {
  46. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  47. makefileName += "/Makefile2";
  48. std::string cmakefileName = makefileName;
  49. cmakefileName += ".cmake";
  50. // Open the output files.
  51. std::ofstream makefileStream(makefileName.c_str());
  52. if(!makefileStream)
  53. {
  54. cmSystemTools::Error("Error can not open for write: ",
  55. makefileName.c_str());
  56. cmSystemTools::ReportLastSystemError("");
  57. return;
  58. }
  59. // Write the do not edit header.
  60. this->WriteDisclaimer(makefileStream);
  61. // Write some rules to make things look nice.
  62. makefileStream
  63. << "# Disable some common implicit rules to speed things up.\n"
  64. << ".SUFFIXES:\n"
  65. << ".SUFFIXES:.hpuxmakemusthaverule\n\n";
  66. // Write standard variables to the makefile.
  67. this->OutputMakeVariables(makefileStream);
  68. // Build command to run CMake to check if anything needs regenerating.
  69. std::string runRule =
  70. "@$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  71. runRule += " --check-rerun ";
  72. runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
  73. // Most unix makes will pass the command line flags to make down to
  74. // sub-invoked makes via an environment variable. However, some
  75. // makes do not support that, so you have to pass the flags
  76. // explicitly.
  77. const char* depRule = "$(MAKE) -f Makefile2 $(MAKESILENT) all.depends";
  78. const char* allRule = "$(MAKE) -f Makefile2 $(MAKESILENT) all";
  79. if(m_PassMakeflags)
  80. {
  81. depRule = "$(MAKE) -f Makefile2 $(MAKESILENT) -$(MAKEFLAGS) all.depends";
  82. allRule = "$(MAKE) -f Makefile2 $(MAKESILENT) -$(MAKEFLAGS) all";
  83. }
  84. // Write the main entry point target. This must be the VERY first
  85. // target so that make with no arguments will run it.
  86. {
  87. std::vector<std::string> depends;
  88. std::vector<std::string> commands;
  89. commands.push_back(runRule);
  90. commands.push_back(depRule);
  91. commands.push_back(allRule);
  92. this->OutputMakeRule(
  93. makefileStream,
  94. "Default target executed when no arguments are given to make.",
  95. "default_target",
  96. depends,
  97. commands);
  98. }
  99. // Write special target to silence make output. This must be after
  100. // the default target in case VERBOSE is set (which changes the name).
  101. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  102. {
  103. makefileStream
  104. << "# Suppress display of executed commands.\n"
  105. << "$(VERBOSE).SILENT:\n\n";
  106. }
  107. // Get the set of targets.
  108. const cmTargets& targets = m_Makefile->GetTargets();
  109. // Output top level dependency rule.
  110. {
  111. std::vector<std::string> depends;
  112. std::vector<std::string> commands;
  113. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  114. {
  115. if(t->second.IsInAll())
  116. {
  117. std::string dep = this->GetTargetDirectory(t->second);
  118. dep += "/";
  119. dep += t->first;
  120. dep += ".depends";
  121. depends.push_back(dep);
  122. }
  123. }
  124. this->OutputMakeRule(makefileStream, "all dependencies", "all.depends",
  125. depends, commands);
  126. }
  127. // Output top level build rule.
  128. {
  129. std::vector<std::string> depends;
  130. std::vector<std::string> commands;
  131. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  132. {
  133. if(t->second.IsInAll())
  134. {
  135. depends.push_back(t->first+".requires");
  136. }
  137. }
  138. this->OutputMakeRule(makefileStream, "all", "all",
  139. depends, commands);
  140. }
  141. // Write include statements to get rules for each target.
  142. makefileStream
  143. << "# Include target rule files.\n";
  144. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  145. {
  146. std::string ruleFileName = this->GetTargetDirectory(t->second);
  147. ruleFileName += "/";
  148. ruleFileName += t->first;
  149. ruleFileName += ".make";
  150. makefileStream
  151. << m_IncludeDirective << " "
  152. << this->ConvertToOutputForExisting(ruleFileName.c_str()).c_str()
  153. << "\n";
  154. }
  155. }
  156. //----------------------------------------------------------------------------
  157. void cmLocalUnixMakefileGenerator2::GenerateCMakefile()
  158. {
  159. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  160. makefileName += "/Makefile2";
  161. std::string cmakefileName = makefileName;
  162. cmakefileName += ".cmake";
  163. // Open the output file.
  164. std::ofstream cmakefileStream(cmakefileName.c_str());
  165. if(!cmakefileStream)
  166. {
  167. cmSystemTools::Error("Error can not open for write: ",
  168. cmakefileName.c_str());
  169. cmSystemTools::ReportLastSystemError("");
  170. return;
  171. }
  172. // Write the do not edit header.
  173. this->WriteDisclaimer(cmakefileStream);
  174. // Get the list of files contributing to this generation step.
  175. // Sort the list and remove duplicates.
  176. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  177. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  178. std::vector<std::string>::iterator new_end = std::unique(lfiles.begin(),
  179. lfiles.end());
  180. lfiles.erase(new_end, lfiles.end());
  181. // Save the list to the cmake file.
  182. cmakefileStream
  183. << "# The corresponding makefile\n"
  184. << "# \"" << makefileName << "\"\n"
  185. << "# was generated from the following files:\n"
  186. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  187. << " \"" << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\"\n";
  188. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  189. i != lfiles.end(); ++i)
  190. {
  191. cmakefileStream
  192. << " \"" << i->c_str() << "\"\n";
  193. }
  194. cmakefileStream
  195. << " )\n\n";
  196. // Set the corresponding makefile in the cmake file.
  197. cmakefileStream
  198. << "# The corresponding makefile is:\n"
  199. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  200. << " \"" << makefileName.c_str() << "\"\n"
  201. << " )\n";
  202. }
  203. //----------------------------------------------------------------------------
  204. void
  205. cmLocalUnixMakefileGenerator2
  206. ::GenerateTargetRuleFile(const cmTarget& target)
  207. {
  208. // Create a directory for this target.
  209. std::string dir = this->GetTargetDirectory(target);
  210. cmSystemTools::MakeDirectory(dir.c_str());
  211. // First generate the object rule files. Save a list of all object
  212. // files for this target.
  213. std::vector<std::string> objects;
  214. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  215. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  216. source != sources.end(); ++source)
  217. {
  218. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  219. !(*source)->GetCustomCommand())
  220. {
  221. // Generate this object file's rule file.
  222. this->GenerateObjectRuleFile(target, *(*source));
  223. // Save the object file full path.
  224. std::string obj = dir;
  225. obj += "/";
  226. obj += this->GetObjectFileName(*(*source));
  227. objects.push_back(obj);
  228. }
  229. }
  230. // If there is no dependencies file, create an empty one.
  231. std::string depFileName = dir;
  232. depFileName += "/";
  233. depFileName += target.GetName();
  234. depFileName += ".depends.make";
  235. if(!cmSystemTools::FileExists(depFileName.c_str()))
  236. {
  237. std::ofstream depFileStream(depFileName.c_str());
  238. this->WriteDisclaimer(depFileStream);
  239. depFileStream
  240. << "# Empty dependencies file for target " << target.GetName() << ".\n"
  241. << "# This may be replaced when dependencies are built.\n";
  242. }
  243. // Open the rule file. This should be copy-if-different because the
  244. // rules may depend on this file itself.
  245. std::string ruleFileName = dir;
  246. ruleFileName += "/";
  247. ruleFileName += target.GetName();
  248. ruleFileName += ".make";
  249. cmGeneratedFileStream ruleFile(ruleFileName.c_str());
  250. std::ostream& ruleFileStream = ruleFile.GetStream();
  251. if(!ruleFileStream)
  252. {
  253. // TODO: Produce error message that accounts for generated stream
  254. // .tmp.
  255. return;
  256. }
  257. this->WriteDisclaimer(ruleFileStream);
  258. ruleFileStream
  259. << "# Rule file for target " << target.GetName() << ".\n\n";
  260. // Include the dependencies for the target.
  261. ruleFileStream
  262. << "# Include any dependencies generated for this rule.\n"
  263. << m_IncludeDirective << " "
  264. << this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
  265. << "\n\n";
  266. // Include the rule file for each object.
  267. if(!objects.empty())
  268. {
  269. ruleFileStream
  270. << "# Include rules for object files.\n";
  271. for(std::vector<std::string>::const_iterator obj = objects.begin();
  272. obj != objects.end(); ++obj)
  273. {
  274. std::string objRuleFileName = *obj;
  275. objRuleFileName += ".make";
  276. ruleFileStream
  277. << m_IncludeDirective << " "
  278. << this->ConvertToOutputForExisting(objRuleFileName.c_str()).c_str()
  279. << "\n";
  280. }
  281. ruleFileStream
  282. << "\n";
  283. }
  284. // Write the dependency generation rule.
  285. {
  286. std::vector<std::string> depends;
  287. std::vector<std::string> commands;
  288. std::string depComment = "dependencies for ";
  289. depComment += target.GetName();
  290. std::string depTarget = dir;
  291. depTarget += "/";
  292. depTarget += target.GetName();
  293. depTarget += ".depends";
  294. for(std::vector<std::string>::const_iterator obj = objects.begin();
  295. obj != objects.end(); ++obj)
  296. {
  297. depends.push_back((*obj)+".depends");
  298. }
  299. depends.push_back(ruleFileName);
  300. std::string touchCmd = "@touch ";
  301. touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
  302. // TODO: Construct dependency generation rule and append command.
  303. commands.push_back(touchCmd);
  304. this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
  305. depends, commands);
  306. }
  307. // Write the requires rule.
  308. {
  309. std::vector<std::string> depends;
  310. std::vector<std::string> commands;
  311. std::string reqComment = "requirements for ";
  312. reqComment += target.GetName();
  313. std::string reqTarget = target.GetName();
  314. reqTarget += ".requires";
  315. for(std::vector<std::string>::const_iterator obj = objects.begin();
  316. obj != objects.end(); ++obj)
  317. {
  318. depends.push_back(*obj);
  319. }
  320. depends.push_back(ruleFileName);
  321. this->OutputMakeRule(ruleFileStream, reqComment.c_str(), reqTarget.c_str(),
  322. depends, commands);
  323. }
  324. #if 0
  325. // Write the build rule.
  326. {
  327. std::vector<std::string> depends;
  328. std::vector<std::string> commands;
  329. std::string buildComment = " target ";
  330. buildComment += objName;
  331. for(std::vector<std::string>::const_iterator obj = objects.begin();
  332. obj != objects.end(); ++obj)
  333. {
  334. depends.push_back(*obj);
  335. }
  336. depends.push_back(ruleFileName);
  337. std::string touchCmd = "@touch ";
  338. touchCmd += this->ConvertToRelativeOutputPath(obj.c_str());
  339. // TODO: Construct build rule and append command.
  340. commands.push_back(touchCmd);
  341. this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
  342. depends, commands);
  343. }
  344. #endif
  345. }
  346. //----------------------------------------------------------------------------
  347. void
  348. cmLocalUnixMakefileGenerator2
  349. ::GenerateObjectRuleFile(const cmTarget& target, const cmSourceFile& source)
  350. {
  351. // Get the full path name of the object file.
  352. std::string objName = this->GetObjectFileName(source);
  353. std::string obj = this->GetTargetDirectory(target);
  354. obj += "/";
  355. obj += objName;
  356. // Create the directory containing the object file. This may be a
  357. // subdirectory under the target's directory.
  358. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  359. cmSystemTools::MakeDirectory(dir.c_str());
  360. // If there is no dependencies file, create an empty one.
  361. std::string depFileName = obj;
  362. depFileName += ".depends.make";
  363. if(!cmSystemTools::FileExists(depFileName.c_str()))
  364. {
  365. std::ofstream depFileStream(depFileName.c_str());
  366. this->WriteDisclaimer(depFileStream);
  367. depFileStream
  368. << "# Empty dependencies file for object file "
  369. << objName.c_str() << ".\n"
  370. << "# This may be replaced when dependencies are built.\n";
  371. }
  372. // Open the rule file. This should be copy-if-different because the
  373. // rules may depend on this file itself.
  374. std::string ruleFileName = obj;
  375. ruleFileName += ".make";
  376. cmGeneratedFileStream ruleFile(ruleFileName.c_str());
  377. std::ostream& ruleFileStream = ruleFile.GetStream();
  378. if(!ruleFileStream)
  379. {
  380. // TODO: Produce error message that accounts for generated stream
  381. // .tmp.
  382. return;
  383. }
  384. this->WriteDisclaimer(ruleFileStream);
  385. ruleFileStream
  386. << "# Rule file for object file " << objName.c_str() << ".\n\n";
  387. // Include the dependencies for the target.
  388. ruleFileStream
  389. << "# Include any dependencies generated for this rule.\n"
  390. << m_IncludeDirective << " "
  391. << this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
  392. << "\n\n";
  393. // Write the dependency generation rule.
  394. std::string depTarget = obj;
  395. depTarget += ".depends";
  396. {
  397. std::vector<std::string> depends;
  398. std::vector<std::string> commands;
  399. std::string depComment = "dependencies for ";
  400. depComment += objName;
  401. depends.push_back(source.GetFullPath());
  402. depends.push_back(ruleFileName);
  403. std::string touchCmd = "@touch ";
  404. touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
  405. // TODO: Construct dependency generation rule and append command.
  406. commands.push_back(touchCmd);
  407. this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
  408. depends, commands);
  409. }
  410. // Write the build rule.
  411. {
  412. std::vector<std::string> depends;
  413. std::vector<std::string> commands;
  414. std::string buildComment = "object ";
  415. buildComment += objName;
  416. depends.push_back(depTarget);
  417. depends.push_back(ruleFileName);
  418. std::string touchCmd = "@touch ";
  419. touchCmd += this->ConvertToRelativeOutputPath(obj.c_str());
  420. // TODO: Construct build rule and append command.
  421. commands.push_back(touchCmd);
  422. this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
  423. depends, commands);
  424. }
  425. }
  426. //----------------------------------------------------------------------------
  427. void cmLocalUnixMakefileGenerator2::WriteDisclaimer(std::ostream& os)
  428. {
  429. os
  430. << "# CMAKE generated file: DO NOT EDIT!\n"
  431. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  432. << " Generator, CMake Version "
  433. << cmMakefile::GetMajorVersion() << "."
  434. << cmMakefile::GetMinorVersion() << "\n\n";
  435. }
  436. //----------------------------------------------------------------------------
  437. std::string
  438. cmLocalUnixMakefileGenerator2
  439. ::GetTargetDirectory(const cmTarget& target)
  440. {
  441. std::string dir = m_Makefile->GetStartOutputDirectory();
  442. dir += "/";
  443. dir += target.GetName();
  444. dir += ".dir";
  445. return dir;
  446. }
  447. //----------------------------------------------------------------------------
  448. std::string
  449. cmLocalUnixMakefileGenerator2
  450. ::GetObjectFileName(const cmSourceFile& source)
  451. {
  452. // If the full path to the source file includes this directory,
  453. // we want to use the relative path for the filename of the
  454. // object file. Otherwise, we will use just the filename
  455. // portion.
  456. std::string objectName;
  457. if((cmSystemTools::GetFilenamePath(
  458. source.GetFullPath()).find(
  459. m_Makefile->GetCurrentDirectory()) == 0)
  460. || (cmSystemTools::GetFilenamePath(
  461. source.GetFullPath()).find(
  462. m_Makefile->GetCurrentOutputDirectory()) == 0))
  463. {
  464. objectName = source.GetSourceName();
  465. }
  466. else
  467. {
  468. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  469. }
  470. // Append the object file extension.
  471. objectName +=
  472. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  473. source.GetSourceExtension().c_str());
  474. return objectName;
  475. }