cmLocalUnixMakefileGenerator3.cxx 71 KB

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