cmLocalUnixMakefileGenerator3.cxx 74 KB

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