cmLocalUnixMakefileGenerator3.cxx 76 KB

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