cmLocalUnixMakefileGenerator3.cxx 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmDepends.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGlobalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmMakefileTargetGenerator.h"
  19. #include "cmSourceFile.h"
  20. #include "cmake.h"
  21. #include "cmVersion.h"
  22. // Include dependency scanners for supported languages. Only the
  23. // C/C++ scanner is needed for bootstrapping CMake.
  24. #include "cmDependsC.h"
  25. #ifdef CMAKE_BUILD_WITH_CMAKE
  26. # include "cmDependsFortran.h"
  27. # include "cmDependsJava.h"
  28. #endif
  29. #include <memory> // auto_ptr
  30. #include <queue>
  31. //----------------------------------------------------------------------------
  32. cmLocalUnixMakefileGenerator3::cmLocalUnixMakefileGenerator3()
  33. {
  34. this->SilentNoColon = false;
  35. this->WindowsShell = false;
  36. this->IncludeDirective = "include";
  37. this->MakefileVariableSize = 0;
  38. this->IgnoreLibPrefix = false;
  39. this->PassMakeflags = false;
  40. this->DefineWindowsNULL = false;
  41. this->UnixCD = true;
  42. this->ForceVerboseMakefiles=false;
  43. this->ColorMakefile = false;
  44. this->SkipPreprocessedSourceRules = false;
  45. this->SkipAssemblySourceRules = false;
  46. this->NativeEchoCommand = "@echo ";
  47. this->NativeEchoWindows = true;
  48. }
  49. //----------------------------------------------------------------------------
  50. cmLocalUnixMakefileGenerator3::~cmLocalUnixMakefileGenerator3()
  51. {
  52. }
  53. //----------------------------------------------------------------------------
  54. void cmLocalUnixMakefileGenerator3::Configure()
  55. {
  56. // Include the rule file for each object.
  57. this->HomeRelativeOutputPath =
  58. cmSystemTools::RelativePath(this->Makefile->GetHomeOutputDirectory(),
  59. this->Makefile->GetStartOutputDirectory());
  60. if (this->HomeRelativeOutputPath.size())
  61. {
  62. this->HomeRelativeOutputPath += "/";
  63. }
  64. this->cmLocalGenerator::Configure();
  65. }
  66. //----------------------------------------------------------------------------
  67. void cmLocalUnixMakefileGenerator3::Generate()
  68. {
  69. // Setup our configuration variables for this directory.
  70. this->ConfigureOutputPaths();
  71. // Record whether some options are enabled to avoid checking many
  72. // times later.
  73. this->ColorMakefile = this->Makefile->IsOn("CMAKE_COLOR_MAKEFILE");
  74. this->SkipPreprocessedSourceRules =
  75. this->Makefile->IsOn("CMAKE_SKIP_PREPROCESSED_SOURCE_RULES");
  76. this->SkipAssemblySourceRules =
  77. this->Makefile->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES");
  78. // Generate the rule files for each target.
  79. cmTargets& targets = this->Makefile->GetTargets();
  80. std::string empty;
  81. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  82. {
  83. cmMakefileTargetGenerator *tg =
  84. cmMakefileTargetGenerator::New(this, t->first, &(t->second));
  85. if (tg)
  86. {
  87. this->TargetGenerators.push_back(tg);
  88. t->second.TraceVSDependencies(empty, this->Makefile);
  89. tg->WriteRuleFiles();
  90. }
  91. }
  92. // write the local Makefile
  93. this->WriteLocalMakefile();
  94. // Write the cmake file with information for this directory.
  95. this->WriteDirectoryInformationFile();
  96. }
  97. //----------------------------------------------------------------------------
  98. // return info about progress actions
  99. unsigned long cmLocalUnixMakefileGenerator3::GetNumberOfProgressActions()
  100. {
  101. unsigned long result = 0;
  102. for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
  103. this->TargetGenerators.begin();
  104. mtgIter != this->TargetGenerators.end(); ++mtgIter)
  105. {
  106. result += (*mtgIter)->GetNumberOfProgressActions();
  107. }
  108. return result;
  109. }
  110. //----------------------------------------------------------------------------
  111. // return info about progress actions
  112. unsigned long cmLocalUnixMakefileGenerator3
  113. ::GetNumberOfProgressActionsForTarget(const char *name)
  114. {
  115. for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
  116. this->TargetGenerators.begin();
  117. mtgIter != this->TargetGenerators.end(); ++mtgIter)
  118. {
  119. if (!strcmp(name,(*mtgIter)->GetTargetName()))
  120. {
  121. return (*mtgIter)->GetNumberOfProgressActions();
  122. }
  123. }
  124. return 0;
  125. }
  126. //----------------------------------------------------------------------------
  127. // writes the progreess variables and also closes out the targets
  128. void cmLocalUnixMakefileGenerator3
  129. ::WriteProgressVariables(unsigned long total,
  130. unsigned long &current)
  131. {
  132. // delete the makefile target generator objects
  133. for (std::vector<cmMakefileTargetGenerator *>::iterator mtgIter =
  134. this->TargetGenerators.begin();
  135. mtgIter != this->TargetGenerators.end(); ++mtgIter)
  136. {
  137. (*mtgIter)->WriteProgressVariables(total,current);
  138. delete *mtgIter;
  139. }
  140. this->TargetGenerators.clear();
  141. }
  142. void cmLocalUnixMakefileGenerator3::WriteAllProgressVariable()
  143. {
  144. // write the top level progress for the all target
  145. std::string progressFile = cmake::GetCMakeFilesDirectory();
  146. progressFile += "/progress.make";
  147. std::string progressFileNameFull =
  148. this->ConvertToFullPath(progressFile.c_str());
  149. cmGeneratedFileStream ruleFileStream(progressFileNameFull.c_str());
  150. if(!ruleFileStream)
  151. {
  152. return;
  153. }
  154. cmGlobalUnixMakefileGenerator3 *gg =
  155. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  156. ruleFileStream << "CMAKE_ALL_PROGRESS = "
  157. << gg->GetNumberOfProgressActionsInAll(this) << "\n";
  158. }
  159. //----------------------------------------------------------------------------
  160. void cmLocalUnixMakefileGenerator3::ConfigureOutputPaths()
  161. {
  162. // Format the library and executable output paths.
  163. if(const char* libOut =
  164. this->Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  165. {
  166. this->LibraryOutputPath = libOut;
  167. this->FormatOutputPath(this->LibraryOutputPath, "LIBRARY");
  168. }
  169. if(const char* exeOut =
  170. this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  171. {
  172. this->ExecutableOutputPath = exeOut;
  173. this->FormatOutputPath(this->ExecutableOutputPath, "EXECUTABLE");
  174. }
  175. // Store the configuration name that will be generated.
  176. if(const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
  177. {
  178. // Use the build type given by the user.
  179. this->ConfigurationName = config;
  180. }
  181. else
  182. {
  183. // No configuration type given.
  184. this->ConfigurationName = "";
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmLocalUnixMakefileGenerator3::FormatOutputPath(std::string& path,
  189. const char* name)
  190. {
  191. if(!path.empty())
  192. {
  193. // Convert the output path to a full path in case it is
  194. // specified as a relative path. Treat a relative path as
  195. // relative to the current output directory for this makefile.
  196. path = cmSystemTools::CollapseFullPath
  197. (path.c_str(), this->Makefile->GetStartOutputDirectory());
  198. // Add a trailing slash for easy appending later.
  199. if(path.empty() || path[path.size()-1] != '/')
  200. {
  201. path += "/";
  202. }
  203. // Make sure the output path exists on disk.
  204. if(!cmSystemTools::MakeDirectory(path.c_str()))
  205. {
  206. cmSystemTools::Error("Error failed to create ",
  207. name, "_OUTPUT_PATH directory:", path.c_str());
  208. }
  209. // Add this as a link directory automatically.
  210. this->Makefile->AddLinkDirectory(path.c_str());
  211. }
  212. }
  213. //----------------------------------------------------------------------------
  214. void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
  215. {
  216. // generate the includes
  217. std::string ruleFileName = "Makefile";
  218. // Open the rule file. This should be copy-if-different because the
  219. // rules may depend on this file itself.
  220. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  221. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  222. if(!ruleFileStream)
  223. {
  224. return;
  225. }
  226. // always write the top makefile
  227. if (this->Parent)
  228. {
  229. ruleFileStream.SetCopyIfDifferent(true);
  230. }
  231. // write the all rules
  232. this->WriteLocalAllRules(ruleFileStream);
  233. // only write local targets unless at the top Keep track of targets already
  234. // listed.
  235. std::set<cmStdString> emittedTargets;
  236. if (this->Parent)
  237. {
  238. // write our targets, and while doing it collect up the object
  239. // file rules
  240. this->WriteLocalMakefileTargets(ruleFileStream,emittedTargets);
  241. }
  242. else
  243. {
  244. cmGlobalUnixMakefileGenerator3 *gg =
  245. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  246. gg->WriteConvenienceRules(ruleFileStream,emittedTargets);
  247. }
  248. bool do_preprocess_rules =
  249. this->GetCreatePreprocessedSourceRules();
  250. bool do_assembly_rules =
  251. this->GetCreateAssemblySourceRules();
  252. // now write out the object rules
  253. // for each object file name
  254. for (std::map<cmStdString, LocalObjectInfo>::iterator lo =
  255. this->LocalObjectFiles.begin();
  256. lo != this->LocalObjectFiles.end(); ++lo)
  257. {
  258. // Add a convenience rule for building the object file.
  259. this->WriteObjectConvenienceRule(ruleFileStream,
  260. "target to build an object file",
  261. lo->first.c_str(), lo->second);
  262. // Check whether preprocessing and assembly rules make sense.
  263. // They make sense only for C and C++ sources.
  264. bool lang_is_c_or_cxx = false;
  265. for(std::vector<LocalObjectEntry>::const_iterator ei =
  266. lo->second.begin(); ei != lo->second.end(); ++ei)
  267. {
  268. if(ei->Language == "C" || ei->Language == "CXX")
  269. {
  270. lang_is_c_or_cxx = true;
  271. }
  272. }
  273. // Add convenience rules for preprocessed and assembly files.
  274. if(lang_is_c_or_cxx && (do_preprocess_rules || do_assembly_rules))
  275. {
  276. std::string::size_type dot_pos = lo->first.rfind(".");
  277. std::string base = lo->first.substr(0, dot_pos);
  278. if(do_preprocess_rules)
  279. {
  280. this->WriteObjectConvenienceRule(
  281. ruleFileStream, "target to preprocess a source file",
  282. (base + ".i").c_str(), lo->second);
  283. }
  284. if(do_assembly_rules)
  285. {
  286. this->WriteObjectConvenienceRule(
  287. ruleFileStream, "target to generate assembly for a file",
  288. (base + ".s").c_str(), lo->second);
  289. }
  290. }
  291. }
  292. // add a help target as long as there isn;t a real target named help
  293. if(emittedTargets.insert("help").second)
  294. {
  295. cmGlobalUnixMakefileGenerator3 *gg =
  296. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  297. gg->WriteHelpRule(ruleFileStream,this);
  298. }
  299. this->WriteSpecialTargetsBottom(ruleFileStream);
  300. }
  301. //----------------------------------------------------------------------------
  302. void
  303. cmLocalUnixMakefileGenerator3
  304. ::WriteObjectConvenienceRule(std::ostream& ruleFileStream,
  305. const char* comment, const char* output,
  306. LocalObjectInfo const& targets)
  307. {
  308. // Recursively make the rule for each target using the object file.
  309. std::vector<std::string> commands;
  310. for(std::vector<LocalObjectEntry>::const_iterator t = targets.begin();
  311. t != targets.end(); ++t)
  312. {
  313. std::string tgtMakefileName =
  314. this->GetRelativeTargetDirectory(*(t->Target));
  315. std::string targetName = tgtMakefileName;
  316. tgtMakefileName += "/build.make";
  317. targetName += "/";
  318. targetName += output;
  319. commands.push_back(
  320. this->GetRecursiveMakeCall(tgtMakefileName.c_str(), targetName.c_str())
  321. );
  322. this->CreateCDCommand(commands,
  323. this->Makefile->GetHomeOutputDirectory(),
  324. this->Makefile->GetStartOutputDirectory());
  325. }
  326. // Write the rule to the makefile.
  327. std::vector<std::string> no_depends;
  328. this->WriteMakeRule(ruleFileStream, comment,
  329. output, no_depends, commands, true, true);
  330. }
  331. //----------------------------------------------------------------------------
  332. void cmLocalUnixMakefileGenerator3
  333. ::WriteLocalMakefileTargets(std::ostream& ruleFileStream,
  334. std::set<cmStdString> &emitted)
  335. {
  336. std::vector<std::string> depends;
  337. std::vector<std::string> commands;
  338. // for each target we just provide a rule to cd up to the top and do a make
  339. // on the target
  340. cmTargets& targets = this->Makefile->GetTargets();
  341. std::string localName;
  342. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  343. {
  344. if((t->second.GetType() == cmTarget::EXECUTABLE) ||
  345. (t->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  346. (t->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  347. (t->second.GetType() == cmTarget::MODULE_LIBRARY) ||
  348. (t->second.GetType() == cmTarget::UTILITY))
  349. {
  350. emitted.insert(t->second.GetName());
  351. // for subdirs add a rule to build this specific target by name.
  352. localName = this->GetRelativeTargetDirectory(t->second);
  353. localName += "/rule";
  354. commands.clear();
  355. depends.clear();
  356. // Build the target for this pass.
  357. std::string tmp = cmake::GetCMakeFilesDirectoryPostSlash();
  358. tmp += "Makefile2";
  359. commands.push_back(this->GetRecursiveMakeCall
  360. (tmp.c_str(),localName.c_str()));
  361. this->CreateCDCommand(commands,
  362. this->Makefile->GetHomeOutputDirectory(),
  363. this->Makefile->GetStartOutputDirectory());
  364. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  365. localName.c_str(), depends, commands, true);
  366. // Add a target with the canonical name (no prefix, suffix or path).
  367. if(localName != t->second.GetName())
  368. {
  369. commands.clear();
  370. depends.push_back(localName);
  371. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  372. t->second.GetName(), depends, commands, true);
  373. }
  374. // Add a fast rule to build the target
  375. std::string makefileName = this->GetRelativeTargetDirectory(t->second);
  376. makefileName += "/build.make";
  377. // make sure the makefile name is suitable for a makefile
  378. std::string makeTargetName =
  379. this->GetRelativeTargetDirectory(t->second);
  380. makeTargetName += "/build";
  381. localName = t->second.GetName();
  382. localName += "/fast";
  383. depends.clear();
  384. commands.clear();
  385. commands.push_back(this->GetRecursiveMakeCall
  386. (makefileName.c_str(), makeTargetName.c_str()));
  387. this->CreateCDCommand(commands,
  388. this->Makefile->GetHomeOutputDirectory(),
  389. this->Makefile->GetStartOutputDirectory());
  390. this->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  391. localName.c_str(), depends, commands, true);
  392. }
  393. }
  394. }
  395. //----------------------------------------------------------------------------
  396. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  397. {
  398. std::string infoFileName = this->Makefile->GetStartOutputDirectory();
  399. infoFileName += cmake::GetCMakeFilesDirectory();
  400. infoFileName += "/CMakeDirectoryInformation.cmake";
  401. // Open the output file.
  402. cmGeneratedFileStream infoFileStream(infoFileName.c_str());
  403. if(!infoFileStream)
  404. {
  405. return;
  406. }
  407. // Write the do not edit header.
  408. this->WriteDisclaimer(infoFileStream);
  409. // Tell the dependency scanner to use unix paths if necessary.
  410. if(cmSystemTools::GetForceUnixPaths())
  411. {
  412. infoFileStream
  413. << "# Force unix paths in dependencies.\n"
  414. << "SET(CMAKE_FORCE_UNIX_PATHS 1)\n"
  415. << "\n";
  416. }
  417. // Store the include search path for this directory.
  418. infoFileStream
  419. << "# The C and CXX include file search paths:\n";
  420. infoFileStream
  421. << "SET(CMAKE_C_INCLUDE_PATH\n";
  422. std::vector<std::string> includeDirs;
  423. this->GetIncludeDirectories(includeDirs, false);
  424. for(std::vector<std::string>::iterator i = includeDirs.begin();
  425. i != includeDirs.end(); ++i)
  426. {
  427. infoFileStream
  428. << " \"" << this->Convert(i->c_str(),HOME_OUTPUT).c_str() << "\"\n";
  429. }
  430. infoFileStream
  431. << " )\n";
  432. infoFileStream
  433. << "SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})\n";
  434. // Store the include regular expressions for this directory.
  435. infoFileStream
  436. << "\n"
  437. << "# The C and CXX include file regular expressions for "
  438. << "this directory.\n";
  439. infoFileStream
  440. << "SET(CMAKE_C_INCLUDE_REGEX_SCAN ";
  441. this->WriteCMakeArgument(infoFileStream,
  442. this->Makefile->GetIncludeRegularExpression());
  443. infoFileStream
  444. << ")\n";
  445. infoFileStream
  446. << "SET(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  447. this->WriteCMakeArgument(infoFileStream,
  448. this->Makefile->GetComplainRegularExpression());
  449. infoFileStream
  450. << ")\n";
  451. infoFileStream
  452. << "SET(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  453. infoFileStream
  454. << "SET(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
  455. "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  456. }
  457. //----------------------------------------------------------------------------
  458. std::string
  459. cmLocalUnixMakefileGenerator3
  460. ::ConvertToFullPath(const std::string& localPath)
  461. {
  462. std::string dir = this->Makefile->GetStartOutputDirectory();
  463. dir += "/";
  464. dir += localPath;
  465. return dir;
  466. }
  467. const std::string &cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  468. {
  469. return this->HomeRelativeOutputPath;
  470. }
  471. //----------------------------------------------------------------------------
  472. void
  473. cmLocalUnixMakefileGenerator3
  474. ::WriteMakeRule(std::ostream& os,
  475. const char* comment,
  476. const char* target,
  477. const std::vector<std::string>& depends,
  478. const std::vector<std::string>& commands,
  479. bool symbolic,
  480. bool in_help)
  481. {
  482. // Make sure there is a target.
  483. if(!target || !*target)
  484. {
  485. cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
  486. comment);
  487. return;
  488. }
  489. std::string replace;
  490. // Write the comment describing the rule in the makefile.
  491. if(comment)
  492. {
  493. replace = comment;
  494. std::string::size_type lpos = 0;
  495. std::string::size_type rpos;
  496. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  497. {
  498. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  499. lpos = rpos+1;
  500. }
  501. os << "# " << replace.substr(lpos) << "\n";
  502. }
  503. // Construct the left hand side of the rule.
  504. replace = target;
  505. std::string tgt = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  506. tgt = this->ConvertToMakeTarget(tgt.c_str());
  507. const char* space = "";
  508. if(tgt.size() == 1)
  509. {
  510. // Add a space before the ":" to avoid drive letter confusion on
  511. // Windows.
  512. space = " ";
  513. }
  514. // Mark the rule as symbolic if requested.
  515. if(symbolic)
  516. {
  517. if(const char* sym =
  518. this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE"))
  519. {
  520. os << tgt.c_str() << space << ": " << sym << "\n";
  521. }
  522. }
  523. // Write the rule.
  524. if(depends.empty())
  525. {
  526. // No dependencies. The commands will always run.
  527. os << tgt.c_str() << space << ":\n";
  528. }
  529. else
  530. {
  531. // Split dependencies into multiple rule lines. This allows for
  532. // very long dependency lists even on older make implementations.
  533. for(std::vector<std::string>::const_iterator dep = depends.begin();
  534. dep != depends.end(); ++dep)
  535. {
  536. replace = *dep;
  537. replace = this->Convert(replace.c_str(),HOME_OUTPUT,MAKEFILE);
  538. replace = this->ConvertToMakeTarget(replace.c_str());
  539. os << tgt.c_str() << space << ": " << replace.c_str() << "\n";
  540. }
  541. }
  542. // Write the list of commands.
  543. for(std::vector<std::string>::const_iterator i = commands.begin();
  544. i != commands.end(); ++i)
  545. {
  546. replace = *i;
  547. os << "\t" << replace.c_str() << "\n";
  548. }
  549. os << "\n";
  550. // Add the output to the local help if requested.
  551. if(in_help)
  552. {
  553. this->LocalHelp.push_back(target);
  554. }
  555. }
  556. //----------------------------------------------------------------------------
  557. void
  558. cmLocalUnixMakefileGenerator3
  559. ::WriteMakeVariables(std::ostream& makefileStream)
  560. {
  561. this->WriteDivider(makefileStream);
  562. makefileStream
  563. << "# Set environment variables for the build.\n"
  564. << "\n";
  565. if(this->DefineWindowsNULL)
  566. {
  567. makefileStream
  568. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  569. << "NULL=\n"
  570. << "!ELSE\n"
  571. << "NULL=nul\n"
  572. << "!ENDIF\n";
  573. }
  574. if(this->WindowsShell)
  575. {
  576. makefileStream
  577. << "SHELL = cmd.exe\n"
  578. << "\n";
  579. }
  580. else
  581. {
  582. makefileStream
  583. << "# The shell in which to execute make rules.\n"
  584. << "SHELL = /bin/sh\n"
  585. << "\n";
  586. }
  587. std::string cmakecommand =
  588. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  589. makefileStream
  590. << "# The CMake executable.\n"
  591. << "CMAKE_COMMAND = "
  592. << this->Convert(cmakecommand.c_str(), FULL, SHELL).c_str()
  593. << "\n"
  594. << "\n";
  595. makefileStream
  596. << "# The command to remove a file.\n"
  597. << "RM = "
  598. << this->Convert(cmakecommand.c_str(),FULL,SHELL).c_str()
  599. << " -E remove -f\n"
  600. << "\n";
  601. if(this->Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  602. {
  603. makefileStream
  604. << "# The program to use to edit the cache.\n"
  605. << "CMAKE_EDIT_COMMAND = "
  606. << (this->ConvertToOutputForExisting(
  607. this->Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))) << "\n"
  608. << "\n";
  609. }
  610. makefileStream
  611. << "# The top-level source directory on which CMake was run.\n"
  612. << "CMAKE_SOURCE_DIR = "
  613. << this->Convert(this->Makefile->GetHomeDirectory(), FULL, SHELL)
  614. << "\n"
  615. << "\n";
  616. makefileStream
  617. << "# The top-level build directory on which CMake was run.\n"
  618. << "CMAKE_BINARY_DIR = "
  619. << this->Convert(this->Makefile->GetHomeOutputDirectory(), FULL, SHELL)
  620. << "\n"
  621. << "\n";
  622. }
  623. //----------------------------------------------------------------------------
  624. void
  625. cmLocalUnixMakefileGenerator3
  626. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  627. {
  628. this->WriteDivider(makefileStream);
  629. makefileStream
  630. << "# Special targets provided by cmake.\n"
  631. << "\n";
  632. std::vector<std::string> no_commands;
  633. std::vector<std::string> no_depends;
  634. // Special target to cleanup operation of make tool.
  635. // This should be the first target except for the default_target in
  636. // the interface Makefile.
  637. this->WriteMakeRule(
  638. makefileStream, "Disable implicit rules so canoncical targets will work.",
  639. ".SUFFIXES", no_depends, no_commands, false);
  640. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  641. std::vector<std::string> depends;
  642. depends.push_back(".hpux_make_needs_suffix_list");
  643. this->WriteMakeRule(makefileStream, 0,
  644. ".SUFFIXES", depends, no_commands, false);
  645. // Write special target to silence make output. This must be after
  646. // the default target in case VERBOSE is set (which changes the
  647. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  648. // "VERBOSE=1" to be added as a make variable which will change the
  649. // name of this special target. This gives a make-time choice to
  650. // the user.
  651. if((this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  652. || (this->ForceVerboseMakefiles))
  653. {
  654. makefileStream
  655. << "# Produce verbose output by default.\n"
  656. << "VERBOSE = 1\n"
  657. << "\n";
  658. }
  659. if(this->SilentNoColon)
  660. {
  661. makefileStream << "$(VERBOSE).SILENT\n";
  662. }
  663. else
  664. {
  665. this->WriteMakeRule(makefileStream,
  666. "Suppress display of executed commands.",
  667. "$(VERBOSE).SILENT",
  668. no_depends,
  669. no_commands, false);
  670. }
  671. // Special symbolic target that never exists to force dependers to
  672. // run their rules.
  673. this->WriteMakeRule
  674. (makefileStream,
  675. "A target that is always out of date.",
  676. "cmake_force", no_depends, no_commands, true);
  677. // Variables for reference by other rules.
  678. this->WriteMakeVariables(makefileStream);
  679. }
  680. //----------------------------------------------------------------------------
  681. void cmLocalUnixMakefileGenerator3
  682. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  683. {
  684. this->WriteDivider(makefileStream);
  685. makefileStream
  686. << "# Special targets to cleanup operation of make.\n"
  687. << "\n";
  688. // Write special "cmake_check_build_system" target to run cmake with
  689. // the --check-build-system flag.
  690. {
  691. // Build command to run CMake to check if anything needs regenerating.
  692. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  693. cmakefileName += "Makefile.cmake";
  694. std::string runRule =
  695. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  696. runRule += " --check-build-system ";
  697. runRule += this->Convert(cmakefileName.c_str(),NONE,SHELL);
  698. runRule += " 0";
  699. std::vector<std::string> no_depends;
  700. std::vector<std::string> commands;
  701. commands.push_back(runRule);
  702. if(this->Parent)
  703. {
  704. this->CreateCDCommand(commands,
  705. this->Makefile->GetHomeOutputDirectory(),
  706. this->Makefile->GetStartOutputDirectory());
  707. }
  708. this->WriteMakeRule(makefileStream,
  709. "Special rule to run CMake to check the build system "
  710. "integrity.\n"
  711. "No rule that depends on this can have "
  712. "commands that come from listfiles\n"
  713. "because they might be regenerated.",
  714. "cmake_check_build_system",
  715. no_depends,
  716. commands, true);
  717. }
  718. }
  719. //----------------------------------------------------------------------------
  720. void
  721. cmLocalUnixMakefileGenerator3
  722. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  723. const char* realTarget,
  724. const char* helpTarget)
  725. {
  726. // A rule is only needed if the names are different.
  727. if(strcmp(realTarget, helpTarget) != 0)
  728. {
  729. // The helper target depends on the real target.
  730. std::vector<std::string> depends;
  731. depends.push_back(realTarget);
  732. // There are no commands.
  733. std::vector<std::string> no_commands;
  734. // Write the rule.
  735. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  736. helpTarget, depends, no_commands, true);
  737. }
  738. }
  739. //----------------------------------------------------------------------------
  740. std::string
  741. cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(cmTarget& target)
  742. {
  743. std::string dir = this->HomeRelativeOutputPath;
  744. dir += this->GetTargetDirectory(target);
  745. return this->Convert(dir.c_str(),NONE,UNCHANGED);
  746. }
  747. //----------------------------------------------------------------------------
  748. void
  749. cmLocalUnixMakefileGenerator3
  750. ::AppendRuleDepend(std::vector<std::string>& depends,
  751. const char* ruleFileName)
  752. {
  753. // Add a dependency on the rule file itself unless an option to skip
  754. // it is specifically enabled by the user or project.
  755. const char* nodep =
  756. this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  757. if(!nodep || cmSystemTools::IsOff(nodep))
  758. {
  759. depends.push_back(ruleFileName);
  760. }
  761. }
  762. //----------------------------------------------------------------------------
  763. void
  764. cmLocalUnixMakefileGenerator3
  765. ::AppendCustomDepends(std::vector<std::string>& depends,
  766. const std::vector<cmCustomCommand>& ccs)
  767. {
  768. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  769. i != ccs.end(); ++i)
  770. {
  771. this->AppendCustomDepend(depends, *i);
  772. }
  773. }
  774. //----------------------------------------------------------------------------
  775. void
  776. cmLocalUnixMakefileGenerator3
  777. ::AppendCustomDepend(std::vector<std::string>& depends,
  778. const cmCustomCommand& cc)
  779. {
  780. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  781. d != cc.GetDepends().end(); ++d)
  782. {
  783. // Lookup the real name of the dependency in case it is a CMake target.
  784. std::string dep = this->GetRealDependency
  785. (d->c_str(), this->ConfigurationName.c_str());
  786. depends.push_back(dep);
  787. }
  788. }
  789. //----------------------------------------------------------------------------
  790. void
  791. cmLocalUnixMakefileGenerator3
  792. ::AppendCustomCommands(std::vector<std::string>& commands,
  793. const std::vector<cmCustomCommand>& ccs)
  794. {
  795. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  796. i != ccs.end(); ++i)
  797. {
  798. this->AppendCustomCommand(commands, *i);
  799. }
  800. }
  801. //----------------------------------------------------------------------------
  802. void
  803. cmLocalUnixMakefileGenerator3
  804. ::AppendCustomCommand(std::vector<std::string>& commands,
  805. const cmCustomCommand& cc)
  806. {
  807. // if the command specified a working directory use it.
  808. const char* dir = this->Makefile->GetStartOutputDirectory();
  809. const char* workingDir = cc.GetWorkingDirectory();
  810. if(workingDir)
  811. {
  812. dir = workingDir;
  813. }
  814. bool escapeOldStyle = cc.GetEscapeOldStyle();
  815. bool escapeAllowMakeVars = cc.GetEscapeAllowMakeVars();
  816. // Add each command line to the set of commands.
  817. std::vector<std::string> commands1;
  818. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  819. cl != cc.GetCommandLines().end(); ++cl)
  820. {
  821. // Build the command line in a single string.
  822. const cmCustomCommandLine& commandLine = *cl;
  823. std::string cmd = commandLine[0];
  824. if (cmd.size())
  825. {
  826. cmSystemTools::ReplaceString(cmd, "/./", "/");
  827. // Convert the command to a relative path only if the current
  828. // working directory will be the start-output directory.
  829. if(!workingDir)
  830. {
  831. cmd = this->Convert(cmd.c_str(),START_OUTPUT);
  832. }
  833. if(cmd.find("/") == cmd.npos &&
  834. commandLine[0].find("/") != cmd.npos)
  835. {
  836. // Add a leading "./" for executables in the current directory.
  837. cmd = "./" + cmd;
  838. }
  839. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  840. for(unsigned int j=1; j < commandLine.size(); ++j)
  841. {
  842. cmd += " ";
  843. if(escapeOldStyle)
  844. {
  845. cmd += this->EscapeForShellOldStyle(commandLine[j].c_str());
  846. }
  847. else
  848. {
  849. cmd += this->EscapeForShell(commandLine[j].c_str(),
  850. escapeAllowMakeVars);
  851. }
  852. }
  853. commands1.push_back(cmd);
  854. }
  855. }
  856. // Setup the proper working directory for the commands.
  857. this->CreateCDCommand(commands1, dir,
  858. this->Makefile->GetHomeOutputDirectory());
  859. // push back the custom commands
  860. commands.insert(commands.end(), commands1.begin(), commands1.end());
  861. }
  862. //----------------------------------------------------------------------------
  863. void
  864. cmLocalUnixMakefileGenerator3
  865. ::AppendCleanCommand(std::vector<std::string>& commands,
  866. const std::vector<std::string>& files,
  867. cmTarget& target, const char* filename)
  868. {
  869. if(!files.empty())
  870. {
  871. std::string cleanfile = this->Makefile->GetCurrentOutputDirectory();
  872. cleanfile += "/";
  873. cleanfile += this->GetTargetDirectory(target);
  874. cleanfile += "/cmake_clean";
  875. if(filename)
  876. {
  877. cleanfile += "_";
  878. cleanfile += filename;
  879. }
  880. cleanfile += ".cmake";
  881. std::string cleanfilePath = this->Convert(cleanfile.c_str(), FULL);
  882. std::ofstream fout(cleanfilePath.c_str());
  883. if(!fout)
  884. {
  885. cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
  886. }
  887. fout << "FILE(REMOVE\n";
  888. std::string remove = "$(CMAKE_COMMAND) -P ";
  889. remove += this->Convert(cleanfile.c_str(), START_OUTPUT, SHELL);
  890. for(std::vector<std::string>::const_iterator f = files.begin();
  891. f != files.end(); ++f)
  892. {
  893. fout << "\"" << this->Convert(f->c_str(),START_OUTPUT,UNCHANGED)
  894. << "\"\n";
  895. }
  896. fout << ")\n";
  897. commands.push_back(remove);
  898. }
  899. }
  900. //----------------------------------------------------------------------------
  901. void
  902. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  903. const char* text,
  904. EchoColor color)
  905. {
  906. // Choose the color for the text.
  907. std::string color_name;
  908. #ifdef CMAKE_BUILD_WITH_CMAKE
  909. if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
  910. {
  911. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  912. // This color set is readable on both black and white backgrounds.
  913. switch(color)
  914. {
  915. case EchoNormal:
  916. break;
  917. case EchoDepend:
  918. color_name = "--magenta --bold ";
  919. break;
  920. case EchoBuild:
  921. color_name = "--green ";
  922. break;
  923. case EchoLink:
  924. color_name = "--red --bold ";
  925. break;
  926. case EchoGenerate:
  927. color_name = "--blue --bold ";
  928. break;
  929. case EchoGlobal:
  930. color_name = "--cyan ";
  931. break;
  932. }
  933. }
  934. #else
  935. (void)color;
  936. #endif
  937. // Echo one line at a time.
  938. std::string line;
  939. line.reserve(200);
  940. for(const char* c = text;; ++c)
  941. {
  942. if(*c == '\n' || *c == '\0')
  943. {
  944. // Avoid writing a blank last line on end-of-string.
  945. if(*c != '\0' || !line.empty())
  946. {
  947. // Add a command to echo this line.
  948. std::string cmd;
  949. if(color_name.empty())
  950. {
  951. // Use the native echo command.
  952. cmd = this->NativeEchoCommand;
  953. cmd += this->EscapeForShell(line.c_str(), false,
  954. this->NativeEchoWindows);
  955. }
  956. else
  957. {
  958. // Use cmake to echo the text in color.
  959. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  960. cmd += color_name;
  961. cmd += this->EscapeForShell(line.c_str());
  962. }
  963. commands.push_back(cmd);
  964. }
  965. // Reset the line to emtpy.
  966. line = "";
  967. // Terminate on end-of-string.
  968. if(*c == '\0')
  969. {
  970. return;
  971. }
  972. }
  973. else if(*c != '\r')
  974. {
  975. // Append this character to the current line.
  976. line += *c;
  977. }
  978. }
  979. }
  980. //----------------------------------------------------------------------------
  981. //take a tgt path and convert it into a make target, it could be full, or
  982. //relative
  983. std::string cmLocalUnixMakefileGenerator3
  984. ::ConvertToMakeTarget(const char* tgt)
  985. {
  986. // Make targets should not have a leading './' for a file in the
  987. // directory containing the makefile.
  988. std::string ret = tgt;
  989. if(ret.size() > 2 && (ret[0] == '.') &&
  990. ( (ret[1] == '/') || ret[1] == '\\'))
  991. {
  992. std::string upath = ret;
  993. cmSystemTools::ConvertToUnixSlashes(upath);
  994. if(upath.find(2, '/') == upath.npos)
  995. {
  996. ret = ret.substr(2, ret.size()-2);
  997. }
  998. }
  999. return ret;
  1000. }
  1001. //----------------------------------------------------------------------------
  1002. std::string
  1003. cmLocalUnixMakefileGenerator3
  1004. ::CreateMakeVariable(const char* sin, const char* s2in)
  1005. {
  1006. std::string s = sin;
  1007. std::string s2 = s2in;
  1008. std::string unmodified = s;
  1009. unmodified += s2;
  1010. // if there is no restriction on the length of make variables
  1011. // and there are no "." charactors in the string, then return the
  1012. // unmodified combination.
  1013. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  1014. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  1015. {
  1016. return unmodified;
  1017. }
  1018. // see if the variable has been defined before and return
  1019. // the modified version of the variable
  1020. std::map<cmStdString, cmStdString>::iterator i =
  1021. this->MakeVariableMap.find(unmodified);
  1022. if(i != this->MakeVariableMap.end())
  1023. {
  1024. return i->second;
  1025. }
  1026. // start with the unmodified variable
  1027. std::string ret = unmodified;
  1028. // if this there is no value for this->MakefileVariableSize then
  1029. // the string must have bad characters in it
  1030. if(!this->MakefileVariableSize)
  1031. {
  1032. cmSystemTools::ReplaceString(ret, ".", "_");
  1033. cmSystemTools::ReplaceString(ret, "-", "__");
  1034. int ni = 0;
  1035. char buffer[5];
  1036. // make sure the _ version is not already used, if
  1037. // it is used then add number to the end of the variable
  1038. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1039. {
  1040. ++ni;
  1041. sprintf(buffer, "%04d", ni);
  1042. ret = unmodified + buffer;
  1043. }
  1044. this->ShortMakeVariableMap[ret] = "1";
  1045. this->MakeVariableMap[unmodified] = ret;
  1046. return ret;
  1047. }
  1048. // if the string is greater the 32 chars it is an invalid vairable name
  1049. // for borland make
  1050. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1051. {
  1052. int keep = this->MakefileVariableSize - 8;
  1053. int size = keep + 3;
  1054. std::string str1 = s;
  1055. std::string str2 = s2;
  1056. // we must shorten the combined string by 4 charactors
  1057. // keep no more than 24 charactors from the second string
  1058. if(static_cast<int>(str2.size()) > keep)
  1059. {
  1060. str2 = str2.substr(0, keep);
  1061. }
  1062. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1063. {
  1064. str1 = str1.substr(0, size - str2.size());
  1065. }
  1066. char buffer[5];
  1067. int ni = 0;
  1068. sprintf(buffer, "%04d", ni);
  1069. ret = str1 + str2 + buffer;
  1070. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1071. {
  1072. ++ni;
  1073. sprintf(buffer, "%04d", ni);
  1074. ret = str1 + str2 + buffer;
  1075. }
  1076. if(ni == 1000)
  1077. {
  1078. cmSystemTools::Error("Borland makefile variable length too long");
  1079. return unmodified;
  1080. }
  1081. // once an unused variable is found
  1082. this->ShortMakeVariableMap[ret] = "1";
  1083. }
  1084. // always make an entry into the unmodified to variable map
  1085. this->MakeVariableMap[unmodified] = ret;
  1086. return ret;
  1087. }
  1088. //----------------------------------------------------------------------------
  1089. bool cmLocalUnixMakefileGenerator3::ScanDependencies(const char* tgtInfo)
  1090. {
  1091. // The info file for this target
  1092. std::string infoFile = tgtInfo;
  1093. // Read the directory information file.
  1094. cmMakefile* mf = this->Makefile;
  1095. bool haveDirectoryInfo = false;
  1096. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1097. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1098. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1099. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  1100. !cmSystemTools::GetErrorOccuredFlag())
  1101. {
  1102. haveDirectoryInfo = true;
  1103. }
  1104. // read in the target info file
  1105. if(!mf->ReadListFile(0, infoFile.c_str()) ||
  1106. cmSystemTools::GetErrorOccuredFlag())
  1107. {
  1108. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1109. }
  1110. // Test whether we need to force Unix paths.
  1111. if(haveDirectoryInfo)
  1112. {
  1113. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1114. {
  1115. if(!cmSystemTools::IsOff(force))
  1116. {
  1117. cmSystemTools::SetForceUnixPaths(true);
  1118. }
  1119. }
  1120. }
  1121. else
  1122. {
  1123. cmSystemTools::Error("Directory Information file not found");
  1124. }
  1125. // create the file stream for the depends file
  1126. std::string dir = cmSystemTools::GetFilenamePath(infoFile);
  1127. // Open the rule file. This should be copy-if-different because the
  1128. // rules may depend on this file itself.
  1129. std::string ruleFileNameFull = dir;
  1130. ruleFileNameFull += "/depend.make";
  1131. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1132. ruleFileStream.SetCopyIfDifferent(true);
  1133. if(!ruleFileStream)
  1134. {
  1135. return false;
  1136. }
  1137. std::string internalRuleFileNameFull = dir;
  1138. internalRuleFileNameFull += "/depend.internal";
  1139. cmGeneratedFileStream
  1140. internalRuleFileStream(internalRuleFileNameFull.c_str());
  1141. internalRuleFileStream.SetCopyIfDifferent(true);
  1142. if(!internalRuleFileStream)
  1143. {
  1144. return false;
  1145. }
  1146. this->WriteDisclaimer(ruleFileStream);
  1147. this->WriteDisclaimer(internalRuleFileStream);
  1148. // for each language we need to scan, scan it
  1149. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1150. std::vector<std::string> langs;
  1151. cmSystemTools::ExpandListArgument(langStr, langs);
  1152. for (std::vector<std::string>::iterator li =
  1153. langs.begin(); li != langs.end(); ++li)
  1154. {
  1155. // construct the checker
  1156. std::string lang = li->c_str();
  1157. // Get the set of include directories.
  1158. std::vector<std::string> includes;
  1159. if(haveDirectoryInfo)
  1160. {
  1161. std::string includePathVar = "CMAKE_";
  1162. includePathVar += lang;
  1163. includePathVar += "_INCLUDE_PATH";
  1164. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  1165. {
  1166. cmSystemTools::ExpandListArgument(includePath, includes);
  1167. }
  1168. }
  1169. // Get the include file regular expression.
  1170. std::string includeRegexScan = "^.*$";
  1171. std::string includeRegexComplain = "^$";
  1172. if(haveDirectoryInfo)
  1173. {
  1174. std::string scanRegexVar = "CMAKE_";
  1175. scanRegexVar += lang;
  1176. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  1177. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  1178. {
  1179. includeRegexScan = scanRegex;
  1180. }
  1181. std::string complainRegexVar = "CMAKE_";
  1182. complainRegexVar += lang;
  1183. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  1184. if(const char* complainRegex =
  1185. mf->GetDefinition(complainRegexVar.c_str()))
  1186. {
  1187. includeRegexComplain = complainRegex;
  1188. }
  1189. }
  1190. // Create the scanner for this language
  1191. cmDepends *scanner = 0;
  1192. if(lang == "C" || lang == "CXX" || lang == "RC")
  1193. {
  1194. std::string includeCacheFileName = dir;
  1195. includeCacheFileName += "/";
  1196. includeCacheFileName += lang;
  1197. includeCacheFileName += ".includecache";
  1198. // TODO: Handle RC (resource files) dependencies correctly.
  1199. scanner = new cmDependsC(includes,
  1200. includeRegexScan.c_str(),
  1201. includeRegexComplain.c_str(),
  1202. includeCacheFileName);
  1203. scanner->SetLocalGenerator(this);
  1204. }
  1205. #ifdef CMAKE_BUILD_WITH_CMAKE
  1206. else if(lang == "Fortran")
  1207. {
  1208. scanner = new cmDependsFortran(includes);
  1209. }
  1210. else if(lang == "Java")
  1211. {
  1212. scanner = new cmDependsJava();
  1213. }
  1214. #endif
  1215. if (scanner)
  1216. {
  1217. scanner->SetFileComparison
  1218. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1219. // for each file we need to scan
  1220. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  1221. srcLang += lang;
  1222. const char *srcStr = mf->GetSafeDefinition(srcLang.c_str());
  1223. std::vector<std::string> srcs;
  1224. cmSystemTools::ExpandListArgument(srcStr, srcs);
  1225. for (std::vector<std::string>::iterator si =
  1226. srcs.begin(); si != srcs.end(); ++si)
  1227. {
  1228. std::string &src = *si;
  1229. ++si;
  1230. // make sure the object file is relative to home output
  1231. std::string obj = *si;
  1232. obj = this->Convert(obj.c_str(),HOME_OUTPUT,MAKEFILE);
  1233. scanner->Write(src.c_str(),obj.c_str(),
  1234. ruleFileStream, internalRuleFileStream);
  1235. }
  1236. // free the scanner for this language
  1237. delete scanner;
  1238. }
  1239. }
  1240. // dependencies were generated, so touch the mark file
  1241. ruleFileNameFull += ".mark";
  1242. std::ofstream fmark(ruleFileNameFull.c_str());
  1243. fmark << "Dependencies updated>" << std::endl;
  1244. return true;
  1245. }
  1246. //----------------------------------------------------------------------------
  1247. void cmLocalUnixMakefileGenerator3
  1248. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1249. {
  1250. this->WriteDisclaimer(ruleFileStream);
  1251. // Write the main entry point target. This must be the VERY first
  1252. // target so that make with no arguments will run it.
  1253. {
  1254. // Just depend on the all target to drive the build.
  1255. std::vector<std::string> depends;
  1256. std::vector<std::string> no_commands;
  1257. depends.push_back("all");
  1258. // Write the rule.
  1259. this->WriteMakeRule(ruleFileStream,
  1260. "Default target executed when no arguments are "
  1261. "given to make.",
  1262. "default_target",
  1263. depends,
  1264. no_commands, true);
  1265. }
  1266. this->WriteSpecialTargetsTop(ruleFileStream);
  1267. // Include the progress variables for the target.
  1268. std::string progressFile = cmake::GetCMakeFilesDirectory();
  1269. progressFile += "/progress.make";
  1270. std::string progressFileNameFull =
  1271. this->ConvertToFullPath(progressFile.c_str());
  1272. ruleFileStream
  1273. << "# Include the progress variables for this target.\n"
  1274. << this->IncludeDirective << " "
  1275. << this->Convert(progressFileNameFull.c_str(),
  1276. cmLocalGenerator::START_OUTPUT,
  1277. cmLocalGenerator::MAKEFILE) << "\n\n";
  1278. // Write all global targets
  1279. this->WriteDivider(ruleFileStream);
  1280. ruleFileStream
  1281. << "# Targets provided globally by CMake.\n"
  1282. << "\n";
  1283. cmTargets* targets = &(this->Makefile->GetTargets());
  1284. cmTargets::iterator glIt;
  1285. for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
  1286. {
  1287. if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  1288. {
  1289. std::string targetString = "Special rule for the target " + glIt->first;
  1290. std::vector<std::string> commands;
  1291. std::vector<std::string> depends;
  1292. const char* text = glIt->second.GetProperty("EchoString");
  1293. if ( !text )
  1294. {
  1295. text = "Running external command ...";
  1296. }
  1297. std::set<cmStdString>::const_iterator dit;
  1298. for ( dit = glIt->second.GetUtilities().begin();
  1299. dit != glIt->second.GetUtilities().end();
  1300. ++ dit )
  1301. {
  1302. depends.push_back(dit->c_str());
  1303. }
  1304. this->AppendEcho(commands, text,
  1305. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1306. // Global targets store their rules in pre- and post-build commands.
  1307. this->AppendCustomDepends(depends,
  1308. glIt->second.GetPreBuildCommands());
  1309. this->AppendCustomDepends(depends,
  1310. glIt->second.GetPostBuildCommands());
  1311. this->AppendCustomCommands(commands,
  1312. glIt->second.GetPreBuildCommands());
  1313. this->AppendCustomCommands(commands,
  1314. glIt->second.GetPostBuildCommands());
  1315. std::string targetName = glIt->second.GetName();
  1316. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1317. targetName.c_str(), depends, commands, true);
  1318. // Provide a "/fast" version of the target.
  1319. depends.clear();
  1320. if((targetName == "install") || (targetName == "install_local"))
  1321. {
  1322. // Provide a fast install target that does not depend on all
  1323. // but has the same command.
  1324. depends.push_back("preinstall/fast");
  1325. }
  1326. else
  1327. {
  1328. // Just forward to the real target so at least it will work.
  1329. depends.push_back(targetName);
  1330. commands.clear();
  1331. }
  1332. targetName += "/fast";
  1333. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1334. targetName.c_str(), depends, commands, true);
  1335. }
  1336. }
  1337. std::vector<std::string> depends;
  1338. std::vector<std::string> commands;
  1339. // Write the all rule.
  1340. std::string dir;
  1341. std::string recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1342. recursiveTarget += "/all";
  1343. dir = this->Convert(recursiveTarget.c_str(),HOME_OUTPUT,MAKEFILE);
  1344. depends.push_back("cmake_check_build_system");
  1345. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1346. progressDir += cmake::GetCMakeFilesDirectory();
  1347. {
  1348. cmOStringStream progCmd;
  1349. progCmd <<
  1350. "$(CMAKE_COMMAND) -E cmake_progress_start ";
  1351. progCmd << this->Convert(progressDir.c_str(),
  1352. cmLocalGenerator::FULL,
  1353. cmLocalGenerator::SHELL);
  1354. progCmd << " $(CMAKE_ALL_PROGRESS)";
  1355. commands.push_back(progCmd.str());
  1356. }
  1357. std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1358. mf2Dir += "Makefile2";
  1359. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1360. recursiveTarget.c_str()));
  1361. this->CreateCDCommand(commands,
  1362. this->Makefile->GetHomeOutputDirectory(),
  1363. this->Makefile->GetStartOutputDirectory());
  1364. {
  1365. cmOStringStream progCmd;
  1366. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1367. progCmd << this->Convert(progressDir.c_str(),
  1368. cmLocalGenerator::FULL,
  1369. cmLocalGenerator::SHELL);
  1370. progCmd << " 0";
  1371. commands.push_back(progCmd.str());
  1372. }
  1373. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1374. depends, commands, true);
  1375. // Write the clean rule.
  1376. recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1377. recursiveTarget += "/clean";
  1378. dir = this->Convert(recursiveTarget.c_str(),HOME_OUTPUT,MAKEFILE);
  1379. commands.clear();
  1380. depends.clear();
  1381. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1382. recursiveTarget.c_str()));
  1383. this->CreateCDCommand(commands,
  1384. this->Makefile->GetHomeOutputDirectory(),
  1385. this->Makefile->GetStartOutputDirectory());
  1386. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1387. depends, commands, true);
  1388. commands.clear();
  1389. depends.clear();
  1390. depends.push_back("clean");
  1391. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1392. depends, commands, true);
  1393. // Write the preinstall rule.
  1394. recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1395. recursiveTarget += "/preinstall";
  1396. dir = this->Convert(recursiveTarget.c_str(), HOME_OUTPUT,MAKEFILE);
  1397. commands.clear();
  1398. depends.clear();
  1399. const char* noall =
  1400. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1401. if(!noall || cmSystemTools::IsOff(noall))
  1402. {
  1403. // Drive the build before installing.
  1404. depends.push_back("all");
  1405. }
  1406. else
  1407. {
  1408. // At least make sure the build system is up to date.
  1409. depends.push_back("cmake_check_build_system");
  1410. }
  1411. commands.push_back
  1412. (this->GetRecursiveMakeCall(mf2Dir.c_str(), recursiveTarget.c_str()));
  1413. this->CreateCDCommand(commands,
  1414. this->Makefile->GetHomeOutputDirectory(),
  1415. this->Makefile->GetStartOutputDirectory());
  1416. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1417. "preinstall", depends, commands, true);
  1418. depends.clear();
  1419. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1420. "preinstall/fast", depends, commands, true);
  1421. // write the depend rule, really a recompute depends rule
  1422. depends.clear();
  1423. commands.clear();
  1424. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  1425. cmakefileName += "Makefile.cmake";
  1426. this->Convert(cmakefileName.c_str(),HOME_OUTPUT,
  1427. cmLocalGenerator::MAKEFILE);
  1428. std::string runRule =
  1429. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1430. runRule += " --check-build-system ";
  1431. runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  1432. cmLocalGenerator::SHELL);
  1433. runRule += " 1";
  1434. commands.push_back(runRule);
  1435. this->WriteMakeRule(ruleFileStream, "clear depends",
  1436. "depend",
  1437. depends, commands, true);
  1438. }
  1439. //----------------------------------------------------------------------------
  1440. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  1441. bool verbose,
  1442. bool clear)
  1443. {
  1444. // Get the list of target files to check
  1445. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1446. if(!infoDef)
  1447. {
  1448. return;
  1449. }
  1450. std::vector<std::string> files;
  1451. cmSystemTools::ExpandListArgument(infoDef, files);
  1452. // For each info file run the check
  1453. cmDependsC checker;
  1454. checker.SetVerbose(verbose);
  1455. checker.SetFileComparison
  1456. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1457. for(std::vector<std::string>::iterator l = files.begin();
  1458. l != files.end(); ++l)
  1459. {
  1460. // either clear or check the files
  1461. std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
  1462. std::string internalDependFile = dir + "/depend.internal";
  1463. std::string dependFile = dir + "/depend.make";
  1464. if (clear)
  1465. {
  1466. checker.Clear(internalDependFile.c_str());
  1467. checker.Clear(dependFile.c_str());
  1468. }
  1469. else
  1470. {
  1471. checker.Check(dependFile.c_str(), internalDependFile.c_str());
  1472. }
  1473. }
  1474. }
  1475. void cmLocalUnixMakefileGenerator3
  1476. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1477. {
  1478. // now write all the language stuff
  1479. // Set the set of files to check for dependency integrity.
  1480. std::set<cmStdString> checkSetLangs;
  1481. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  1482. checkSet = this->GetIntegrityCheckSet()[target.GetName()];
  1483. for(std::map<cmStdString,
  1484. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  1485. l = checkSet.begin(); l != checkSet.end(); ++l)
  1486. {
  1487. checkSetLangs.insert(l->first);
  1488. }
  1489. // list the languages
  1490. cmakefileStream
  1491. << "# The set of files whose dependency integrity should be checked:\n";
  1492. cmakefileStream
  1493. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  1494. for(std::set<cmStdString>::iterator
  1495. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1496. {
  1497. cmakefileStream << " \"" << l->c_str() << "\"\n";
  1498. }
  1499. cmakefileStream << " )\n";
  1500. // now list the files for each language
  1501. for(std::set<cmStdString>::iterator
  1502. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1503. {
  1504. cmakefileStream
  1505. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  1506. // get the check set for this local gen and language
  1507. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  1508. checkSet[*l];
  1509. // for each file
  1510. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator
  1511. csIter = iCheckSet.begin();
  1512. csIter != iCheckSet.end(); ++csIter)
  1513. {
  1514. cmakefileStream << " \"" << (*csIter)->GetFullPath() << "\"\n";
  1515. // Get the full path name of the object file.
  1516. std::string obj = this->Makefile->GetStartOutputDirectory();
  1517. obj += "/";
  1518. obj += this->GetObjectFileName(target, **csIter);
  1519. cmakefileStream << " \"" <<
  1520. this->Convert(obj.c_str(),
  1521. cmLocalGenerator::FULL).c_str() << "\"\n";
  1522. }
  1523. cmakefileStream << " )\n";
  1524. }
  1525. }
  1526. //----------------------------------------------------------------------------
  1527. std::string
  1528. cmLocalUnixMakefileGenerator3
  1529. ::GetObjectFileName(cmTarget& target,
  1530. const cmSourceFile& source,
  1531. std::string* nameWithoutTargetDir)
  1532. {
  1533. // Get the object file name independent of target.
  1534. std::string objectName = this->GetObjectFileNameWithoutTarget(source);
  1535. if(nameWithoutTargetDir)
  1536. {
  1537. *nameWithoutTargetDir = objectName;
  1538. }
  1539. // Prepend the target directory.
  1540. std::string obj;
  1541. const char* fileTargetDirectory =
  1542. source.GetProperty("MACOSX_PACKAGE_LOCATION");
  1543. if ( fileTargetDirectory )
  1544. {
  1545. objectName = cmSystemTools::GetFilenameName(objectName.c_str());
  1546. std::string targetName;
  1547. std::string targetNameReal;
  1548. std::string targetNamePDB;
  1549. target.GetExecutableNames(targetName, targetNameReal,
  1550. targetNamePDB, this->ConfigurationName.c_str());
  1551. if ( target.GetPropertyAsBool("MACOSX_BUNDLE") )
  1552. {
  1553. // Construct the full path version of the names.
  1554. obj = this->ExecutableOutputPath;
  1555. if(obj.empty())
  1556. {
  1557. obj = this->Makefile->GetStartOutputDirectory();
  1558. obj += "/";
  1559. }
  1560. obj += targetName + ".app/Contents/";
  1561. obj += fileTargetDirectory;
  1562. }
  1563. else
  1564. {
  1565. // Framework not handled yet
  1566. abort();
  1567. }
  1568. obj = cmSystemTools::RelativePath
  1569. (this->Makefile->GetHomeOutputDirectory(), obj.c_str());
  1570. }
  1571. else
  1572. {
  1573. obj = this->GetTargetDirectory(target);
  1574. }
  1575. obj += "/";
  1576. obj += objectName;
  1577. return obj;
  1578. }
  1579. //----------------------------------------------------------------------------
  1580. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1581. {
  1582. os
  1583. << "# CMAKE generated file: DO NOT EDIT!\n"
  1584. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1585. << " Generator, CMake Version "
  1586. << cmVersion::GetMajorVersion() << "."
  1587. << cmVersion::GetMinorVersion() << "\n\n";
  1588. }
  1589. //----------------------------------------------------------------------------
  1590. std::string
  1591. cmLocalUnixMakefileGenerator3
  1592. ::GetRecursiveMakeCall(const char *makefile, const char* tgt)
  1593. {
  1594. // Call make on the given file.
  1595. std::string cmd;
  1596. cmd += "$(MAKE) -f ";
  1597. cmd += this->Convert(makefile,NONE,MAKEFILE);
  1598. cmd += " ";
  1599. // Passg down verbosity level.
  1600. if(this->GetMakeSilentFlag().size())
  1601. {
  1602. cmd += this->GetMakeSilentFlag();
  1603. cmd += " ";
  1604. }
  1605. // Most unix makes will pass the command line flags to make down to
  1606. // sub-invoked makes via an environment variable. However, some
  1607. // makes do not support that, so you have to pass the flags
  1608. // explicitly.
  1609. if(this->GetPassMakeflags())
  1610. {
  1611. cmd += "-$(MAKEFLAGS) ";
  1612. }
  1613. // Add the target.
  1614. if (tgt && tgt[0] != '\0')
  1615. {
  1616. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  1617. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  1618. cmd += tgt2;
  1619. }
  1620. return cmd;
  1621. }
  1622. //----------------------------------------------------------------------------
  1623. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1624. {
  1625. os
  1626. << "#======================================"
  1627. << "=======================================\n";
  1628. }
  1629. //----------------------------------------------------------------------------
  1630. void
  1631. cmLocalUnixMakefileGenerator3
  1632. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1633. {
  1634. // Write the given string to the stream with escaping to get it back
  1635. // into CMake through the lexical scanner.
  1636. os << "\"";
  1637. for(const char* c = s; *c; ++c)
  1638. {
  1639. if(*c == '\\')
  1640. {
  1641. os << "\\\\";
  1642. }
  1643. else if(*c == '"')
  1644. {
  1645. os << "\\\"";
  1646. }
  1647. else
  1648. {
  1649. os << *c;
  1650. }
  1651. }
  1652. os << "\"";
  1653. }
  1654. //----------------------------------------------------------------------------
  1655. std::string
  1656. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1657. {
  1658. // Split the path into its components.
  1659. std::vector<std::string> components;
  1660. cmSystemTools::SplitPath(p, components);
  1661. // Return an empty path if there are no components.
  1662. if(components.empty())
  1663. {
  1664. return "\"\"";
  1665. }
  1666. // Choose a slash direction and fix root component.
  1667. const char* slash = "/";
  1668. #if defined(_WIN32) && !defined(__CYGWIN__)
  1669. if(!cmSystemTools::GetForceUnixPaths())
  1670. {
  1671. slash = "\\";
  1672. for(std::string::iterator i = components[0].begin();
  1673. i != components[0].end(); ++i)
  1674. {
  1675. if(*i == '/')
  1676. {
  1677. *i = '\\';
  1678. }
  1679. }
  1680. }
  1681. #endif
  1682. // Begin the quoted result with the root component.
  1683. std::string result = "\"";
  1684. result += components[0];
  1685. // Now add the rest of the components separated by the proper slash
  1686. // direction for this platform.
  1687. bool first = true;
  1688. for(unsigned int i=1; i < components.size(); ++i)
  1689. {
  1690. // Only the last component can be empty to avoid double slashes.
  1691. if(components[i].length() > 0 || (i == (components.size()-1)))
  1692. {
  1693. if(!first)
  1694. {
  1695. result += slash;
  1696. }
  1697. result += components[i];
  1698. first = false;
  1699. }
  1700. }
  1701. // Close the quoted result.
  1702. result += "\"";
  1703. return result;
  1704. }
  1705. //----------------------------------------------------------------------------
  1706. std::string
  1707. cmLocalUnixMakefileGenerator3::GetTargetDirectory(cmTarget& target)
  1708. {
  1709. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1710. dir += target.GetName();
  1711. dir += ".dir";
  1712. return dir;
  1713. }
  1714. void cmLocalUnixMakefileGenerator3
  1715. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  1716. const char *retDir)
  1717. {
  1718. // do we need to cd?
  1719. if (!strcmp(tgtDir,retDir))
  1720. {
  1721. return;
  1722. }
  1723. if(!this->UnixCD)
  1724. {
  1725. // On Windows we must perform each step separately and then change
  1726. // back because the shell keeps the working directory between
  1727. // commands.
  1728. std::string cmd = "cd ";
  1729. cmd += this->ConvertToOutputForExisting(tgtDir);
  1730. commands.insert(commands.begin(),cmd);
  1731. // Change back to the starting directory. Any trailing slash must be
  1732. // removed to avoid problems with Borland Make.
  1733. std::string back = retDir;
  1734. if(back.size() && back[back.size()-1] == '/')
  1735. {
  1736. back = back.substr(0, back.size()-1);
  1737. }
  1738. cmd = "cd ";
  1739. cmd += this->ConvertToOutputForExisting(back.c_str());
  1740. commands.push_back(cmd);
  1741. }
  1742. else
  1743. {
  1744. // On UNIX we must construct a single shell command to change
  1745. // directory and build because make resets the directory between
  1746. // each command.
  1747. std::vector<std::string>::iterator i = commands.begin();
  1748. for (; i != commands.end(); ++i)
  1749. {
  1750. std::string cmd = "cd ";
  1751. cmd += this->ConvertToOutputForExisting(tgtDir);
  1752. cmd += " && ";
  1753. cmd += *i;
  1754. *i = cmd;
  1755. }
  1756. }
  1757. }
  1758. void cmLocalUnixMakefileGenerator3
  1759. ::GetTargetObjectFileDirectories(cmTarget* target,
  1760. std::vector<std::string>& dirs)
  1761. {
  1762. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1763. dir += "/";
  1764. dir += this->GetTargetDirectory(*target);
  1765. dirs.push_back(dir);
  1766. }