cmLocalUnixMakefileGenerator3.cxx 75 KB

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