cmLocalUnixMakefileGenerator3.cxx 53 KB

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