cmLocalUnixMakefileGenerator3.cxx 77 KB

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