cmLocalUnixMakefileGenerator3.cxx 73 KB

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