cmLocalUnixMakefileGenerator3.cxx 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  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. for(std::vector<std::string>::const_iterator i = commands.begin();
  662. i != commands.end(); ++i)
  663. {
  664. replace = *i;
  665. os << "\t" << replace << "\n";
  666. }
  667. if(symbolic && !this->WatcomWMake)
  668. {
  669. os << ".PHONY : " << cmMakeSafe(tgt) << "\n";
  670. }
  671. os << "\n";
  672. // Add the output to the local help if requested.
  673. if(in_help)
  674. {
  675. this->LocalHelp.insert(this->LocalHelp.end(),
  676. outputs.begin(), outputs.end());
  677. }
  678. }
  679. //----------------------------------------------------------------------------
  680. std::string
  681. cmLocalUnixMakefileGenerator3
  682. ::ConvertShellCommand(std::string const& cmd, RelativeRoot root)
  683. {
  684. if(this->WatcomWMake &&
  685. cmSystemTools::FileIsFullPath(cmd.c_str()) &&
  686. cmd.find_first_of("( )") != cmd.npos)
  687. {
  688. // On Watcom WMake use the windows short path for the command
  689. // name. This is needed to avoid funny quoting problems on
  690. // lines with shell redirection operators.
  691. std::string scmd;
  692. if(cmSystemTools::GetShortPath(cmd, scmd))
  693. {
  694. return this->Convert(scmd, NONE, SHELL);
  695. }
  696. }
  697. return this->Convert(cmd, root, SHELL);
  698. }
  699. //----------------------------------------------------------------------------
  700. void
  701. cmLocalUnixMakefileGenerator3
  702. ::WriteMakeVariables(std::ostream& makefileStream)
  703. {
  704. this->WriteDivider(makefileStream);
  705. makefileStream
  706. << "# Set environment variables for the build.\n"
  707. << "\n";
  708. if(this->DefineWindowsNULL)
  709. {
  710. makefileStream
  711. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  712. << "NULL=\n"
  713. << "!ELSE\n"
  714. << "NULL=nul\n"
  715. << "!ENDIF\n";
  716. }
  717. if(this->WindowsShell)
  718. {
  719. makefileStream
  720. << "SHELL = cmd.exe\n"
  721. << "\n";
  722. }
  723. else
  724. {
  725. #if !defined(__VMS)
  726. makefileStream
  727. << "# The shell in which to execute make rules.\n"
  728. << "SHELL = /bin/sh\n"
  729. << "\n";
  730. #endif
  731. }
  732. std::string cmakecommand =
  733. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  734. makefileStream
  735. << "# The CMake executable.\n"
  736. << "CMAKE_COMMAND = "
  737. << this->ConvertShellCommand(cmakecommand, FULL)
  738. << "\n"
  739. << "\n";
  740. makefileStream
  741. << "# The command to remove a file.\n"
  742. << "RM = "
  743. << this->ConvertShellCommand(cmakecommand, FULL)
  744. << " -E remove -f\n"
  745. << "\n";
  746. makefileStream
  747. << "# Escaping for special characters.\n"
  748. << "EQUALS = =\n"
  749. << "\n";
  750. makefileStream
  751. << "# The top-level source directory on which CMake was run.\n"
  752. << "CMAKE_SOURCE_DIR = "
  753. << this->Convert(this->Makefile->GetHomeDirectory(), FULL, SHELL)
  754. << "\n"
  755. << "\n";
  756. makefileStream
  757. << "# The top-level build directory on which CMake was run.\n"
  758. << "CMAKE_BINARY_DIR = "
  759. << this->Convert(this->Makefile->GetHomeOutputDirectory(), FULL, SHELL)
  760. << "\n"
  761. << "\n";
  762. }
  763. //----------------------------------------------------------------------------
  764. void
  765. cmLocalUnixMakefileGenerator3
  766. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  767. {
  768. this->WriteDivider(makefileStream);
  769. makefileStream
  770. << "# Special targets provided by cmake.\n"
  771. << "\n";
  772. std::vector<std::string> no_commands;
  773. std::vector<std::string> no_depends;
  774. // Special target to cleanup operation of make tool.
  775. // This should be the first target except for the default_target in
  776. // the interface Makefile.
  777. this->WriteMakeRule(
  778. makefileStream, "Disable implicit rules so canonical targets will work.",
  779. ".SUFFIXES", no_depends, no_commands, false);
  780. if(!this->NMake && !this->WatcomWMake && !this->BorlandMakeCurlyHack)
  781. {
  782. // turn off RCS and SCCS automatic stuff from gmake
  783. makefileStream
  784. << "# Remove some rules from gmake that .SUFFIXES does not remove.\n"
  785. << "SUFFIXES =\n\n";
  786. }
  787. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  788. std::vector<std::string> depends;
  789. depends.push_back(".hpux_make_needs_suffix_list");
  790. this->WriteMakeRule(makefileStream, 0,
  791. ".SUFFIXES", depends, no_commands, false);
  792. if(this->WatcomWMake)
  793. {
  794. // Switch on WMake feature, if an error or interrupt occurs during
  795. // makefile processing, the current target being made may be deleted
  796. // without prompting (the same as command line -e option).
  797. makefileStream <<
  798. "\n"
  799. ".ERASE\n"
  800. "\n"
  801. ;
  802. }
  803. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  804. {
  805. makefileStream
  806. << "# Produce verbose output by default.\n"
  807. << "VERBOSE = 1\n"
  808. << "\n";
  809. }
  810. if(this->WatcomWMake)
  811. {
  812. makefileStream <<
  813. "!ifndef VERBOSE\n"
  814. ".SILENT\n"
  815. "!endif\n"
  816. "\n"
  817. ;
  818. }
  819. else
  820. {
  821. // Write special target to silence make output. This must be after
  822. // the default target in case VERBOSE is set (which changes the
  823. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  824. // "VERBOSE=1" to be added as a make variable which will change the
  825. // name of this special target. This gives a make-time choice to
  826. // the user.
  827. this->WriteMakeRule(makefileStream,
  828. "Suppress display of executed commands.",
  829. "$(VERBOSE).SILENT",
  830. no_depends,
  831. no_commands, false);
  832. }
  833. // Work-around for makes that drop rules that have no dependencies
  834. // or commands.
  835. cmGlobalUnixMakefileGenerator3* gg =
  836. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  837. std::string hack = gg->GetEmptyRuleHackDepends();
  838. if(!hack.empty())
  839. {
  840. no_depends.push_back(hack);
  841. }
  842. std::string hack_cmd = gg->GetEmptyRuleHackCommand();
  843. if(!hack_cmd.empty())
  844. {
  845. no_commands.push_back(hack_cmd);
  846. }
  847. // Special symbolic target that never exists to force dependers to
  848. // run their rules.
  849. this->WriteMakeRule
  850. (makefileStream,
  851. "A target that is always out of date.",
  852. "cmake_force", no_depends, no_commands, true);
  853. // Variables for reference by other rules.
  854. this->WriteMakeVariables(makefileStream);
  855. }
  856. //----------------------------------------------------------------------------
  857. void cmLocalUnixMakefileGenerator3
  858. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  859. {
  860. this->WriteDivider(makefileStream);
  861. makefileStream
  862. << "# Special targets to cleanup operation of make.\n"
  863. << "\n";
  864. // Write special "cmake_check_build_system" target to run cmake with
  865. // the --check-build-system flag.
  866. {
  867. // Build command to run CMake to check if anything needs regenerating.
  868. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  869. cmakefileName += "Makefile.cmake";
  870. std::string runRule =
  871. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  872. runRule += " --check-build-system ";
  873. runRule += this->Convert(cmakefileName,NONE,SHELL);
  874. runRule += " 0";
  875. std::vector<std::string> no_depends;
  876. std::vector<std::string> commands;
  877. commands.push_back(runRule);
  878. if(this->Parent)
  879. {
  880. this->CreateCDCommand(commands,
  881. this->Makefile->GetHomeOutputDirectory(),
  882. cmLocalGenerator::START_OUTPUT);
  883. }
  884. this->WriteMakeRule(makefileStream,
  885. "Special rule to run CMake to check the build system "
  886. "integrity.\n"
  887. "No rule that depends on this can have "
  888. "commands that come from listfiles\n"
  889. "because they might be regenerated.",
  890. "cmake_check_build_system",
  891. no_depends,
  892. commands, true);
  893. }
  894. }
  895. //----------------------------------------------------------------------------
  896. void
  897. cmLocalUnixMakefileGenerator3
  898. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  899. const std::string& realTarget,
  900. const std::string& helpTarget)
  901. {
  902. // A rule is only needed if the names are different.
  903. if(realTarget != helpTarget)
  904. {
  905. // The helper target depends on the real target.
  906. std::vector<std::string> depends;
  907. depends.push_back(realTarget);
  908. // There are no commands.
  909. std::vector<std::string> no_commands;
  910. // Write the rule.
  911. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  912. helpTarget, depends, no_commands, true);
  913. }
  914. }
  915. //----------------------------------------------------------------------------
  916. std::string
  917. cmLocalUnixMakefileGenerator3
  918. ::GetRelativeTargetDirectory(cmTarget const& target)
  919. {
  920. std::string dir = this->HomeRelativeOutputPath;
  921. dir += this->GetTargetDirectory(target);
  922. return this->Convert(dir,NONE,UNCHANGED);
  923. }
  924. //----------------------------------------------------------------------------
  925. void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
  926. const std::string& newFlags)
  927. {
  928. if(this->WatcomWMake && !newFlags.empty())
  929. {
  930. std::string newf = newFlags;
  931. if(newf.find("\\\"") != newf.npos)
  932. {
  933. cmSystemTools::ReplaceString(newf, "\\\"", "\"");
  934. this->cmLocalGenerator::AppendFlags(flags, newf);
  935. return;
  936. }
  937. }
  938. this->cmLocalGenerator::AppendFlags(flags, newFlags);
  939. }
  940. //----------------------------------------------------------------------------
  941. void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
  942. const char* newFlags)
  943. {
  944. this->cmLocalGenerator::AppendFlags(flags, newFlags);
  945. }
  946. //----------------------------------------------------------------------------
  947. void
  948. cmLocalUnixMakefileGenerator3
  949. ::AppendRuleDepend(std::vector<std::string>& depends,
  950. const char* ruleFileName)
  951. {
  952. // Add a dependency on the rule file itself unless an option to skip
  953. // it is specifically enabled by the user or project.
  954. const char* nodep =
  955. this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  956. if(!nodep || cmSystemTools::IsOff(nodep))
  957. {
  958. depends.push_back(ruleFileName);
  959. }
  960. }
  961. //----------------------------------------------------------------------------
  962. void
  963. cmLocalUnixMakefileGenerator3
  964. ::AppendRuleDepends(std::vector<std::string>& depends,
  965. std::vector<std::string> const& ruleFiles)
  966. {
  967. // Add a dependency on the rule file itself unless an option to skip
  968. // it is specifically enabled by the user or project.
  969. if(!this->Makefile->IsOn("CMAKE_SKIP_RULE_DEPENDENCY"))
  970. {
  971. depends.insert(depends.end(), ruleFiles.begin(), ruleFiles.end());
  972. }
  973. }
  974. //----------------------------------------------------------------------------
  975. void
  976. cmLocalUnixMakefileGenerator3
  977. ::AppendCustomDepends(std::vector<std::string>& depends,
  978. const std::vector<cmCustomCommand>& ccs)
  979. {
  980. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  981. i != ccs.end(); ++i)
  982. {
  983. cmCustomCommandGenerator ccg(*i, this->ConfigurationName,
  984. this->Makefile);
  985. this->AppendCustomDepend(depends, ccg);
  986. }
  987. }
  988. //----------------------------------------------------------------------------
  989. void
  990. cmLocalUnixMakefileGenerator3
  991. ::AppendCustomDepend(std::vector<std::string>& depends,
  992. cmCustomCommandGenerator const& ccg)
  993. {
  994. for(std::vector<std::string>::const_iterator d = ccg.GetDepends().begin();
  995. d != ccg.GetDepends().end(); ++d)
  996. {
  997. // Lookup the real name of the dependency in case it is a CMake target.
  998. std::string dep;
  999. if(this->GetRealDependency(*d, this->ConfigurationName,
  1000. dep))
  1001. {
  1002. depends.push_back(dep);
  1003. }
  1004. }
  1005. }
  1006. //----------------------------------------------------------------------------
  1007. void
  1008. cmLocalUnixMakefileGenerator3
  1009. ::AppendCustomCommands(std::vector<std::string>& commands,
  1010. const std::vector<cmCustomCommand>& ccs,
  1011. cmTarget* target,
  1012. cmLocalGenerator::RelativeRoot relative)
  1013. {
  1014. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1015. i != ccs.end(); ++i)
  1016. {
  1017. cmCustomCommandGenerator ccg(*i, this->ConfigurationName,
  1018. this->Makefile);
  1019. this->AppendCustomCommand(commands, ccg, target, true, relative);
  1020. }
  1021. }
  1022. //----------------------------------------------------------------------------
  1023. void
  1024. cmLocalUnixMakefileGenerator3
  1025. ::AppendCustomCommand(std::vector<std::string>& commands,
  1026. cmCustomCommandGenerator const& ccg,
  1027. cmTarget* target,
  1028. bool echo_comment,
  1029. cmLocalGenerator::RelativeRoot relative,
  1030. std::ostream* content)
  1031. {
  1032. // Optionally create a command to display the custom command's
  1033. // comment text. This is used for pre-build, pre-link, and
  1034. // post-build command comments. Custom build step commands have
  1035. // their comments generated elsewhere.
  1036. if(echo_comment)
  1037. {
  1038. const char* comment = ccg.GetComment();
  1039. if(comment && !*comment)
  1040. {
  1041. this->AppendEcho(commands, comment,
  1042. cmLocalUnixMakefileGenerator3::EchoGenerate);
  1043. }
  1044. }
  1045. // if the command specified a working directory use it.
  1046. std::string dir = this->Makefile->GetStartOutputDirectory();
  1047. std::string workingDir = ccg.GetWorkingDirectory();
  1048. if(!workingDir.empty())
  1049. {
  1050. dir = workingDir;
  1051. }
  1052. if(content)
  1053. {
  1054. *content << dir;
  1055. }
  1056. // Add each command line to the set of commands.
  1057. std::vector<std::string> commands1;
  1058. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  1059. {
  1060. // Build the command line in a single string.
  1061. std::string cmd = ccg.GetCommand(c);
  1062. if (!cmd.empty())
  1063. {
  1064. // Use "call " before any invocations of .bat or .cmd files
  1065. // invoked as custom commands in the WindowsShell.
  1066. //
  1067. bool useCall = false;
  1068. if (this->WindowsShell)
  1069. {
  1070. std::string suffix;
  1071. if (cmd.size() > 4)
  1072. {
  1073. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size()-4));
  1074. if (suffix == ".bat" || suffix == ".cmd")
  1075. {
  1076. useCall = true;
  1077. }
  1078. }
  1079. }
  1080. cmSystemTools::ReplaceString(cmd, "/./", "/");
  1081. // Convert the command to a relative path only if the current
  1082. // working directory will be the start-output directory.
  1083. bool had_slash = cmd.find("/") != cmd.npos;
  1084. if(workingDir.empty())
  1085. {
  1086. cmd = this->Convert(cmd,START_OUTPUT);
  1087. }
  1088. bool has_slash = cmd.find("/") != cmd.npos;
  1089. if(had_slash && !has_slash)
  1090. {
  1091. // This command was specified as a path to a file in the
  1092. // current directory. Add a leading "./" so it can run
  1093. // without the current directory being in the search path.
  1094. cmd = "./" + cmd;
  1095. }
  1096. std::string launcher =
  1097. this->MakeLauncher(ccg, target,
  1098. workingDir.empty()? START_OUTPUT : NONE);
  1099. cmd = launcher + this->ConvertShellCommand(cmd, NONE);
  1100. ccg.AppendArguments(c, cmd);
  1101. if(content)
  1102. {
  1103. // Rule content does not include the launcher.
  1104. *content << (cmd.c_str()+launcher.size());
  1105. }
  1106. if(this->BorlandMakeCurlyHack)
  1107. {
  1108. // Borland Make has a very strange bug. If the first curly
  1109. // brace anywhere in the command string is a left curly, it
  1110. // must be written {{} instead of just {. Otherwise some
  1111. // curly braces are removed. The hack can be skipped if the
  1112. // first curly brace is the last character.
  1113. std::string::size_type lcurly = cmd.find("{");
  1114. if(lcurly != cmd.npos && lcurly < (cmd.size()-1))
  1115. {
  1116. std::string::size_type rcurly = cmd.find("}");
  1117. if(rcurly == cmd.npos || rcurly > lcurly)
  1118. {
  1119. // The first curly is a left curly. Use the hack.
  1120. std::string hack_cmd = cmd.substr(0, lcurly);
  1121. hack_cmd += "{{}";
  1122. hack_cmd += cmd.substr(lcurly+1);
  1123. cmd = hack_cmd;
  1124. }
  1125. }
  1126. }
  1127. if (launcher.empty())
  1128. {
  1129. if (useCall)
  1130. {
  1131. cmd = "call " + cmd;
  1132. }
  1133. else if (this->NMake && cmd[0]=='"')
  1134. {
  1135. cmd = "echo >nul && " + cmd;
  1136. }
  1137. }
  1138. commands1.push_back(cmd);
  1139. }
  1140. }
  1141. // Setup the proper working directory for the commands.
  1142. this->CreateCDCommand(commands1, dir.c_str(), relative);
  1143. // push back the custom commands
  1144. commands.insert(commands.end(), commands1.begin(), commands1.end());
  1145. }
  1146. //----------------------------------------------------------------------------
  1147. std::string
  1148. cmLocalUnixMakefileGenerator3::MakeLauncher(
  1149. cmCustomCommandGenerator const& ccg,
  1150. cmTarget* target, RelativeRoot relative)
  1151. {
  1152. // Short-circuit if there is no launcher.
  1153. const char* prop = "RULE_LAUNCH_CUSTOM";
  1154. const char* val = this->GetRuleLauncher(target, prop);
  1155. if(!(val && *val))
  1156. {
  1157. return "";
  1158. }
  1159. // Expand rules in the empty string. It may insert the launcher and
  1160. // perform replacements.
  1161. RuleVariables vars;
  1162. vars.RuleLauncher = prop;
  1163. vars.CMTarget = target;
  1164. std::string output;
  1165. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1166. if(!outputs.empty())
  1167. {
  1168. output = this->Convert(outputs[0], relative, SHELL);
  1169. }
  1170. vars.Output = output.c_str();
  1171. std::string launcher;
  1172. this->ExpandRuleVariables(launcher, vars);
  1173. if(!launcher.empty())
  1174. {
  1175. launcher += " ";
  1176. }
  1177. return launcher;
  1178. }
  1179. //----------------------------------------------------------------------------
  1180. void
  1181. cmLocalUnixMakefileGenerator3
  1182. ::AppendCleanCommand(std::vector<std::string>& commands,
  1183. const std::vector<std::string>& files,
  1184. cmTarget& target, const char* filename)
  1185. {
  1186. std::string cleanfile = this->Makefile->GetCurrentOutputDirectory();
  1187. cleanfile += "/";
  1188. cleanfile += this->GetTargetDirectory(target);
  1189. cleanfile += "/cmake_clean";
  1190. if(filename)
  1191. {
  1192. cleanfile += "_";
  1193. cleanfile += filename;
  1194. }
  1195. cleanfile += ".cmake";
  1196. std::string cleanfilePath = this->Convert(cleanfile, FULL);
  1197. cmsys::ofstream fout(cleanfilePath.c_str());
  1198. if(!fout)
  1199. {
  1200. cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
  1201. }
  1202. if(!files.empty())
  1203. {
  1204. fout << "file(REMOVE_RECURSE\n";
  1205. for(std::vector<std::string>::const_iterator f = files.begin();
  1206. f != files.end(); ++f)
  1207. {
  1208. std::string fc = this->Convert(*f,START_OUTPUT,UNCHANGED);
  1209. fout << " " << this->EscapeForCMake(fc) << "\n";
  1210. }
  1211. fout << ")\n";
  1212. }
  1213. std::string remove = "$(CMAKE_COMMAND) -P ";
  1214. remove += this->Convert(cleanfile, START_OUTPUT, SHELL);
  1215. commands.push_back(remove);
  1216. // For the main clean rule add per-language cleaning.
  1217. if(!filename)
  1218. {
  1219. // Get the set of source languages in the target.
  1220. std::set<std::string> languages;
  1221. target.GetLanguages(languages,
  1222. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1223. fout << "\n"
  1224. << "# Per-language clean rules from dependency scanning.\n"
  1225. << "foreach(lang";
  1226. for(std::set<std::string>::const_iterator l = languages.begin();
  1227. l != languages.end(); ++l)
  1228. {
  1229. fout << " " << *l;
  1230. }
  1231. fout << ")\n"
  1232. << " include(" << this->GetTargetDirectory(target)
  1233. << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
  1234. << "endforeach()\n";
  1235. }
  1236. }
  1237. //----------------------------------------------------------------------------
  1238. void
  1239. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  1240. std::string const& text,
  1241. EchoColor color,
  1242. EchoProgress const* progress)
  1243. {
  1244. // Choose the color for the text.
  1245. std::string color_name;
  1246. if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
  1247. {
  1248. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  1249. // This color set is readable on both black and white backgrounds.
  1250. switch(color)
  1251. {
  1252. case EchoNormal:
  1253. break;
  1254. case EchoDepend:
  1255. color_name = "--magenta --bold ";
  1256. break;
  1257. case EchoBuild:
  1258. color_name = "--green ";
  1259. break;
  1260. case EchoLink:
  1261. color_name = "--green --bold ";
  1262. break;
  1263. case EchoGenerate:
  1264. color_name = "--blue --bold ";
  1265. break;
  1266. case EchoGlobal:
  1267. color_name = "--cyan ";
  1268. break;
  1269. }
  1270. }
  1271. // Echo one line at a time.
  1272. std::string line;
  1273. line.reserve(200);
  1274. for(const char* c = text.c_str();; ++c)
  1275. {
  1276. if(*c == '\n' || *c == '\0')
  1277. {
  1278. // Avoid writing a blank last line on end-of-string.
  1279. if(*c != '\0' || !line.empty())
  1280. {
  1281. // Add a command to echo this line.
  1282. std::string cmd;
  1283. if(color_name.empty() && !progress)
  1284. {
  1285. // Use the native echo command.
  1286. cmd = "@echo ";
  1287. cmd += this->EscapeForShell(line, false, true);
  1288. }
  1289. else
  1290. {
  1291. // Use cmake to echo the text in color.
  1292. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  1293. cmd += color_name;
  1294. if (progress)
  1295. {
  1296. cmd += "--progress-dir=";
  1297. cmd += this->Convert(progress->Dir,
  1298. cmLocalGenerator::FULL,
  1299. cmLocalGenerator::SHELL);
  1300. cmd += " ";
  1301. cmd += "--progress-num=";
  1302. cmd += progress->Arg;
  1303. cmd += " ";
  1304. }
  1305. cmd += this->EscapeForShell(line);
  1306. }
  1307. commands.push_back(cmd);
  1308. }
  1309. // Reset the line to emtpy.
  1310. line = "";
  1311. // Progress appears only on first line.
  1312. progress = 0;
  1313. // Terminate on end-of-string.
  1314. if(*c == '\0')
  1315. {
  1316. return;
  1317. }
  1318. }
  1319. else if(*c != '\r')
  1320. {
  1321. // Append this character to the current line.
  1322. line += *c;
  1323. }
  1324. }
  1325. }
  1326. //----------------------------------------------------------------------------
  1327. std::string
  1328. cmLocalUnixMakefileGenerator3
  1329. ::CreateMakeVariable(const std::string& sin, const std::string& s2in)
  1330. {
  1331. std::string s = sin;
  1332. std::string s2 = s2in;
  1333. std::string unmodified = s;
  1334. unmodified += s2;
  1335. // if there is no restriction on the length of make variables
  1336. // and there are no "." characters in the string, then return the
  1337. // unmodified combination.
  1338. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  1339. && (!this->MakefileVariableSize && unmodified.find('+') == s.npos)
  1340. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  1341. {
  1342. return unmodified;
  1343. }
  1344. // see if the variable has been defined before and return
  1345. // the modified version of the variable
  1346. std::map<std::string, std::string>::iterator i =
  1347. this->MakeVariableMap.find(unmodified);
  1348. if(i != this->MakeVariableMap.end())
  1349. {
  1350. return i->second;
  1351. }
  1352. // start with the unmodified variable
  1353. std::string ret = unmodified;
  1354. // if this there is no value for this->MakefileVariableSize then
  1355. // the string must have bad characters in it
  1356. if(!this->MakefileVariableSize)
  1357. {
  1358. cmSystemTools::ReplaceString(ret, ".", "_");
  1359. cmSystemTools::ReplaceString(ret, "-", "__");
  1360. cmSystemTools::ReplaceString(ret, "+", "___");
  1361. int ni = 0;
  1362. char buffer[5];
  1363. // make sure the _ version is not already used, if
  1364. // it is used then add number to the end of the variable
  1365. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1366. {
  1367. ++ni;
  1368. sprintf(buffer, "%04d", ni);
  1369. ret = unmodified + buffer;
  1370. }
  1371. this->ShortMakeVariableMap[ret] = "1";
  1372. this->MakeVariableMap[unmodified] = ret;
  1373. return ret;
  1374. }
  1375. // if the string is greater than 32 chars it is an invalid variable name
  1376. // for borland make
  1377. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1378. {
  1379. int keep = this->MakefileVariableSize - 8;
  1380. int size = keep + 3;
  1381. std::string str1 = s;
  1382. std::string str2 = s2;
  1383. // we must shorten the combined string by 4 characters
  1384. // keep no more than 24 characters from the second string
  1385. if(static_cast<int>(str2.size()) > keep)
  1386. {
  1387. str2 = str2.substr(0, keep);
  1388. }
  1389. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1390. {
  1391. str1 = str1.substr(0, size - str2.size());
  1392. }
  1393. char buffer[5];
  1394. int ni = 0;
  1395. sprintf(buffer, "%04d", ni);
  1396. ret = str1 + str2 + buffer;
  1397. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1398. {
  1399. ++ni;
  1400. sprintf(buffer, "%04d", ni);
  1401. ret = str1 + str2 + buffer;
  1402. }
  1403. if(ni == 1000)
  1404. {
  1405. cmSystemTools::Error("Borland makefile variable length too long");
  1406. return unmodified;
  1407. }
  1408. // once an unused variable is found
  1409. this->ShortMakeVariableMap[ret] = "1";
  1410. }
  1411. // always make an entry into the unmodified to variable map
  1412. this->MakeVariableMap[unmodified] = ret;
  1413. return ret;
  1414. }
  1415. //----------------------------------------------------------------------------
  1416. bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
  1417. bool verbose,
  1418. bool color)
  1419. {
  1420. // read in the target info file
  1421. if(!this->Makefile->ReadListFile(0, tgtInfo) ||
  1422. cmSystemTools::GetErrorOccuredFlag())
  1423. {
  1424. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1425. }
  1426. // Check if any multiple output pairs have a missing file.
  1427. this->CheckMultipleOutputs(verbose);
  1428. std::string dir = cmSystemTools::GetFilenamePath(tgtInfo);
  1429. std::string internalDependFile = dir + "/depend.internal";
  1430. std::string dependFile = dir + "/depend.make";
  1431. // If the target DependInfo.cmake file has changed since the last
  1432. // time dependencies were scanned then force rescanning. This may
  1433. // happen when a new source file is added and CMake regenerates the
  1434. // project but no other sources were touched.
  1435. bool needRescanDependInfo = false;
  1436. cmFileTimeComparison* ftc =
  1437. this->GlobalGenerator->GetCMakeInstance()->GetFileComparison();
  1438. {
  1439. int result;
  1440. if(!ftc->FileTimeCompare(internalDependFile.c_str(), tgtInfo, &result) ||
  1441. result < 0)
  1442. {
  1443. if(verbose)
  1444. {
  1445. std::ostringstream msg;
  1446. msg << "Dependee \"" << tgtInfo
  1447. << "\" is newer than depender \""
  1448. << internalDependFile << "\"." << std::endl;
  1449. cmSystemTools::Stdout(msg.str().c_str());
  1450. }
  1451. needRescanDependInfo = true;
  1452. }
  1453. }
  1454. // If the directory information is newer than depend.internal, include dirs
  1455. // may have changed. In this case discard all old dependencies.
  1456. bool needRescanDirInfo = false;
  1457. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1458. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1459. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1460. {
  1461. int result;
  1462. if(!ftc->FileTimeCompare(internalDependFile.c_str(),
  1463. dirInfoFile.c_str(), &result) || result < 0)
  1464. {
  1465. if(verbose)
  1466. {
  1467. std::ostringstream msg;
  1468. msg << "Dependee \"" << dirInfoFile
  1469. << "\" is newer than depender \""
  1470. << internalDependFile << "\"." << std::endl;
  1471. cmSystemTools::Stdout(msg.str().c_str());
  1472. }
  1473. needRescanDirInfo = true;
  1474. }
  1475. }
  1476. // Check the implicit dependencies to see if they are up to date.
  1477. // The build.make file may have explicit dependencies for the object
  1478. // files but these will not affect the scanning process so they need
  1479. // not be considered.
  1480. std::map<std::string, cmDepends::DependencyVector> validDependencies;
  1481. bool needRescanDependencies = false;
  1482. if (needRescanDirInfo == false)
  1483. {
  1484. cmDependsC checker;
  1485. checker.SetVerbose(verbose);
  1486. checker.SetFileComparison(ftc);
  1487. // cmDependsC::Check() fills the vector validDependencies() with the
  1488. // dependencies for those files where they are still valid, i.e. neither
  1489. // the files themselves nor any files they depend on have changed.
  1490. // We don't do that if the CMakeDirectoryInformation.cmake file has
  1491. // changed, because then potentially all dependencies have changed.
  1492. // This information is given later on to cmDependsC, which then only
  1493. // rescans the files where it did not get valid dependencies via this
  1494. // dependency vector. This means that in the normal case, when only
  1495. // few or one file have been edited, then also only this one file is
  1496. // actually scanned again, instead of all files for this target.
  1497. needRescanDependencies = !checker.Check(dependFile.c_str(),
  1498. internalDependFile.c_str(),
  1499. validDependencies);
  1500. }
  1501. if(needRescanDependInfo || needRescanDirInfo || needRescanDependencies)
  1502. {
  1503. // The dependencies must be regenerated.
  1504. std::string targetName = cmSystemTools::GetFilenameName(dir);
  1505. targetName = targetName.substr(0, targetName.length()-4);
  1506. std::string message = "Scanning dependencies of target ";
  1507. message += targetName;
  1508. cmSystemTools::MakefileColorEcho(
  1509. cmsysTerminal_Color_ForegroundMagenta |
  1510. cmsysTerminal_Color_ForegroundBold,
  1511. message.c_str(), true, color);
  1512. return this->ScanDependencies(dir.c_str(), validDependencies);
  1513. }
  1514. // The dependencies are already up-to-date.
  1515. return true;
  1516. }
  1517. //----------------------------------------------------------------------------
  1518. bool
  1519. cmLocalUnixMakefileGenerator3
  1520. ::ScanDependencies(const char* targetDir,
  1521. std::map<std::string, cmDepends::DependencyVector>& validDeps)
  1522. {
  1523. // Read the directory information file.
  1524. cmMakefile* mf = this->Makefile;
  1525. bool haveDirectoryInfo = false;
  1526. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1527. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1528. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1529. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  1530. !cmSystemTools::GetErrorOccuredFlag())
  1531. {
  1532. haveDirectoryInfo = true;
  1533. }
  1534. // Lookup useful directory information.
  1535. if(haveDirectoryInfo)
  1536. {
  1537. // Test whether we need to force Unix paths.
  1538. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1539. {
  1540. if(!cmSystemTools::IsOff(force))
  1541. {
  1542. cmSystemTools::SetForceUnixPaths(true);
  1543. }
  1544. }
  1545. // Setup relative path top directories.
  1546. this->RelativePathsConfigured = true;
  1547. if(const char* relativePathTopSource =
  1548. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE"))
  1549. {
  1550. this->RelativePathTopSource = relativePathTopSource;
  1551. }
  1552. if(const char* relativePathTopBinary =
  1553. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY"))
  1554. {
  1555. this->RelativePathTopBinary = relativePathTopBinary;
  1556. }
  1557. }
  1558. else
  1559. {
  1560. cmSystemTools::Error("Directory Information file not found");
  1561. }
  1562. // create the file stream for the depends file
  1563. std::string dir = targetDir;
  1564. // Open the make depends file. This should be copy-if-different
  1565. // because the make tool may try to reload it needlessly otherwise.
  1566. std::string ruleFileNameFull = dir;
  1567. ruleFileNameFull += "/depend.make";
  1568. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1569. ruleFileStream.SetCopyIfDifferent(true);
  1570. if(!ruleFileStream)
  1571. {
  1572. return false;
  1573. }
  1574. // Open the cmake dependency tracking file. This should not be
  1575. // copy-if-different because dependencies are re-scanned when it is
  1576. // older than the DependInfo.cmake.
  1577. std::string internalRuleFileNameFull = dir;
  1578. internalRuleFileNameFull += "/depend.internal";
  1579. cmGeneratedFileStream
  1580. internalRuleFileStream(internalRuleFileNameFull.c_str());
  1581. if(!internalRuleFileStream)
  1582. {
  1583. return false;
  1584. }
  1585. this->WriteDisclaimer(ruleFileStream);
  1586. this->WriteDisclaimer(internalRuleFileStream);
  1587. // for each language we need to scan, scan it
  1588. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1589. std::vector<std::string> langs;
  1590. cmSystemTools::ExpandListArgument(langStr, langs);
  1591. for (std::vector<std::string>::iterator li =
  1592. langs.begin(); li != langs.end(); ++li)
  1593. {
  1594. // construct the checker
  1595. std::string lang = *li;
  1596. // Create the scanner for this language
  1597. cmDepends *scanner = 0;
  1598. if(lang == "C" || lang == "CXX" || lang == "RC" || lang == "ASM")
  1599. {
  1600. // TODO: Handle RC (resource files) dependencies correctly.
  1601. scanner = new cmDependsC(this, targetDir, lang, &validDeps);
  1602. }
  1603. #ifdef CMAKE_BUILD_WITH_CMAKE
  1604. else if(lang == "Fortran")
  1605. {
  1606. scanner = new cmDependsFortran(this);
  1607. }
  1608. else if(lang == "Java")
  1609. {
  1610. scanner = new cmDependsJava();
  1611. }
  1612. #endif
  1613. if (scanner)
  1614. {
  1615. scanner->SetLocalGenerator(this);
  1616. scanner->SetFileComparison
  1617. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1618. scanner->SetLanguage(lang);
  1619. scanner->SetTargetDirectory(dir.c_str());
  1620. scanner->Write(ruleFileStream, internalRuleFileStream);
  1621. // free the scanner for this language
  1622. delete scanner;
  1623. }
  1624. }
  1625. return true;
  1626. }
  1627. //----------------------------------------------------------------------------
  1628. void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose)
  1629. {
  1630. // Nothing populates multiple output pairs anymore, but we need to
  1631. // honor it when working in a build tree generated by an older CMake.
  1632. cmMakefile* mf = this->Makefile;
  1633. // Get the string listing the multiple output pairs.
  1634. const char* pairs_string = mf->GetDefinition("CMAKE_MULTIPLE_OUTPUT_PAIRS");
  1635. if(!pairs_string)
  1636. {
  1637. return;
  1638. }
  1639. // Convert the string to a list and preserve empty entries.
  1640. std::vector<std::string> pairs;
  1641. cmSystemTools::ExpandListArgument(pairs_string, pairs, true);
  1642. for(std::vector<std::string>::const_iterator i = pairs.begin();
  1643. i != pairs.end() && (i+1) != pairs.end();)
  1644. {
  1645. const std::string& depender = *i++;
  1646. const std::string& dependee = *i++;
  1647. // If the depender is missing then delete the dependee to make
  1648. // sure both will be regenerated.
  1649. if(cmSystemTools::FileExists(dependee.c_str()) &&
  1650. !cmSystemTools::FileExists(depender.c_str()))
  1651. {
  1652. if(verbose)
  1653. {
  1654. std::ostringstream msg;
  1655. msg << "Deleting primary custom command output \"" << dependee
  1656. << "\" because another output \""
  1657. << depender << "\" does not exist." << std::endl;
  1658. cmSystemTools::Stdout(msg.str().c_str());
  1659. }
  1660. cmSystemTools::RemoveFile(dependee);
  1661. }
  1662. }
  1663. }
  1664. //----------------------------------------------------------------------------
  1665. void cmLocalUnixMakefileGenerator3
  1666. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1667. {
  1668. this->WriteDisclaimer(ruleFileStream);
  1669. // Write the main entry point target. This must be the VERY first
  1670. // target so that make with no arguments will run it.
  1671. {
  1672. // Just depend on the all target to drive the build.
  1673. std::vector<std::string> depends;
  1674. std::vector<std::string> no_commands;
  1675. depends.push_back("all");
  1676. // Write the rule.
  1677. this->WriteMakeRule(ruleFileStream,
  1678. "Default target executed when no arguments are "
  1679. "given to make.",
  1680. "default_target",
  1681. depends,
  1682. no_commands, true);
  1683. // Help out users that try "gmake target1 target2 -j".
  1684. cmGlobalUnixMakefileGenerator3* gg =
  1685. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  1686. if(gg->AllowNotParallel())
  1687. {
  1688. std::vector<std::string> no_depends;
  1689. this->WriteMakeRule(ruleFileStream,
  1690. "Allow only one \"make -f Makefile2\" at a time, but pass parallelism.",
  1691. ".NOTPARALLEL", no_depends, no_commands, false);
  1692. }
  1693. }
  1694. this->WriteSpecialTargetsTop(ruleFileStream);
  1695. // Include the progress variables for the target.
  1696. // Write all global targets
  1697. this->WriteDivider(ruleFileStream);
  1698. ruleFileStream
  1699. << "# Targets provided globally by CMake.\n"
  1700. << "\n";
  1701. cmTargets* targets = &(this->Makefile->GetTargets());
  1702. cmTargets::iterator glIt;
  1703. for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
  1704. {
  1705. if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  1706. {
  1707. std::string targetString = "Special rule for the target " + glIt->first;
  1708. std::vector<std::string> commands;
  1709. std::vector<std::string> depends;
  1710. const char* text = glIt->second.GetProperty("EchoString");
  1711. if ( !text )
  1712. {
  1713. text = "Running external command ...";
  1714. }
  1715. depends.insert(depends.end(), glIt->second.GetUtilities().begin(),
  1716. glIt->second.GetUtilities().end());
  1717. this->AppendEcho(commands, text,
  1718. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1719. // Global targets store their rules in pre- and post-build commands.
  1720. this->AppendCustomDepends(depends,
  1721. glIt->second.GetPreBuildCommands());
  1722. this->AppendCustomDepends(depends,
  1723. glIt->second.GetPostBuildCommands());
  1724. this->AppendCustomCommands(commands,
  1725. glIt->second.GetPreBuildCommands(),
  1726. &glIt->second,
  1727. cmLocalGenerator::START_OUTPUT);
  1728. this->AppendCustomCommands(commands,
  1729. glIt->second.GetPostBuildCommands(),
  1730. &glIt->second,
  1731. cmLocalGenerator::START_OUTPUT);
  1732. std::string targetName = glIt->second.GetName();
  1733. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1734. targetName, depends, commands, true);
  1735. // Provide a "/fast" version of the target.
  1736. depends.clear();
  1737. if((targetName == "install")
  1738. || (targetName == "install_local")
  1739. || (targetName == "install_strip"))
  1740. {
  1741. // Provide a fast install target that does not depend on all
  1742. // but has the same command.
  1743. depends.push_back("preinstall/fast");
  1744. }
  1745. else
  1746. {
  1747. // Just forward to the real target so at least it will work.
  1748. depends.push_back(targetName);
  1749. commands.clear();
  1750. }
  1751. targetName += "/fast";
  1752. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1753. targetName, depends, commands, true);
  1754. }
  1755. }
  1756. std::vector<std::string> depends;
  1757. std::vector<std::string> commands;
  1758. // Write the all rule.
  1759. std::string dir;
  1760. std::string recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1761. recursiveTarget += "/all";
  1762. depends.push_back("cmake_check_build_system");
  1763. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1764. progressDir += cmake::GetCMakeFilesDirectory();
  1765. {
  1766. std::ostringstream progCmd;
  1767. progCmd <<
  1768. "$(CMAKE_COMMAND) -E cmake_progress_start ";
  1769. progCmd << this->Convert(progressDir,
  1770. cmLocalGenerator::FULL,
  1771. cmLocalGenerator::SHELL);
  1772. std::string progressFile = cmake::GetCMakeFilesDirectory();
  1773. progressFile += "/progress.marks";
  1774. std::string progressFileNameFull =
  1775. this->ConvertToFullPath(progressFile);
  1776. progCmd << " " << this->Convert(progressFileNameFull,
  1777. cmLocalGenerator::FULL,
  1778. cmLocalGenerator::SHELL);
  1779. commands.push_back(progCmd.str());
  1780. }
  1781. std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1782. mf2Dir += "Makefile2";
  1783. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1784. recursiveTarget));
  1785. this->CreateCDCommand(commands,
  1786. this->Makefile->GetHomeOutputDirectory(),
  1787. cmLocalGenerator::START_OUTPUT);
  1788. {
  1789. std::ostringstream progCmd;
  1790. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1791. progCmd << this->Convert(progressDir,
  1792. cmLocalGenerator::FULL,
  1793. cmLocalGenerator::SHELL);
  1794. progCmd << " 0";
  1795. commands.push_back(progCmd.str());
  1796. }
  1797. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1798. depends, commands, true);
  1799. // Write the clean rule.
  1800. recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1801. recursiveTarget += "/clean";
  1802. commands.clear();
  1803. depends.clear();
  1804. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1805. recursiveTarget));
  1806. this->CreateCDCommand(commands,
  1807. this->Makefile->GetHomeOutputDirectory(),
  1808. cmLocalGenerator::START_OUTPUT);
  1809. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1810. depends, commands, true);
  1811. commands.clear();
  1812. depends.clear();
  1813. depends.push_back("clean");
  1814. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1815. depends, commands, true);
  1816. // Write the preinstall rule.
  1817. recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1818. recursiveTarget += "/preinstall";
  1819. commands.clear();
  1820. depends.clear();
  1821. const char* noall =
  1822. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1823. if(!noall || cmSystemTools::IsOff(noall))
  1824. {
  1825. // Drive the build before installing.
  1826. depends.push_back("all");
  1827. }
  1828. else
  1829. {
  1830. // At least make sure the build system is up to date.
  1831. depends.push_back("cmake_check_build_system");
  1832. }
  1833. commands.push_back
  1834. (this->GetRecursiveMakeCall(mf2Dir.c_str(), recursiveTarget));
  1835. this->CreateCDCommand(commands,
  1836. this->Makefile->GetHomeOutputDirectory(),
  1837. cmLocalGenerator::START_OUTPUT);
  1838. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1839. "preinstall", depends, commands, true);
  1840. depends.clear();
  1841. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1842. "preinstall/fast", depends, commands, true);
  1843. // write the depend rule, really a recompute depends rule
  1844. depends.clear();
  1845. commands.clear();
  1846. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  1847. cmakefileName += "Makefile.cmake";
  1848. std::string runRule =
  1849. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1850. runRule += " --check-build-system ";
  1851. runRule += this->Convert(cmakefileName,cmLocalGenerator::NONE,
  1852. cmLocalGenerator::SHELL);
  1853. runRule += " 1";
  1854. commands.push_back(runRule);
  1855. this->CreateCDCommand(commands,
  1856. this->Makefile->GetHomeOutputDirectory(),
  1857. cmLocalGenerator::START_OUTPUT);
  1858. this->WriteMakeRule(ruleFileStream, "clear depends",
  1859. "depend",
  1860. depends, commands, true);
  1861. }
  1862. //----------------------------------------------------------------------------
  1863. void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile* mf,
  1864. bool verbose)
  1865. {
  1866. // Get the list of target files to check
  1867. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1868. if(!infoDef)
  1869. {
  1870. return;
  1871. }
  1872. std::vector<std::string> files;
  1873. cmSystemTools::ExpandListArgument(infoDef, files);
  1874. // Each depend information file corresponds to a target. Clear the
  1875. // dependencies for that target.
  1876. cmDepends clearer;
  1877. clearer.SetVerbose(verbose);
  1878. for(std::vector<std::string>::iterator l = files.begin();
  1879. l != files.end(); ++l)
  1880. {
  1881. std::string dir = cmSystemTools::GetFilenamePath(*l);
  1882. // Clear the implicit dependency makefile.
  1883. std::string dependFile = dir + "/depend.make";
  1884. clearer.Clear(dependFile.c_str());
  1885. // Remove the internal dependency check file to force
  1886. // regeneration.
  1887. std::string internalDependFile = dir + "/depend.internal";
  1888. cmSystemTools::RemoveFile(internalDependFile);
  1889. }
  1890. }
  1891. void cmLocalUnixMakefileGenerator3
  1892. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1893. {
  1894. ImplicitDependLanguageMap const& implicitLangs =
  1895. this->GetImplicitDepends(target);
  1896. // list the languages
  1897. cmakefileStream
  1898. << "# The set of languages for which implicit dependencies are needed:\n";
  1899. cmakefileStream
  1900. << "set(CMAKE_DEPENDS_LANGUAGES\n";
  1901. for(ImplicitDependLanguageMap::const_iterator
  1902. l = implicitLangs.begin(); l != implicitLangs.end(); ++l)
  1903. {
  1904. cmakefileStream << " \"" << l->first << "\"\n";
  1905. }
  1906. cmakefileStream << " )\n";
  1907. // now list the files for each language
  1908. cmakefileStream
  1909. << "# The set of files for implicit dependencies of each language:\n";
  1910. for(ImplicitDependLanguageMap::const_iterator
  1911. l = implicitLangs.begin(); l != implicitLangs.end(); ++l)
  1912. {
  1913. cmakefileStream
  1914. << "set(CMAKE_DEPENDS_CHECK_" << l->first << "\n";
  1915. ImplicitDependFileMap const& implicitPairs = l->second;
  1916. // for each file pair
  1917. for(ImplicitDependFileMap::const_iterator pi = implicitPairs.begin();
  1918. pi != implicitPairs.end(); ++pi)
  1919. {
  1920. for(cmDepends::DependencyVector::const_iterator di = pi->second.begin();
  1921. di != pi->second.end(); ++ di)
  1922. {
  1923. cmakefileStream << " \"" << *di << "\" ";
  1924. cmakefileStream << "\"" << pi->first << "\"\n";
  1925. }
  1926. }
  1927. cmakefileStream << " )\n";
  1928. // Tell the dependency scanner what compiler is used.
  1929. std::string cidVar = "CMAKE_";
  1930. cidVar += l->first;
  1931. cidVar += "_COMPILER_ID";
  1932. const char* cid = this->Makefile->GetDefinition(cidVar);
  1933. if(cid && *cid)
  1934. {
  1935. cmakefileStream
  1936. << "set(CMAKE_" << l->first << "_COMPILER_ID \""
  1937. << cid << "\")\n";
  1938. }
  1939. }
  1940. // Build a list of preprocessor definitions for the target.
  1941. std::set<std::string> defines;
  1942. this->AddCompileDefinitions(defines, &target,
  1943. this->ConfigurationName);
  1944. if(!defines.empty())
  1945. {
  1946. cmakefileStream
  1947. << "\n"
  1948. << "# Preprocessor definitions for this target.\n"
  1949. << "set(CMAKE_TARGET_DEFINITIONS\n";
  1950. for(std::set<std::string>::const_iterator di = defines.begin();
  1951. di != defines.end(); ++di)
  1952. {
  1953. cmakefileStream
  1954. << " " << this->EscapeForCMake(*di) << "\n";
  1955. }
  1956. cmakefileStream
  1957. << " )\n";
  1958. }
  1959. // Store include transform rule properties. Write the directory
  1960. // rules first because they may be overridden by later target rules.
  1961. std::vector<std::string> transformRules;
  1962. if(const char* xform =
  1963. this->Makefile->GetProperty("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM"))
  1964. {
  1965. cmSystemTools::ExpandListArgument(xform, transformRules);
  1966. }
  1967. if(const char* xform =
  1968. target.GetProperty("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM"))
  1969. {
  1970. cmSystemTools::ExpandListArgument(xform, transformRules);
  1971. }
  1972. if(!transformRules.empty())
  1973. {
  1974. cmakefileStream
  1975. << "set(CMAKE_INCLUDE_TRANSFORMS\n";
  1976. for(std::vector<std::string>::const_iterator tri = transformRules.begin();
  1977. tri != transformRules.end(); ++tri)
  1978. {
  1979. cmakefileStream << " " << this->EscapeForCMake(*tri) << "\n";
  1980. }
  1981. cmakefileStream
  1982. << " )\n";
  1983. }
  1984. }
  1985. //----------------------------------------------------------------------------
  1986. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1987. {
  1988. os
  1989. << "# CMAKE generated file: DO NOT EDIT!\n"
  1990. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1991. << " Generator, CMake Version "
  1992. << cmVersion::GetMajorVersion() << "."
  1993. << cmVersion::GetMinorVersion() << "\n\n";
  1994. }
  1995. //----------------------------------------------------------------------------
  1996. std::string
  1997. cmLocalUnixMakefileGenerator3
  1998. ::GetRecursiveMakeCall(const char *makefile, const std::string& tgt)
  1999. {
  2000. // Call make on the given file.
  2001. std::string cmd;
  2002. cmd += "$(MAKE) -f ";
  2003. cmd += this->Convert(makefile,NONE,SHELL);
  2004. cmd += " ";
  2005. // Pass down verbosity level.
  2006. if(!this->GetMakeSilentFlag().empty())
  2007. {
  2008. cmd += this->GetMakeSilentFlag();
  2009. cmd += " ";
  2010. }
  2011. // Most unix makes will pass the command line flags to make down to
  2012. // sub-invoked makes via an environment variable. However, some
  2013. // makes do not support that, so you have to pass the flags
  2014. // explicitly.
  2015. if(this->GetPassMakeflags())
  2016. {
  2017. cmd += "-$(MAKEFLAGS) ";
  2018. }
  2019. // Add the target.
  2020. if (!tgt.empty())
  2021. {
  2022. // The make target is always relative to the top of the build tree.
  2023. std::string tgt2 = this->Convert(tgt, HOME_OUTPUT);
  2024. // The target may have been written with windows paths.
  2025. cmSystemTools::ConvertToOutputSlashes(tgt2);
  2026. // Escape one extra time if the make tool requires it.
  2027. if(this->MakeCommandEscapeTargetTwice)
  2028. {
  2029. tgt2 = this->EscapeForShell(tgt2, true, false);
  2030. }
  2031. // The target name is now a string that should be passed verbatim
  2032. // on the command line.
  2033. cmd += this->EscapeForShell(tgt2, true, false);
  2034. }
  2035. return cmd;
  2036. }
  2037. //----------------------------------------------------------------------------
  2038. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  2039. {
  2040. os
  2041. << "#======================================"
  2042. << "=======================================\n";
  2043. }
  2044. //----------------------------------------------------------------------------
  2045. void
  2046. cmLocalUnixMakefileGenerator3
  2047. ::WriteCMakeArgument(std::ostream& os, const char* s)
  2048. {
  2049. // Write the given string to the stream with escaping to get it back
  2050. // into CMake through the lexical scanner.
  2051. os << "\"";
  2052. for(const char* c = s; *c; ++c)
  2053. {
  2054. if(*c == '\\')
  2055. {
  2056. os << "\\\\";
  2057. }
  2058. else if(*c == '"')
  2059. {
  2060. os << "\\\"";
  2061. }
  2062. else
  2063. {
  2064. os << *c;
  2065. }
  2066. }
  2067. os << "\"";
  2068. }
  2069. //----------------------------------------------------------------------------
  2070. std::string
  2071. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p,
  2072. bool useWatcomQuote)
  2073. {
  2074. // Split the path into its components.
  2075. std::vector<std::string> components;
  2076. cmSystemTools::SplitPath(p, components);
  2077. // Open the quoted result.
  2078. std::string result;
  2079. if(useWatcomQuote)
  2080. {
  2081. #if defined(_WIN32) && !defined(__CYGWIN__)
  2082. result = "'";
  2083. #else
  2084. result = "\"'";
  2085. #endif
  2086. }
  2087. else
  2088. {
  2089. result = "\"";
  2090. }
  2091. // Return an empty path if there are no components.
  2092. if(!components.empty())
  2093. {
  2094. // Choose a slash direction and fix root component.
  2095. const char* slash = "/";
  2096. #if defined(_WIN32) && !defined(__CYGWIN__)
  2097. if(!cmSystemTools::GetForceUnixPaths())
  2098. {
  2099. slash = "\\";
  2100. for(std::string::iterator i = components[0].begin();
  2101. i != components[0].end(); ++i)
  2102. {
  2103. if(*i == '/')
  2104. {
  2105. *i = '\\';
  2106. }
  2107. }
  2108. }
  2109. #endif
  2110. // Begin the quoted result with the root component.
  2111. result += components[0];
  2112. if (components.size() > 1)
  2113. {
  2114. // Now add the rest of the components separated by the proper slash
  2115. // direction for this platform.
  2116. bool first = true;
  2117. for(unsigned int i=1; i < components.size() - 1; ++i)
  2118. {
  2119. if(!components[i].empty())
  2120. {
  2121. if(!first)
  2122. {
  2123. result += slash;
  2124. }
  2125. result += components[i];
  2126. first = false;
  2127. }
  2128. }
  2129. // Only the last component can be empty to avoid double slashes.
  2130. if(!first)
  2131. {
  2132. result += slash;
  2133. }
  2134. result += components.back();
  2135. }
  2136. }
  2137. // Close the quoted result.
  2138. if(useWatcomQuote)
  2139. {
  2140. #if defined(_WIN32) && !defined(__CYGWIN__)
  2141. result += "'";
  2142. #else
  2143. result += "'\"";
  2144. #endif
  2145. }
  2146. else
  2147. {
  2148. result += "\"";
  2149. }
  2150. return result;
  2151. }
  2152. //----------------------------------------------------------------------------
  2153. std::string
  2154. cmLocalUnixMakefileGenerator3
  2155. ::GetTargetDirectory(cmTarget const& target) const
  2156. {
  2157. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  2158. dir += target.GetName();
  2159. #if defined(__VMS)
  2160. dir += "_dir";
  2161. #else
  2162. dir += ".dir";
  2163. #endif
  2164. return dir;
  2165. }
  2166. //----------------------------------------------------------------------------
  2167. cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap const&
  2168. cmLocalUnixMakefileGenerator3::GetImplicitDepends(cmTarget const& tgt)
  2169. {
  2170. return this->ImplicitDepends[tgt.GetName()];
  2171. }
  2172. //----------------------------------------------------------------------------
  2173. void
  2174. cmLocalUnixMakefileGenerator3::AddImplicitDepends(cmTarget const& tgt,
  2175. const std::string& lang,
  2176. const char* obj,
  2177. const char* src)
  2178. {
  2179. this->ImplicitDepends[tgt.GetName()][lang][obj].push_back(src);
  2180. }
  2181. //----------------------------------------------------------------------------
  2182. void cmLocalUnixMakefileGenerator3
  2183. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  2184. cmLocalGenerator::RelativeRoot relRetDir)
  2185. {
  2186. const char* retDir = this->GetRelativeRootPath(relRetDir);
  2187. // do we need to cd?
  2188. if (!strcmp(tgtDir,retDir))
  2189. {
  2190. return;
  2191. }
  2192. // In a Windows shell we must change drive letter too. The shell
  2193. // used by NMake and Borland make does not support "cd /d" so this
  2194. // feature simply cannot work with them (Borland make does not even
  2195. // support changing the drive letter with just "d:").
  2196. const char* cd_cmd = this->MinGWMake? "cd /d " : "cd ";
  2197. if(!this->UnixCD)
  2198. {
  2199. // On Windows we must perform each step separately and then change
  2200. // back because the shell keeps the working directory between
  2201. // commands.
  2202. std::string cmd = cd_cmd;
  2203. cmd += this->ConvertToOutputForExisting(tgtDir, relRetDir);
  2204. commands.insert(commands.begin(),cmd);
  2205. // Change back to the starting directory.
  2206. cmd = cd_cmd;
  2207. cmd += this->ConvertToOutputForExisting(relRetDir, tgtDir);
  2208. commands.push_back(cmd);
  2209. }
  2210. else
  2211. {
  2212. // On UNIX we must construct a single shell command to change
  2213. // directory and build because make resets the directory between
  2214. // each command.
  2215. std::string outputForExisting =
  2216. this->ConvertToOutputForExisting(tgtDir, relRetDir);
  2217. std::string prefix = cd_cmd + outputForExisting + " && ";
  2218. std::transform(commands.begin(), commands.end(), commands.begin(),
  2219. std::bind1st(std::plus<std::string>(), prefix));
  2220. }
  2221. }