cmLocalUnixMakefileGenerator3.cxx 77 KB

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