1
0

cmLocalUnixMakefileGenerator3.cxx 72 KB

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