cmLocalUnixMakefileGenerator3.cxx 77 KB

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