cmLocalUnixMakefileGenerator3.cxx 72 KB

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