cmLocalUnixMakefileGenerator3.cxx 76 KB

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