cmLocalUnixMakefileGenerator3.cxx 62 KB

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