cmLocalUnixMakefileGenerator3.cxx 72 KB

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