cmLocalUnixMakefileGenerator3.cxx 77 KB

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