cmLocalUnixMakefileGenerator2.cxx 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. #include <queue>
  19. //----------------------------------------------------------------------------
  20. cmLocalUnixMakefileGenerator2::cmLocalUnixMakefileGenerator2()
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. cmLocalUnixMakefileGenerator2::~cmLocalUnixMakefileGenerator2()
  25. {
  26. }
  27. //----------------------------------------------------------------------------
  28. void cmLocalUnixMakefileGenerator2::Generate(bool fromTheTop)
  29. {
  30. // TODO: Account for control-c during Makefile generation.
  31. // Generate old style for now.
  32. this->cmLocalUnixMakefileGenerator::Generate(fromTheTop);
  33. // Generate the rule files for each target.
  34. const cmTargets& targets = m_Makefile->GetTargets();
  35. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  36. {
  37. // TODO: Dispatch generation of each target type.
  38. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  39. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  40. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  41. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  42. {
  43. this->GenerateTargetRuleFile(t->second);
  44. }
  45. }
  46. // Generate the main makefile.
  47. this->GenerateMakefile();
  48. // Generate the cmake file that keeps the makefile up to date.
  49. this->GenerateCMakefile();
  50. }
  51. //----------------------------------------------------------------------------
  52. void cmLocalUnixMakefileGenerator2::GenerateMakefile()
  53. {
  54. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  55. makefileName += "/Makefile2";
  56. std::string cmakefileName = makefileName;
  57. cmakefileName += ".cmake";
  58. // Open the output files.
  59. std::ofstream makefileStream(makefileName.c_str());
  60. if(!makefileStream)
  61. {
  62. cmSystemTools::Error("Error can not open for write: ",
  63. makefileName.c_str());
  64. cmSystemTools::ReportLastSystemError("");
  65. return;
  66. }
  67. // Write the do not edit header.
  68. this->WriteDisclaimer(makefileStream);
  69. // Write some rules to make things look nice.
  70. makefileStream
  71. << "# Disable some common implicit rules to speed things up.\n"
  72. << ".SUFFIXES:\n"
  73. << ".SUFFIXES:.hpuxmakemusthaverule\n\n";
  74. // Write standard variables to the makefile.
  75. this->OutputMakeVariables(makefileStream);
  76. // Build command to run CMake to check if anything needs regenerating.
  77. std::string runRule =
  78. "@$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  79. runRule += " --check-rerun ";
  80. runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
  81. // Construct recursive calls for the "all" rules.
  82. std::string depRule;
  83. std::string allRule;
  84. this->AppendRecursiveMake(depRule, "Makefile2", "all.depends");
  85. this->AppendRecursiveMake(allRule, "Makefile2", "all");
  86. // Write the main entry point target. This must be the VERY first
  87. // target so that make with no arguments will run it.
  88. {
  89. std::vector<std::string> depends;
  90. std::vector<std::string> commands;
  91. commands.push_back(runRule);
  92. commands.push_back(depRule);
  93. commands.push_back(allRule);
  94. this->OutputMakeRule(
  95. makefileStream,
  96. "Default target executed when no arguments are given to make.",
  97. "default_target",
  98. depends,
  99. commands);
  100. }
  101. // Write special target to silence make output. This must be after
  102. // the default target in case VERBOSE is set (which changes the name).
  103. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  104. {
  105. makefileStream
  106. << "# Suppress display of executed commands.\n"
  107. << "$(VERBOSE).SILENT:\n\n";
  108. }
  109. // Get the set of targets.
  110. const cmTargets& targets = m_Makefile->GetTargets();
  111. // Output top level dependency rule.
  112. {
  113. std::vector<std::string> depends;
  114. std::vector<std::string> commands;
  115. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  116. {
  117. // TODO: Dispatch generation of each target type.
  118. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  119. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  120. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  121. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  122. {
  123. if(t->second.IsInAll())
  124. {
  125. std::string dep = this->GetTargetDirectory(t->second);
  126. dep += "/";
  127. dep += t->first;
  128. dep += ".depends";
  129. depends.push_back(dep);
  130. }
  131. }
  132. }
  133. this->OutputMakeRule(makefileStream, "all dependencies", "all.depends",
  134. depends, commands);
  135. }
  136. // Output top level build rule.
  137. {
  138. std::vector<std::string> depends;
  139. std::vector<std::string> commands;
  140. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  141. {
  142. // TODO: Dispatch generation of each target type.
  143. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  144. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  145. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  146. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  147. {
  148. if(t->second.IsInAll())
  149. {
  150. depends.push_back(t->first+".requires");
  151. }
  152. }
  153. }
  154. this->OutputMakeRule(makefileStream, "all", "all",
  155. depends, commands);
  156. }
  157. // Write include statements to get rules for each target.
  158. makefileStream
  159. << "# Include target rule files.\n";
  160. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  161. {
  162. // TODO: Dispatch generation of each target type.
  163. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  164. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  165. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  166. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  167. {
  168. std::string ruleFileName = this->GetTargetDirectory(t->second);
  169. ruleFileName += "/";
  170. ruleFileName += t->first;
  171. ruleFileName += ".make";
  172. makefileStream
  173. << m_IncludeDirective << " "
  174. << this->ConvertToOutputForExisting(ruleFileName.c_str()).c_str()
  175. << "\n";
  176. }
  177. }
  178. // Write jump-and-build rules that were recorded in the map.
  179. this->WriteJumpAndBuildRules(makefileStream);
  180. }
  181. //----------------------------------------------------------------------------
  182. void cmLocalUnixMakefileGenerator2::GenerateCMakefile()
  183. {
  184. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  185. makefileName += "/Makefile2";
  186. std::string cmakefileName = makefileName;
  187. cmakefileName += ".cmake";
  188. // Open the output file.
  189. std::ofstream cmakefileStream(cmakefileName.c_str());
  190. if(!cmakefileStream)
  191. {
  192. cmSystemTools::Error("Error can not open for write: ",
  193. cmakefileName.c_str());
  194. cmSystemTools::ReportLastSystemError("");
  195. return;
  196. }
  197. // Write the do not edit header.
  198. this->WriteDisclaimer(cmakefileStream);
  199. // Get the list of files contributing to this generation step.
  200. // Sort the list and remove duplicates.
  201. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  202. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  203. std::vector<std::string>::iterator new_end = std::unique(lfiles.begin(),
  204. lfiles.end());
  205. lfiles.erase(new_end, lfiles.end());
  206. // Save the list to the cmake file.
  207. cmakefileStream
  208. << "# The corresponding makefile\n"
  209. << "# \"" << makefileName << "\"\n"
  210. << "# was generated from the following files:\n"
  211. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  212. << " \"" << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\"\n";
  213. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  214. i != lfiles.end(); ++i)
  215. {
  216. cmakefileStream
  217. << " \"" << i->c_str() << "\"\n";
  218. }
  219. cmakefileStream
  220. << " )\n\n";
  221. // Set the corresponding makefile in the cmake file.
  222. cmakefileStream
  223. << "# The corresponding makefile is:\n"
  224. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  225. << " \"" << makefileName.c_str() << "\"\n"
  226. << " )\n";
  227. }
  228. //----------------------------------------------------------------------------
  229. void
  230. cmLocalUnixMakefileGenerator2
  231. ::GenerateTargetRuleFile(const cmTarget& target)
  232. {
  233. // Create a directory for this target.
  234. std::string dir = this->GetTargetDirectory(target);
  235. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  236. // First generate the object rule files. Save a list of all object
  237. // files for this target.
  238. std::vector<std::string> objects;
  239. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  240. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  241. source != sources.end(); ++source)
  242. {
  243. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  244. !(*source)->GetCustomCommand() &&
  245. !m_GlobalGenerator->IgnoreFile((*source)->GetSourceExtension().c_str()))
  246. {
  247. // Generate this object file's rule file.
  248. this->GenerateObjectRuleFile(target, *(*source));
  249. // Save the object file name.
  250. objects.push_back(this->GetObjectFileName(target, *(*source)));
  251. }
  252. }
  253. // If there is no dependencies file, create an empty one.
  254. std::string depFileName = dir;
  255. depFileName += "/";
  256. depFileName += target.GetName();
  257. depFileName += ".depends.make";
  258. std::string depFileNameFull = this->ConvertToFullPath(depFileName);
  259. if(!cmSystemTools::FileExists(depFileNameFull.c_str()))
  260. {
  261. std::ofstream depFileStream(depFileNameFull.c_str());
  262. this->WriteDisclaimer(depFileStream);
  263. depFileStream
  264. << "# Empty dependencies file for target " << target.GetName() << ".\n"
  265. << "# This may be replaced when dependencies are built.\n";
  266. }
  267. // Open the rule file. This should be copy-if-different because the
  268. // rules may depend on this file itself.
  269. std::string ruleFileName = dir;
  270. ruleFileName += "/";
  271. ruleFileName += target.GetName();
  272. ruleFileName += ".make";
  273. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  274. cmGeneratedFileStream ruleFile(ruleFileNameFull.c_str());
  275. std::ostream& ruleFileStream = ruleFile.GetStream();
  276. if(!ruleFileStream)
  277. {
  278. // TODO: Produce error message that accounts for generated stream
  279. // .tmp.
  280. return;
  281. }
  282. this->WriteDisclaimer(ruleFileStream);
  283. ruleFileStream
  284. << "# Rule file for target " << target.GetName() << ".\n\n";
  285. // Include the dependencies for the target.
  286. ruleFileStream
  287. << "# Include any dependencies generated for this rule.\n"
  288. << m_IncludeDirective << " "
  289. << this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
  290. << "\n\n";
  291. // Include the rule file for each object.
  292. if(!objects.empty())
  293. {
  294. ruleFileStream
  295. << "# Include make rules for object files.\n";
  296. for(std::vector<std::string>::const_iterator obj = objects.begin();
  297. obj != objects.end(); ++obj)
  298. {
  299. std::string objRuleFileName = *obj;
  300. objRuleFileName += ".make";
  301. ruleFileStream
  302. << m_IncludeDirective << " "
  303. << this->ConvertToOutputForExisting(objRuleFileName.c_str()).c_str()
  304. << "\n";
  305. }
  306. ruleFileStream
  307. << "\n";
  308. }
  309. // Write the dependency generation rule.
  310. {
  311. std::vector<std::string> depends;
  312. std::vector<std::string> commands;
  313. std::string depComment = "dependencies for ";
  314. depComment += target.GetName();
  315. std::string depTarget = dir;
  316. depTarget += "/";
  317. depTarget += target.GetName();
  318. depTarget += ".depends";
  319. for(std::vector<std::string>::const_iterator obj = objects.begin();
  320. obj != objects.end(); ++obj)
  321. {
  322. depends.push_back((*obj)+".depends");
  323. }
  324. depends.push_back(ruleFileName);
  325. this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
  326. depends, commands);
  327. }
  328. #if 0
  329. // Write the requires rule.
  330. {
  331. std::vector<std::string> depends;
  332. std::vector<std::string> commands;
  333. std::string reqComment = "requirements for ";
  334. reqComment += target.GetName();
  335. std::string reqTarget = target.GetName();
  336. reqTarget += ".requires";
  337. for(std::vector<std::string>::const_iterator obj = objects.begin();
  338. obj != objects.end(); ++obj)
  339. {
  340. depends.push_back(*obj);
  341. }
  342. depends.push_back(ruleFileName);
  343. this->OutputMakeRule(ruleFileStream, reqComment.c_str(), reqTarget.c_str(),
  344. depends, commands);
  345. }
  346. #endif
  347. // Write the build rule.
  348. switch(target.GetType())
  349. {
  350. case cmTarget::STATIC_LIBRARY:
  351. this->WriteStaticLibraryRule(ruleFileStream, ruleFileName.c_str(),
  352. target, objects);
  353. break;
  354. case cmTarget::SHARED_LIBRARY:
  355. this->WriteSharedLibraryRule(ruleFileStream, ruleFileName.c_str(),
  356. target, objects);
  357. break;
  358. case cmTarget::MODULE_LIBRARY:
  359. this->WriteModuleLibraryRule(ruleFileStream, ruleFileName.c_str(),
  360. target, objects);
  361. break;
  362. case cmTarget::EXECUTABLE:
  363. this->WriteExecutableRule(ruleFileStream, ruleFileName.c_str(),
  364. target, objects);
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. //----------------------------------------------------------------------------
  371. void
  372. cmLocalUnixMakefileGenerator2
  373. ::GenerateObjectRuleFile(const cmTarget& target, const cmSourceFile& source)
  374. {
  375. // Identify the language of the source file.
  376. const char* lang = this->GetSourceFileLanguage(source);
  377. if(!lang)
  378. {
  379. // If language is not known, this is an error.
  380. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  381. "\" has unknown type.");
  382. return;
  383. }
  384. // Get the full path name of the object file.
  385. std::string obj = this->GetObjectFileName(target, source);
  386. // Create the directory containing the object file. This may be a
  387. // subdirectory under the target's directory.
  388. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  389. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  390. // If there is no dependencies file, create an empty one.
  391. std::string depFileName = obj;
  392. depFileName += ".depends.make";
  393. std::string depFileNameFull = this->ConvertToFullPath(depFileName);
  394. if(!cmSystemTools::FileExists(depFileNameFull.c_str()))
  395. {
  396. std::ofstream depFileStream(depFileNameFull.c_str());
  397. this->WriteDisclaimer(depFileStream);
  398. depFileStream
  399. << "# Empty dependencies file for object file " << obj.c_str() << ".\n"
  400. << "# This may be replaced when dependencies are built.\n";
  401. }
  402. // Open the rule file for writing. This should be copy-if-different
  403. // because the rules may depend on this file itself.
  404. std::string ruleFileName = obj;
  405. ruleFileName += ".make";
  406. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  407. cmGeneratedFileStream ruleFile(ruleFileNameFull.c_str());
  408. std::ostream& ruleFileStream = ruleFile.GetStream();
  409. if(!ruleFileStream)
  410. {
  411. // TODO: Produce error message that accounts for generated stream
  412. // .tmp.
  413. return;
  414. }
  415. this->WriteDisclaimer(ruleFileStream);
  416. ruleFileStream
  417. << "# Rule file for object file " << obj.c_str() << ".\n\n";
  418. // Include the dependencies for the target.
  419. ruleFileStream
  420. << "# Include any dependencies generated for this rule.\n"
  421. << m_IncludeDirective << " "
  422. << this->ConvertToOutputForExisting(depFileName.c_str()).c_str()
  423. << "\n\n";
  424. // Create the list of dependencies known at cmake time. These are
  425. // shared between the object file and dependency scanning rule.
  426. std::vector<std::string> depends;
  427. depends.push_back(source.GetFullPath());
  428. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  429. {
  430. std::vector<std::string> deps;
  431. cmSystemTools::ExpandListArgument(objectDeps, deps);
  432. for(std::vector<std::string>::iterator i = deps.begin();
  433. i != deps.end(); ++i)
  434. {
  435. depends.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  436. }
  437. }
  438. depends.push_back(ruleFileName);
  439. // Write the dependency generation rule.
  440. std::string depTarget = obj;
  441. depTarget += ".depends";
  442. {
  443. std::string depComment = "dependencies for ";
  444. depComment += obj;
  445. cmOStringStream depCmd;
  446. // TODO: Account for source file properties and directory-level
  447. // definitions when scanning for dependencies.
  448. depCmd << "$(CMAKE_COMMAND) -E cmake_depends " << lang << " "
  449. << this->ConvertToRelativeOutputPath(obj.c_str()) << " "
  450. << this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  451. std::vector<std::string> includeDirs;
  452. this->GetIncludeDirectories(includeDirs);
  453. for(std::vector<std::string>::iterator i = includeDirs.begin();
  454. i != includeDirs.end(); ++i)
  455. {
  456. depCmd << " -I" << this->ConvertToRelativeOutputPath(i->c_str());
  457. }
  458. std::vector<std::string> commands;
  459. commands.push_back(depCmd.str());
  460. std::string touchCmd = "@touch ";
  461. touchCmd += this->ConvertToRelativeOutputPath(depTarget.c_str());
  462. commands.push_back(touchCmd);
  463. this->OutputMakeRule(ruleFileStream, depComment.c_str(), depTarget.c_str(),
  464. depends, commands);
  465. }
  466. // Write the build rule.
  467. {
  468. // Build the set of compiler flags.
  469. std::string flags;
  470. // Add the export symbol definition for shared library objects.
  471. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  472. (target.GetType() == cmTarget::MODULE_LIBRARY));
  473. if(shared)
  474. {
  475. flags += "-D";
  476. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  477. {
  478. flags += custom_export_name;
  479. }
  480. else
  481. {
  482. std::string in = target.GetName();
  483. in += "_EXPORTS";
  484. flags += cmSystemTools::MakeCindentifier(in.c_str());
  485. }
  486. }
  487. // Add flags from source file properties.
  488. this->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  489. // Add language-specific flags.
  490. this->AddLanguageFlags(flags, lang);
  491. // Add shared-library flags if needed.
  492. this->AddSharedFlags(flags, lang, shared);
  493. // Add include directory flags.
  494. this->AppendFlags(flags, this->GetIncludeFlags(lang));
  495. // Get the output paths for source and object files.
  496. std::string sourceFile =
  497. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  498. std::string objectFile =
  499. this->ConvertToRelativeOutputPath(obj.c_str());
  500. // Construct the compile rules.
  501. std::vector<std::string> commands;
  502. std::string compileRuleVar = "CMAKE_";
  503. compileRuleVar += lang;
  504. compileRuleVar += "_COMPILE_OBJECT";
  505. std::string compileRule =
  506. m_Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  507. cmSystemTools::ExpandListArgument(compileRule, commands);
  508. // Expand placeholders in the commands.
  509. for(std::vector<std::string>::iterator i = commands.begin();
  510. i != commands.end(); ++i)
  511. {
  512. this->ExpandRuleVariables(*i,
  513. lang,
  514. 0, // no objects
  515. 0, // no target
  516. 0, // no link libs
  517. sourceFile.c_str(),
  518. objectFile.c_str(),
  519. flags.c_str());
  520. }
  521. // Write the rule.
  522. std::string buildComment = lang;
  523. buildComment += " object";
  524. this->OutputMakeRule(ruleFileStream, buildComment.c_str(), obj.c_str(),
  525. depends, commands);
  526. }
  527. }
  528. //----------------------------------------------------------------------------
  529. void cmLocalUnixMakefileGenerator2::WriteDisclaimer(std::ostream& os)
  530. {
  531. os
  532. << "# CMAKE generated file: DO NOT EDIT!\n"
  533. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  534. << " Generator, CMake Version "
  535. << cmMakefile::GetMajorVersion() << "."
  536. << cmMakefile::GetMinorVersion() << "\n\n";
  537. }
  538. //----------------------------------------------------------------------------
  539. void
  540. cmLocalUnixMakefileGenerator2
  541. ::WriteExecutableRule(std::ostream& ruleFileStream,
  542. const char* ruleFileName,
  543. const cmTarget& target,
  544. std::vector<std::string>& objects)
  545. {
  546. std::vector<std::string> commands;
  547. // Build list of dependencies. TODO: depend on other targets.
  548. std::vector<std::string> depends;
  549. for(std::vector<std::string>::const_iterator obj = objects.begin();
  550. obj != objects.end(); ++obj)
  551. {
  552. depends.push_back(*obj);
  553. }
  554. // Add dependencies on libraries that will be linked.
  555. std::set<cmStdString> emitted;
  556. emitted.insert(target.GetName());
  557. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  558. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  559. lib != tlibs.end(); ++lib)
  560. {
  561. // Don't emit the same library twice for this target.
  562. if(emitted.insert(lib->first).second)
  563. {
  564. // Add this dependency.
  565. this->AppendLibDepend(depends, lib->first.c_str());
  566. }
  567. }
  568. depends.push_back(ruleFileName);
  569. // Construct the full path to the executable that will be generated.
  570. std::string targetFullPath = m_ExecutableOutputPath;
  571. if(targetFullPath.length() == 0)
  572. {
  573. targetFullPath = m_Makefile->GetCurrentOutputDirectory();
  574. if(targetFullPath.size() && targetFullPath[targetFullPath.size()-1] != '/')
  575. {
  576. targetFullPath += "/";
  577. }
  578. }
  579. #ifdef __APPLE__
  580. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  581. {
  582. // Make bundle directories
  583. targetFullPath += name;
  584. targetFullPath += ".app/Contents/MacOS/";
  585. }
  586. #endif
  587. targetFullPath += target.GetName();
  588. targetFullPath += cmSystemTools::GetExecutableExtension();
  589. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  590. // Get the language to use for linking this executable.
  591. const char* linkLanguage =
  592. target.GetLinkerLanguage(this->GetGlobalGenerator());
  593. // Build a list of linker flags.
  594. std::string linkFlags;
  595. // Add flags to create an executable.
  596. this->AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  597. if(target.GetPropertyAsBool("WIN32_EXECUTABLE"))
  598. {
  599. this->AppendFlags(linkFlags,
  600. m_Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  601. }
  602. else
  603. {
  604. this->AppendFlags(linkFlags,
  605. m_Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  606. }
  607. // Add language-specific flags.
  608. this->AddLanguageFlags(linkFlags, linkLanguage);
  609. // Add flags to deal with shared libraries. Any library being
  610. // linked in might be shared, so always use shared flags for an
  611. // executable.
  612. this->AddSharedFlags(linkFlags, linkLanguage, true);
  613. // Add target-specific flags.
  614. this->AppendFlags(linkFlags, target.GetProperty("LINK_FLAGS"));
  615. // TODO: Pre-build and pre-link rules.
  616. // Construct the main link rule.
  617. std::string linkRuleVar = "CMAKE_";
  618. linkRuleVar += linkLanguage;
  619. linkRuleVar += "_LINK_EXECUTABLE";
  620. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  621. cmSystemTools::ExpandListArgument(linkRule, commands);
  622. // TODO: Post-build rules.
  623. // Collect up flags to link in needed libraries.
  624. cmOStringStream linklibs;
  625. this->OutputLinkLibraries(linklibs, 0, target);
  626. // Construct object file lists that may be needed to expand the
  627. // rule. TODO: Store these in make variables with line continuation
  628. // to avoid excessively long lines.
  629. std::string objs;
  630. std::string objsQuoted;
  631. const char* space = "";
  632. for(std::vector<std::string>::iterator i = objects.begin();
  633. i != objects.end(); ++i)
  634. {
  635. objs += space;
  636. objs += *i;
  637. objsQuoted += space;
  638. objsQuoted += "\"";
  639. objsQuoted += *i;
  640. objsQuoted += "\"";
  641. space = " ";
  642. }
  643. // Expand placeholders in the commands.
  644. for(std::vector<std::string>::iterator i = commands.begin();
  645. i != commands.end(); ++i)
  646. {
  647. this->ExpandRuleVariables(*i,
  648. linkLanguage,
  649. objs.c_str(),
  650. targetFullPath.c_str(),
  651. linklibs.str().c_str(),
  652. 0,
  653. 0,
  654. /*flags.c_str() why?*/ "",
  655. 0,
  656. 0,
  657. 0,
  658. linkFlags.c_str());
  659. }
  660. // Write the build rule.
  661. std::string buildComment = linkLanguage;
  662. buildComment += " executable";
  663. this->OutputMakeRule(ruleFileStream, buildComment.c_str(),
  664. targetFullPath.c_str(),
  665. depends, commands);
  666. // TODO: Add "local" target and canonical target name as rules.
  667. // Write the requires rule.
  668. {
  669. std::vector<std::string> depends;
  670. std::vector<std::string> commands;
  671. std::string reqComment = "requirements for ";
  672. reqComment += target.GetName();
  673. std::string reqTarget = target.GetName();
  674. reqTarget += ".requires";
  675. depends.push_back(targetFullPath);
  676. this->OutputMakeRule(ruleFileStream, reqComment.c_str(), reqTarget.c_str(),
  677. depends, commands);
  678. }
  679. }
  680. //----------------------------------------------------------------------------
  681. void
  682. cmLocalUnixMakefileGenerator2
  683. ::WriteStaticLibraryRule(std::ostream& ruleFileStream,
  684. const char* ruleFileName,
  685. const cmTarget& target,
  686. std::vector<std::string>& objects)
  687. {
  688. const char* linkLanguage =
  689. target.GetLinkerLanguage(this->GetGlobalGenerator());
  690. std::string linkRuleVar = "CMAKE_";
  691. linkRuleVar += linkLanguage;
  692. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  693. std::string extraFlags;
  694. this->AppendFlags(extraFlags, target.GetProperty("STATIC_LIBRARY_FLAGS"));
  695. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  696. linkRuleVar.c_str(), extraFlags.c_str());
  697. }
  698. //----------------------------------------------------------------------------
  699. void
  700. cmLocalUnixMakefileGenerator2
  701. ::WriteSharedLibraryRule(std::ostream& ruleFileStream,
  702. const char* ruleFileName,
  703. const cmTarget& target,
  704. std::vector<std::string>& objects)
  705. {
  706. const char* linkLanguage =
  707. target.GetLinkerLanguage(this->GetGlobalGenerator());
  708. std::string linkRuleVar = "CMAKE_";
  709. linkRuleVar += linkLanguage;
  710. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  711. std::string extraFlags;
  712. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  713. this->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  714. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  715. {
  716. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  717. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  718. i != sources.end(); ++i)
  719. {
  720. if((*i)->GetSourceExtension() == "def")
  721. {
  722. extraFlags += " ";
  723. extraFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  724. extraFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  725. }
  726. }
  727. }
  728. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  729. linkRuleVar.c_str(), extraFlags.c_str());
  730. }
  731. //----------------------------------------------------------------------------
  732. void
  733. cmLocalUnixMakefileGenerator2
  734. ::WriteModuleLibraryRule(std::ostream& ruleFileStream,
  735. const char* ruleFileName,
  736. const cmTarget& target,
  737. std::vector<std::string>& objects)
  738. {
  739. const char* linkLanguage =
  740. target.GetLinkerLanguage(this->GetGlobalGenerator());
  741. std::string linkRuleVar = "CMAKE_";
  742. linkRuleVar += linkLanguage;
  743. linkRuleVar += "_CREATE_SHARED_MODULE";
  744. std::string extraFlags;
  745. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  746. this->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  747. // TODO: Should .def files be supported here also?
  748. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  749. linkRuleVar.c_str(), extraFlags.c_str());
  750. }
  751. //----------------------------------------------------------------------------
  752. void
  753. cmLocalUnixMakefileGenerator2
  754. ::WriteLibraryRule(std::ostream& ruleFileStream,
  755. const char* ruleFileName,
  756. const cmTarget& target,
  757. std::vector<std::string>& objects,
  758. const char* linkRuleVar,
  759. const char* extraFlags)
  760. {
  761. std::vector<std::string> commands;
  762. // Build list of dependencies. TODO: depend on other targets.
  763. std::vector<std::string> depends;
  764. for(std::vector<std::string>::const_iterator obj = objects.begin();
  765. obj != objects.end(); ++obj)
  766. {
  767. depends.push_back(*obj);
  768. }
  769. depends.push_back(ruleFileName);
  770. const char* linkLanguage =
  771. target.GetLinkerLanguage(this->GetGlobalGenerator());
  772. std::string linkFlags;
  773. this->AppendFlags(linkFlags, extraFlags);
  774. std::string targetName;
  775. std::string targetNameSO;
  776. std::string targetNameReal;
  777. std::string targetNameBase;
  778. this->GetLibraryNames(target.GetName(), target,
  779. targetName, targetNameSO,
  780. targetNameReal, targetNameBase);
  781. std::string outpath;
  782. std::string outdir;
  783. if(m_UseRelativePaths)
  784. {
  785. outdir = this->ConvertToRelativeOutputPath(m_LibraryOutputPath.c_str());
  786. }
  787. else
  788. {
  789. outdir = m_LibraryOutputPath;
  790. }
  791. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  792. {
  793. outpath = "\"`cd ";
  794. }
  795. outpath += outdir;
  796. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  797. {
  798. outpath += ";pwd`\"/";
  799. }
  800. if(outdir.size() == 0 && m_UseRelativePaths && !m_WindowsShell)
  801. {
  802. outpath = "\"`pwd`\"/";
  803. }
  804. // The full path versions of the names.
  805. std::string targetFullPath = outpath + targetName;
  806. std::string targetFullPathSO = outpath + targetNameSO;
  807. std::string targetFullPathReal = outpath + targetNameReal;
  808. std::string targetFullPathBase = outpath + targetNameBase;
  809. // If not using relative paths then the output path needs to be
  810. // converted here
  811. if(!m_UseRelativePaths)
  812. {
  813. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  814. targetFullPathSO = this->ConvertToRelativeOutputPath(targetFullPathSO.c_str());
  815. targetFullPathReal = this->ConvertToRelativeOutputPath(targetFullPathReal.c_str());
  816. targetFullPathBase = this->ConvertToRelativeOutputPath(targetFullPathBase.c_str());
  817. }
  818. // Add a command to remove any existing files for this library.
  819. std::string remove = "$(CMAKE_COMMAND) -E remove -f ";
  820. remove += targetFullPathReal;
  821. if(targetFullPathSO != targetFullPathReal)
  822. {
  823. remove += " ";
  824. remove += targetFullPathSO;
  825. }
  826. if(targetFullPath != targetFullPathSO &&
  827. targetFullPath != targetFullPathReal)
  828. {
  829. remove += " ";
  830. remove += targetFullPath;
  831. }
  832. commands.push_back(remove);
  833. // TODO: Pre-build and pre-link rules.
  834. // Construct the main link rule.
  835. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar);
  836. cmSystemTools::ExpandListArgument(linkRule, commands);
  837. // Add a rule to create necessary symlinks for the library.
  838. if(targetFullPath != targetFullPathReal)
  839. {
  840. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  841. symlink += targetFullPathReal;
  842. symlink += " ";
  843. symlink += targetFullPathSO;
  844. symlink += " ";
  845. symlink += targetFullPath;
  846. commands.push_back(symlink);
  847. }
  848. // TODO: Post-build rules.
  849. // Collect up flags to link in needed libraries.
  850. cmOStringStream linklibs;
  851. this->OutputLinkLibraries(linklibs, target.GetName(), target);
  852. // Construct object file lists that may be needed to expand the
  853. // rule. TODO: Store these in make variables with line continuation
  854. // to avoid excessively long lines.
  855. std::string objs;
  856. std::string objsQuoted;
  857. const char* space = "";
  858. for(std::vector<std::string>::iterator i = objects.begin();
  859. i != objects.end(); ++i)
  860. {
  861. objs += space;
  862. objs += *i;
  863. objsQuoted += space;
  864. objsQuoted += "\"";
  865. objsQuoted += *i;
  866. objsQuoted += "\"";
  867. space = " ";
  868. }
  869. // Expand placeholders in the commands.
  870. for(std::vector<std::string>::iterator i = commands.begin();
  871. i != commands.end(); ++i)
  872. {
  873. this->ExpandRuleVariables(*i,
  874. linkLanguage,
  875. objs.c_str(),
  876. targetFullPathReal.c_str(),
  877. linklibs.str().c_str(),
  878. 0, 0, 0, objsQuoted.c_str(),
  879. targetFullPathBase.c_str(),
  880. targetNameSO.c_str(),
  881. linkFlags.c_str());
  882. }
  883. // Write the build rule.
  884. std::string buildComment = linkLanguage;
  885. buildComment += " static library";
  886. this->OutputMakeRule(ruleFileStream, buildComment.c_str(),
  887. targetFullPath.c_str(),
  888. depends, commands);
  889. // TODO: Add "local" target and canonical target name as rules.
  890. // Write the requires rule.
  891. {
  892. std::vector<std::string> depends;
  893. std::vector<std::string> commands;
  894. std::string reqComment = "requirements for ";
  895. reqComment += target.GetName();
  896. std::string reqTarget = target.GetName();
  897. reqTarget += ".requires";
  898. depends.push_back(targetFullPath);
  899. this->OutputMakeRule(ruleFileStream, reqComment.c_str(), reqTarget.c_str(),
  900. depends, commands);
  901. }
  902. }
  903. //----------------------------------------------------------------------------
  904. std::string
  905. cmLocalUnixMakefileGenerator2
  906. ::GetTargetDirectory(const cmTarget& target)
  907. {
  908. std::string dir = target.GetName();
  909. dir += ".dir";
  910. return dir;
  911. }
  912. //----------------------------------------------------------------------------
  913. std::string
  914. cmLocalUnixMakefileGenerator2
  915. ::GetObjectFileName(const cmTarget& target,
  916. const cmSourceFile& source)
  917. {
  918. // If the full path to the source file includes this directory,
  919. // we want to use the relative path for the filename of the
  920. // object file. Otherwise, we will use just the filename
  921. // portion.
  922. std::string objectName;
  923. if((cmSystemTools::GetFilenamePath(
  924. source.GetFullPath()).find(
  925. m_Makefile->GetCurrentDirectory()) == 0)
  926. || (cmSystemTools::GetFilenamePath(
  927. source.GetFullPath()).find(
  928. m_Makefile->GetCurrentOutputDirectory()) == 0))
  929. {
  930. objectName = source.GetSourceName();
  931. }
  932. else
  933. {
  934. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  935. }
  936. // Append the object file extension.
  937. objectName +=
  938. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  939. source.GetSourceExtension().c_str());
  940. // Convert to a safe name.
  941. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  942. // Prepend the target directory.
  943. std::string obj = this->GetTargetDirectory(target);
  944. obj += "/";
  945. obj += objectName;
  946. return obj;
  947. }
  948. //----------------------------------------------------------------------------
  949. const char*
  950. cmLocalUnixMakefileGenerator2
  951. ::GetSourceFileLanguage(const cmSourceFile& source)
  952. {
  953. // Identify the language of the source file.
  954. return (m_GlobalGenerator
  955. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  956. }
  957. //----------------------------------------------------------------------------
  958. std::string
  959. cmLocalUnixMakefileGenerator2
  960. ::ConvertToFullPath(const std::string& localPath)
  961. {
  962. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  963. dir += "/";
  964. dir += localPath;
  965. return dir;
  966. }
  967. //----------------------------------------------------------------------------
  968. void cmLocalUnixMakefileGenerator2::AddLanguageFlags(std::string& flags,
  969. const char* lang)
  970. {
  971. // Add language-specific flags.
  972. std::string flagsVar = "CMAKE_";
  973. flagsVar += lang;
  974. flagsVar += "_FLAGS";
  975. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  976. }
  977. //----------------------------------------------------------------------------
  978. void cmLocalUnixMakefileGenerator2::AddSharedFlags(std::string& flags,
  979. const char* lang,
  980. bool shared)
  981. {
  982. std::string flagsVar;
  983. // Add flags for dealing with shared libraries for this language.
  984. if(shared)
  985. {
  986. flagsVar = "CMAKE_SHARED_LIBRARY_";
  987. flagsVar += lang;
  988. flagsVar += "_FLAGS";
  989. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  990. }
  991. // Add flags specific to shared builds.
  992. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  993. {
  994. flagsVar = "CMAKE_SHARED_BUILD_";
  995. flagsVar += lang;
  996. flagsVar += "_FLAGS";
  997. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  998. }
  999. }
  1000. //----------------------------------------------------------------------------
  1001. void cmLocalUnixMakefileGenerator2::AddConfigVariableFlags(std::string& flags,
  1002. const char* var)
  1003. {
  1004. // Add the flags from the variable itself.
  1005. std::string flagsVar = var;
  1006. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1007. // Add the flags from the build-type specific variable.
  1008. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1009. if(buildType && *buildType)
  1010. {
  1011. flagsVar += "_";
  1012. flagsVar += cmSystemTools::UpperCase(buildType);
  1013. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1014. }
  1015. }
  1016. //----------------------------------------------------------------------------
  1017. void cmLocalUnixMakefileGenerator2::AppendFlags(std::string& flags,
  1018. const char* newFlags)
  1019. {
  1020. if(newFlags && *newFlags)
  1021. {
  1022. if(flags.size())
  1023. {
  1024. flags += " ";
  1025. }
  1026. flags += newFlags;
  1027. }
  1028. }
  1029. //----------------------------------------------------------------------------
  1030. void
  1031. cmLocalUnixMakefileGenerator2
  1032. ::AppendLibDepend(std::vector<std::string>& depends, const char* name)
  1033. {
  1034. // There are a few cases for the name of the target:
  1035. // - CMake target in this directory: depend on it.
  1036. // - CMake target in another directory: depend and add jump-and-build.
  1037. // - Full path to an outside file: depend on it.
  1038. // - Other format (like -lm): do nothing.
  1039. // If it is a CMake target there will be a definition for it.
  1040. std::string dirVar = name;
  1041. dirVar += "_CMAKE_PATH";
  1042. const char* dir = m_Makefile->GetDefinition(dirVar.c_str());
  1043. if(dir && *dir)
  1044. {
  1045. // This is a CMake target somewhere in this project.
  1046. bool jumpAndBuild = false;
  1047. // Get the path to the library.
  1048. std::string libPath;
  1049. if(this->SamePath(m_Makefile->GetCurrentOutputDirectory(), dir))
  1050. {
  1051. // The target is in the current directory so this makefile will
  1052. // know about it already.
  1053. libPath = m_LibraryOutputPath;
  1054. }
  1055. else
  1056. {
  1057. // The target is in another directory. Get the path to it.
  1058. if(m_LibraryOutputPath.size())
  1059. {
  1060. libPath = m_LibraryOutputPath;
  1061. }
  1062. else
  1063. {
  1064. libPath = dir;
  1065. libPath += "/";
  1066. }
  1067. // We need to add a jump-and-build rule for this library.
  1068. jumpAndBuild = true;
  1069. }
  1070. // Add the name of the library's file. This depends on the type
  1071. // of the library.
  1072. std::string typeVar = name;
  1073. typeVar += "_LIBRARY_TYPE";
  1074. std::string libType = m_Makefile->GetSafeDefinition(typeVar.c_str());
  1075. std::string prefix;
  1076. std::string suffix;
  1077. if(libType == "SHARED")
  1078. {
  1079. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1080. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1081. }
  1082. else if(libType == "MODULE")
  1083. {
  1084. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1085. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1086. }
  1087. else if(libType == "STATIC")
  1088. {
  1089. prefix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1090. suffix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1091. }
  1092. libPath += prefix;
  1093. libPath += name;
  1094. libPath += suffix;
  1095. if(jumpAndBuild)
  1096. {
  1097. // We need to add a jump-and-build rule for this library.
  1098. cmLocalUnixMakefileGenerator2::RemoteTarget rt;
  1099. rt.m_BuildDirectory = dir;
  1100. rt.m_FilePath =libPath;
  1101. m_JumpAndBuild[name] = rt;
  1102. }
  1103. // Add a dependency on the library.
  1104. depends.push_back(this->ConvertToRelativeOutputPath(libPath.c_str()));
  1105. }
  1106. else
  1107. {
  1108. // This is not a CMake target. If it exists and is a full path we
  1109. // can depend on it.
  1110. if(cmSystemTools::FileExists(name) && cmSystemTools::FileIsFullPath(name))
  1111. {
  1112. depends.push_back(this->ConvertToRelativeOutputPath(name));
  1113. }
  1114. }
  1115. }
  1116. //----------------------------------------------------------------------------
  1117. void
  1118. cmLocalUnixMakefileGenerator2
  1119. ::AppendRecursiveMake(std::string& cmd, const char* file, const char* tgt)
  1120. {
  1121. // Call make on the given file.
  1122. cmd += "$(MAKE) -f ";
  1123. cmd += file;
  1124. // Pass down verbosity level.
  1125. cmd += " $(MAKESILENT) ";
  1126. // Most unix makes will pass the command line flags to make down to
  1127. // sub-invoked makes via an environment variable. However, some
  1128. // makes do not support that, so you have to pass the flags
  1129. // explicitly.
  1130. if(m_PassMakeflags)
  1131. {
  1132. cmd += "-$(MAKEFLAGS) ";
  1133. }
  1134. // Add the target.
  1135. cmd += tgt;
  1136. }
  1137. //----------------------------------------------------------------------------
  1138. void
  1139. cmLocalUnixMakefileGenerator2
  1140. ::WriteJumpAndBuildRules(std::ostream& makefileStream)
  1141. {
  1142. std::vector<std::string> depends;
  1143. std::vector<std::string> commands;
  1144. commands.push_back("");
  1145. for(std::map<cmStdString, RemoteTarget>::iterator
  1146. jump = m_JumpAndBuild.begin(); jump != m_JumpAndBuild.end(); ++jump)
  1147. {
  1148. const cmLocalUnixMakefileGenerator2::RemoteTarget& rt = jump->second;
  1149. std::string& cmd = commands[0];
  1150. if(m_WindowsShell)
  1151. {
  1152. // TODO: implement windows version.
  1153. cmd = "";
  1154. }
  1155. else
  1156. {
  1157. cmd = "cd ";
  1158. cmd += this->ConvertToOutputForExisting(rt.m_BuildDirectory.c_str());
  1159. cmd += "; ";
  1160. std::string tgt = jump->first;
  1161. tgt += ".requires";
  1162. this->AppendRecursiveMake(cmd, "Makefile2", tgt.c_str());
  1163. }
  1164. this->OutputMakeRule(makefileStream, "jump rule for",
  1165. rt.m_FilePath.c_str(), depends, commands);
  1166. }
  1167. }
  1168. //----------------------------------------------------------------------------
  1169. bool
  1170. cmLocalUnixMakefileGenerator2
  1171. ::ScanDependencies(std::vector<std::string> const& args)
  1172. {
  1173. // Format of arguments is:
  1174. // $(CMAKE_COMMAND), cmake_depends, <lang>, <obj>, <src>, [include-flags]
  1175. // The caller has ensured that all required arguments exist.
  1176. // The file to which to write dependencies.
  1177. const char* objFile = args[3].c_str();
  1178. // The source file at which to start the scan.
  1179. const char* srcFile = args[4].c_str();
  1180. // Convert the include flags to full paths.
  1181. std::vector<std::string> includes;
  1182. for(unsigned int i=5; i < args.size(); ++i)
  1183. {
  1184. if(args[i].substr(0, 2) == "-I")
  1185. {
  1186. // Get the include path without the -I flag.
  1187. std::string inc = args[i].substr(2);
  1188. includes.push_back(cmSystemTools::CollapseFullPath(inc.c_str()));
  1189. }
  1190. }
  1191. // Dispatch the scan for each language.
  1192. std::string const& lang = args[2];
  1193. if(lang == "C" || lang == "CXX")
  1194. {
  1195. return cmLocalUnixMakefileGenerator2::ScanDependenciesC(objFile, srcFile,
  1196. includes);
  1197. }
  1198. return false;
  1199. }
  1200. //----------------------------------------------------------------------------
  1201. void
  1202. cmLocalUnixMakefileGenerator2ScanDependenciesC(
  1203. std::ifstream& fin,
  1204. std::set<cmStdString>& encountered,
  1205. std::queue<cmStdString>& unscanned)
  1206. {
  1207. // Regular expression to identify C preprocessor include directives.
  1208. cmsys::RegularExpression
  1209. includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  1210. // Read one line at a time.
  1211. std::string line;
  1212. while(cmSystemTools::GetLineFromStream(fin, line))
  1213. {
  1214. // Match include directives.
  1215. if(includeLine.find(line.c_str()))
  1216. {
  1217. // Get the file being included.
  1218. std::string includeFile = includeLine.match(1);
  1219. // Queue the file if it has not yet been encountered.
  1220. if(encountered.find(includeFile) == encountered.end())
  1221. {
  1222. encountered.insert(includeFile);
  1223. unscanned.push(includeFile);
  1224. }
  1225. }
  1226. }
  1227. }
  1228. //----------------------------------------------------------------------------
  1229. bool
  1230. cmLocalUnixMakefileGenerator2
  1231. ::ScanDependenciesC(const char* objFile, const char* srcFile,
  1232. std::vector<std::string> const& includes)
  1233. {
  1234. // Walk the dependency graph starting with the source file.
  1235. std::set<cmStdString> dependencies;
  1236. std::set<cmStdString> encountered;
  1237. std::set<cmStdString> scanned;
  1238. std::queue<cmStdString> unscanned;
  1239. unscanned.push(srcFile);
  1240. encountered.insert(srcFile);
  1241. while(!unscanned.empty())
  1242. {
  1243. // Get the next file to scan.
  1244. std::string fname = unscanned.front();
  1245. unscanned.pop();
  1246. // If not a full path, find the file in the include path.
  1247. std::string fullName;
  1248. if(cmSystemTools::FileIsFullPath(fname.c_str()))
  1249. {
  1250. fullName = fname;
  1251. }
  1252. else
  1253. {
  1254. for(std::vector<std::string>::const_iterator i = includes.begin();
  1255. i != includes.end(); ++i)
  1256. {
  1257. std::string temp = *i;
  1258. temp += "/";
  1259. temp += fname;
  1260. if(cmSystemTools::FileExists(temp.c_str()))
  1261. {
  1262. fullName = temp;
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. // Scan the file if it has not been scanned already.
  1268. if(scanned.find(fullName) == scanned.end())
  1269. {
  1270. // Record scanned files.
  1271. scanned.insert(fullName);
  1272. // Try to scan the file. Just leave it out if we cannot find
  1273. // it.
  1274. std::ifstream fin(fullName.c_str());
  1275. if(fin)
  1276. {
  1277. // Add this file as a dependency.
  1278. dependencies.insert(fullName);
  1279. // Scan this file for new dependencies.
  1280. cmLocalUnixMakefileGenerator2ScanDependenciesC(fin, encountered,
  1281. unscanned);
  1282. }
  1283. }
  1284. }
  1285. // Write the dependencies to the output file.
  1286. std::string depMakeFile = objFile;
  1287. depMakeFile += ".depends.make";
  1288. std::ofstream fout(depMakeFile.c_str());
  1289. fout << "# Dependencies for " << objFile << std::endl;
  1290. for(std::set<cmStdString>::iterator i=dependencies.begin();
  1291. i != dependencies.end(); ++i)
  1292. {
  1293. fout << objFile << " : " << i->c_str() << std::endl;
  1294. }
  1295. fout << std::endl;
  1296. fout << "# Dependencies for " << objFile << ".depends" << std::endl;
  1297. for(std::set<cmStdString>::iterator i=dependencies.begin();
  1298. i != dependencies.end(); ++i)
  1299. {
  1300. fout << objFile << ".depends : " << i->c_str() << std::endl;
  1301. }
  1302. return true;
  1303. }