cmLocalUnixMakefileGenerator3.cxx 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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 "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmDepends.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmMakefileTargetGenerator.h"
  19. #include "cmSourceFile.h"
  20. #include "cmake.h"
  21. // Include dependency scanners for supported languages. Only the
  22. // C/C++ scanner is needed for bootstrapping CMake.
  23. #include "cmDependsC.h"
  24. #ifdef CMAKE_BUILD_WITH_CMAKE
  25. # include "cmDependsFortran.h"
  26. # include "cmDependsJava.h"
  27. #endif
  28. #include <memory> // auto_ptr
  29. #include <queue>
  30. //----------------------------------------------------------------------------
  31. cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
  32. {
  33. this->SilentNoColon = false;
  34. this->WindowsShell = false;
  35. this->IncludeDirective = "include";
  36. this->MakefileVariableSize = 0;
  37. this->IgnoreLibPrefix = false;
  38. this->PassMakeflags = false;
  39. this->EchoNeedsQuote = true;
  40. this->DefineWindowsNULL = false;
  41. this->UnixCD = true;
  42. this->ForceVerboseMakefiles=false;
  43. this->ColorMakefile = false;
  44. }
  45. //----------------------------------------------------------------------------
  46. cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
  47. {
  48. }
  49. //----------------------------------------------------------------------------
  50. void cmLocalUnixMakefileGenerator3::Configure()
  51. {
  52. // Include the rule file for each object.
  53. this->HomeRelativeOutputPath =
  54. cmSystemTools::RelativePath(this->Makefile->GetHomeOutputDirectory(),
  55. this->Makefile->GetStartOutputDirectory());
  56. if (this->HomeRelativeOutputPath.size())
  57. {
  58. this->HomeRelativeOutputPath += "/";
  59. }
  60. this->cmLocalGenerator::Configure();
  61. }
  62. //----------------------------------------------------------------------------
  63. void cmLocalUnixMakefileGenerator3::Generate()
  64. {
  65. // Setup our configuration variables for this directory.
  66. this->ConfigureOutputPaths();
  67. // Record whether color makefiles are enabled to avoid checking many
  68. // times later.
  69. this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE");
  70. // Generate the rule files for each target.
  71. cmTargets& targets = this->Makefile->GetTargets();
  72. std::string empty;
  73. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  74. {
  75. cmMakefileTargetGenerator *tg =
  76. cmMakefileTargetGenerator::New(this, t->first, &(t->second));
  77. if (tg)
  78. {
  79. this->TargetGenerators.push_back(tg);
  80. t->second.TraceVSDependencies(empty, this->Makefile);
  81. tg->WriteRuleFiles();
  82. }
  83. }
  84. // write the local Makefile
  85. this->WriteLocalMakefile();
  86. // Write the cmake file with information for this directory.
  87. this->WriteDirectoryInformationFile();
  88. // delete the makefile target generator objects
  89. for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
  90. this->TargetGenerators.begin();
  91. mtgIter != this->TargetGenerators.end(); ++mtgIter)
  92. {
  93. delete *mtgIter;
  94. }
  95. this->TargetGenerators.clear();
  96. }
  97. //----------------------------------------------------------------------------
  98. void cmLocalUnixMakefileGenerator3::ConfigureOutputPaths()
  99. {
  100. // Format the library and executable output paths.
  101. if(const char* libOut =
  102. this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  103. {
  104. this->LibraryOutputPath = libOut;
  105. this->FormatOutputPath(this->LibraryOutputPath, "LIBRARY");
  106. }
  107. if(const char* exeOut =
  108. this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  109. {
  110. this->ExecutableOutputPath = exeOut;
  111. this->FormatOutputPath(this->ExecutableOutputPath, "EXECUTABLE");
  112. }
  113. // Store the configuration name that will be generated.
  114. if(const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
  115. {
  116. // Use the build type given by the user.
  117. this->ConfigurationName = config;
  118. }
  119. else
  120. {
  121. // No configuration type given.
  122. this->ConfigurationName = "";
  123. }
  124. }
  125. //----------------------------------------------------------------------------
  126. void cmLocalUnixMakefileGenerator3::FormatOutputPath(std::string& path,
  127. const char* name)
  128. {
  129. if(!path.empty())
  130. {
  131. // Convert the output path to a full path in case it is
  132. // specified as a relative path. Treat a relative path as
  133. // relative to the current output directory for this makefile.
  134. path = cmSystemTools::CollapseFullPath
  135. (path.c_str(), this->Makefile->GetStartOutputDirectory());
  136. // Add a trailing slash for easy appending later.
  137. if(path.empty() || path[path.size()-1] != '/')
  138. {
  139. path += "/";
  140. }
  141. // Make sure the output path exists on disk.
  142. if(!cmSystemTools::MakeDirectory(path.c_str()))
  143. {
  144. cmSystemTools::Error("Error failed to create ",
  145. name, "_OUTPUT_PATH directory:", path.c_str());
  146. }
  147. // Add this as a link directory automatically.
  148. this->Makefile->AddLinkDirectory(path.c_str());
  149. }
  150. }
  151. //----------------------------------------------------------------------------
  152. void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
  153. {
  154. // generate the includes
  155. std::string ruleFileName = "Makefile";
  156. // Open the rule file. This should be copy-if-different because the
  157. // rules may depend on this file itself.
  158. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  159. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  160. if(!ruleFileStream)
  161. {
  162. return;
  163. }
  164. // always write the top makefile
  165. if (this->Parent)
  166. {
  167. ruleFileStream.SetCopyIfDifferent(true);
  168. }
  169. // write the all rules
  170. this->WriteLocalAllRules(ruleFileStream);
  171. // only write local targets unless at the top Keep track of targets already
  172. // listed.
  173. std::set<cmStdString> emittedTargets;
  174. if (this->Parent)
  175. {
  176. // write our targets, and while doing it collect up the object
  177. // file rules
  178. this->WriteLocalMakefileTargets(ruleFileStream,emittedTargets);
  179. }
  180. else
  181. {
  182. cmGlobalUnixMakefileGenerator3 *gg =
  183. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  184. gg->WriteConvenienceRules(ruleFileStream,emittedTargets);
  185. }
  186. std::vector<std::string> depends;
  187. std::vector<std::string> commands;
  188. // now write out the object rules
  189. // for each object file name
  190. for (std::map<cmStdString,std::vector<cmTarget *> >::iterator lo =
  191. this->LocalObjectFiles.begin();
  192. lo != this->LocalObjectFiles.end(); ++lo)
  193. {
  194. commands.clear();
  195. // for each target using the object file
  196. for (std::vector<cmTarget *>::iterator to =
  197. lo->second.begin(); to != lo->second.end(); ++to)
  198. {
  199. std::string tgtMakefileName = this->GetRelativeTargetDirectory(**to);
  200. std::string targetName = tgtMakefileName;
  201. tgtMakefileName += "/build.make";
  202. targetName += "/";
  203. targetName += lo->first.c_str();
  204. commands.push_back(this->GetRecursiveMakeCall
  205. (tgtMakefileName.c_str(),targetName.c_str()));
  206. this->CreateCDCommand(commands,
  207. this->Makefile->GetHomeOutputDirectory(),
  208. this->Makefile->GetStartOutputDirectory());
  209. }
  210. this->WriteMakeRule(ruleFileStream,
  211. "target for object file",
  212. lo->first.c_str(), depends, commands, false);
  213. }
  214. // add a help target as long as there isn;t a real target named help
  215. if(emittedTargets.insert("help").second)
  216. {
  217. cmGlobalUnixMakefileGenerator3 *gg =
  218. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  219. gg->WriteHelpRule(ruleFileStream,this);
  220. }
  221. this->WriteSpecialTargetsBottom(ruleFileStream);
  222. }
  223. void cmLocalUnixMakefileGenerator3
  224. ::WriteLocalMakefileTargets(std::ostream& ruleFileStream,
  225. std::set<cmStdString> &emitted)
  226. {
  227. std::vector<std::string> depends;
  228. std::vector<std::string> commands;
  229. // for each target we just provide a rule to cd up to the top and do a make
  230. // on the target
  231. cmTargets& targets = this->Makefile->GetTargets();
  232. std::string localName;
  233. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  234. {
  235. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  236. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  237. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  238. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  239. (t->second.GetType() == cmTarget::UTILITY))
  240. {
  241. emitted.insert(t->second.GetName());
  242. // for subdirs add a rule to build this specific target by name.
  243. localName = this->GetRelativeTargetDirectory(t->second);
  244. localName += "/rule";
  245. commands.clear();
  246. depends.clear();
  247. // Build the target for this pass.
  248. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  249. tmp += "Makefile2";
  250. commands.push_back(this->GetRecursiveMakeCall
  251. (tmp.c_str(),localName.c_str()));
  252. this->CreateCDCommand(commands,
  253. this->Makefile->GetHomeOutputDirectory(),
  254. this->Makefile->GetStartOutputDirectory());
  255. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  256. localName.c_str(), depends, commands, true);
  257. // Add a target with the canonical name (no prefix, suffix or path).
  258. if(localName != t->second.GetName())
  259. {
  260. commands.clear();
  261. depends.push_back(localName);
  262. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  263. t->second.GetName(), depends, commands, true);
  264. }
  265. // Add a fast rule to build the target
  266. std::string makefileName = this->GetRelativeTargetDirectory(t->second);
  267. makefileName += "/build.make";
  268. std::string makeTargetName =
  269. this->GetRelativeTargetDirectory(t->second);
  270. makeTargetName += "/build";
  271. localName = t->second.GetName();
  272. localName += "/fast";
  273. depends.clear();
  274. commands.clear();
  275. commands.push_back(this->GetRecursiveMakeCall
  276. (makefileName.c_str(), makeTargetName.c_str()));
  277. this->CreateCDCommand(commands,
  278. this->Makefile->GetHomeOutputDirectory(),
  279. this->Makefile->GetStartOutputDirectory());
  280. this->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  281. localName.c_str(), depends, commands, true);
  282. }
  283. }
  284. }
  285. //----------------------------------------------------------------------------
  286. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  287. {
  288. std::string infoFileName = this->Makefile->GetStartOutputDirectory();
  289. infoFileName += cmake::GetCMakeFilesDirectory();
  290. infoFileName += "/CMakeDirectoryInformation.cmake";
  291. // Open the output file.
  292. cmGeneratedFileStream infoFileStream(infoFileName.c_str());
  293. if(!infoFileStream)
  294. {
  295. return;
  296. }
  297. // Write the do not edit header.
  298. this->WriteDisclaimer(infoFileStream);
  299. // Tell the dependency scanner to use unix paths if necessary.
  300. if(cmSystemTools::GetForceUnixPaths())
  301. {
  302. infoFileStream
  303. << "# Force unix paths in dependencies.\n"
  304. << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
  305. << "\n";
  306. }
  307. // Store the include search path for this directory.
  308. infoFileStream
  309. << "# The C and CXX include file search paths:\n";
  310. infoFileStream
  311. << "SET(CMAKE_C_INCLUDE_PATH\n";
  312. std::vector<std::string> includeDirs;
  313. this->GetIncludeDirectories(includeDirs);
  314. for(std::vector<std::string>::iterator i = includeDirs.begin();
  315. i != includeDirs.end(); ++i)
  316. {
  317. // Note: This path conversion must match that used for
  318. // CMAKE_GENERATED_FILES so that the file names match.
  319. infoFileStream
  320. << " \"" << this->Convert(i->c_str(),HOME_OUTPUT).c_str() << "\"\n";
  321. }
  322. infoFileStream
  323. << " )\n";
  324. infoFileStream
  325. << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
  326. // Store the include regular expressions for this directory.
  327. infoFileStream
  328. << "\n"
  329. << "# The C and CXX include file regular expressions for "
  330. << "this directory.\n";
  331. infoFileStream
  332. << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
  333. this->WriteCMakeArgument(infoFileStream,
  334. this->Makefile->GetIncludeRegularExpression());
  335. infoFileStream
  336. << ")\n";
  337. infoFileStream
  338. << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  339. this->WriteCMakeArgument(infoFileStream,
  340. this->Makefile->GetComplainRegularExpression());
  341. infoFileStream
  342. << ")\n";
  343. infoFileStream
  344. << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  345. infoFileStream
  346. << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
  347. "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  348. // Store the set of available generated files.
  349. infoFileStream
  350. << "\n"
  351. << "# The set of files generated by rules in this directory:\n";
  352. infoFileStream
  353. << "SET(CMAKE_GENERATED_FILES\n";
  354. for(std::vector<cmSourceFile*>::const_iterator
  355. i = this->Makefile->GetSourceFiles().begin();
  356. i != this->Makefile->GetSourceFiles().end(); ++i)
  357. {
  358. cmSourceFile* src = *i;
  359. if(src->GetPropertyAsBool("GENERATED"))
  360. {
  361. // Note: This path conversion must match that used for
  362. // CMAKE_C_INCLUDE_PATH so that the file names match.
  363. infoFileStream
  364. << " \""
  365. << this->Convert(src->GetFullPath().c_str(), HOME_OUTPUT)
  366. << "\"\n";
  367. }
  368. }
  369. infoFileStream
  370. << ")\n";
  371. }
  372. //----------------------------------------------------------------------------
  373. std::string
  374. cmLocalUnixMakefileGenerator3
  375. ::ConvertToFullPath(const std::string& localPath)
  376. {
  377. std::string dir = this->Makefile->GetStartOutputDirectory();
  378. dir += "/";
  379. dir += localPath;
  380. return dir;
  381. }
  382. const std::string &cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  383. {
  384. return this->HomeRelativeOutputPath;
  385. }
  386. //----------------------------------------------------------------------------
  387. void
  388. cmLocalUnixMakefileGenerator3
  389. ::WriteMakeRule(std::ostream& os,
  390. const char* comment,
  391. const char* target,
  392. const std::vector<std::string>& depends,
  393. const std::vector<std::string>& commands,
  394. bool symbolic)
  395. {
  396. // Make sure there is a target.
  397. if(!target || !*target)
  398. {
  399. cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
  400. comment);
  401. return;
  402. }
  403. std::string replace;
  404. // Write the comment describing the rule in the makefile.
  405. if(comment)
  406. {
  407. replace = comment;
  408. std::string::size_type lpos = 0;
  409. std::string::size_type rpos;
  410. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  411. {
  412. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  413. lpos = rpos+1;
  414. }
  415. os << "# " << replace.substr(lpos) << "\n";
  416. }
  417. // Construct the left hand side of the rule.
  418. replace = target;
  419. std::string tgt = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  420. tgt = this->ConvertToMakeTarget(tgt.c_str());
  421. const char* space = "";
  422. if(tgt.size() == 1)
  423. {
  424. // Add a space before the ":" to avoid drive letter confusion on
  425. // Windows.
  426. space = " ";
  427. }
  428. // Mark the rule as symbolic if requested.
  429. if(symbolic)
  430. {
  431. if(const char* sym =
  432. this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE"))
  433. {
  434. os << tgt.c_str() << space << ": " << sym << "\n";
  435. }
  436. }
  437. // Write the rule.
  438. if(depends.empty())
  439. {
  440. // No dependencies. The commands will always run.
  441. os << tgt.c_str() << space << ":\n";
  442. }
  443. else
  444. {
  445. // Split dependencies into multiple rule lines. This allows for
  446. // very long dependency lists even on older make implementations.
  447. for(std::vector<std::string>::const_iterator dep = depends.begin();
  448. dep != depends.end(); ++dep)
  449. {
  450. replace = *dep;
  451. replace = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  452. replace = this->ConvertToMakeTarget(replace.c_str());
  453. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  454. }
  455. }
  456. // Write the list of commands.
  457. for(std::vector<std::string>::const_iterator i = commands.begin();
  458. i != commands.end(); ++i)
  459. {
  460. replace = *i;
  461. os << "\t" << replace.c_str() << "\n";
  462. }
  463. os << "\n";
  464. }
  465. //----------------------------------------------------------------------------
  466. void
  467. cmLocalUnixMakefileGenerator3
  468. ::WriteMakeVariables(std::ostream& makefileStream)
  469. {
  470. this->WriteDivider(makefileStream);
  471. makefileStream
  472. << "# Set environment variables for the build.\n"
  473. << "\n";
  474. if(this->DefineWindowsNULL)
  475. {
  476. makefileStream
  477. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  478. << "NULL=\n"
  479. << "!ELSE\n"
  480. << "NULL=nul\n"
  481. << "!ENDIF\n";
  482. }
  483. if(this->WindowsShell)
  484. {
  485. makefileStream
  486. << "SHELL = cmd.exe\n"
  487. << "\n";
  488. }
  489. else
  490. {
  491. makefileStream
  492. << "# The shell in which to execute make rules.\n"
  493. << "SHELL = /bin/sh\n"
  494. << "\n";
  495. }
  496. std::string cmakecommand =
  497. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  498. makefileStream
  499. << "# The CMake executable.\n"
  500. << "CMAKE_COMMAND = "
  501. << this->Convert(cmakecommand.c_str(), FULL, SHELL).c_str()
  502. << "\n"
  503. << "\n";
  504. makefileStream
  505. << "# The command to remove a file.\n"
  506. << "RM = "
  507. << this->Convert(cmakecommand.c_str(),FULL,SHELL).c_str()
  508. << " -E remove -f\n"
  509. << "\n";
  510. if(this->Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  511. {
  512. makefileStream
  513. << "# The program to use to edit the cache.\n"
  514. << "CMAKE_EDIT_COMMAND = "
  515. << (this->ConvertToOutputForExisting(
  516. this->Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  517. << "\n";
  518. }
  519. makefileStream
  520. << "# The top-level source directory on which CMake was run.\n"
  521. << "CMAKE_SOURCE_DIR = "
  522. << this->Convert(this->Makefile->GetHomeDirectory(), FULL, SHELL)
  523. << "\n"
  524. << "\n";
  525. makefileStream
  526. << "# The top-level build directory on which CMake was run.\n"
  527. << "CMAKE_BINARY_DIR = "
  528. << this->Convert(this->Makefile->GetHomeOutputDirectory(), FULL, SHELL)
  529. << "\n"
  530. << "\n";
  531. }
  532. //----------------------------------------------------------------------------
  533. void
  534. cmLocalUnixMakefileGenerator3
  535. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  536. {
  537. this->WriteDivider(makefileStream);
  538. makefileStream
  539. << "# Special targets provided by cmake.\n"
  540. << "\n";
  541. // Write special target to silence make output. This must be after
  542. // the default target in case VERBOSE is set (which changes the
  543. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  544. // "VERBOSE=1" to be added as a make variable which will change the
  545. // name of this special target. This gives a make-time choice to
  546. // the user.
  547. std::vector<std::string> commands;
  548. std::vector<std::string> no_depends;
  549. commands.clear();
  550. if((this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  551. || (this->ForceVerboseMakefiles))
  552. {
  553. makefileStream
  554. << "# Produce verbose output by default.\n"
  555. << "VERBOSE = 1\n"
  556. << "\n";
  557. }
  558. if(this->SilentNoColon)
  559. {
  560. makefileStream << "$(VERBOSE).SILENT\n";
  561. }
  562. else
  563. {
  564. this->WriteMakeRule(makefileStream,
  565. "Suppress display of executed commands.",
  566. "$(VERBOSE).SILENT",
  567. no_depends,
  568. commands, false);
  569. }
  570. // Special target to cleanup operation of make tool.
  571. std::vector<std::string> depends;
  572. this->WriteMakeRule
  573. (makefileStream,
  574. "Disable implicit rules so canoncical targets will work.",
  575. ".SUFFIXES",
  576. depends, commands, false);
  577. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  578. depends.push_back(".hpux_make_needs_suffix_list");
  579. this->WriteMakeRule(makefileStream, 0,
  580. ".SUFFIXES", depends, commands, false);
  581. }
  582. //----------------------------------------------------------------------------
  583. void cmLocalUnixMakefileGenerator3
  584. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  585. {
  586. this->WriteDivider(makefileStream);
  587. makefileStream
  588. << "# Special targets to cleanup operation of make.\n"
  589. << "\n";
  590. // Write special "cmake_check_build_system" target to run cmake with
  591. // the --check-build-system flag.
  592. {
  593. // Build command to run CMake to check if anything needs regenerating.
  594. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  595. cmakefileName += "Makefile.cmake";
  596. std::string runRule =
  597. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  598. runRule += " --check-build-system ";
  599. runRule += this->Convert(cmakefileName.c_str(),NONE,SHELL);
  600. runRule += " 0";
  601. std::vector<std::string> no_depends;
  602. std::vector<std::string> commands;
  603. commands.push_back(runRule);
  604. if(this->Parent)
  605. {
  606. this->CreateCDCommand(commands,
  607. this->Makefile->GetHomeOutputDirectory(),
  608. this->Makefile->GetStartOutputDirectory());
  609. }
  610. this->WriteMakeRule(makefileStream,
  611. "Special rule to run CMake to check the build system "
  612. "integrity.\n"
  613. "No rule that depends on this can have "
  614. "commands that come from listfiles\n"
  615. "because they might be regenerated.",
  616. "cmake_check_build_system",
  617. no_depends,
  618. commands, true);
  619. }
  620. std::vector<std::string> no_commands;
  621. }
  622. //----------------------------------------------------------------------------
  623. void
  624. cmLocalUnixMakefileGenerator3
  625. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  626. const char* realTarget,
  627. const char* helpTarget)
  628. {
  629. // A rule is only needed if the names are different.
  630. if(strcmp(realTarget, helpTarget) != 0)
  631. {
  632. // The helper target depends on the real target.
  633. std::vector<std::string> depends;
  634. depends.push_back(realTarget);
  635. // There are no commands.
  636. std::vector<std::string> no_commands;
  637. // Write the rule.
  638. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  639. helpTarget, depends, no_commands, true);
  640. }
  641. }
  642. //----------------------------------------------------------------------------
  643. std::string
  644. cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(cmTarget& target)
  645. {
  646. std::string dir = this->HomeRelativeOutputPath;
  647. dir += this->GetTargetDirectory(target);
  648. return this->Convert(dir.c_str(),NONE,MAKEFILE);
  649. }
  650. //----------------------------------------------------------------------------
  651. void
  652. cmLocalUnixMakefileGenerator3
  653. ::AppendRuleDepend(std::vector<std::string>& depends,
  654. const char* ruleFileName)
  655. {
  656. // Add a dependency on the rule file itself unless an option to skip
  657. // it is specifically enabled by the user or project.
  658. const char* nodep =
  659. this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  660. if(!nodep || cmSystemTools::IsOff(nodep))
  661. {
  662. depends.push_back(ruleFileName);
  663. }
  664. }
  665. //----------------------------------------------------------------------------
  666. void
  667. cmLocalUnixMakefileGenerator3
  668. ::AppendCustomDepends(std::vector<std::string>& depends,
  669. const std::vector<cmCustomCommand>& ccs)
  670. {
  671. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  672. i != ccs.end(); ++i)
  673. {
  674. this->AppendCustomDepend(depends, *i);
  675. }
  676. }
  677. //----------------------------------------------------------------------------
  678. void
  679. cmLocalUnixMakefileGenerator3
  680. ::AppendCustomDepend(std::vector<std::string>& depends,
  681. const cmCustomCommand& cc)
  682. {
  683. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  684. d != cc.GetDepends().end(); ++d)
  685. {
  686. // Lookup the real name of the dependency in case it is a CMake target.
  687. std::string dep = this->GetRealDependency
  688. (d->c_str(), this->ConfigurationName.c_str());
  689. depends.push_back(dep);
  690. }
  691. }
  692. //----------------------------------------------------------------------------
  693. void
  694. cmLocalUnixMakefileGenerator3
  695. ::AppendCustomCommands(std::vector<std::string>& commands,
  696. const std::vector<cmCustomCommand>& ccs)
  697. {
  698. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  699. i != ccs.end(); ++i)
  700. {
  701. this->AppendCustomCommand(commands, *i);
  702. }
  703. }
  704. //----------------------------------------------------------------------------
  705. void
  706. cmLocalUnixMakefileGenerator3
  707. ::AppendCustomCommand(std::vector<std::string>& commands,
  708. const cmCustomCommand& cc)
  709. {
  710. std::vector<std::string> commands1;
  711. // Add each command line to the set of commands.
  712. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  713. cl != cc.GetCommandLines().end(); ++cl)
  714. {
  715. // Build the command line in a single string.
  716. const cmCustomCommandLine& commandLine = *cl;
  717. std::string cmd = commandLine[0];
  718. if (cmd.size())
  719. {
  720. cmSystemTools::ReplaceString(cmd, "/./", "/");
  721. cmd = this->Convert(cmd.c_str(),START_OUTPUT);
  722. if(cmd.find("/") == cmd.npos &&
  723. commandLine[0].find("/") != cmd.npos)
  724. {
  725. // Add a leading "./" for executables in the current directory.
  726. cmd = "./" + cmd;
  727. }
  728. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  729. for(unsigned int j=1; j < commandLine.size(); ++j)
  730. {
  731. cmd += " ";
  732. bool forceOn = cmSystemTools::GetForceUnixPaths();
  733. if(forceOn && this->WindowsShell)
  734. {
  735. cmSystemTools::SetForceUnixPaths(false);
  736. }
  737. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  738. if(forceOn && this->WindowsShell)
  739. {
  740. cmSystemTools::SetForceUnixPaths(true);
  741. }
  742. }
  743. commands1.push_back(cmd);
  744. }
  745. }
  746. // push back the custom commands
  747. const char* dir = this->Makefile->GetStartOutputDirectory();
  748. // if the command specified a working directory use it.
  749. if(cc.GetWorkingDirectory())
  750. {
  751. dir = cc.GetWorkingDirectory();
  752. }
  753. this->CreateCDCommand(commands1, dir,
  754. this->Makefile->GetHomeOutputDirectory());
  755. commands.insert(commands.end(), commands1.begin(), commands1.end());
  756. }
  757. //----------------------------------------------------------------------------
  758. void
  759. cmLocalUnixMakefileGenerator3
  760. ::AppendCleanCommand(std::vector<std::string>& commands,
  761. const std::vector<std::string>& files,
  762. cmTarget& target, const char* filename)
  763. {
  764. if(!files.empty())
  765. {
  766. std::string cleanfile = this->Makefile->GetCurrentOutputDirectory();
  767. cleanfile += "/";
  768. cleanfile += this->GetTargetDirectory(target);
  769. cleanfile += "/cmake_clean";
  770. if(filename)
  771. {
  772. cleanfile += "_";
  773. cleanfile += filename;
  774. }
  775. cleanfile += ".cmake";
  776. std::string cleanfilePath = this->Convert(cleanfile.c_str(), FULL);
  777. std::ofstream fout(cleanfilePath.c_str());
  778. if(!fout)
  779. {
  780. cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
  781. }
  782. fout << "FILE(REMOVE\n";
  783. std::string remove = "$(CMAKE_COMMAND) -P ";
  784. remove += this->Convert(cleanfile.c_str(), START_OUTPUT, SHELL);
  785. for(std::vector<std::string>::const_iterator f = files.begin();
  786. f != files.end(); ++f)
  787. {
  788. fout << "\"" << this->Convert(f->c_str(),START_OUTPUT,UNCHANGED)
  789. << "\"\n";
  790. }
  791. fout << ")\n";
  792. commands.push_back(remove);
  793. }
  794. }
  795. //----------------------------------------------------------------------------
  796. void
  797. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  798. const char* text,
  799. EchoColor color)
  800. {
  801. // Choose the color for the text.
  802. std::string color_name;
  803. #ifdef CMAKE_BUILD_WITH_CMAKE
  804. if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
  805. {
  806. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  807. // This color set is readable on both black and white backgrounds.
  808. switch(color)
  809. {
  810. case EchoNormal:
  811. break;
  812. case EchoDepend:
  813. color_name = "--magenta --bold ";
  814. break;
  815. case EchoBuild:
  816. color_name = "--green ";
  817. break;
  818. case EchoLink:
  819. color_name = "--red --bold ";
  820. break;
  821. case EchoGenerate:
  822. color_name = "--blue --bold ";
  823. break;
  824. case EchoGlobal:
  825. color_name = "--cyan ";
  826. break;
  827. }
  828. }
  829. #else
  830. (void)color;
  831. #endif
  832. // Echo one line at a time.
  833. std::string line;
  834. line.reserve(200);
  835. for(const char* c = text;; ++c)
  836. {
  837. if(*c == '\n' || *c == '\0')
  838. {
  839. // Avoid writing a blank last line on end-of-string.
  840. if(*c != '\0' || !line.empty())
  841. {
  842. // Add a command to echo this line.
  843. std::string cmd;
  844. if(color_name.empty())
  845. {
  846. // Use the native echo command.
  847. cmd = "@echo ";
  848. if(this->EchoNeedsQuote)
  849. {
  850. cmd += "\"";
  851. }
  852. cmd += line;
  853. if(this->EchoNeedsQuote)
  854. {
  855. cmd += "\"";
  856. }
  857. }
  858. else
  859. {
  860. // Use cmake to echo the text in color.
  861. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  862. cmd += color_name;
  863. cmd += "\"";
  864. cmd += line;
  865. cmd += "\"";
  866. }
  867. commands.push_back(cmd);
  868. }
  869. // Reset the line to emtpy.
  870. line = "";
  871. // Terminate on end-of-string.
  872. if(*c == '\0')
  873. {
  874. return;
  875. }
  876. }
  877. else if(*c != '\r')
  878. {
  879. // Append this character to the current line.
  880. line += *c;
  881. }
  882. }
  883. }
  884. //----------------------------------------------------------------------------
  885. //take a tgt path and convert it into a make target, it could be full, or
  886. //relative
  887. std::string cmLocalUnixMakefileGenerator3
  888. ::ConvertToMakeTarget(const char* tgt)
  889. {
  890. // Make targets should not have a leading './' for a file in the
  891. // directory containing the makefile.
  892. std::string ret = tgt;
  893. if(ret.size() > 2 && (ret[0] == '.') &&
  894. ( (ret[1] == '/') || ret[1] == '\\'))
  895. {
  896. std::string upath = ret;
  897. cmSystemTools::ConvertToUnixSlashes(upath);
  898. if(upath.find(2, '/') == upath.npos)
  899. {
  900. ret = ret.substr(2, ret.size()-2);
  901. }
  902. }
  903. return ret;
  904. }
  905. //----------------------------------------------------------------------------
  906. std::string&
  907. cmLocalUnixMakefileGenerator3::CreateSafeUniqueObjectFileName(const char* sin)
  908. {
  909. // Look for an existing mapped name for this object file.
  910. std::map<cmStdString,cmStdString>::iterator it =
  911. this->UniqueObjectNamesMap.find(sin);
  912. // If no entry exists create one.
  913. if(it == this->UniqueObjectNamesMap.end())
  914. {
  915. // Start with the original name.
  916. std::string ssin = sin;
  917. // Avoid full paths by removing leading slashes.
  918. std::string::size_type pos = 0;
  919. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
  920. ssin = ssin.substr(pos);
  921. // Avoid full paths by removing colons.
  922. cmSystemTools::ReplaceString(ssin, ":", "_");
  923. // Avoid relative paths that go up the tree.
  924. cmSystemTools::ReplaceString(ssin, "../", "__/");
  925. // Avoid spaces.
  926. cmSystemTools::ReplaceString(ssin, " ", "_");
  927. // Mangle the name if necessary.
  928. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  929. {
  930. bool done;
  931. int cc = 0;
  932. char rpstr[100];
  933. sprintf(rpstr, "_p_");
  934. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  935. std::string sssin = sin;
  936. do
  937. {
  938. done = true;
  939. for ( it = this->UniqueObjectNamesMap.begin();
  940. it != this->UniqueObjectNamesMap.end();
  941. ++ it )
  942. {
  943. if ( it->second == ssin )
  944. {
  945. done = false;
  946. }
  947. }
  948. if ( done )
  949. {
  950. break;
  951. }
  952. sssin = ssin;
  953. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  954. sprintf(rpstr, "_p%d_", cc++);
  955. }
  956. while ( !done );
  957. }
  958. // Insert the newly mapped object file name.
  959. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  960. it = this->UniqueObjectNamesMap.insert(e).first;
  961. }
  962. // Return the map entry.
  963. return it->second;
  964. }
  965. //----------------------------------------------------------------------------
  966. std::string
  967. cmLocalUnixMakefileGenerator3
  968. ::CreateMakeVariable(const char* sin, const char* s2in)
  969. {
  970. std::string s = sin;
  971. std::string s2 = s2in;
  972. std::string unmodified = s;
  973. unmodified += s2;
  974. // if there is no restriction on the length of make variables
  975. // and there are no "." charactors in the string, then return the
  976. // unmodified combination.
  977. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  978. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  979. {
  980. return unmodified;
  981. }
  982. // see if the variable has been defined before and return
  983. // the modified version of the variable
  984. std::map<cmStdString, cmStdString>::iterator i =
  985. this->MakeVariableMap.find(unmodified);
  986. if(i != this->MakeVariableMap.end())
  987. {
  988. return i->second;
  989. }
  990. // start with the unmodified variable
  991. std::string ret = unmodified;
  992. // if this there is no value for this->MakefileVariableSize then
  993. // the string must have bad characters in it
  994. if(!this->MakefileVariableSize)
  995. {
  996. cmSystemTools::ReplaceString(ret, ".", "_");
  997. cmSystemTools::ReplaceString(ret, "-", "__");
  998. int ni = 0;
  999. char buffer[5];
  1000. // make sure the _ version is not already used, if
  1001. // it is used then add number to the end of the variable
  1002. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1003. {
  1004. ++ni;
  1005. sprintf(buffer, "%04d", ni);
  1006. ret = unmodified + buffer;
  1007. }
  1008. this->ShortMakeVariableMap[ret] = "1";
  1009. this->MakeVariableMap[unmodified] = ret;
  1010. return ret;
  1011. }
  1012. // if the string is greater the 32 chars it is an invalid vairable name
  1013. // for borland make
  1014. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1015. {
  1016. int keep = this->MakefileVariableSize - 8;
  1017. int size = keep + 3;
  1018. std::string str1 = s;
  1019. std::string str2 = s2;
  1020. // we must shorten the combined string by 4 charactors
  1021. // keep no more than 24 charactors from the second string
  1022. if(static_cast<int>(str2.size()) > keep)
  1023. {
  1024. str2 = str2.substr(0, keep);
  1025. }
  1026. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1027. {
  1028. str1 = str1.substr(0, size - str2.size());
  1029. }
  1030. char buffer[5];
  1031. int ni = 0;
  1032. sprintf(buffer, "%04d", ni);
  1033. ret = str1 + str2 + buffer;
  1034. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1035. {
  1036. ++ni;
  1037. sprintf(buffer, "%04d", ni);
  1038. ret = str1 + str2 + buffer;
  1039. }
  1040. if(ni == 1000)
  1041. {
  1042. cmSystemTools::Error("Borland makefile variable length too long");
  1043. return unmodified;
  1044. }
  1045. // once an unused variable is found
  1046. this->ShortMakeVariableMap[ret] = "1";
  1047. }
  1048. // always make an entry into the unmodified to variable map
  1049. this->MakeVariableMap[unmodified] = ret;
  1050. return ret;
  1051. }
  1052. //----------------------------------------------------------------------------
  1053. bool cmLocalUnixMakefileGenerator3::ScanDependencies(const char* tgtInfo)
  1054. {
  1055. // The info file for this target
  1056. std::string const& infoFile = tgtInfo;
  1057. // Read the directory information file.
  1058. cmMakefile* mf = this->Makefile;
  1059. bool haveDirectoryInfo = false;
  1060. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1061. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1062. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1063. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  1064. !cmSystemTools::GetErrorOccuredFlag())
  1065. {
  1066. haveDirectoryInfo = true;
  1067. }
  1068. // read in the target info file
  1069. if(!mf->ReadListFile(0, infoFile.c_str()) ||
  1070. cmSystemTools::GetErrorOccuredFlag())
  1071. {
  1072. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1073. }
  1074. // Test whether we need to force Unix paths.
  1075. if(haveDirectoryInfo)
  1076. {
  1077. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1078. {
  1079. if(!cmSystemTools::IsOff(force))
  1080. {
  1081. cmSystemTools::SetForceUnixPaths(true);
  1082. }
  1083. }
  1084. }
  1085. else
  1086. {
  1087. cmSystemTools::Error("Directory Information file not found");
  1088. }
  1089. // create the file stream for the depends file
  1090. std::string dir = cmSystemTools::GetFilenamePath(infoFile);
  1091. // Open the rule file. This should be copy-if-different because the
  1092. // rules may depend on this file itself.
  1093. std::string ruleFileNameFull = dir;
  1094. ruleFileNameFull += "/depend.make";
  1095. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1096. ruleFileStream.SetCopyIfDifferent(true);
  1097. if(!ruleFileStream)
  1098. {
  1099. return false;
  1100. }
  1101. std::string internalRuleFileNameFull = dir;
  1102. internalRuleFileNameFull += "/depend.internal";
  1103. cmGeneratedFileStream
  1104. internalRuleFileStream(internalRuleFileNameFull.c_str());
  1105. internalRuleFileStream.SetCopyIfDifferent(true);
  1106. if(!internalRuleFileStream)
  1107. {
  1108. return false;
  1109. }
  1110. this->WriteDisclaimer(ruleFileStream);
  1111. this->WriteDisclaimer(internalRuleFileStream);
  1112. // Get the set of generated files.
  1113. std::vector<std::string> generatedFilesVec;
  1114. if(haveDirectoryInfo)
  1115. {
  1116. if(const char* generated = mf->GetDefinition("CMAKE_GENERATED_FILES"))
  1117. {
  1118. cmSystemTools::ExpandListArgument(generated, generatedFilesVec);
  1119. }
  1120. }
  1121. // Sort for efficient lookup.
  1122. std::set<cmStdString> generatedFiles;
  1123. for(std::vector<std::string>::iterator gfi = generatedFilesVec.begin();
  1124. gfi != generatedFilesVec.end(); ++gfi)
  1125. {
  1126. generatedFiles.insert(*gfi);
  1127. }
  1128. // for each language we need to scan, scan it
  1129. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1130. std::vector<std::string> langs;
  1131. cmSystemTools::ExpandListArgument(langStr, langs);
  1132. for (std::vector<std::string>::iterator li =
  1133. langs.begin(); li != langs.end(); ++li)
  1134. {
  1135. // construct the checker
  1136. std::string lang = li->c_str();
  1137. // Get the set of include directories.
  1138. std::vector<std::string> includes;
  1139. if(haveDirectoryInfo)
  1140. {
  1141. std::string includePathVar = "CMAKE_";
  1142. includePathVar += lang;
  1143. includePathVar += "_INCLUDE_PATH";
  1144. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  1145. {
  1146. cmSystemTools::ExpandListArgument(includePath, includes);
  1147. }
  1148. }
  1149. // Get the include file regular expression.
  1150. std::string includeRegexScan = "^.*$";
  1151. std::string includeRegexComplain = "^$";
  1152. if(haveDirectoryInfo)
  1153. {
  1154. std::string scanRegexVar = "CMAKE_";
  1155. scanRegexVar += lang;
  1156. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  1157. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  1158. {
  1159. includeRegexScan = scanRegex;
  1160. }
  1161. std::string complainRegexVar = "CMAKE_";
  1162. complainRegexVar += lang;
  1163. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  1164. if(const char* complainRegex =
  1165. mf->GetDefinition(complainRegexVar.c_str()))
  1166. {
  1167. includeRegexComplain = complainRegex;
  1168. }
  1169. }
  1170. // Create the scanner for this language
  1171. cmDepends *scanner = 0;
  1172. if(lang == "C" || lang == "CXX" || lang == "RC")
  1173. {
  1174. std::string includeCacheFileName = dir;
  1175. includeCacheFileName += "/includecache.";
  1176. includeCacheFileName += lang;
  1177. // TODO: Handle RC (resource files) dependencies correctly.
  1178. scanner = new cmDependsC(includes,
  1179. includeRegexScan.c_str(),
  1180. includeRegexComplain.c_str(),
  1181. generatedFiles, includeCacheFileName);
  1182. scanner->SetLocalGenerator(this);
  1183. }
  1184. #ifdef CMAKE_BUILD_WITH_CMAKE
  1185. else if(lang == "Fortran")
  1186. {
  1187. scanner = new cmDependsFortran(includes);
  1188. }
  1189. else if(lang == "Java")
  1190. {
  1191. scanner = new cmDependsJava();
  1192. }
  1193. #endif
  1194. if (scanner)
  1195. {
  1196. scanner->SetFileComparison
  1197. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1198. // for each file we need to scan
  1199. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  1200. srcLang += lang;
  1201. const char *srcStr = mf->GetSafeDefinition(srcLang.c_str());
  1202. std::vector<std::string> srcs;
  1203. cmSystemTools::ExpandListArgument(srcStr, srcs);
  1204. for (std::vector<std::string>::iterator si =
  1205. srcs.begin(); si != srcs.end(); ++si)
  1206. {
  1207. std::string &src = *si;
  1208. ++si;
  1209. // make sure the object file is relative to home output
  1210. std::string obj = *si;
  1211. obj = this->Convert(obj.c_str(),HOME_OUTPUT,MAKEFILE);
  1212. scanner->Write(src.c_str(),obj.c_str(),
  1213. ruleFileStream, internalRuleFileStream);
  1214. }
  1215. // free the scanner for this language
  1216. delete scanner;
  1217. }
  1218. }
  1219. // dependencies were generated, so touch the mark file
  1220. ruleFileNameFull += ".mark";
  1221. std::ofstream fmark(ruleFileNameFull.c_str());
  1222. fmark << "Dependencies updated>" << std::endl;
  1223. return true;
  1224. }
  1225. //----------------------------------------------------------------------------
  1226. void cmLocalUnixMakefileGenerator3
  1227. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1228. {
  1229. this->WriteDisclaimer(ruleFileStream);
  1230. this->WriteMakeVariables(ruleFileStream);
  1231. // Write the main entry point target. This must be the VERY first
  1232. // target so that make with no arguments will run it.
  1233. {
  1234. // Just depend on the all target to drive the build.
  1235. std::vector<std::string> depends;
  1236. std::vector<std::string> no_commands;
  1237. depends.push_back("all");
  1238. // Write the rule.
  1239. this->WriteMakeRule(ruleFileStream,
  1240. "Default target executed when no arguments are "
  1241. "given to make.",
  1242. "default_target",
  1243. depends,
  1244. no_commands, true);
  1245. }
  1246. // Write all global targets
  1247. cmTargets* targets = &(this->Makefile->GetTargets());
  1248. cmTargets::iterator glIt;
  1249. for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
  1250. {
  1251. if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  1252. {
  1253. std::string targetString = "Special rule for the target " + glIt->first;
  1254. std::vector<std::string> commands;
  1255. std::vector<std::string> depends;
  1256. const char* text = glIt->second.GetProperty("EchoString");
  1257. if ( !text )
  1258. {
  1259. text = "Running external command ...";
  1260. }
  1261. std::set<cmStdString>::const_iterator dit;
  1262. for ( dit = glIt->second.GetUtilities().begin();
  1263. dit != glIt->second.GetUtilities().end();
  1264. ++ dit )
  1265. {
  1266. depends.push_back(dit->c_str());
  1267. }
  1268. this->AppendEcho(commands, text,
  1269. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1270. // Global targets store their rules in pre- and post-build commands.
  1271. this->AppendCustomDepends(depends,
  1272. glIt->second.GetPreBuildCommands());
  1273. this->AppendCustomDepends(depends,
  1274. glIt->second.GetPostBuildCommands());
  1275. this->AppendCustomCommands(commands,
  1276. glIt->second.GetPreBuildCommands());
  1277. this->AppendCustomCommands(commands,
  1278. glIt->second.GetPostBuildCommands());
  1279. std::string targetName = glIt->second.GetName();
  1280. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1281. targetName.c_str(), depends, commands, true);
  1282. // Provide a "/fast" version of the target.
  1283. depends.clear();
  1284. if(targetName == "install")
  1285. {
  1286. // Provide a fast install target that does not depend on all
  1287. // but has the same command.
  1288. depends.push_back("preinstall/fast");
  1289. }
  1290. else
  1291. {
  1292. // Just forward to the real target so at least it will work.
  1293. depends.push_back(targetName);
  1294. commands.clear();
  1295. }
  1296. targetName += "/fast";
  1297. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1298. targetName.c_str(), depends, commands, true);
  1299. }
  1300. }
  1301. this->WriteSpecialTargetsTop(ruleFileStream);
  1302. std::vector<std::string> depends;
  1303. std::vector<std::string> commands;
  1304. // Write the all rule.
  1305. std::string dir = this->Makefile->GetStartOutputDirectory();
  1306. dir += "/all";
  1307. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1308. depends.push_back("cmake_check_build_system");
  1309. if (!this->Parent)
  1310. {
  1311. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1312. progressDir += cmake::GetCMakeFilesDirectory();
  1313. cmOStringStream progCmd;
  1314. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # src files
  1315. progCmd << this->Convert(progressDir.c_str(),
  1316. cmLocalGenerator::FULL,
  1317. cmLocalGenerator::SHELL);
  1318. cmGlobalUnixMakefileGenerator3 *gg =
  1319. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  1320. int n = gg->GetNumberOfSourceFiles();
  1321. if(n > 100)
  1322. {
  1323. n = 100;
  1324. }
  1325. progCmd << " " << n;
  1326. commands.push_back(progCmd.str());
  1327. }
  1328. std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1329. mf2Dir += "Makefile2";
  1330. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),dir.c_str()));
  1331. this->CreateCDCommand(commands,
  1332. this->Makefile->GetHomeOutputDirectory(),
  1333. this->Makefile->GetStartOutputDirectory());
  1334. if (!this->Parent)
  1335. {
  1336. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1337. progressDir += cmake::GetCMakeFilesDirectory();
  1338. cmOStringStream progCmd;
  1339. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1340. progCmd << this->Convert(progressDir.c_str(),
  1341. cmLocalGenerator::FULL,
  1342. cmLocalGenerator::SHELL);
  1343. progCmd << " 0";
  1344. commands.push_back(progCmd.str());
  1345. }
  1346. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1347. depends, commands, true);
  1348. // Write the clean rule.
  1349. dir = this->Makefile->GetStartOutputDirectory();
  1350. dir += "/clean";
  1351. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1352. commands.clear();
  1353. depends.clear();
  1354. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),dir.c_str()));
  1355. this->CreateCDCommand(commands,
  1356. this->Makefile->GetHomeOutputDirectory(),
  1357. this->Makefile->GetStartOutputDirectory());
  1358. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1359. depends, commands, true);
  1360. commands.clear();
  1361. depends.clear();
  1362. depends.push_back("clean");
  1363. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1364. depends, commands, true);
  1365. // Write the preinstall rule.
  1366. dir = this->Makefile->GetStartOutputDirectory();
  1367. dir += "/preinstall";
  1368. dir = this->Convert(dir.c_str(), HOME_OUTPUT,MAKEFILE);
  1369. commands.clear();
  1370. depends.clear();
  1371. const char* noall =
  1372. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1373. if(!noall || cmSystemTools::IsOff(noall))
  1374. {
  1375. // Drive the build before installing.
  1376. depends.push_back("all");
  1377. }
  1378. else
  1379. {
  1380. // At least make sure the build system is up to date.
  1381. depends.push_back("cmake_check_build_system");
  1382. }
  1383. commands.push_back
  1384. (this->GetRecursiveMakeCall(mf2Dir.c_str(), dir.c_str()));
  1385. this->CreateCDCommand(commands,
  1386. this->Makefile->GetHomeOutputDirectory(),
  1387. this->Makefile->GetStartOutputDirectory());
  1388. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1389. "preinstall", depends, commands, true);
  1390. depends.clear();
  1391. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1392. "preinstall/fast", depends, commands, true);
  1393. // write the depend rule, really a recompute depends rule
  1394. depends.clear();
  1395. commands.clear();
  1396. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  1397. cmakefileName += "Makefile.cmake";
  1398. this->Convert(cmakefileName.c_str(),HOME_OUTPUT,
  1399. cmLocalGenerator::MAKEFILE);
  1400. std::string runRule =
  1401. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1402. runRule += " --check-build-system ";
  1403. runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  1404. cmLocalGenerator::SHELL);
  1405. runRule += " 1";
  1406. commands.push_back(runRule);
  1407. this->WriteMakeRule(ruleFileStream, "clear depends",
  1408. "depend",
  1409. depends, commands, true);
  1410. }
  1411. //----------------------------------------------------------------------------
  1412. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  1413. bool verbose,
  1414. bool clear)
  1415. {
  1416. // Get the list of target files to check
  1417. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1418. if(!infoDef)
  1419. {
  1420. return;
  1421. }
  1422. std::vector<std::string> files;
  1423. cmSystemTools::ExpandListArgument(infoDef, files);
  1424. // For each info file run the check
  1425. cmDependsC checker;
  1426. checker.SetVerbose(verbose);
  1427. checker.SetFileComparison
  1428. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1429. for(std::vector<std::string>::iterator l = files.begin();
  1430. l != files.end(); ++l)
  1431. {
  1432. // either clear or check the files
  1433. std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
  1434. std::string internalDependFile = dir + "/depend.internal";
  1435. std::string dependFile = dir + "/depend.make";
  1436. if (clear)
  1437. {
  1438. checker.Clear(internalDependFile.c_str());
  1439. checker.Clear(dependFile.c_str());
  1440. }
  1441. else
  1442. {
  1443. checker.Check(dependFile.c_str(), internalDependFile.c_str());
  1444. }
  1445. }
  1446. }
  1447. void cmLocalUnixMakefileGenerator3
  1448. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1449. {
  1450. // now write all the language stuff
  1451. // Set the set of files to check for dependency integrity.
  1452. std::set<cmStdString> checkSetLangs;
  1453. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  1454. checkSet = this->GetIntegrityCheckSet()[target.GetName()];
  1455. for(std::map<cmStdString,
  1456. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  1457. l = checkSet.begin(); l != checkSet.end(); ++l)
  1458. {
  1459. checkSetLangs.insert(l->first);
  1460. }
  1461. // list the languages
  1462. cmakefileStream
  1463. << "# The set of files whose dependency integrity should be checked:\n";
  1464. cmakefileStream
  1465. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  1466. for(std::set<cmStdString>::iterator
  1467. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1468. {
  1469. cmakefileStream << " \"" << l->c_str() << "\"\n";
  1470. }
  1471. cmakefileStream << " )\n";
  1472. // now list the files for each language
  1473. for(std::set<cmStdString>::iterator
  1474. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1475. {
  1476. cmakefileStream
  1477. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  1478. // get the check set for this local gen and language
  1479. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  1480. checkSet[*l];
  1481. // for each file
  1482. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator
  1483. csIter = iCheckSet.begin();
  1484. csIter != iCheckSet.end(); ++csIter)
  1485. {
  1486. cmakefileStream << " \"" << (*csIter)->GetFullPath() << "\"\n";
  1487. // Get the full path name of the object file.
  1488. std::string obj = this->Makefile->GetStartOutputDirectory();
  1489. obj += "/";
  1490. obj += this->GetObjectFileName(target, **csIter);
  1491. cmakefileStream << " \"" <<
  1492. this->Convert(obj.c_str(),
  1493. cmLocalGenerator::FULL).c_str() << "\"\n";
  1494. }
  1495. cmakefileStream << " )\n";
  1496. }
  1497. }
  1498. //----------------------------------------------------------------------------
  1499. std::string
  1500. cmLocalUnixMakefileGenerator3
  1501. ::GetObjectFileName(cmTarget& target,
  1502. const cmSourceFile& source,
  1503. std::string* nameWithoutTargetDir)
  1504. {
  1505. // If the source file is located below the current binary directory
  1506. // then use that relative path for the object file name.
  1507. std::string objectName = this->Convert(source.GetFullPath().c_str(),
  1508. START_OUTPUT);
  1509. if(cmSystemTools::FileIsFullPath(objectName.c_str()) ||
  1510. objectName.empty() || objectName[0] == '.')
  1511. {
  1512. // If the source file is located below the current source
  1513. // directory then use that relative path for the object file name.
  1514. // Otherwise just use the relative path from the current binary
  1515. // directory.
  1516. std::string relFromSource = this->Convert(source.GetFullPath().c_str(),
  1517. START);
  1518. if(!cmSystemTools::FileIsFullPath(relFromSource.c_str()) &&
  1519. !relFromSource.empty() && relFromSource[0] != '.')
  1520. {
  1521. objectName = relFromSource;
  1522. }
  1523. }
  1524. // Replace the original source file extension with the object file
  1525. // extension.
  1526. std::string::size_type dot_pos = objectName.rfind(".");
  1527. if(dot_pos != std::string::npos)
  1528. {
  1529. objectName = objectName.substr(0, dot_pos);
  1530. }
  1531. if ( source.GetPropertyAsBool("KEEP_EXTENSION") )
  1532. {
  1533. if ( !source.GetSourceExtension().empty() )
  1534. {
  1535. objectName += "." + source.GetSourceExtension();
  1536. }
  1537. }
  1538. else
  1539. {
  1540. objectName +=
  1541. this->GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1542. source.GetSourceExtension().c_str());
  1543. }
  1544. // Convert to a safe name.
  1545. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1546. // Prepend the target directory.
  1547. std::string obj;
  1548. const char* fileTargetDirectory =
  1549. source.GetProperty("MACOSX_PACKAGE_LOCATION");
  1550. if ( fileTargetDirectory )
  1551. {
  1552. std::string targetName;
  1553. std::string targetNameReal;
  1554. target.GetExecutableNames(targetName, targetNameReal,
  1555. this->ConfigurationName.c_str());
  1556. if ( target.GetPropertyAsBool("MACOSX_BUNDLE") )
  1557. {
  1558. // Construct the full path version of the names.
  1559. obj = this->ExecutableOutputPath;
  1560. if(obj.empty())
  1561. {
  1562. obj = this->Makefile->GetStartOutputDirectory();
  1563. obj += "/";
  1564. }
  1565. obj += targetName + ".app/Contents/";
  1566. obj += fileTargetDirectory;
  1567. }
  1568. else
  1569. {
  1570. // Framework not handled yet
  1571. abort();
  1572. }
  1573. obj = cmSystemTools::RelativePath
  1574. (this->Makefile->GetHomeOutputDirectory(), obj.c_str());
  1575. }
  1576. else
  1577. {
  1578. obj = this->GetTargetDirectory(target);
  1579. }
  1580. obj += "/";
  1581. obj += objectName;
  1582. if(nameWithoutTargetDir)
  1583. {
  1584. *nameWithoutTargetDir = objectName;
  1585. }
  1586. return obj;
  1587. }
  1588. //----------------------------------------------------------------------------
  1589. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1590. {
  1591. os
  1592. << "# CMAKE generated file: DO NOT EDIT!\n"
  1593. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1594. << " Generator, CMake Version "
  1595. << cmMakefile::GetMajorVersion() << "."
  1596. << cmMakefile::GetMinorVersion() << "\n\n";
  1597. }
  1598. //----------------------------------------------------------------------------
  1599. std::string
  1600. cmLocalUnixMakefileGenerator3
  1601. ::GetRecursiveMakeCall(const char *makefile, const char* tgt)
  1602. {
  1603. // Call make on the given file.
  1604. std::string cmd;
  1605. cmd += "$(MAKE) -f ";
  1606. cmd += makefile;
  1607. cmd += " ";
  1608. // Passg down verbosity level.
  1609. if(this->GetMakeSilentFlag().size())
  1610. {
  1611. cmd += this->GetMakeSilentFlag();
  1612. cmd += " ";
  1613. }
  1614. // Most unix makes will pass the command line flags to make down to
  1615. // sub-invoked makes via an environment variable. However, some
  1616. // makes do not support that, so you have to pass the flags
  1617. // explicitly.
  1618. if(this->GetPassMakeflags())
  1619. {
  1620. cmd += "-$(MAKEFLAGS) ";
  1621. }
  1622. // Add the target.
  1623. if (tgt && tgt[0] != '\0')
  1624. {
  1625. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  1626. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  1627. cmd += tgt2;
  1628. }
  1629. return cmd;
  1630. }
  1631. //----------------------------------------------------------------------------
  1632. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1633. {
  1634. os
  1635. << "#======================================"
  1636. << "=======================================\n";
  1637. }
  1638. //----------------------------------------------------------------------------
  1639. void
  1640. cmLocalUnixMakefileGenerator3
  1641. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1642. {
  1643. // Write the given string to the stream with escaping to get it back
  1644. // into CMake through the lexical scanner.
  1645. os << "\"";
  1646. for(const char* c = s; *c; ++c)
  1647. {
  1648. if(*c == '\\')
  1649. {
  1650. os << "\\\\";
  1651. }
  1652. else if(*c == '"')
  1653. {
  1654. os << "\\\"";
  1655. }
  1656. else
  1657. {
  1658. os << *c;
  1659. }
  1660. }
  1661. os << "\"";
  1662. }
  1663. //----------------------------------------------------------------------------
  1664. std::string
  1665. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1666. {
  1667. // Split the path into its components.
  1668. std::vector<std::string> components;
  1669. cmSystemTools::SplitPath(p, components);
  1670. // Return an empty path if there are no components.
  1671. if(components.empty())
  1672. {
  1673. return "\"\"";
  1674. }
  1675. // Choose a slash direction and fix root component.
  1676. const char* slash = "/";
  1677. #if defined(_WIN32) && !defined(__CYGWIN__)
  1678. if(!cmSystemTools::GetForceUnixPaths())
  1679. {
  1680. slash = "\\";
  1681. for(std::string::iterator i = components[0].begin();
  1682. i != components[0].end(); ++i)
  1683. {
  1684. if(*i == '/')
  1685. {
  1686. *i = '\\';
  1687. }
  1688. }
  1689. }
  1690. #endif
  1691. // Begin the quoted result with the root component.
  1692. std::string result = "\"";
  1693. result += components[0];
  1694. // Now add the rest of the components separated by the proper slash
  1695. // direction for this platform.
  1696. bool first = true;
  1697. for(unsigned int i=1; i < components.size(); ++i)
  1698. {
  1699. // Only the last component can be empty to avoid double slashes.
  1700. if(components[i].length() > 0 || (i == (components.size()-1)))
  1701. {
  1702. if(!first)
  1703. {
  1704. result += slash;
  1705. }
  1706. result += components[i];
  1707. first = false;
  1708. }
  1709. }
  1710. // Close the quoted result.
  1711. result += "\"";
  1712. return result;
  1713. }
  1714. //----------------------------------------------------------------------------
  1715. std::string
  1716. cmLocalUnixMakefileGenerator3::GetTargetDirectory(cmTarget& target)
  1717. {
  1718. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1719. dir += target.GetName();
  1720. dir += ".dir";
  1721. return dir;
  1722. }
  1723. void cmLocalUnixMakefileGenerator3
  1724. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  1725. const char *retDir)
  1726. {
  1727. // do we need to cd?
  1728. if (!strcmp(tgtDir,retDir))
  1729. {
  1730. return;
  1731. }
  1732. if(!this->UnixCD)
  1733. {
  1734. // On Windows we must perform each step separately and then change
  1735. // back because the shell keeps the working directory between
  1736. // commands.
  1737. std::string cmd = "cd ";
  1738. cmd += this->ConvertToOutputForExisting(tgtDir);
  1739. commands.insert(commands.begin(),cmd);
  1740. // Change back to the starting directory. Any trailing slash must be
  1741. // removed to avoid problems with Borland Make.
  1742. std::string back = retDir;
  1743. if(back.size() && back[back.size()-1] == '/')
  1744. {
  1745. back = back.substr(0, back.size()-1);
  1746. }
  1747. cmd = "cd ";
  1748. cmd += this->ConvertToOutputForExisting(back.c_str());
  1749. commands.push_back(cmd);
  1750. }
  1751. else
  1752. {
  1753. // On UNIX we must construct a single shell command to change
  1754. // directory and build because make resets the directory between
  1755. // each command.
  1756. std::vector<std::string>::iterator i = commands.begin();
  1757. for (; i != commands.end(); ++i)
  1758. {
  1759. std::string cmd = "cd ";
  1760. cmd += this->ConvertToOutputForExisting(tgtDir);
  1761. cmd += " && ";
  1762. cmd += *i;
  1763. *i = cmd;
  1764. }
  1765. }
  1766. }
  1767. //----------------------------------------------------------------------------
  1768. const char*
  1769. cmLocalUnixMakefileGenerator3
  1770. ::GetSourceFileLanguage(const cmSourceFile& source)
  1771. {
  1772. const char* lang = source.GetProperty("LANGUAGE");
  1773. if ( lang )
  1774. {
  1775. return lang;
  1776. }
  1777. // Identify the language of the source file.
  1778. return (this->GlobalGenerator
  1779. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1780. }