cmLocalUnixMakefileGenerator3.cxx 73 KB

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