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