cmLocalUnixMakefileGenerator2.cxx 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  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. // Open the output file.
  55. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  56. makefileName += "/Makefile2";
  57. std::ofstream makefileStream(makefileName.c_str());
  58. if(!makefileStream)
  59. {
  60. cmSystemTools::Error("Error can not open for write: ",
  61. makefileName.c_str());
  62. cmSystemTools::ReportLastSystemError("");
  63. return;
  64. }
  65. // Write the do not edit header.
  66. this->WriteDisclaimer(makefileStream);
  67. // Write standard variables to the makefile.
  68. this->WriteMakeVariables(makefileStream);
  69. // Write special targets that belong at the top of the file.
  70. this->WriteSpecialTargetsTop(makefileStream);
  71. // Write the directory-level build rules.
  72. this->WriteAllRule(makefileStream);
  73. // Write include statements to get rules for each target.
  74. this->WriteTargetIncludes(makefileStream);
  75. // Write jump-and-build rules that were recorded in the map.
  76. this->WriteJumpAndBuildRules(makefileStream);
  77. // Write special targets that belong at the bottom of the file.
  78. this->WriteSpecialTargetsBottom(makefileStream);
  79. }
  80. //----------------------------------------------------------------------------
  81. void cmLocalUnixMakefileGenerator2::GenerateCMakefile()
  82. {
  83. std::string makefileName = m_Makefile->GetStartOutputDirectory();
  84. makefileName += "/Makefile2";
  85. std::string cmakefileName = makefileName;
  86. cmakefileName += ".cmake";
  87. // Open the output file.
  88. std::ofstream cmakefileStream(cmakefileName.c_str());
  89. if(!cmakefileStream)
  90. {
  91. cmSystemTools::Error("Error can not open for write: ",
  92. cmakefileName.c_str());
  93. cmSystemTools::ReportLastSystemError("");
  94. return;
  95. }
  96. // Write the do not edit header.
  97. this->WriteDisclaimer(cmakefileStream);
  98. // Get the list of files contributing to this generation step.
  99. // Sort the list and remove duplicates.
  100. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  101. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  102. std::vector<std::string>::iterator new_end = std::unique(lfiles.begin(),
  103. lfiles.end());
  104. lfiles.erase(new_end, lfiles.end());
  105. // Save the list to the cmake file.
  106. cmakefileStream
  107. << "# The corresponding makefile\n"
  108. << "# \"" << makefileName << "\"\n"
  109. << "# was generated from the following files:\n"
  110. << "SET(CMAKE_MAKEFILE_DEPENDS\n"
  111. << " \"" << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\"\n";
  112. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  113. i != lfiles.end(); ++i)
  114. {
  115. cmakefileStream
  116. << " \"" << i->c_str() << "\"\n";
  117. }
  118. cmakefileStream
  119. << " )\n\n";
  120. // Set the corresponding makefile in the cmake file.
  121. cmakefileStream
  122. << "# The corresponding makefile is:\n"
  123. << "SET(CMAKE_MAKEFILE_OUTPUTS\n"
  124. << " \"" << makefileName.c_str() << "\"\n"
  125. << " )\n\n";
  126. // Set the set of files to check for dependency integrity.
  127. cmakefileStream
  128. << "# The set of files whose dependency integrity should be checked:\n"
  129. << "SET(CMAKE_DEPENDS_CHECK\n";
  130. for(std::set<cmStdString>::const_iterator i = m_CheckDependFiles.begin();
  131. i != m_CheckDependFiles.end(); ++i)
  132. {
  133. cmakefileStream
  134. << " \"" << i->c_str() << "\"\n";
  135. }
  136. cmakefileStream
  137. << " )\n";
  138. }
  139. //----------------------------------------------------------------------------
  140. void
  141. cmLocalUnixMakefileGenerator2
  142. ::GenerateTargetRuleFile(const cmTarget& target)
  143. {
  144. // Create a directory for this target.
  145. std::string dir = this->GetTargetDirectory(target);
  146. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  147. // First generate the object rule files. Save a list of all object
  148. // files for this target.
  149. std::vector<std::string> objects;
  150. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  151. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  152. source != sources.end(); ++source)
  153. {
  154. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  155. !(*source)->GetCustomCommand() &&
  156. !m_GlobalGenerator->IgnoreFile((*source)->GetSourceExtension().c_str()))
  157. {
  158. // Generate this object file's rule file.
  159. this->GenerateObjectRuleFile(target, *(*source));
  160. // Save the object file name.
  161. objects.push_back(this->GetObjectFileName(target, *(*source)));
  162. }
  163. }
  164. // Generate the build-time dependencies file for this target.
  165. std::string depBase = dir;
  166. depBase += "/";
  167. depBase += target.GetName();
  168. std::string depMakeFile = this->GenerateDependsMakeFile(depBase.c_str());
  169. // Open the rule file. This should be copy-if-different because the
  170. // rules may depend on this file itself.
  171. std::string ruleFileName = dir;
  172. ruleFileName += "/";
  173. ruleFileName += target.GetName();
  174. ruleFileName += ".make";
  175. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  176. cmGeneratedFileStream ruleFile(ruleFileNameFull.c_str());
  177. std::ostream& ruleFileStream = ruleFile.GetStream();
  178. if(!ruleFileStream)
  179. {
  180. // TODO: Produce error message that accounts for generated stream
  181. // .tmp.
  182. return;
  183. }
  184. this->WriteDisclaimer(ruleFileStream);
  185. ruleFileStream
  186. << "# Rule file for target " << target.GetName() << ".\n\n";
  187. // Include the dependencies for the target.
  188. ruleFileStream
  189. << "# Include any dependencies generated for this rule.\n"
  190. << m_IncludeDirective << " "
  191. << this->ConvertToOutputForExisting(depMakeFile.c_str()).c_str()
  192. << "\n\n";
  193. // Include the rule file for each object.
  194. if(!objects.empty())
  195. {
  196. ruleFileStream
  197. << "# Include make rules for object files.\n";
  198. for(std::vector<std::string>::const_iterator obj = objects.begin();
  199. obj != objects.end(); ++obj)
  200. {
  201. std::string objRuleFileName = *obj;
  202. objRuleFileName += ".make";
  203. ruleFileStream
  204. << m_IncludeDirective << " "
  205. << this->ConvertToOutputForExisting(objRuleFileName.c_str()).c_str()
  206. << "\n";
  207. }
  208. ruleFileStream
  209. << "\n";
  210. }
  211. // Write the dependency generation rule.
  212. {
  213. std::vector<std::string> depends;
  214. std::vector<std::string> no_commands;
  215. std::string depEcho = "Building dependencies for ";
  216. depEcho += target.GetName();
  217. depEcho += "...";
  218. std::string depTarget = dir;
  219. depTarget += "/";
  220. depTarget += target.GetName();
  221. depTarget += ".depends";
  222. for(std::vector<std::string>::const_iterator obj = objects.begin();
  223. obj != objects.end(); ++obj)
  224. {
  225. depends.push_back((*obj)+".depends");
  226. }
  227. depends.push_back(ruleFileName);
  228. this->WriteMakeRule(ruleFileStream, 0, depEcho.c_str(),
  229. depTarget.c_str(), depends, no_commands);
  230. }
  231. // Write the build rule.
  232. switch(target.GetType())
  233. {
  234. case cmTarget::STATIC_LIBRARY:
  235. this->WriteStaticLibraryRule(ruleFileStream, ruleFileName.c_str(),
  236. target, objects);
  237. break;
  238. case cmTarget::SHARED_LIBRARY:
  239. this->WriteSharedLibraryRule(ruleFileStream, ruleFileName.c_str(),
  240. target, objects);
  241. break;
  242. case cmTarget::MODULE_LIBRARY:
  243. this->WriteModuleLibraryRule(ruleFileStream, ruleFileName.c_str(),
  244. target, objects);
  245. break;
  246. case cmTarget::EXECUTABLE:
  247. this->WriteExecutableRule(ruleFileStream, ruleFileName.c_str(),
  248. target, objects);
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. //----------------------------------------------------------------------------
  255. void
  256. cmLocalUnixMakefileGenerator2
  257. ::GenerateObjectRuleFile(const cmTarget& target, const cmSourceFile& source)
  258. {
  259. // Identify the language of the source file.
  260. const char* lang = this->GetSourceFileLanguage(source);
  261. if(!lang)
  262. {
  263. // If language is not known, this is an error.
  264. cmSystemTools::Error("Source file \"", source.GetFullPath().c_str(),
  265. "\" has unknown type.");
  266. return;
  267. }
  268. // Get the full path name of the object file.
  269. std::string obj = this->GetObjectFileName(target, source);
  270. // The object file should be checked for dependency integrity.
  271. m_CheckDependFiles.insert(obj);
  272. // Create the directory containing the object file. This may be a
  273. // subdirectory under the target's directory.
  274. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  275. cmSystemTools::MakeDirectory(this->ConvertToFullPath(dir).c_str());
  276. // Generate the build-time dependencies file for this object file.
  277. std::string depMakeFile = this->GenerateDependsMakeFile(obj.c_str());
  278. // Open the rule file for writing. This should be copy-if-different
  279. // because the rules may depend on this file itself.
  280. std::string ruleFileName = obj;
  281. ruleFileName += ".make";
  282. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  283. cmGeneratedFileStream ruleFile(ruleFileNameFull.c_str());
  284. std::ostream& ruleFileStream = ruleFile.GetStream();
  285. if(!ruleFileStream)
  286. {
  287. // TODO: Produce error message that accounts for generated stream
  288. // .tmp.
  289. return;
  290. }
  291. this->WriteDisclaimer(ruleFileStream);
  292. ruleFileStream
  293. << "# Rule file for object file " << obj.c_str() << ".\n\n";
  294. // Include the dependencies for the target.
  295. ruleFileStream
  296. << "# Include any dependencies generated for this rule.\n"
  297. << m_IncludeDirective << " "
  298. << this->ConvertToOutputForExisting(depMakeFile.c_str()).c_str()
  299. << "\n\n";
  300. // Create the list of dependencies known at cmake time. These are
  301. // shared between the object file and dependency scanning rule.
  302. std::vector<std::string> depends;
  303. depends.push_back(source.GetFullPath());
  304. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  305. {
  306. std::vector<std::string> deps;
  307. cmSystemTools::ExpandListArgument(objectDeps, deps);
  308. for(std::vector<std::string>::iterator i = deps.begin();
  309. i != deps.end(); ++i)
  310. {
  311. depends.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  312. }
  313. }
  314. depends.push_back(ruleFileName);
  315. // Write the dependency generation rule.
  316. std::string depTarget = obj;
  317. depTarget += ".depends";
  318. {
  319. std::string depEcho = "Scanning ";
  320. depEcho += lang;
  321. depEcho += " dependencies of ";
  322. depEcho += this->ConvertToRelativeOutputPath(obj.c_str());
  323. depEcho += "...";
  324. // Add a command to call CMake to scan dependencies. CMake will
  325. // touch the corresponding depends file after scanning dependencies.
  326. cmOStringStream depCmd;
  327. // TODO: Account for source file properties and directory-level
  328. // definitions when scanning for dependencies.
  329. depCmd << "$(CMAKE_COMMAND) -E cmake_depends " << lang << " "
  330. << this->ConvertToRelativeOutputPath(obj.c_str()) << " "
  331. << this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  332. std::vector<std::string> includeDirs;
  333. this->GetIncludeDirectories(includeDirs);
  334. for(std::vector<std::string>::iterator i = includeDirs.begin();
  335. i != includeDirs.end(); ++i)
  336. {
  337. depCmd << " -I" << this->ConvertToRelativeOutputPath(i->c_str());
  338. }
  339. std::vector<std::string> commands;
  340. commands.push_back(depCmd.str());
  341. // Write the rule.
  342. this->WriteMakeRule(ruleFileStream, 0, depEcho.c_str(),
  343. depTarget.c_str(), depends, commands);
  344. }
  345. // Write the build rule.
  346. {
  347. // Build the set of compiler flags.
  348. std::string flags;
  349. // Add the export symbol definition for shared library objects.
  350. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  351. (target.GetType() == cmTarget::MODULE_LIBRARY));
  352. if(shared)
  353. {
  354. flags += "-D";
  355. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  356. {
  357. flags += custom_export_name;
  358. }
  359. else
  360. {
  361. std::string in = target.GetName();
  362. in += "_EXPORTS";
  363. flags += cmSystemTools::MakeCindentifier(in.c_str());
  364. }
  365. }
  366. // Add flags from source file properties.
  367. this->AppendFlags(flags, source.GetProperty("COMPILE_FLAGS"));
  368. // Add language-specific flags.
  369. this->AddLanguageFlags(flags, lang);
  370. // Add shared-library flags if needed.
  371. this->AddSharedFlags(flags, lang, shared);
  372. // Add include directory flags.
  373. this->AppendFlags(flags, this->GetIncludeFlags(lang));
  374. // Get the output paths for source and object files.
  375. std::string sourceFile =
  376. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  377. std::string objectFile =
  378. this->ConvertToRelativeOutputPath(obj.c_str());
  379. // Construct the compile rules.
  380. std::vector<std::string> commands;
  381. std::string compileRuleVar = "CMAKE_";
  382. compileRuleVar += lang;
  383. compileRuleVar += "_COMPILE_OBJECT";
  384. std::string compileRule =
  385. m_Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  386. cmSystemTools::ExpandListArgument(compileRule, commands);
  387. // Expand placeholders in the commands.
  388. for(std::vector<std::string>::iterator i = commands.begin();
  389. i != commands.end(); ++i)
  390. {
  391. this->ExpandRuleVariables(*i,
  392. lang,
  393. 0, // no objects
  394. 0, // no target
  395. 0, // no link libs
  396. sourceFile.c_str(),
  397. objectFile.c_str(),
  398. flags.c_str());
  399. }
  400. // Write the rule.
  401. std::string buildEcho = "Building ";
  402. buildEcho += lang;
  403. buildEcho += " object ";
  404. buildEcho += this->ConvertToRelativeOutputPath(obj.c_str());
  405. buildEcho += "...";
  406. this->WriteMakeRule(ruleFileStream, 0, buildEcho.c_str(),
  407. obj.c_str(), depends, commands);
  408. }
  409. }
  410. //----------------------------------------------------------------------------
  411. std::string
  412. cmLocalUnixMakefileGenerator2
  413. ::GenerateDependsMakeFile(const char* file)
  414. {
  415. // Check if the build-time dependencies file exists.
  416. std::string depMarkFile = file;
  417. depMarkFile += ".depends";
  418. std::string depMakeFile = depMarkFile;
  419. depMakeFile += ".make";
  420. std::string depMakeFileFull = this->ConvertToFullPath(depMakeFile);
  421. if(cmSystemTools::FileExists(depMakeFileFull.c_str()))
  422. {
  423. // The build-time dependencies file already exists. Check it.
  424. this->CheckDependencies(m_Makefile->GetStartOutputDirectory(), file);
  425. }
  426. else
  427. {
  428. // The build-time dependencies file does not exist. Create an
  429. // empty one.
  430. std::string depMarkFileFull = this->ConvertToFullPath(depMarkFile);
  431. this->WriteEmptyDependMakeFile(file, depMarkFileFull.c_str(),
  432. depMakeFileFull.c_str());
  433. }
  434. return depMakeFile;
  435. }
  436. //----------------------------------------------------------------------------
  437. void
  438. cmLocalUnixMakefileGenerator2
  439. ::WriteMakeRule(std::ostream& os,
  440. const char* comment,
  441. const char* preEcho,
  442. const char* target,
  443. const std::vector<std::string>& depends,
  444. const std::vector<std::string>& commands,
  445. const char* postEcho)
  446. {
  447. // Make sure there is a target.
  448. if(!target || !*target)
  449. {
  450. cmSystemTools::Error("No target for WriteMakeRule!");
  451. return;
  452. }
  453. std::string replace;
  454. // Write the comment describing the rule in the makefile.
  455. if(comment)
  456. {
  457. replace = comment;
  458. m_Makefile->ExpandVariablesInString(replace);
  459. std::string::size_type lpos = 0;
  460. std::string::size_type rpos;
  461. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  462. {
  463. os << "# " << replace.substr(lpos, rpos-lpos);
  464. lpos = rpos+1;
  465. }
  466. os << "# " << replace.substr(lpos) << "\n";
  467. }
  468. // Construct the left hand side of the rule.
  469. replace = target;
  470. m_Makefile->ExpandVariablesInString(replace);
  471. std::string tgt = this->ConvertToRelativeOutputPath(replace.c_str());
  472. tgt = this->ConvertToMakeTarget(tgt.c_str());
  473. const char* space = "";
  474. if(tgt.size() == 1)
  475. {
  476. // Add a space before the ":" to avoid drive letter confusion on
  477. // Windows.
  478. space = " ";
  479. }
  480. // Write the rule.
  481. if(depends.empty())
  482. {
  483. // No dependencies. The commands will always run.
  484. os << tgt.c_str() << space << ":\n";
  485. }
  486. else
  487. {
  488. // Split dependencies into multiple rule lines. This allows for
  489. // very long dependency lists even on older make implementations.
  490. for(std::vector<std::string>::const_iterator dep = depends.begin();
  491. dep != depends.end(); ++dep)
  492. {
  493. replace = *dep;
  494. m_Makefile->ExpandVariablesInString(replace);
  495. replace = this->ConvertToRelativeOutputPath(replace.c_str());
  496. replace = this->ConvertToMakeTarget(replace.c_str());
  497. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  498. }
  499. }
  500. // Write the list of commands.
  501. bool first = true;
  502. for(std::vector<std::string>::const_iterator i = commands.begin();
  503. i != commands.end(); ++i)
  504. {
  505. replace = *i;
  506. m_Makefile->ExpandVariablesInString(replace);
  507. if(first && preEcho)
  508. {
  509. this->OutputEcho(os, preEcho);
  510. }
  511. os << "\t" << replace.c_str() << "\n";
  512. first = false;
  513. }
  514. if(postEcho)
  515. {
  516. this->OutputEcho(os, postEcho);
  517. }
  518. os << "\n";
  519. }
  520. //----------------------------------------------------------------------------
  521. void cmLocalUnixMakefileGenerator2::WriteDivider(std::ostream& os)
  522. {
  523. os
  524. << "#======================================"
  525. << "=======================================\n";
  526. }
  527. //----------------------------------------------------------------------------
  528. void cmLocalUnixMakefileGenerator2::WriteDisclaimer(std::ostream& os)
  529. {
  530. os
  531. << "# CMAKE generated file: DO NOT EDIT!\n"
  532. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  533. << " Generator, CMake Version "
  534. << cmMakefile::GetMajorVersion() << "."
  535. << cmMakefile::GetMinorVersion() << "\n\n";
  536. }
  537. //----------------------------------------------------------------------------
  538. void
  539. cmLocalUnixMakefileGenerator2
  540. ::WriteMakeVariables(std::ostream& makefileStream)
  541. {
  542. this->WriteDivider(makefileStream);
  543. makefileStream
  544. << "# Set environment variables for the build.\n"
  545. << "\n";
  546. if(m_WindowsShell)
  547. {
  548. makefileStream
  549. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  550. << "NULL=\n"
  551. << "!ELSE\n"
  552. << "NULL=nul\n"
  553. << "!ENDIF\n";
  554. }
  555. else
  556. {
  557. makefileStream
  558. << "# The shell in which to execute make rules.\n"
  559. << "SHELL = /bin/sh\n"
  560. << "\n";
  561. }
  562. std::string cmakecommand =
  563. this->ConvertToOutputForExisting(
  564. m_Makefile->GetRequiredDefinition("CMAKE_COMMAND"));
  565. makefileStream
  566. << "# The CMake executable.\n"
  567. << "CMAKE_COMMAND = " << cmakecommand.c_str() << "\n"
  568. << "\n";
  569. makefileStream
  570. << "# The command to remove a file.\n"
  571. << "RM = " << cmakecommand.c_str() << " -E remove -f\n"
  572. << "\n";
  573. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  574. {
  575. makefileStream
  576. << "# The program to use to edit the cache.\n"
  577. << "CMAKE_EDIT_COMMAND = "
  578. << (this->ConvertToOutputForExisting(
  579. m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  580. << "\n";
  581. }
  582. makefileStream
  583. << "# The source directory corresponding to this makefile.\n"
  584. << "CMAKE_CURRENT_SOURCE = "
  585. << this->ConvertToRelativeOutputPath(m_Makefile->GetStartDirectory())
  586. << "\n"
  587. << "\n";
  588. makefileStream
  589. << "# The build directory corresponding to this makefile.\n"
  590. << "CMAKE_CURRENT_BINARY = "
  591. << this->ConvertToRelativeOutputPath(m_Makefile->GetStartOutputDirectory())
  592. << "\n"
  593. << "\n";
  594. makefileStream
  595. << "# The top-level source directory on which CMake was run.\n"
  596. << "CMAKE_SOURCE_DIR = "
  597. << this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  598. << "\n"
  599. << "\n";
  600. makefileStream
  601. << "# The top-level build directory on which CMake was run.\n"
  602. << "CMAKE_BINARY_DIR = "
  603. << this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  604. << "\n"
  605. << "\n";
  606. }
  607. //----------------------------------------------------------------------------
  608. void
  609. cmLocalUnixMakefileGenerator2
  610. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  611. {
  612. this->WriteDivider(makefileStream);
  613. makefileStream
  614. << "# Special targets provided by cmake.\n"
  615. << "\n";
  616. // Build command to run CMake to check if anything needs regenerating.
  617. std::string cmakefileName = m_Makefile->GetStartOutputDirectory();
  618. cmakefileName += "/Makefile2.cmake";
  619. std::string runRule =
  620. "@$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  621. runRule += " --check-build-system ";
  622. runRule += this->ConvertToRelativeOutputPath(cmakefileName.c_str());
  623. // Write the main entry point target. This must be the VERY first
  624. // target so that make with no arguments will run it.
  625. {
  626. std::vector<std::string> depends;
  627. std::vector<std::string> commands;
  628. // Check the build system in this directory.
  629. depends.push_back("cmake_check_build_system");
  630. // Recursively build dependencies.
  631. commands.push_back(this->GetRecursiveMakeCall("all.depends"));
  632. // Recursively build targets.
  633. commands.push_back(this->GetRecursiveMakeCall("all.build"));
  634. // Write the rule.
  635. std::string preEcho = "Entering directory ";
  636. preEcho += m_Makefile->GetStartOutputDirectory();
  637. std::string postEcho = "Finished directory ";
  638. postEcho += m_Makefile->GetStartOutputDirectory();
  639. this->WriteMakeRule(makefileStream,
  640. "Default target executed when no arguments are "
  641. "given to make.",
  642. preEcho.c_str(),
  643. "all",
  644. depends,
  645. commands,
  646. postEcho.c_str());
  647. }
  648. // Write special "cmake_check_build_system" target to run cmake with
  649. // the --check-build-system flag.
  650. {
  651. std::vector<std::string> no_depends;
  652. std::vector<std::string> commands;
  653. commands.push_back(runRule);
  654. this->WriteMakeRule(makefileStream,
  655. "Special rule to run CMake to check the build system "
  656. "integrity.",
  657. "Checking build system integrity...",
  658. "cmake_check_build_system",
  659. no_depends,
  660. commands);
  661. }
  662. // Write special "rebuild_cache" target to re-run cmake.
  663. {
  664. std::vector<std::string> no_depends;
  665. std::vector<std::string> commands;
  666. commands.push_back(
  667. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  668. this->WriteMakeRule(makefileStream,
  669. "Special rule to re-run CMake using make.",
  670. "Running CMake to regenerate build system...",
  671. "rebuild_cache",
  672. no_depends,
  673. commands);
  674. }
  675. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  676. // Otherwise default to the interactive command-line interface.
  677. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  678. {
  679. std::vector<std::string> no_depends;
  680. std::vector<std::string> commands;
  681. commands.push_back(
  682. "$(CMAKE_EDIT_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  683. this->WriteMakeRule(makefileStream,
  684. "Special rule to re-run CMake cache editor using make.",
  685. "Running CMake cache editor...",
  686. "edit_cache",
  687. no_depends,
  688. commands);
  689. }
  690. else
  691. {
  692. std::vector<std::string> no_depends;
  693. std::vector<std::string> commands;
  694. commands.push_back(
  695. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  696. this->WriteMakeRule(makefileStream,
  697. "Special rule to re-run CMake cache editor using make.",
  698. "Running interactive CMake command-line interface...",
  699. "edit_cache",
  700. no_depends,
  701. commands);
  702. }
  703. }
  704. //----------------------------------------------------------------------------
  705. void
  706. cmLocalUnixMakefileGenerator2
  707. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  708. {
  709. this->WriteDivider(makefileStream);
  710. makefileStream
  711. << "# Special targets to cleanup operation of make.\n"
  712. << "\n";
  713. std::vector<std::string> no_commands;
  714. // Write special target to silence make output. This must be after
  715. // the default target in case VERBOSE is set (which changes the name).
  716. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  717. {
  718. std::vector<std::string> no_depends;
  719. this->WriteMakeRule(makefileStream,
  720. "Suppress display of executed commands.",
  721. 0,
  722. "$(VERBOSE).SILENT",
  723. no_depends,
  724. no_commands);
  725. }
  726. // Special target to cleanup operation of make tool.
  727. std::vector<std::string> depends;
  728. depends.push_back(".hpux_make_must_have_this_dependency_here");
  729. this->WriteMakeRule(makefileStream,
  730. "Disable some common implicit rules to speed things up.",
  731. 0,
  732. ".SUFFIXES",
  733. depends,
  734. no_commands);
  735. }
  736. //----------------------------------------------------------------------------
  737. void
  738. cmLocalUnixMakefileGenerator2
  739. ::WriteAllRule(std::ostream& makefileStream)
  740. {
  741. // Write section header.
  742. this->WriteDivider(makefileStream);
  743. makefileStream
  744. << "# Main rules for this directory.\n"
  745. << "\n";
  746. const cmTargets& targets = m_Makefile->GetTargets();
  747. // Output top level dependency rule.
  748. {
  749. std::vector<std::string> depends;
  750. std::vector<std::string> commands;
  751. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  752. {
  753. // TODO: Dispatch generation of each target type.
  754. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  755. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  756. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  757. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  758. {
  759. if(t->second.IsInAll())
  760. {
  761. std::string dep = this->GetTargetDirectory(t->second);
  762. dep += "/";
  763. dep += t->first;
  764. dep += ".depends";
  765. depends.push_back(dep);
  766. }
  767. }
  768. }
  769. this->WriteMakeRule(makefileStream,
  770. "Main dependencies target for this directory.",
  771. 0,
  772. "all.depends",
  773. depends,
  774. commands);
  775. }
  776. // Output top level build rule.
  777. {
  778. std::vector<std::string> depends;
  779. std::vector<std::string> commands;
  780. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  781. {
  782. // TODO: Dispatch generation of each target type.
  783. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  784. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  785. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  786. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  787. {
  788. if(t->second.IsInAll())
  789. {
  790. depends.push_back(t->first+".requires");
  791. }
  792. }
  793. }
  794. this->WriteMakeRule(makefileStream,
  795. "Main build target for this directory.",
  796. 0,
  797. "all.build",
  798. depends,
  799. commands);
  800. }
  801. }
  802. //----------------------------------------------------------------------------
  803. void
  804. cmLocalUnixMakefileGenerator2
  805. ::WriteRequiresRule(std::ostream& ruleFileStream, const cmTarget& target,
  806. const char* targetFullPath)
  807. {
  808. std::vector<std::string> depends;
  809. std::vector<std::string> no_commands;
  810. std::string reqComment = "Requirements for target ";
  811. reqComment += target.GetName();
  812. std::string reqTarget = target.GetName();
  813. reqTarget += ".requires";
  814. depends.push_back(targetFullPath);
  815. this->WriteMakeRule(ruleFileStream, reqComment.c_str(), 0,
  816. reqTarget.c_str(), depends, no_commands);
  817. }
  818. //----------------------------------------------------------------------------
  819. void
  820. cmLocalUnixMakefileGenerator2
  821. ::WriteTargetIncludes(std::ostream& makefileStream)
  822. {
  823. bool first = true;
  824. const cmTargets& targets = m_Makefile->GetTargets();
  825. for(cmTargets::const_iterator t = targets.begin(); t != targets.end(); ++t)
  826. {
  827. // TODO: Handle the rest of the target types.
  828. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  829. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  830. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  831. (t->second.GetType() == cmTarget::MODULE_LIBRARY))
  832. {
  833. // Write the header for this section.
  834. if(first)
  835. {
  836. this->WriteDivider(makefileStream);
  837. makefileStream
  838. << "# Include rule files for each target in this directory.\n"
  839. << "\n";
  840. first = false;
  841. }
  842. // Construct the rule file name for this target.
  843. std::string ruleFileName = this->GetTargetDirectory(t->second);
  844. ruleFileName += "/";
  845. ruleFileName += t->first;
  846. ruleFileName += ".make";
  847. makefileStream
  848. << m_IncludeDirective << " "
  849. << this->ConvertToOutputForExisting(ruleFileName.c_str()).c_str()
  850. << "\n";
  851. }
  852. }
  853. if(!first)
  854. {
  855. makefileStream << "\n";
  856. }
  857. }
  858. //----------------------------------------------------------------------------
  859. void
  860. cmLocalUnixMakefileGenerator2
  861. ::WriteExecutableRule(std::ostream& ruleFileStream,
  862. const char* ruleFileName,
  863. const cmTarget& target,
  864. std::vector<std::string>& objects)
  865. {
  866. std::vector<std::string> commands;
  867. // Build list of dependencies. TODO: depend on other targets.
  868. std::vector<std::string> depends;
  869. for(std::vector<std::string>::const_iterator obj = objects.begin();
  870. obj != objects.end(); ++obj)
  871. {
  872. depends.push_back(*obj);
  873. }
  874. // Add dependencies on libraries that will be linked.
  875. std::set<cmStdString> emitted;
  876. emitted.insert(target.GetName());
  877. const cmTarget::LinkLibraries& tlibs = target.GetLinkLibraries();
  878. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  879. lib != tlibs.end(); ++lib)
  880. {
  881. // Don't emit the same library twice for this target.
  882. if(emitted.insert(lib->first).second)
  883. {
  884. // Add this dependency.
  885. this->AppendLibDepend(depends, lib->first.c_str());
  886. }
  887. }
  888. depends.push_back(ruleFileName);
  889. // Construct the full path to the executable that will be generated.
  890. std::string targetFullPath = m_ExecutableOutputPath;
  891. if(targetFullPath.length() == 0)
  892. {
  893. targetFullPath = m_Makefile->GetStartOutputDirectory();
  894. if(targetFullPath.size() && targetFullPath[targetFullPath.size()-1] != '/')
  895. {
  896. targetFullPath += "/";
  897. }
  898. }
  899. #ifdef __APPLE__
  900. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  901. {
  902. // Make bundle directories
  903. targetFullPath += target.GetName();
  904. targetFullPath += ".app/Contents/MacOS/";
  905. }
  906. #endif
  907. targetFullPath += target.GetName();
  908. targetFullPath += cmSystemTools::GetExecutableExtension();
  909. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  910. // Get the language to use for linking this executable.
  911. const char* linkLanguage =
  912. target.GetLinkerLanguage(this->GetGlobalGenerator());
  913. // Build a list of compiler flags and linker flags.
  914. std::string flags;
  915. std::string linkFlags;
  916. // Add flags to create an executable.
  917. this->AddConfigVariableFlags(linkFlags, "CMAKE_EXE_LINKER_FLAGS");
  918. if(target.GetPropertyAsBool("WIN32_EXECUTABLE"))
  919. {
  920. this->AppendFlags(linkFlags,
  921. m_Makefile->GetDefinition("CMAKE_CREATE_WIN32_EXE"));
  922. }
  923. else
  924. {
  925. this->AppendFlags(linkFlags,
  926. m_Makefile->GetDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  927. }
  928. // Add language-specific flags.
  929. this->AddLanguageFlags(flags, linkLanguage);
  930. // Add flags to deal with shared libraries. Any library being
  931. // linked in might be shared, so always use shared flags for an
  932. // executable.
  933. this->AddSharedFlags(flags, linkLanguage, true);
  934. // Add target-specific linker flags.
  935. this->AppendFlags(linkFlags, target.GetProperty("LINK_FLAGS"));
  936. // TODO: Pre-build and pre-link rules.
  937. // Construct the main link rule.
  938. std::string linkRuleVar = "CMAKE_";
  939. linkRuleVar += linkLanguage;
  940. linkRuleVar += "_LINK_EXECUTABLE";
  941. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar.c_str());
  942. cmSystemTools::ExpandListArgument(linkRule, commands);
  943. // TODO: Post-build rules.
  944. // Collect up flags to link in needed libraries.
  945. cmOStringStream linklibs;
  946. this->OutputLinkLibraries(linklibs, 0, target);
  947. // Construct object file lists that may be needed to expand the
  948. // rule. TODO: Store these in make variables with line continuation
  949. // to avoid excessively long lines.
  950. std::string objs;
  951. std::string objsQuoted;
  952. const char* space = "";
  953. for(std::vector<std::string>::iterator i = objects.begin();
  954. i != objects.end(); ++i)
  955. {
  956. objs += space;
  957. objs += this->ConvertToRelativeOutputPath(i->c_str());
  958. objsQuoted += space;
  959. objsQuoted += "\"";
  960. objsQuoted += this->ConvertToRelativeOutputPath(i->c_str());
  961. objsQuoted += "\"";
  962. space = " ";
  963. }
  964. // Expand placeholders in the commands.
  965. for(std::vector<std::string>::iterator i = commands.begin();
  966. i != commands.end(); ++i)
  967. {
  968. this->ExpandRuleVariables(*i,
  969. linkLanguage,
  970. objs.c_str(),
  971. targetFullPath.c_str(),
  972. linklibs.str().c_str(),
  973. 0,
  974. 0,
  975. flags.c_str(),
  976. 0,
  977. 0,
  978. 0,
  979. linkFlags.c_str());
  980. }
  981. // Write the build rule.
  982. std::string buildEcho = "Linking ";
  983. buildEcho += linkLanguage;
  984. buildEcho += " executable ";
  985. buildEcho += this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  986. buildEcho += "...";
  987. this->WriteMakeRule(ruleFileStream, 0, buildEcho.c_str(),
  988. targetFullPath.c_str(), depends, commands);
  989. // TODO: Add "local" target and canonical target name as rules.
  990. // Write driver rule for this target.
  991. this->WriteRequiresRule(ruleFileStream, target, targetFullPath.c_str());
  992. }
  993. //----------------------------------------------------------------------------
  994. void
  995. cmLocalUnixMakefileGenerator2
  996. ::WriteStaticLibraryRule(std::ostream& ruleFileStream,
  997. const char* ruleFileName,
  998. const cmTarget& target,
  999. std::vector<std::string>& objects)
  1000. {
  1001. const char* linkLanguage =
  1002. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1003. std::string linkRuleVar = "CMAKE_";
  1004. linkRuleVar += linkLanguage;
  1005. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  1006. std::string extraFlags;
  1007. this->AppendFlags(extraFlags, target.GetProperty("STATIC_LIBRARY_FLAGS"));
  1008. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  1009. linkRuleVar.c_str(), extraFlags.c_str());
  1010. }
  1011. //----------------------------------------------------------------------------
  1012. void
  1013. cmLocalUnixMakefileGenerator2
  1014. ::WriteSharedLibraryRule(std::ostream& ruleFileStream,
  1015. const char* ruleFileName,
  1016. const cmTarget& target,
  1017. std::vector<std::string>& objects)
  1018. {
  1019. const char* linkLanguage =
  1020. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1021. std::string linkRuleVar = "CMAKE_";
  1022. linkRuleVar += linkLanguage;
  1023. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  1024. std::string extraFlags;
  1025. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1026. this->AddConfigVariableFlags(extraFlags, "CMAKE_SHARED_LINKER_FLAGS");
  1027. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1028. {
  1029. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1030. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1031. i != sources.end(); ++i)
  1032. {
  1033. if((*i)->GetSourceExtension() == "def")
  1034. {
  1035. extraFlags += " ";
  1036. extraFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1037. extraFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  1038. }
  1039. }
  1040. }
  1041. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  1042. linkRuleVar.c_str(), extraFlags.c_str());
  1043. }
  1044. //----------------------------------------------------------------------------
  1045. void
  1046. cmLocalUnixMakefileGenerator2
  1047. ::WriteModuleLibraryRule(std::ostream& ruleFileStream,
  1048. const char* ruleFileName,
  1049. const cmTarget& target,
  1050. std::vector<std::string>& objects)
  1051. {
  1052. const char* linkLanguage =
  1053. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1054. std::string linkRuleVar = "CMAKE_";
  1055. linkRuleVar += linkLanguage;
  1056. linkRuleVar += "_CREATE_SHARED_MODULE";
  1057. std::string extraFlags;
  1058. this->AppendFlags(extraFlags, target.GetProperty("LINK_FLAGS"));
  1059. this->AddConfigVariableFlags(extraFlags, "CMAKE_MODULE_LINKER_FLAGS");
  1060. // TODO: .def files should be supported here also.
  1061. this->WriteLibraryRule(ruleFileStream, ruleFileName, target, objects,
  1062. linkRuleVar.c_str(), extraFlags.c_str());
  1063. }
  1064. //----------------------------------------------------------------------------
  1065. void
  1066. cmLocalUnixMakefileGenerator2
  1067. ::WriteLibraryRule(std::ostream& ruleFileStream,
  1068. const char* ruleFileName,
  1069. const cmTarget& target,
  1070. std::vector<std::string>& objects,
  1071. const char* linkRuleVar,
  1072. const char* extraFlags)
  1073. {
  1074. // TODO: Merge the methods that call this method to avoid
  1075. // code duplication.
  1076. std::vector<std::string> commands;
  1077. // Build list of dependencies. TODO: depend on other targets.
  1078. std::vector<std::string> depends;
  1079. for(std::vector<std::string>::const_iterator obj = objects.begin();
  1080. obj != objects.end(); ++obj)
  1081. {
  1082. depends.push_back(*obj);
  1083. }
  1084. depends.push_back(ruleFileName);
  1085. const char* linkLanguage =
  1086. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1087. std::string linkFlags;
  1088. this->AppendFlags(linkFlags, extraFlags);
  1089. std::string targetName;
  1090. std::string targetNameSO;
  1091. std::string targetNameReal;
  1092. std::string targetNameBase;
  1093. this->GetLibraryNames(target.GetName(), target,
  1094. targetName, targetNameSO,
  1095. targetNameReal, targetNameBase);
  1096. std::string outpath;
  1097. std::string outdir;
  1098. if(m_UseRelativePaths)
  1099. {
  1100. outdir = this->ConvertToRelativeOutputPath(m_LibraryOutputPath.c_str());
  1101. }
  1102. else
  1103. {
  1104. outdir = m_LibraryOutputPath;
  1105. }
  1106. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  1107. {
  1108. outpath = "\"`cd ";
  1109. }
  1110. outpath += outdir;
  1111. if(!m_WindowsShell && m_UseRelativePaths && outdir.size())
  1112. {
  1113. outpath += ";pwd`\"/";
  1114. }
  1115. if(outdir.size() == 0 && m_UseRelativePaths && !m_WindowsShell)
  1116. {
  1117. outpath = "\"`pwd`\"/";
  1118. }
  1119. // The full path versions of the names.
  1120. std::string targetFullPath = outpath + targetName;
  1121. std::string targetFullPathSO = outpath + targetNameSO;
  1122. std::string targetFullPathReal = outpath + targetNameReal;
  1123. std::string targetFullPathBase = outpath + targetNameBase;
  1124. // If not using relative paths then the output path needs to be
  1125. // converted here
  1126. if(!m_UseRelativePaths)
  1127. {
  1128. targetFullPath = this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1129. targetFullPathSO = this->ConvertToRelativeOutputPath(targetFullPathSO.c_str());
  1130. targetFullPathReal = this->ConvertToRelativeOutputPath(targetFullPathReal.c_str());
  1131. targetFullPathBase = this->ConvertToRelativeOutputPath(targetFullPathBase.c_str());
  1132. }
  1133. // Add a command to remove any existing files for this library.
  1134. std::string remove = "$(CMAKE_COMMAND) -E remove -f ";
  1135. remove += targetFullPathReal;
  1136. if(targetFullPathSO != targetFullPathReal)
  1137. {
  1138. remove += " ";
  1139. remove += targetFullPathSO;
  1140. }
  1141. if(targetFullPath != targetFullPathSO &&
  1142. targetFullPath != targetFullPathReal)
  1143. {
  1144. remove += " ";
  1145. remove += targetFullPath;
  1146. }
  1147. commands.push_back(remove);
  1148. // TODO: Pre-build and pre-link rules.
  1149. // Construct the main link rule.
  1150. std::string linkRule = m_Makefile->GetRequiredDefinition(linkRuleVar);
  1151. cmSystemTools::ExpandListArgument(linkRule, commands);
  1152. // Add a rule to create necessary symlinks for the library.
  1153. if(targetFullPath != targetFullPathReal)
  1154. {
  1155. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  1156. symlink += targetFullPathReal;
  1157. symlink += " ";
  1158. symlink += targetFullPathSO;
  1159. symlink += " ";
  1160. symlink += targetFullPath;
  1161. commands.push_back(symlink);
  1162. }
  1163. // TODO: Post-build rules.
  1164. // Collect up flags to link in needed libraries.
  1165. cmOStringStream linklibs;
  1166. this->OutputLinkLibraries(linklibs, target.GetName(), target);
  1167. // Construct object file lists that may be needed to expand the
  1168. // rule. TODO: Store these in make variables with line continuation
  1169. // to avoid excessively long lines.
  1170. std::string objs;
  1171. std::string objsQuoted;
  1172. const char* space = "";
  1173. for(std::vector<std::string>::iterator i = objects.begin();
  1174. i != objects.end(); ++i)
  1175. {
  1176. objs += space;
  1177. objs += this->ConvertToRelativeOutputPath(i->c_str());
  1178. objsQuoted += space;
  1179. objsQuoted += "\"";
  1180. objsQuoted += this->ConvertToRelativeOutputPath(i->c_str());
  1181. objsQuoted += "\"";
  1182. space = " ";
  1183. }
  1184. // Expand placeholders in the commands.
  1185. for(std::vector<std::string>::iterator i = commands.begin();
  1186. i != commands.end(); ++i)
  1187. {
  1188. this->ExpandRuleVariables(*i,
  1189. linkLanguage,
  1190. objs.c_str(),
  1191. targetFullPathReal.c_str(),
  1192. linklibs.str().c_str(),
  1193. 0, 0, 0, objsQuoted.c_str(),
  1194. targetFullPathBase.c_str(),
  1195. targetNameSO.c_str(),
  1196. linkFlags.c_str());
  1197. }
  1198. // Write the build rule.
  1199. std::string buildEcho = "Linking ";
  1200. buildEcho += linkLanguage;
  1201. switch(target.GetType())
  1202. {
  1203. case cmTarget::STATIC_LIBRARY:
  1204. buildEcho += " static library "; break;
  1205. case cmTarget::SHARED_LIBRARY:
  1206. buildEcho += " shared library "; break;
  1207. case cmTarget::MODULE_LIBRARY:
  1208. buildEcho += " shared module "; break;
  1209. default:
  1210. buildEcho += " library "; break;
  1211. }
  1212. buildEcho += this->ConvertToRelativeOutputPath(targetFullPath.c_str());
  1213. buildEcho += "...";
  1214. this->WriteMakeRule(ruleFileStream, 0, buildEcho.c_str(),
  1215. targetFullPath.c_str(), depends, commands);
  1216. // TODO: Add "local" target and canonical target name as rules.
  1217. // Write driver rule for this target.
  1218. this->WriteRequiresRule(ruleFileStream, target, targetFullPath.c_str());
  1219. }
  1220. //----------------------------------------------------------------------------
  1221. std::string
  1222. cmLocalUnixMakefileGenerator2
  1223. ::GetTargetDirectory(const cmTarget& target)
  1224. {
  1225. std::string dir = target.GetName();
  1226. dir += ".dir";
  1227. return dir;
  1228. }
  1229. //----------------------------------------------------------------------------
  1230. std::string
  1231. cmLocalUnixMakefileGenerator2
  1232. ::GetObjectFileName(const cmTarget& target,
  1233. const cmSourceFile& source)
  1234. {
  1235. // If the full path to the source file includes this directory,
  1236. // we want to use the relative path for the filename of the
  1237. // object file. Otherwise, we will use just the filename
  1238. // portion.
  1239. std::string objectName;
  1240. if((cmSystemTools::GetFilenamePath(
  1241. source.GetFullPath()).find(
  1242. m_Makefile->GetCurrentDirectory()) == 0)
  1243. || (cmSystemTools::GetFilenamePath(
  1244. source.GetFullPath()).find(
  1245. m_Makefile->GetStartOutputDirectory()) == 0))
  1246. {
  1247. objectName = source.GetSourceName();
  1248. }
  1249. else
  1250. {
  1251. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  1252. }
  1253. // Append the object file extension.
  1254. objectName +=
  1255. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1256. source.GetSourceExtension().c_str());
  1257. // Convert to a safe name.
  1258. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1259. // Prepend the target directory.
  1260. std::string obj = this->GetTargetDirectory(target);
  1261. obj += "/";
  1262. obj += objectName;
  1263. return obj;
  1264. }
  1265. //----------------------------------------------------------------------------
  1266. const char*
  1267. cmLocalUnixMakefileGenerator2
  1268. ::GetSourceFileLanguage(const cmSourceFile& source)
  1269. {
  1270. // Identify the language of the source file.
  1271. return (m_GlobalGenerator
  1272. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1273. }
  1274. //----------------------------------------------------------------------------
  1275. std::string
  1276. cmLocalUnixMakefileGenerator2
  1277. ::ConvertToFullPath(const std::string& localPath)
  1278. {
  1279. std::string dir = m_Makefile->GetStartOutputDirectory();
  1280. dir += "/";
  1281. dir += localPath;
  1282. return dir;
  1283. }
  1284. //----------------------------------------------------------------------------
  1285. void cmLocalUnixMakefileGenerator2::AddLanguageFlags(std::string& flags,
  1286. const char* lang)
  1287. {
  1288. // Add language-specific flags.
  1289. std::string flagsVar = "CMAKE_";
  1290. flagsVar += lang;
  1291. flagsVar += "_FLAGS";
  1292. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1293. }
  1294. //----------------------------------------------------------------------------
  1295. void cmLocalUnixMakefileGenerator2::AddSharedFlags(std::string& flags,
  1296. const char* lang,
  1297. bool shared)
  1298. {
  1299. std::string flagsVar;
  1300. // Add flags for dealing with shared libraries for this language.
  1301. if(shared)
  1302. {
  1303. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1304. flagsVar += lang;
  1305. flagsVar += "_FLAGS";
  1306. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1307. }
  1308. // Add flags specific to shared builds.
  1309. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1310. {
  1311. flagsVar = "CMAKE_SHARED_BUILD_";
  1312. flagsVar += lang;
  1313. flagsVar += "_FLAGS";
  1314. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1315. }
  1316. }
  1317. //----------------------------------------------------------------------------
  1318. void cmLocalUnixMakefileGenerator2::AddConfigVariableFlags(std::string& flags,
  1319. const char* var)
  1320. {
  1321. // Add the flags from the variable itself.
  1322. std::string flagsVar = var;
  1323. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1324. // Add the flags from the build-type specific variable.
  1325. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1326. if(buildType && *buildType)
  1327. {
  1328. flagsVar += "_";
  1329. flagsVar += cmSystemTools::UpperCase(buildType);
  1330. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1331. }
  1332. }
  1333. //----------------------------------------------------------------------------
  1334. void cmLocalUnixMakefileGenerator2::AppendFlags(std::string& flags,
  1335. const char* newFlags)
  1336. {
  1337. if(newFlags && *newFlags)
  1338. {
  1339. if(flags.size())
  1340. {
  1341. flags += " ";
  1342. }
  1343. flags += newFlags;
  1344. }
  1345. }
  1346. //----------------------------------------------------------------------------
  1347. void
  1348. cmLocalUnixMakefileGenerator2
  1349. ::AppendLibDepend(std::vector<std::string>& depends, const char* name)
  1350. {
  1351. // There are a few cases for the name of the target:
  1352. // - CMake target in this directory: depend on it.
  1353. // - CMake target in another directory: depend and add jump-and-build.
  1354. // - Full path to an outside file: depend on it.
  1355. // - Other format (like -lm): do nothing.
  1356. // If it is a CMake target there will be a definition for it.
  1357. std::string dirVar = name;
  1358. dirVar += "_CMAKE_PATH";
  1359. const char* dir = m_Makefile->GetDefinition(dirVar.c_str());
  1360. if(dir && *dir)
  1361. {
  1362. // This is a CMake target somewhere in this project.
  1363. bool jumpAndBuild = false;
  1364. // Get the path to the library.
  1365. std::string libPath;
  1366. if(this->SamePath(m_Makefile->GetStartOutputDirectory(), dir))
  1367. {
  1368. // The target is in the current directory so this makefile will
  1369. // know about it already.
  1370. libPath = m_LibraryOutputPath;
  1371. }
  1372. else
  1373. {
  1374. // The target is in another directory. Get the path to it.
  1375. if(m_LibraryOutputPath.size())
  1376. {
  1377. libPath = m_LibraryOutputPath;
  1378. }
  1379. else
  1380. {
  1381. libPath = dir;
  1382. libPath += "/";
  1383. }
  1384. // We need to add a jump-and-build rule for this library.
  1385. jumpAndBuild = true;
  1386. }
  1387. // Add the name of the library's file. This depends on the type
  1388. // of the library.
  1389. std::string typeVar = name;
  1390. typeVar += "_LIBRARY_TYPE";
  1391. std::string libType = m_Makefile->GetSafeDefinition(typeVar.c_str());
  1392. std::string prefix;
  1393. std::string suffix;
  1394. if(libType == "SHARED")
  1395. {
  1396. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1397. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1398. }
  1399. else if(libType == "MODULE")
  1400. {
  1401. prefix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1402. suffix = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1403. }
  1404. else if(libType == "STATIC")
  1405. {
  1406. prefix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1407. suffix = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1408. }
  1409. libPath += prefix;
  1410. libPath += name;
  1411. libPath += suffix;
  1412. if(jumpAndBuild)
  1413. {
  1414. // We need to add a jump-and-build rule for this library.
  1415. cmLocalUnixMakefileGenerator2::RemoteTarget rt;
  1416. rt.m_BuildDirectory = dir;
  1417. rt.m_FilePath =libPath;
  1418. m_JumpAndBuild[name] = rt;
  1419. }
  1420. // Add a dependency on the library.
  1421. depends.push_back(this->ConvertToRelativeOutputPath(libPath.c_str()));
  1422. }
  1423. else
  1424. {
  1425. // This is not a CMake target. If it exists and is a full path we
  1426. // can depend on it.
  1427. if(cmSystemTools::FileExists(name) && cmSystemTools::FileIsFullPath(name))
  1428. {
  1429. depends.push_back(this->ConvertToRelativeOutputPath(name));
  1430. }
  1431. }
  1432. }
  1433. //----------------------------------------------------------------------------
  1434. std::string
  1435. cmLocalUnixMakefileGenerator2
  1436. ::GetRecursiveMakeCall(const char* tgt)
  1437. {
  1438. // Call make on the given file.
  1439. std::string cmd = "$(MAKE) -f Makefile2 ";
  1440. // Pass down verbosity level.
  1441. if(m_MakeSilentFlag.size())
  1442. {
  1443. cmd += m_MakeSilentFlag;
  1444. cmd += " ";
  1445. }
  1446. // Most unix makes will pass the command line flags to make down to
  1447. // sub-invoked makes via an environment variable. However, some
  1448. // makes do not support that, so you have to pass the flags
  1449. // explicitly.
  1450. if(m_PassMakeflags)
  1451. {
  1452. cmd += "-$(MAKEFLAGS) ";
  1453. }
  1454. // Add the target.
  1455. cmd += tgt;
  1456. return cmd;
  1457. }
  1458. //----------------------------------------------------------------------------
  1459. void
  1460. cmLocalUnixMakefileGenerator2
  1461. ::WriteJumpAndBuildRules(std::ostream& makefileStream)
  1462. {
  1463. // Write the header for this section.
  1464. if(!m_JumpAndBuild.empty())
  1465. {
  1466. this->WriteDivider(makefileStream);
  1467. makefileStream
  1468. << "# Targets to make sure needed libraries exist.\n"
  1469. << "# These will jump to other directories to build targets.\n"
  1470. << "\n";
  1471. }
  1472. std::vector<std::string> depends;
  1473. std::vector<std::string> commands;
  1474. for(std::map<cmStdString, RemoteTarget>::iterator
  1475. jump = m_JumpAndBuild.begin(); jump != m_JumpAndBuild.end(); ++jump)
  1476. {
  1477. const cmLocalUnixMakefileGenerator2::RemoteTarget& rt = jump->second;
  1478. const char* destination = rt.m_BuildDirectory.c_str();
  1479. // Construct the dependency and build target names.
  1480. std::string dep = jump->first;
  1481. dep += ".dir/";
  1482. dep += jump->first;
  1483. dep += ".depends";
  1484. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  1485. std::string tgt = jump->first;
  1486. tgt += ".requires";
  1487. tgt = this->ConvertToRelativeOutputPath(tgt.c_str());
  1488. // Build the jump-and-build command list.
  1489. commands.clear();
  1490. if(m_WindowsShell)
  1491. {
  1492. // On Windows we must perform each step separately and then jump
  1493. // back because the shell keeps the working directory between
  1494. // commands.
  1495. std::string cmd = "cd ";
  1496. cmd += this->ConvertToOutputForExisting(destination);
  1497. commands.push_back(cmd);
  1498. // Check the build system in destination directory.
  1499. commands.push_back(this->GetRecursiveMakeCall("cmake_check_build_system"));
  1500. // Build the targets's dependencies.
  1501. commands.push_back(this->GetRecursiveMakeCall(dep.c_str()));
  1502. // Build the target.
  1503. commands.push_back(this->GetRecursiveMakeCall(tgt.c_str()));
  1504. // Jump back to the starting directory.
  1505. cmd = "cd ";
  1506. cmd += this->ConvertToOutputForExisting(m_Makefile->GetStartOutputDirectory());
  1507. commands.push_back(cmd);
  1508. }
  1509. else
  1510. {
  1511. // On UNIX we must construct a single shell command to jump and
  1512. // build because make resets the directory between each command.
  1513. std::string cmd = "cd ";
  1514. cmd += this->ConvertToOutputForExisting(destination);
  1515. // Check the build system in destination directory.
  1516. cmd += " && ";
  1517. cmd += this->GetRecursiveMakeCall("cmake_check_build_system");
  1518. // Build the targets's dependencies.
  1519. cmd += " && ";
  1520. cmd += this->GetRecursiveMakeCall(dep.c_str());
  1521. // Build the target.
  1522. cmd += " && ";
  1523. cmd += this->GetRecursiveMakeCall(tgt.c_str());
  1524. // Add the command as a single line.
  1525. commands.push_back(cmd);
  1526. }
  1527. // Write the rule.
  1528. std::string jumpPreEcho = "Jumping to ";
  1529. jumpPreEcho += rt.m_BuildDirectory.c_str();
  1530. jumpPreEcho += " to build ";
  1531. jumpPreEcho += jump->first;
  1532. jumpPreEcho += "...";
  1533. std::string jumpPostEcho = "Returning to ";
  1534. jumpPostEcho += m_Makefile->GetStartOutputDirectory();
  1535. jumpPostEcho += "...";
  1536. this->WriteMakeRule(makefileStream, 0, jumpPreEcho.c_str(),
  1537. rt.m_FilePath.c_str(), depends, commands,
  1538. jumpPostEcho.c_str());
  1539. }
  1540. }
  1541. //----------------------------------------------------------------------------
  1542. bool
  1543. cmLocalUnixMakefileGenerator2
  1544. ::ScanDependencies(std::vector<std::string> const& args)
  1545. {
  1546. // Format of arguments is:
  1547. // $(CMAKE_COMMAND), cmake_depends, <lang>, <obj>, <src>, [include-flags]
  1548. // The caller has ensured that all required arguments exist.
  1549. // The file to which to write dependencies.
  1550. const char* objFile = args[3].c_str();
  1551. // The source file at which to start the scan.
  1552. const char* srcFile = args[4].c_str();
  1553. // Convert the include flags to full paths.
  1554. std::vector<std::string> includes;
  1555. for(unsigned int i=5; i < args.size(); ++i)
  1556. {
  1557. if(args[i].substr(0, 2) == "-I")
  1558. {
  1559. // Get the include path without the -I flag.
  1560. std::string inc = args[i].substr(2);
  1561. includes.push_back(cmSystemTools::CollapseFullPath(inc.c_str()));
  1562. }
  1563. }
  1564. // Dispatch the scan for each language.
  1565. std::string const& lang = args[2];
  1566. if(lang == "C" || lang == "CXX")
  1567. {
  1568. return cmLocalUnixMakefileGenerator2::ScanDependenciesC(objFile, srcFile,
  1569. includes);
  1570. }
  1571. return false;
  1572. }
  1573. //----------------------------------------------------------------------------
  1574. void
  1575. cmLocalUnixMakefileGenerator2ScanDependenciesC(
  1576. std::ifstream& fin,
  1577. std::set<cmStdString>& encountered,
  1578. std::queue<cmStdString>& unscanned)
  1579. {
  1580. // Regular expression to identify C preprocessor include directives.
  1581. cmsys::RegularExpression
  1582. includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  1583. // Read one line at a time.
  1584. std::string line;
  1585. while(cmSystemTools::GetLineFromStream(fin, line))
  1586. {
  1587. // Match include directives. TODO: Support include regex and
  1588. // ignore regex. Possibly also support directory-based inclusion
  1589. // in dependencies.
  1590. if(includeLine.find(line.c_str()))
  1591. {
  1592. // Get the file being included.
  1593. std::string includeFile = includeLine.match(1);
  1594. // Queue the file if it has not yet been encountered.
  1595. if(encountered.find(includeFile) == encountered.end())
  1596. {
  1597. encountered.insert(includeFile);
  1598. unscanned.push(includeFile);
  1599. }
  1600. }
  1601. }
  1602. }
  1603. //----------------------------------------------------------------------------
  1604. bool
  1605. cmLocalUnixMakefileGenerator2
  1606. ::ScanDependenciesC(const char* objFile, const char* srcFile,
  1607. std::vector<std::string> const& includes)
  1608. {
  1609. // Walk the dependency graph starting with the source file.
  1610. std::set<cmStdString> dependencies;
  1611. std::set<cmStdString> encountered;
  1612. std::set<cmStdString> scanned;
  1613. std::queue<cmStdString> unscanned;
  1614. unscanned.push(srcFile);
  1615. encountered.insert(srcFile);
  1616. while(!unscanned.empty())
  1617. {
  1618. // Get the next file to scan.
  1619. std::string fname = unscanned.front();
  1620. unscanned.pop();
  1621. // If not a full path, find the file in the include path.
  1622. std::string fullName;
  1623. if(cmSystemTools::FileIsFullPath(fname.c_str()))
  1624. {
  1625. fullName = fname;
  1626. }
  1627. else
  1628. {
  1629. for(std::vector<std::string>::const_iterator i = includes.begin();
  1630. i != includes.end(); ++i)
  1631. {
  1632. std::string temp = *i;
  1633. temp += "/";
  1634. temp += fname;
  1635. if(cmSystemTools::FileExists(temp.c_str()))
  1636. {
  1637. fullName = temp;
  1638. break;
  1639. }
  1640. }
  1641. }
  1642. // Scan the file if it was found and has not been scanned already.
  1643. if(fullName.size() && (scanned.find(fullName) == scanned.end()))
  1644. {
  1645. // Record scanned files.
  1646. scanned.insert(fullName);
  1647. // Try to scan the file. Just leave it out if we cannot find
  1648. // it.
  1649. std::ifstream fin(fullName.c_str());
  1650. if(fin)
  1651. {
  1652. // Add this file as a dependency.
  1653. dependencies.insert(fullName);
  1654. // Scan this file for new dependencies.
  1655. cmLocalUnixMakefileGenerator2ScanDependenciesC(fin, encountered,
  1656. unscanned);
  1657. }
  1658. }
  1659. }
  1660. // Write the dependencies to the output file.
  1661. std::string depMarkFile = objFile;
  1662. std::string depMakeFile = objFile;
  1663. depMarkFile += ".depends";
  1664. depMakeFile += ".depends.make";
  1665. std::ofstream fout(depMakeFile.c_str());
  1666. fout << "# Dependencies for " << objFile << std::endl;
  1667. for(std::set<cmStdString>::iterator i=dependencies.begin();
  1668. i != dependencies.end(); ++i)
  1669. {
  1670. fout << objFile << ": "
  1671. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  1672. << std::endl;
  1673. }
  1674. fout << std::endl;
  1675. fout << "# Dependencies for " << depMarkFile.c_str() << std::endl;
  1676. for(std::set<cmStdString>::iterator i=dependencies.begin();
  1677. i != dependencies.end(); ++i)
  1678. {
  1679. fout << depMarkFile.c_str() << ": "
  1680. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  1681. << std::endl;
  1682. }
  1683. // If we could write the dependencies, touch the corresponding
  1684. // depends file to mark dependencies up to date.
  1685. if(fout)
  1686. {
  1687. std::ofstream fmark(depMarkFile.c_str());
  1688. fmark << "Dependencies updated for " << objFile << "\n";
  1689. }
  1690. return true;
  1691. }
  1692. //----------------------------------------------------------------------------
  1693. void
  1694. cmLocalUnixMakefileGenerator2
  1695. ::CheckDependencies(const char* depCheck)
  1696. {
  1697. // Get the list of files to scan. This is given through the command
  1698. // line hook cmake file.
  1699. std::vector<std::string> files;
  1700. cmSystemTools::ExpandListArgument(depCheck, files);
  1701. // Check each file. The current working directory is already
  1702. // correct.
  1703. for(std::vector<std::string>::iterator f = files.begin();
  1704. f != files.end(); ++f)
  1705. {
  1706. cmLocalUnixMakefileGenerator2::CheckDependencies(".", f->c_str());
  1707. }
  1708. }
  1709. //----------------------------------------------------------------------------
  1710. void
  1711. cmLocalUnixMakefileGenerator2
  1712. ::CheckDependencies(const char* dir, const char* file)
  1713. {
  1714. // Check the dependencies associated with the given file whose path
  1715. // is specified relative to the given directory. If any dependency
  1716. // is missing then dependencies should be regenerated.
  1717. bool regenerate = false;
  1718. // Construct the names of the mark and make files.
  1719. std::string depMarkFileFull = dir;
  1720. depMarkFileFull += "/";
  1721. depMarkFileFull += file;
  1722. depMarkFileFull += ".depends";
  1723. std::string depMakeFileFull = depMarkFileFull;
  1724. depMakeFileFull += ".make";
  1725. // Open the dependency makefile.
  1726. std::ifstream fin(depMakeFileFull.c_str());
  1727. if(fin)
  1728. {
  1729. // Parse dependencies.
  1730. std::string line;
  1731. std::string depender;
  1732. std::string dependee;
  1733. while(cmSystemTools::GetLineFromStream(fin, line))
  1734. {
  1735. // Skip empty lines and comments.
  1736. std::string::size_type pos = line.find_first_not_of(" \t\r\n");
  1737. if(pos == std::string::npos || line[pos] == '#')
  1738. {
  1739. continue;
  1740. }
  1741. // Strip leading whitespace.
  1742. if(pos > 0)
  1743. {
  1744. line = line.substr(pos);
  1745. }
  1746. // Skip lines too short to have a dependency.
  1747. if(line.size() < 3)
  1748. {
  1749. continue;
  1750. }
  1751. // Find the colon on the line. Skip the first two characters to
  1752. // avoid finding the colon in a drive letter on Windows. Ignore
  1753. // the line if a colon cannot be found.
  1754. if((pos = line.find(':', 2)) == std::string::npos)
  1755. {
  1756. continue;
  1757. }
  1758. // Split the line into depender and dependee.
  1759. depender = line.substr(0, pos);
  1760. dependee = line.substr(pos+1);
  1761. // Strip whitespace from the dependee.
  1762. if((pos = dependee.find_first_not_of(" \t\r\n")) != std::string::npos &&
  1763. pos > 0)
  1764. {
  1765. dependee = dependee.substr(pos);
  1766. }
  1767. if((pos = dependee.find_last_not_of(" \t\r\n")) != std::string::npos)
  1768. {
  1769. dependee = dependee.substr(0, pos+1);
  1770. }
  1771. // Convert dependee to a full path.
  1772. if(!cmSystemTools::FileIsFullPath(dependee.c_str()))
  1773. {
  1774. dependee = cmSystemTools::CollapseFullPath(dependee.c_str(), dir);
  1775. }
  1776. // If the dependee does not exist, we need to regenerate
  1777. // dependencies and the depender should be removed.
  1778. if(!cmSystemTools::FileExists(dependee.c_str()))
  1779. {
  1780. // Strip whitespace from the depender.
  1781. if((pos = depender.find_last_not_of(" \t\r\n")) != std::string::npos)
  1782. {
  1783. depender = depender.substr(0, pos+1);
  1784. }
  1785. // Convert depender to a full path.
  1786. if(!cmSystemTools::FileIsFullPath(depender.c_str()))
  1787. {
  1788. depender = cmSystemTools::CollapseFullPath(depender.c_str(), dir);
  1789. }
  1790. // Remove the depender.
  1791. cmSystemTools::RemoveFile(depender.c_str());
  1792. // Mark the need for regeneration.
  1793. regenerate = true;
  1794. }
  1795. }
  1796. }
  1797. else
  1798. {
  1799. // Could not open the dependencies file. It needs to be
  1800. // regenerated.
  1801. regenerate = true;
  1802. }
  1803. // If the dependencies file needs to be regenerated, create an empty
  1804. // one and delete the mark file.
  1805. if(regenerate)
  1806. {
  1807. cmLocalUnixMakefileGenerator2
  1808. ::WriteEmptyDependMakeFile(file, depMarkFileFull.c_str(),
  1809. depMakeFileFull.c_str());
  1810. }
  1811. }
  1812. //----------------------------------------------------------------------------
  1813. void
  1814. cmLocalUnixMakefileGenerator2
  1815. ::WriteEmptyDependMakeFile(const char* file,
  1816. const char* depMarkFileFull,
  1817. const char* depMakeFileFull)
  1818. {
  1819. // Remove the dependency mark file to be sure dependencies will be
  1820. // regenerated.
  1821. cmSystemTools::RemoveFile(depMarkFileFull);
  1822. // Write an empty dependency file.
  1823. std::ofstream depFileStream(depMakeFileFull);
  1824. depFileStream
  1825. << "# Empty dependencies file for " << file << ".\n"
  1826. << "# This may be replaced when dependencies are built.\n";
  1827. }