cmLocalUnixMakefileGenerator3.cxx 76 KB

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