cmLocalUnixMakefileGenerator3.cxx 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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. // if the command specified a working directory use it.
  711. const char* dir = this->Makefile->GetStartOutputDirectory();
  712. const char* workingDir = cc.GetWorkingDirectory();
  713. if(workingDir)
  714. {
  715. dir = workingDir;
  716. }
  717. // Add each command line to the set of commands.
  718. std::vector<std::string> commands1;
  719. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  720. cl != cc.GetCommandLines().end(); ++cl)
  721. {
  722. // Build the command line in a single string.
  723. const cmCustomCommandLine& commandLine = *cl;
  724. std::string cmd = commandLine[0];
  725. if (cmd.size())
  726. {
  727. cmSystemTools::ReplaceString(cmd, "/./", "/");
  728. // Convert the command to a relative path only if the current
  729. // working directory will be the start-output directory.
  730. if(!workingDir)
  731. {
  732. cmd = this->Convert(cmd.c_str(),START_OUTPUT);
  733. }
  734. if(cmd.find("/") == cmd.npos &&
  735. commandLine[0].find("/") != cmd.npos)
  736. {
  737. // Add a leading "./" for executables in the current directory.
  738. cmd = "./" + cmd;
  739. }
  740. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  741. for(unsigned int j=1; j < commandLine.size(); ++j)
  742. {
  743. cmd += " ";
  744. bool forceOn = cmSystemTools::GetForceUnixPaths();
  745. if(forceOn && this->WindowsShell)
  746. {
  747. cmSystemTools::SetForceUnixPaths(false);
  748. }
  749. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  750. if(forceOn && this->WindowsShell)
  751. {
  752. cmSystemTools::SetForceUnixPaths(true);
  753. }
  754. }
  755. commands1.push_back(cmd);
  756. }
  757. }
  758. // Setup the proper working directory for the commands.
  759. this->CreateCDCommand(commands1, dir,
  760. this->Makefile->GetHomeOutputDirectory());
  761. // push back the custom commands
  762. commands.insert(commands.end(), commands1.begin(), commands1.end());
  763. }
  764. //----------------------------------------------------------------------------
  765. void
  766. cmLocalUnixMakefileGenerator3
  767. ::AppendCleanCommand(std::vector<std::string>& commands,
  768. const std::vector<std::string>& files,
  769. cmTarget& target, const char* filename)
  770. {
  771. if(!files.empty())
  772. {
  773. std::string cleanfile = this->Makefile->GetCurrentOutputDirectory();
  774. cleanfile += "/";
  775. cleanfile += this->GetTargetDirectory(target);
  776. cleanfile += "/cmake_clean";
  777. if(filename)
  778. {
  779. cleanfile += "_";
  780. cleanfile += filename;
  781. }
  782. cleanfile += ".cmake";
  783. std::string cleanfilePath = this->Convert(cleanfile.c_str(), FULL);
  784. std::ofstream fout(cleanfilePath.c_str());
  785. if(!fout)
  786. {
  787. cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
  788. }
  789. fout << "FILE(REMOVE\n";
  790. std::string remove = "$(CMAKE_COMMAND) -P ";
  791. remove += this->Convert(cleanfile.c_str(), START_OUTPUT, SHELL);
  792. for(std::vector<std::string>::const_iterator f = files.begin();
  793. f != files.end(); ++f)
  794. {
  795. fout << "\"" << this->Convert(f->c_str(),START_OUTPUT,UNCHANGED)
  796. << "\"\n";
  797. }
  798. fout << ")\n";
  799. commands.push_back(remove);
  800. }
  801. }
  802. //----------------------------------------------------------------------------
  803. void
  804. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  805. const char* text,
  806. EchoColor color)
  807. {
  808. // Choose the color for the text.
  809. std::string color_name;
  810. #ifdef CMAKE_BUILD_WITH_CMAKE
  811. if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
  812. {
  813. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  814. // This color set is readable on both black and white backgrounds.
  815. switch(color)
  816. {
  817. case EchoNormal:
  818. break;
  819. case EchoDepend:
  820. color_name = "--magenta --bold ";
  821. break;
  822. case EchoBuild:
  823. color_name = "--green ";
  824. break;
  825. case EchoLink:
  826. color_name = "--red --bold ";
  827. break;
  828. case EchoGenerate:
  829. color_name = "--blue --bold ";
  830. break;
  831. case EchoGlobal:
  832. color_name = "--cyan ";
  833. break;
  834. }
  835. }
  836. #else
  837. (void)color;
  838. #endif
  839. // Echo one line at a time.
  840. std::string line;
  841. line.reserve(200);
  842. for(const char* c = text;; ++c)
  843. {
  844. if(*c == '\n' || *c == '\0')
  845. {
  846. // Avoid writing a blank last line on end-of-string.
  847. if(*c != '\0' || !line.empty())
  848. {
  849. // Add a command to echo this line.
  850. std::string cmd;
  851. if(color_name.empty())
  852. {
  853. // Use the native echo command.
  854. cmd = "@echo ";
  855. if(this->EchoNeedsQuote)
  856. {
  857. cmd += "\"";
  858. }
  859. cmd += line;
  860. if(this->EchoNeedsQuote)
  861. {
  862. cmd += "\"";
  863. }
  864. }
  865. else
  866. {
  867. // Use cmake to echo the text in color.
  868. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  869. cmd += color_name;
  870. cmd += "\"";
  871. cmd += line;
  872. cmd += "\"";
  873. }
  874. commands.push_back(cmd);
  875. }
  876. // Reset the line to emtpy.
  877. line = "";
  878. // Terminate on end-of-string.
  879. if(*c == '\0')
  880. {
  881. return;
  882. }
  883. }
  884. else if(*c != '\r')
  885. {
  886. // Append this character to the current line.
  887. line += *c;
  888. }
  889. }
  890. }
  891. //----------------------------------------------------------------------------
  892. //take a tgt path and convert it into a make target, it could be full, or
  893. //relative
  894. std::string cmLocalUnixMakefileGenerator3
  895. ::ConvertToMakeTarget(const char* tgt)
  896. {
  897. // Make targets should not have a leading './' for a file in the
  898. // directory containing the makefile.
  899. std::string ret = tgt;
  900. if(ret.size() > 2 && (ret[0] == '.') &&
  901. ( (ret[1] == '/') || ret[1] == '\\'))
  902. {
  903. std::string upath = ret;
  904. cmSystemTools::ConvertToUnixSlashes(upath);
  905. if(upath.find(2, '/') == upath.npos)
  906. {
  907. ret = ret.substr(2, ret.size()-2);
  908. }
  909. }
  910. return ret;
  911. }
  912. //----------------------------------------------------------------------------
  913. std::string&
  914. cmLocalUnixMakefileGenerator3::CreateSafeUniqueObjectFileName(const char* sin)
  915. {
  916. // Look for an existing mapped name for this object file.
  917. std::map<cmStdString,cmStdString>::iterator it =
  918. this->UniqueObjectNamesMap.find(sin);
  919. // If no entry exists create one.
  920. if(it == this->UniqueObjectNamesMap.end())
  921. {
  922. // Start with the original name.
  923. std::string ssin = sin;
  924. // Avoid full paths by removing leading slashes.
  925. std::string::size_type pos = 0;
  926. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
  927. ssin = ssin.substr(pos);
  928. // Avoid full paths by removing colons.
  929. cmSystemTools::ReplaceString(ssin, ":", "_");
  930. // Avoid relative paths that go up the tree.
  931. cmSystemTools::ReplaceString(ssin, "../", "__/");
  932. // Avoid spaces.
  933. cmSystemTools::ReplaceString(ssin, " ", "_");
  934. // Mangle the name if necessary.
  935. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  936. {
  937. bool done;
  938. int cc = 0;
  939. char rpstr[100];
  940. sprintf(rpstr, "_p_");
  941. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  942. std::string sssin = sin;
  943. do
  944. {
  945. done = true;
  946. for ( it = this->UniqueObjectNamesMap.begin();
  947. it != this->UniqueObjectNamesMap.end();
  948. ++ it )
  949. {
  950. if ( it->second == ssin )
  951. {
  952. done = false;
  953. }
  954. }
  955. if ( done )
  956. {
  957. break;
  958. }
  959. sssin = ssin;
  960. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  961. sprintf(rpstr, "_p%d_", cc++);
  962. }
  963. while ( !done );
  964. }
  965. // Insert the newly mapped object file name.
  966. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  967. it = this->UniqueObjectNamesMap.insert(e).first;
  968. }
  969. // Return the map entry.
  970. return it->second;
  971. }
  972. //----------------------------------------------------------------------------
  973. std::string
  974. cmLocalUnixMakefileGenerator3
  975. ::CreateMakeVariable(const char* sin, const char* s2in)
  976. {
  977. std::string s = sin;
  978. std::string s2 = s2in;
  979. std::string unmodified = s;
  980. unmodified += s2;
  981. // if there is no restriction on the length of make variables
  982. // and there are no "." charactors in the string, then return the
  983. // unmodified combination.
  984. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  985. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  986. {
  987. return unmodified;
  988. }
  989. // see if the variable has been defined before and return
  990. // the modified version of the variable
  991. std::map<cmStdString, cmStdString>::iterator i =
  992. this->MakeVariableMap.find(unmodified);
  993. if(i != this->MakeVariableMap.end())
  994. {
  995. return i->second;
  996. }
  997. // start with the unmodified variable
  998. std::string ret = unmodified;
  999. // if this there is no value for this->MakefileVariableSize then
  1000. // the string must have bad characters in it
  1001. if(!this->MakefileVariableSize)
  1002. {
  1003. cmSystemTools::ReplaceString(ret, ".", "_");
  1004. cmSystemTools::ReplaceString(ret, "-", "__");
  1005. int ni = 0;
  1006. char buffer[5];
  1007. // make sure the _ version is not already used, if
  1008. // it is used then add number to the end of the variable
  1009. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1010. {
  1011. ++ni;
  1012. sprintf(buffer, "%04d", ni);
  1013. ret = unmodified + buffer;
  1014. }
  1015. this->ShortMakeVariableMap[ret] = "1";
  1016. this->MakeVariableMap[unmodified] = ret;
  1017. return ret;
  1018. }
  1019. // if the string is greater the 32 chars it is an invalid vairable name
  1020. // for borland make
  1021. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1022. {
  1023. int keep = this->MakefileVariableSize - 8;
  1024. int size = keep + 3;
  1025. std::string str1 = s;
  1026. std::string str2 = s2;
  1027. // we must shorten the combined string by 4 charactors
  1028. // keep no more than 24 charactors from the second string
  1029. if(static_cast<int>(str2.size()) > keep)
  1030. {
  1031. str2 = str2.substr(0, keep);
  1032. }
  1033. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1034. {
  1035. str1 = str1.substr(0, size - str2.size());
  1036. }
  1037. char buffer[5];
  1038. int ni = 0;
  1039. sprintf(buffer, "%04d", ni);
  1040. ret = str1 + str2 + buffer;
  1041. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1042. {
  1043. ++ni;
  1044. sprintf(buffer, "%04d", ni);
  1045. ret = str1 + str2 + buffer;
  1046. }
  1047. if(ni == 1000)
  1048. {
  1049. cmSystemTools::Error("Borland makefile variable length too long");
  1050. return unmodified;
  1051. }
  1052. // once an unused variable is found
  1053. this->ShortMakeVariableMap[ret] = "1";
  1054. }
  1055. // always make an entry into the unmodified to variable map
  1056. this->MakeVariableMap[unmodified] = ret;
  1057. return ret;
  1058. }
  1059. //----------------------------------------------------------------------------
  1060. bool cmLocalUnixMakefileGenerator3::ScanDependencies(const char* tgtInfo)
  1061. {
  1062. // The info file for this target
  1063. std::string const& infoFile = tgtInfo;
  1064. // Read the directory information file.
  1065. cmMakefile* mf = this->Makefile;
  1066. bool haveDirectoryInfo = false;
  1067. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1068. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1069. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1070. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  1071. !cmSystemTools::GetErrorOccuredFlag())
  1072. {
  1073. haveDirectoryInfo = true;
  1074. }
  1075. // read in the target info file
  1076. if(!mf->ReadListFile(0, infoFile.c_str()) ||
  1077. cmSystemTools::GetErrorOccuredFlag())
  1078. {
  1079. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1080. }
  1081. // Test whether we need to force Unix paths.
  1082. if(haveDirectoryInfo)
  1083. {
  1084. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1085. {
  1086. if(!cmSystemTools::IsOff(force))
  1087. {
  1088. cmSystemTools::SetForceUnixPaths(true);
  1089. }
  1090. }
  1091. }
  1092. else
  1093. {
  1094. cmSystemTools::Error("Directory Information file not found");
  1095. }
  1096. // create the file stream for the depends file
  1097. std::string dir = cmSystemTools::GetFilenamePath(infoFile);
  1098. // Open the rule file. This should be copy-if-different because the
  1099. // rules may depend on this file itself.
  1100. std::string ruleFileNameFull = dir;
  1101. ruleFileNameFull += "/depend.make";
  1102. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1103. ruleFileStream.SetCopyIfDifferent(true);
  1104. if(!ruleFileStream)
  1105. {
  1106. return false;
  1107. }
  1108. std::string internalRuleFileNameFull = dir;
  1109. internalRuleFileNameFull += "/depend.internal";
  1110. cmGeneratedFileStream
  1111. internalRuleFileStream(internalRuleFileNameFull.c_str());
  1112. internalRuleFileStream.SetCopyIfDifferent(true);
  1113. if(!internalRuleFileStream)
  1114. {
  1115. return false;
  1116. }
  1117. this->WriteDisclaimer(ruleFileStream);
  1118. this->WriteDisclaimer(internalRuleFileStream);
  1119. // Get the set of generated files.
  1120. std::vector<std::string> generatedFilesVec;
  1121. if(haveDirectoryInfo)
  1122. {
  1123. if(const char* generated = mf->GetDefinition("CMAKE_GENERATED_FILES"))
  1124. {
  1125. cmSystemTools::ExpandListArgument(generated, generatedFilesVec);
  1126. }
  1127. }
  1128. // Sort for efficient lookup.
  1129. std::set<cmStdString> generatedFiles;
  1130. for(std::vector<std::string>::iterator gfi = generatedFilesVec.begin();
  1131. gfi != generatedFilesVec.end(); ++gfi)
  1132. {
  1133. generatedFiles.insert(*gfi);
  1134. }
  1135. // for each language we need to scan, scan it
  1136. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1137. std::vector<std::string> langs;
  1138. cmSystemTools::ExpandListArgument(langStr, langs);
  1139. for (std::vector<std::string>::iterator li =
  1140. langs.begin(); li != langs.end(); ++li)
  1141. {
  1142. // construct the checker
  1143. std::string lang = li->c_str();
  1144. // Get the set of include directories.
  1145. std::vector<std::string> includes;
  1146. if(haveDirectoryInfo)
  1147. {
  1148. std::string includePathVar = "CMAKE_";
  1149. includePathVar += lang;
  1150. includePathVar += "_INCLUDE_PATH";
  1151. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  1152. {
  1153. cmSystemTools::ExpandListArgument(includePath, includes);
  1154. }
  1155. }
  1156. // Get the include file regular expression.
  1157. std::string includeRegexScan = "^.*$";
  1158. std::string includeRegexComplain = "^$";
  1159. if(haveDirectoryInfo)
  1160. {
  1161. std::string scanRegexVar = "CMAKE_";
  1162. scanRegexVar += lang;
  1163. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  1164. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  1165. {
  1166. includeRegexScan = scanRegex;
  1167. }
  1168. std::string complainRegexVar = "CMAKE_";
  1169. complainRegexVar += lang;
  1170. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  1171. if(const char* complainRegex =
  1172. mf->GetDefinition(complainRegexVar.c_str()))
  1173. {
  1174. includeRegexComplain = complainRegex;
  1175. }
  1176. }
  1177. // Create the scanner for this language
  1178. cmDepends *scanner = 0;
  1179. if(lang == "C" || lang == "CXX" || lang == "RC")
  1180. {
  1181. std::string includeCacheFileName = dir;
  1182. includeCacheFileName += "/includecache.";
  1183. includeCacheFileName += lang;
  1184. // TODO: Handle RC (resource files) dependencies correctly.
  1185. scanner = new cmDependsC(includes,
  1186. includeRegexScan.c_str(),
  1187. includeRegexComplain.c_str(),
  1188. generatedFiles, includeCacheFileName);
  1189. scanner->SetLocalGenerator(this);
  1190. }
  1191. #ifdef CMAKE_BUILD_WITH_CMAKE
  1192. else if(lang == "Fortran")
  1193. {
  1194. scanner = new cmDependsFortran(includes);
  1195. }
  1196. else if(lang == "Java")
  1197. {
  1198. scanner = new cmDependsJava();
  1199. }
  1200. #endif
  1201. if (scanner)
  1202. {
  1203. scanner->SetFileComparison
  1204. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1205. // for each file we need to scan
  1206. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  1207. srcLang += lang;
  1208. const char *srcStr = mf->GetSafeDefinition(srcLang.c_str());
  1209. std::vector<std::string> srcs;
  1210. cmSystemTools::ExpandListArgument(srcStr, srcs);
  1211. for (std::vector<std::string>::iterator si =
  1212. srcs.begin(); si != srcs.end(); ++si)
  1213. {
  1214. std::string &src = *si;
  1215. ++si;
  1216. // make sure the object file is relative to home output
  1217. std::string obj = *si;
  1218. obj = this->Convert(obj.c_str(),HOME_OUTPUT,MAKEFILE);
  1219. scanner->Write(src.c_str(),obj.c_str(),
  1220. ruleFileStream, internalRuleFileStream);
  1221. }
  1222. // free the scanner for this language
  1223. delete scanner;
  1224. }
  1225. }
  1226. // dependencies were generated, so touch the mark file
  1227. ruleFileNameFull += ".mark";
  1228. std::ofstream fmark(ruleFileNameFull.c_str());
  1229. fmark << "Dependencies updated>" << std::endl;
  1230. return true;
  1231. }
  1232. //----------------------------------------------------------------------------
  1233. void cmLocalUnixMakefileGenerator3
  1234. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1235. {
  1236. this->WriteDisclaimer(ruleFileStream);
  1237. this->WriteMakeVariables(ruleFileStream);
  1238. // Write the main entry point target. This must be the VERY first
  1239. // target so that make with no arguments will run it.
  1240. {
  1241. // Just depend on the all target to drive the build.
  1242. std::vector<std::string> depends;
  1243. std::vector<std::string> no_commands;
  1244. depends.push_back("all");
  1245. // Write the rule.
  1246. this->WriteMakeRule(ruleFileStream,
  1247. "Default target executed when no arguments are "
  1248. "given to make.",
  1249. "default_target",
  1250. depends,
  1251. no_commands, true);
  1252. }
  1253. // Write all global targets
  1254. cmTargets* targets = &(this->Makefile->GetTargets());
  1255. cmTargets::iterator glIt;
  1256. for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
  1257. {
  1258. if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  1259. {
  1260. std::string targetString = "Special rule for the target " + glIt->first;
  1261. std::vector<std::string> commands;
  1262. std::vector<std::string> depends;
  1263. const char* text = glIt->second.GetProperty("EchoString");
  1264. if ( !text )
  1265. {
  1266. text = "Running external command ...";
  1267. }
  1268. std::set<cmStdString>::const_iterator dit;
  1269. for ( dit = glIt->second.GetUtilities().begin();
  1270. dit != glIt->second.GetUtilities().end();
  1271. ++ dit )
  1272. {
  1273. depends.push_back(dit->c_str());
  1274. }
  1275. this->AppendEcho(commands, text,
  1276. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1277. // Global targets store their rules in pre- and post-build commands.
  1278. this->AppendCustomDepends(depends,
  1279. glIt->second.GetPreBuildCommands());
  1280. this->AppendCustomDepends(depends,
  1281. glIt->second.GetPostBuildCommands());
  1282. this->AppendCustomCommands(commands,
  1283. glIt->second.GetPreBuildCommands());
  1284. this->AppendCustomCommands(commands,
  1285. glIt->second.GetPostBuildCommands());
  1286. std::string targetName = glIt->second.GetName();
  1287. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1288. targetName.c_str(), depends, commands, true);
  1289. // Provide a "/fast" version of the target.
  1290. depends.clear();
  1291. if(targetName == "install")
  1292. {
  1293. // Provide a fast install target that does not depend on all
  1294. // but has the same command.
  1295. depends.push_back("preinstall/fast");
  1296. }
  1297. else
  1298. {
  1299. // Just forward to the real target so at least it will work.
  1300. depends.push_back(targetName);
  1301. commands.clear();
  1302. }
  1303. targetName += "/fast";
  1304. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1305. targetName.c_str(), depends, commands, true);
  1306. }
  1307. }
  1308. this->WriteSpecialTargetsTop(ruleFileStream);
  1309. std::vector<std::string> depends;
  1310. std::vector<std::string> commands;
  1311. // Write the all rule.
  1312. std::string dir = this->Makefile->GetStartOutputDirectory();
  1313. dir += "/all";
  1314. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1315. depends.push_back("cmake_check_build_system");
  1316. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1317. progressDir += cmake::GetCMakeFilesDirectory();
  1318. {
  1319. cmOStringStream progCmd;
  1320. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # src files
  1321. progCmd << this->Convert(progressDir.c_str(),
  1322. cmLocalGenerator::FULL,
  1323. cmLocalGenerator::SHELL);
  1324. cmGlobalUnixMakefileGenerator3 *gg =
  1325. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  1326. int n = gg->GetNumberOfSourceFiles();
  1327. if(n > 100)
  1328. {
  1329. n = 100;
  1330. }
  1331. if (this->Parent)
  1332. {
  1333. n = 0;
  1334. }
  1335. progCmd << " " << n;
  1336. commands.push_back(progCmd.str());
  1337. }
  1338. std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1339. mf2Dir += "Makefile2";
  1340. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1341. dir.c_str()));
  1342. this->CreateCDCommand(commands,
  1343. this->Makefile->GetHomeOutputDirectory(),
  1344. this->Makefile->GetStartOutputDirectory());
  1345. if (!this->Parent)
  1346. {
  1347. cmOStringStream progCmd;
  1348. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1349. progCmd << this->Convert(progressDir.c_str(),
  1350. cmLocalGenerator::FULL,
  1351. cmLocalGenerator::SHELL);
  1352. progCmd << " 0";
  1353. commands.push_back(progCmd.str());
  1354. }
  1355. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1356. depends, commands, true);
  1357. // Write the clean rule.
  1358. dir = this->Makefile->GetStartOutputDirectory();
  1359. dir += "/clean";
  1360. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1361. commands.clear();
  1362. depends.clear();
  1363. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1364. dir.c_str()));
  1365. this->CreateCDCommand(commands,
  1366. this->Makefile->GetHomeOutputDirectory(),
  1367. this->Makefile->GetStartOutputDirectory());
  1368. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1369. depends, commands, true);
  1370. commands.clear();
  1371. depends.clear();
  1372. depends.push_back("clean");
  1373. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1374. depends, commands, true);
  1375. // Write the preinstall rule.
  1376. dir = this->Makefile->GetStartOutputDirectory();
  1377. dir += "/preinstall";
  1378. dir = this->Convert(dir.c_str(), HOME_OUTPUT,MAKEFILE);
  1379. commands.clear();
  1380. depends.clear();
  1381. const char* noall =
  1382. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1383. if(!noall || cmSystemTools::IsOff(noall))
  1384. {
  1385. // Drive the build before installing.
  1386. depends.push_back("all");
  1387. }
  1388. else
  1389. {
  1390. // At least make sure the build system is up to date.
  1391. depends.push_back("cmake_check_build_system");
  1392. }
  1393. commands.push_back
  1394. (this->GetRecursiveMakeCall(mf2Dir.c_str(), dir.c_str()));
  1395. this->CreateCDCommand(commands,
  1396. this->Makefile->GetHomeOutputDirectory(),
  1397. this->Makefile->GetStartOutputDirectory());
  1398. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1399. "preinstall", depends, commands, true);
  1400. depends.clear();
  1401. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1402. "preinstall/fast", depends, commands, true);
  1403. // write the depend rule, really a recompute depends rule
  1404. depends.clear();
  1405. commands.clear();
  1406. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  1407. cmakefileName += "Makefile.cmake";
  1408. this->Convert(cmakefileName.c_str(),HOME_OUTPUT,
  1409. cmLocalGenerator::MAKEFILE);
  1410. std::string runRule =
  1411. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1412. runRule += " --check-build-system ";
  1413. runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  1414. cmLocalGenerator::SHELL);
  1415. runRule += " 1";
  1416. commands.push_back(runRule);
  1417. this->WriteMakeRule(ruleFileStream, "clear depends",
  1418. "depend",
  1419. depends, commands, true);
  1420. }
  1421. //----------------------------------------------------------------------------
  1422. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  1423. bool verbose,
  1424. bool clear)
  1425. {
  1426. // Get the list of target files to check
  1427. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1428. if(!infoDef)
  1429. {
  1430. return;
  1431. }
  1432. std::vector<std::string> files;
  1433. cmSystemTools::ExpandListArgument(infoDef, files);
  1434. // For each info file run the check
  1435. cmDependsC checker;
  1436. checker.SetVerbose(verbose);
  1437. checker.SetFileComparison
  1438. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1439. for(std::vector<std::string>::iterator l = files.begin();
  1440. l != files.end(); ++l)
  1441. {
  1442. // either clear or check the files
  1443. std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
  1444. std::string internalDependFile = dir + "/depend.internal";
  1445. std::string dependFile = dir + "/depend.make";
  1446. if (clear)
  1447. {
  1448. checker.Clear(internalDependFile.c_str());
  1449. checker.Clear(dependFile.c_str());
  1450. }
  1451. else
  1452. {
  1453. checker.Check(dependFile.c_str(), internalDependFile.c_str());
  1454. }
  1455. }
  1456. }
  1457. void cmLocalUnixMakefileGenerator3
  1458. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1459. {
  1460. // now write all the language stuff
  1461. // Set the set of files to check for dependency integrity.
  1462. std::set<cmStdString> checkSetLangs;
  1463. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  1464. checkSet = this->GetIntegrityCheckSet()[target.GetName()];
  1465. for(std::map<cmStdString,
  1466. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  1467. l = checkSet.begin(); l != checkSet.end(); ++l)
  1468. {
  1469. checkSetLangs.insert(l->first);
  1470. }
  1471. // list the languages
  1472. cmakefileStream
  1473. << "# The set of files whose dependency integrity should be checked:\n";
  1474. cmakefileStream
  1475. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  1476. for(std::set<cmStdString>::iterator
  1477. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1478. {
  1479. cmakefileStream << " \"" << l->c_str() << "\"\n";
  1480. }
  1481. cmakefileStream << " )\n";
  1482. // now list the files for each language
  1483. for(std::set<cmStdString>::iterator
  1484. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1485. {
  1486. cmakefileStream
  1487. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  1488. // get the check set for this local gen and language
  1489. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  1490. checkSet[*l];
  1491. // for each file
  1492. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator
  1493. csIter = iCheckSet.begin();
  1494. csIter != iCheckSet.end(); ++csIter)
  1495. {
  1496. cmakefileStream << " \"" << (*csIter)->GetFullPath() << "\"\n";
  1497. // Get the full path name of the object file.
  1498. std::string obj = this->Makefile->GetStartOutputDirectory();
  1499. obj += "/";
  1500. obj += this->GetObjectFileName(target, **csIter);
  1501. cmakefileStream << " \"" <<
  1502. this->Convert(obj.c_str(),
  1503. cmLocalGenerator::FULL).c_str() << "\"\n";
  1504. }
  1505. cmakefileStream << " )\n";
  1506. }
  1507. }
  1508. //----------------------------------------------------------------------------
  1509. std::string
  1510. cmLocalUnixMakefileGenerator3
  1511. ::GetObjectFileName(cmTarget& target,
  1512. const cmSourceFile& source,
  1513. std::string* nameWithoutTargetDir)
  1514. {
  1515. // If the source file is located below the current binary directory
  1516. // then use that relative path for the object file name.
  1517. std::string objectName = this->Convert(source.GetFullPath().c_str(),
  1518. START_OUTPUT);
  1519. if(cmSystemTools::FileIsFullPath(objectName.c_str()) ||
  1520. objectName.empty() || objectName[0] == '.')
  1521. {
  1522. // If the source file is located below the current source
  1523. // directory then use that relative path for the object file name.
  1524. // Otherwise just use the relative path from the current binary
  1525. // directory.
  1526. std::string relFromSource = this->Convert(source.GetFullPath().c_str(),
  1527. START);
  1528. if(!cmSystemTools::FileIsFullPath(relFromSource.c_str()) &&
  1529. !relFromSource.empty() && relFromSource[0] != '.')
  1530. {
  1531. objectName = relFromSource;
  1532. }
  1533. }
  1534. // Replace the original source file extension with the object file
  1535. // extension.
  1536. std::string::size_type dot_pos = objectName.rfind(".");
  1537. if(dot_pos != std::string::npos)
  1538. {
  1539. objectName = objectName.substr(0, dot_pos);
  1540. }
  1541. if ( source.GetPropertyAsBool("KEEP_EXTENSION") )
  1542. {
  1543. if ( !source.GetSourceExtension().empty() )
  1544. {
  1545. objectName += "." + source.GetSourceExtension();
  1546. }
  1547. }
  1548. else
  1549. {
  1550. objectName +=
  1551. this->GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1552. source.GetSourceExtension().c_str());
  1553. }
  1554. // Convert to a safe name.
  1555. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1556. // Prepend the target directory.
  1557. std::string obj;
  1558. const char* fileTargetDirectory =
  1559. source.GetProperty("MACOSX_PACKAGE_LOCATION");
  1560. if ( fileTargetDirectory )
  1561. {
  1562. std::string targetName;
  1563. std::string targetNameReal;
  1564. target.GetExecutableNames(targetName, targetNameReal,
  1565. this->ConfigurationName.c_str());
  1566. if ( target.GetPropertyAsBool("MACOSX_BUNDLE") )
  1567. {
  1568. // Construct the full path version of the names.
  1569. obj = this->ExecutableOutputPath;
  1570. if(obj.empty())
  1571. {
  1572. obj = this->Makefile->GetStartOutputDirectory();
  1573. obj += "/";
  1574. }
  1575. obj += targetName + ".app/Contents/";
  1576. obj += fileTargetDirectory;
  1577. }
  1578. else
  1579. {
  1580. // Framework not handled yet
  1581. abort();
  1582. }
  1583. obj = cmSystemTools::RelativePath
  1584. (this->Makefile->GetHomeOutputDirectory(), obj.c_str());
  1585. }
  1586. else
  1587. {
  1588. obj = this->GetTargetDirectory(target);
  1589. }
  1590. obj += "/";
  1591. obj += objectName;
  1592. if(nameWithoutTargetDir)
  1593. {
  1594. *nameWithoutTargetDir = objectName;
  1595. }
  1596. return obj;
  1597. }
  1598. //----------------------------------------------------------------------------
  1599. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1600. {
  1601. os
  1602. << "# CMAKE generated file: DO NOT EDIT!\n"
  1603. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1604. << " Generator, CMake Version "
  1605. << cmMakefile::GetMajorVersion() << "."
  1606. << cmMakefile::GetMinorVersion() << "\n\n";
  1607. }
  1608. //----------------------------------------------------------------------------
  1609. std::string
  1610. cmLocalUnixMakefileGenerator3
  1611. ::GetRecursiveMakeCall(const char *makefile, const char* tgt)
  1612. {
  1613. // Call make on the given file.
  1614. std::string cmd;
  1615. cmd += "$(MAKE) -f ";
  1616. cmd += makefile;
  1617. cmd += " ";
  1618. // Passg down verbosity level.
  1619. if(this->GetMakeSilentFlag().size())
  1620. {
  1621. cmd += this->GetMakeSilentFlag();
  1622. cmd += " ";
  1623. }
  1624. // Most unix makes will pass the command line flags to make down to
  1625. // sub-invoked makes via an environment variable. However, some
  1626. // makes do not support that, so you have to pass the flags
  1627. // explicitly.
  1628. if(this->GetPassMakeflags())
  1629. {
  1630. cmd += "-$(MAKEFLAGS) ";
  1631. }
  1632. // Add the target.
  1633. if (tgt && tgt[0] != '\0')
  1634. {
  1635. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  1636. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  1637. cmd += tgt2;
  1638. }
  1639. return cmd;
  1640. }
  1641. //----------------------------------------------------------------------------
  1642. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1643. {
  1644. os
  1645. << "#======================================"
  1646. << "=======================================\n";
  1647. }
  1648. //----------------------------------------------------------------------------
  1649. void
  1650. cmLocalUnixMakefileGenerator3
  1651. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1652. {
  1653. // Write the given string to the stream with escaping to get it back
  1654. // into CMake through the lexical scanner.
  1655. os << "\"";
  1656. for(const char* c = s; *c; ++c)
  1657. {
  1658. if(*c == '\\')
  1659. {
  1660. os << "\\\\";
  1661. }
  1662. else if(*c == '"')
  1663. {
  1664. os << "\\\"";
  1665. }
  1666. else
  1667. {
  1668. os << *c;
  1669. }
  1670. }
  1671. os << "\"";
  1672. }
  1673. //----------------------------------------------------------------------------
  1674. std::string
  1675. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1676. {
  1677. // Split the path into its components.
  1678. std::vector<std::string> components;
  1679. cmSystemTools::SplitPath(p, components);
  1680. // Return an empty path if there are no components.
  1681. if(components.empty())
  1682. {
  1683. return "\"\"";
  1684. }
  1685. // Choose a slash direction and fix root component.
  1686. const char* slash = "/";
  1687. #if defined(_WIN32) && !defined(__CYGWIN__)
  1688. if(!cmSystemTools::GetForceUnixPaths())
  1689. {
  1690. slash = "\\";
  1691. for(std::string::iterator i = components[0].begin();
  1692. i != components[0].end(); ++i)
  1693. {
  1694. if(*i == '/')
  1695. {
  1696. *i = '\\';
  1697. }
  1698. }
  1699. }
  1700. #endif
  1701. // Begin the quoted result with the root component.
  1702. std::string result = "\"";
  1703. result += components[0];
  1704. // Now add the rest of the components separated by the proper slash
  1705. // direction for this platform.
  1706. bool first = true;
  1707. for(unsigned int i=1; i < components.size(); ++i)
  1708. {
  1709. // Only the last component can be empty to avoid double slashes.
  1710. if(components[i].length() > 0 || (i == (components.size()-1)))
  1711. {
  1712. if(!first)
  1713. {
  1714. result += slash;
  1715. }
  1716. result += components[i];
  1717. first = false;
  1718. }
  1719. }
  1720. // Close the quoted result.
  1721. result += "\"";
  1722. return result;
  1723. }
  1724. //----------------------------------------------------------------------------
  1725. std::string
  1726. cmLocalUnixMakefileGenerator3::GetTargetDirectory(cmTarget& target)
  1727. {
  1728. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1729. dir += target.GetName();
  1730. dir += ".dir";
  1731. return dir;
  1732. }
  1733. void cmLocalUnixMakefileGenerator3
  1734. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  1735. const char *retDir)
  1736. {
  1737. // do we need to cd?
  1738. if (!strcmp(tgtDir,retDir))
  1739. {
  1740. return;
  1741. }
  1742. if(!this->UnixCD)
  1743. {
  1744. // On Windows we must perform each step separately and then change
  1745. // back because the shell keeps the working directory between
  1746. // commands.
  1747. std::string cmd = "cd ";
  1748. cmd += this->ConvertToOutputForExisting(tgtDir);
  1749. commands.insert(commands.begin(),cmd);
  1750. // Change back to the starting directory. Any trailing slash must be
  1751. // removed to avoid problems with Borland Make.
  1752. std::string back = retDir;
  1753. if(back.size() && back[back.size()-1] == '/')
  1754. {
  1755. back = back.substr(0, back.size()-1);
  1756. }
  1757. cmd = "cd ";
  1758. cmd += this->ConvertToOutputForExisting(back.c_str());
  1759. commands.push_back(cmd);
  1760. }
  1761. else
  1762. {
  1763. // On UNIX we must construct a single shell command to change
  1764. // directory and build because make resets the directory between
  1765. // each command.
  1766. std::vector<std::string>::iterator i = commands.begin();
  1767. for (; i != commands.end(); ++i)
  1768. {
  1769. std::string cmd = "cd ";
  1770. cmd += this->ConvertToOutputForExisting(tgtDir);
  1771. cmd += " && ";
  1772. cmd += *i;
  1773. *i = cmd;
  1774. }
  1775. }
  1776. }
  1777. //----------------------------------------------------------------------------
  1778. const char*
  1779. cmLocalUnixMakefileGenerator3
  1780. ::GetSourceFileLanguage(const cmSourceFile& source)
  1781. {
  1782. const char* lang = source.GetProperty("LANGUAGE");
  1783. if ( lang )
  1784. {
  1785. return lang;
  1786. }
  1787. // Identify the language of the source file.
  1788. return (this->GlobalGenerator
  1789. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1790. }