cmLocalUnixMakefileGenerator3.cxx 57 KB

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