cmLocalUnixMakefileGenerator3.cxx 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. this->WriteMakeRule(makefileStream,
  575. "Special rule to run CMake to check the build system "
  576. "integrity.\n"
  577. "No rule that depends on this can have "
  578. "commands that come from listfiles\n"
  579. "because they might be regenerated.",
  580. "cmake_check_build_system",
  581. no_depends,
  582. commands, true);
  583. }
  584. std::vector<std::string> no_commands;
  585. }
  586. //----------------------------------------------------------------------------
  587. void
  588. cmLocalUnixMakefileGenerator3
  589. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  590. const char* realTarget,
  591. const char* helpTarget)
  592. {
  593. // A rule is only needed if the names are different.
  594. if(strcmp(realTarget, helpTarget) != 0)
  595. {
  596. // The helper target depends on the real target.
  597. std::vector<std::string> depends;
  598. depends.push_back(realTarget);
  599. // There are no commands.
  600. std::vector<std::string> no_commands;
  601. // Write the rule.
  602. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  603. helpTarget, depends, no_commands, true);
  604. }
  605. }
  606. //----------------------------------------------------------------------------
  607. std::string
  608. cmLocalUnixMakefileGenerator3::GetRelativeTargetDirectory(cmTarget& target)
  609. {
  610. std::string dir = m_Makefile->GetStartOutputDirectory();
  611. dir += "/";
  612. dir += this->GetTargetDirectory(target);
  613. dir = cmSystemTools::RelativePath(m_Makefile->GetHomeOutputDirectory(), dir.c_str());
  614. return this->Convert(dir.c_str(),NONE,MAKEFILE);
  615. }
  616. //----------------------------------------------------------------------------
  617. void
  618. cmLocalUnixMakefileGenerator3
  619. ::AppendRuleDepend(std::vector<std::string>& depends,
  620. const char* ruleFileName)
  621. {
  622. // Add a dependency on the rule file itself unless an option to skip
  623. // it is specifically enabled by the user or project.
  624. const char* nodep = m_Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  625. if(!nodep || cmSystemTools::IsOff(nodep))
  626. {
  627. depends.push_back(ruleFileName);
  628. }
  629. }
  630. //----------------------------------------------------------------------------
  631. void
  632. cmLocalUnixMakefileGenerator3
  633. ::AppendCustomDepends(std::vector<std::string>& depends,
  634. const std::vector<cmCustomCommand>& ccs)
  635. {
  636. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  637. i != ccs.end(); ++i)
  638. {
  639. this->AppendCustomDepend(depends, *i);
  640. }
  641. }
  642. //----------------------------------------------------------------------------
  643. void
  644. cmLocalUnixMakefileGenerator3
  645. ::AppendCustomDepend(std::vector<std::string>& depends,
  646. const cmCustomCommand& cc)
  647. {
  648. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  649. d != cc.GetDepends().end(); ++d)
  650. {
  651. // Lookup the real name of the dependency in case it is a CMake target.
  652. std::string dep = this->GetRealDependency(d->c_str(),
  653. m_ConfigurationName.c_str());
  654. depends.push_back(dep);
  655. }
  656. }
  657. //----------------------------------------------------------------------------
  658. void
  659. cmLocalUnixMakefileGenerator3
  660. ::AppendCustomCommands(std::vector<std::string>& commands,
  661. const std::vector<cmCustomCommand>& ccs)
  662. {
  663. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  664. i != ccs.end(); ++i)
  665. {
  666. this->AppendCustomCommand(commands, *i);
  667. }
  668. }
  669. //----------------------------------------------------------------------------
  670. void
  671. cmLocalUnixMakefileGenerator3
  672. ::AppendCustomCommand(std::vector<std::string>& commands,
  673. const cmCustomCommand& cc)
  674. {
  675. std::vector<std::string> commands1;
  676. // Add each command line to the set of commands.
  677. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  678. cl != cc.GetCommandLines().end(); ++cl)
  679. {
  680. // Build the command line in a single string.
  681. const cmCustomCommandLine& commandLine = *cl;
  682. std::string cmd = commandLine[0];
  683. if (cmd.size())
  684. {
  685. cmSystemTools::ReplaceString(cmd, "/./", "/");
  686. cmd = this->Convert(cmd.c_str(),START_OUTPUT);
  687. if(cmd.find("/") == cmd.npos &&
  688. commandLine[0].find("/") != cmd.npos)
  689. {
  690. // Add a leading "./" for executables in the current directory.
  691. cmd = "./" + cmd;
  692. }
  693. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  694. for(unsigned int j=1; j < commandLine.size(); ++j)
  695. {
  696. cmd += " ";
  697. bool forceOn = cmSystemTools::GetForceUnixPaths();
  698. if(forceOn && m_WindowsShell)
  699. {
  700. cmSystemTools::SetForceUnixPaths(false);
  701. }
  702. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  703. if(forceOn && m_WindowsShell)
  704. {
  705. cmSystemTools::SetForceUnixPaths(true);
  706. }
  707. }
  708. commands1.push_back(cmd);
  709. }
  710. }
  711. // push back the custom commands
  712. const char* dir = m_Makefile->GetStartOutputDirectory();
  713. // if the command specified a working directory use it.
  714. if(cc.GetWorkingDirectory())
  715. {
  716. dir = cc.GetWorkingDirectory();
  717. }
  718. this->CreateCDCommand(commands1, dir,
  719. m_Makefile->GetHomeOutputDirectory());
  720. commands.insert(commands.end(), commands1.begin(), commands1.end());
  721. }
  722. //----------------------------------------------------------------------------
  723. void
  724. cmLocalUnixMakefileGenerator3
  725. ::AppendCleanCommand(std::vector<std::string>& commands,
  726. const std::vector<std::string>& files)
  727. {
  728. if(!files.empty())
  729. {
  730. std::string remove = "$(CMAKE_COMMAND) -E remove -f";
  731. for(std::vector<std::string>::const_iterator f = files.begin();
  732. f != files.end(); ++f)
  733. {
  734. remove += " ";
  735. remove += this->Convert(f->c_str(),START_OUTPUT,SHELL);
  736. }
  737. commands.push_back(remove);
  738. }
  739. }
  740. //----------------------------------------------------------------------------
  741. void
  742. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  743. const char* text)
  744. {
  745. // Echo one line at a time.
  746. std::string line;
  747. for(const char* c = text;; ++c)
  748. {
  749. if(*c == '\n' || *c == '\0')
  750. {
  751. // Avoid writing a blank last line on end-of-string.
  752. if(*c != '\0' || !line.empty())
  753. {
  754. // Add a command to echo this line.
  755. std::string cmd = "@echo ";
  756. if(m_EchoNeedsQuote)
  757. {
  758. cmd += "\"";
  759. }
  760. cmd += line;
  761. if(m_EchoNeedsQuote)
  762. {
  763. cmd += "\"";
  764. }
  765. commands.push_back(cmd);
  766. }
  767. // Reset the line to emtpy.
  768. line = "";
  769. // Terminate on end-of-string.
  770. if(*c == '\0')
  771. {
  772. return;
  773. }
  774. }
  775. else if(*c != '\r')
  776. {
  777. // Append this character to the current line.
  778. line += *c;
  779. }
  780. }
  781. }
  782. //----------------------------------------------------------------------------
  783. // take a tgt path and convert it into a make target, it could be full, or relative
  784. std::string
  785. cmLocalUnixMakefileGenerator3
  786. ::ConvertToMakeTarget(const char* tgt)
  787. {
  788. // Make targets should not have a leading './' for a file in the
  789. // directory containing the makefile.
  790. std::string ret = tgt;
  791. if(ret.size() > 2 && (ret[0] == '.') &&
  792. ( (ret[1] == '/') || ret[1] == '\\'))
  793. {
  794. std::string upath = ret;
  795. cmSystemTools::ConvertToUnixSlashes(upath);
  796. if(upath.find(2, '/') == upath.npos)
  797. {
  798. ret = ret.substr(2, ret.size()-2);
  799. }
  800. }
  801. return ret;
  802. }
  803. //----------------------------------------------------------------------------
  804. std::string&
  805. cmLocalUnixMakefileGenerator3::CreateSafeUniqueObjectFileName(const char* sin)
  806. {
  807. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  808. {
  809. std::map<cmStdString,cmStdString>::iterator it =
  810. m_UniqueObjectNamesMap.find(sin);
  811. if ( it == m_UniqueObjectNamesMap.end() )
  812. {
  813. std::string ssin = sin;
  814. bool done;
  815. int cc = 0;
  816. char rpstr[100];
  817. sprintf(rpstr, "_p_");
  818. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  819. std::string sssin = sin;
  820. do
  821. {
  822. done = true;
  823. for ( it = m_UniqueObjectNamesMap.begin();
  824. it != m_UniqueObjectNamesMap.end();
  825. ++ it )
  826. {
  827. if ( it->second == ssin )
  828. {
  829. done = false;
  830. }
  831. }
  832. if ( done )
  833. {
  834. break;
  835. }
  836. sssin = ssin;
  837. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  838. sprintf(rpstr, "_p%d_", cc++);
  839. }
  840. while ( !done );
  841. m_UniqueObjectNamesMap[sin] = ssin;
  842. }
  843. }
  844. else
  845. {
  846. m_UniqueObjectNamesMap[sin] = sin;
  847. }
  848. return m_UniqueObjectNamesMap[sin];
  849. }
  850. //----------------------------------------------------------------------------
  851. std::string
  852. cmLocalUnixMakefileGenerator3
  853. ::CreateMakeVariable(const char* sin, const char* s2in)
  854. {
  855. std::string s = sin;
  856. std::string s2 = s2in;
  857. std::string unmodified = s;
  858. unmodified += s2;
  859. // if there is no restriction on the length of make variables
  860. // and there are no "." charactors in the string, then return the
  861. // unmodified combination.
  862. if((!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  863. && (!m_MakefileVariableSize && unmodified.find('-') == s.npos))
  864. {
  865. return unmodified;
  866. }
  867. // see if the variable has been defined before and return
  868. // the modified version of the variable
  869. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  870. if(i != m_MakeVariableMap.end())
  871. {
  872. return i->second;
  873. }
  874. // start with the unmodified variable
  875. std::string ret = unmodified;
  876. // if this there is no value for m_MakefileVariableSize then
  877. // the string must have bad characters in it
  878. if(!m_MakefileVariableSize)
  879. {
  880. cmSystemTools::ReplaceString(ret, ".", "_");
  881. cmSystemTools::ReplaceString(ret, "-", "__");
  882. int ni = 0;
  883. char buffer[5];
  884. // make sure the _ version is not already used, if
  885. // it is used then add number to the end of the variable
  886. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  887. {
  888. ++ni;
  889. sprintf(buffer, "%04d", ni);
  890. ret = unmodified + buffer;
  891. }
  892. m_ShortMakeVariableMap[ret] = "1";
  893. m_MakeVariableMap[unmodified] = ret;
  894. return ret;
  895. }
  896. // if the string is greater the 32 chars it is an invalid vairable name
  897. // for borland make
  898. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  899. {
  900. int keep = m_MakefileVariableSize - 8;
  901. int size = keep + 3;
  902. std::string str1 = s;
  903. std::string str2 = s2;
  904. // we must shorten the combined string by 4 charactors
  905. // keep no more than 24 charactors from the second string
  906. if(static_cast<int>(str2.size()) > keep)
  907. {
  908. str2 = str2.substr(0, keep);
  909. }
  910. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  911. {
  912. str1 = str1.substr(0, size - str2.size());
  913. }
  914. char buffer[5];
  915. int ni = 0;
  916. sprintf(buffer, "%04d", ni);
  917. ret = str1 + str2 + buffer;
  918. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  919. {
  920. ++ni;
  921. sprintf(buffer, "%04d", ni);
  922. ret = str1 + str2 + buffer;
  923. }
  924. if(ni == 1000)
  925. {
  926. cmSystemTools::Error("Borland makefile variable length too long");
  927. return unmodified;
  928. }
  929. // once an unused variable is found
  930. m_ShortMakeVariableMap[ret] = "1";
  931. }
  932. // always make an entry into the unmodified to variable map
  933. m_MakeVariableMap[unmodified] = ret;
  934. return ret;
  935. }
  936. //----------------------------------------------------------------------------
  937. bool
  938. cmLocalUnixMakefileGenerator3
  939. ::ScanDependencies(std::vector<std::string> const& args)
  940. {
  941. // Format of arguments is:
  942. // $(CMAKE_COMMAND), cmake_depends, GeneratorName, home_output_dir, start_output_dir, info file
  943. // The caller has ensured that all required arguments exist.
  944. // The info file for this target
  945. std::string const& infoFile = args[5];
  946. // Read the directory information file.
  947. cmake cm;
  948. cmGlobalGenerator gg;
  949. gg.SetCMakeInstance(&cm);
  950. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  951. lg->SetGlobalGenerator(&gg);
  952. cmMakefile* mf = lg->GetMakefile();
  953. mf->SetHomeOutputDirectory(args[3].c_str());
  954. mf->SetStartOutputDirectory(args[4].c_str());
  955. lg->SetupPathConversions();
  956. bool haveDirectoryInfo = false;
  957. std::string dirInfoFile = args[4];
  958. dirInfoFile += "/CMakeFiles/CMakeDirectoryInformation.cmake";
  959. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  960. !cmSystemTools::GetErrorOccuredFlag())
  961. {
  962. haveDirectoryInfo = true;
  963. }
  964. // read in the target info file
  965. if(!mf->ReadListFile(0, infoFile.c_str()) ||
  966. cmSystemTools::GetErrorOccuredFlag())
  967. {
  968. cmSystemTools::Error("Target DependInfo.cmake file not found");
  969. }
  970. // Test whether we need to force Unix paths.
  971. if(haveDirectoryInfo)
  972. {
  973. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  974. {
  975. if(!cmSystemTools::IsOff(force))
  976. {
  977. cmSystemTools::SetForceUnixPaths(true);
  978. }
  979. }
  980. }
  981. else
  982. {
  983. cmSystemTools::Error("Directory Information file not found");
  984. }
  985. // create the file stream for the depends file
  986. std::string dir = cmSystemTools::GetFilenamePath(infoFile);
  987. // Open the rule file. This should be copy-if-different because the
  988. // rules may depend on this file itself.
  989. std::string ruleFileNameFull = dir;
  990. ruleFileNameFull += "/depend.make";
  991. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  992. ruleFileStream.SetCopyIfDifferent(true);
  993. if(!ruleFileStream)
  994. {
  995. return false;
  996. }
  997. std::string internalRuleFileNameFull = dir;
  998. internalRuleFileNameFull += "/depend.internal";
  999. cmGeneratedFileStream internalRuleFileStream(internalRuleFileNameFull.c_str());
  1000. internalRuleFileStream.SetCopyIfDifferent(true);
  1001. if(!internalRuleFileStream)
  1002. {
  1003. return false;
  1004. }
  1005. this->WriteDisclaimer(ruleFileStream);
  1006. this->WriteDisclaimer(internalRuleFileStream);
  1007. // Get the set of generated files.
  1008. std::vector<std::string> generatedFilesVec;
  1009. if(haveDirectoryInfo)
  1010. {
  1011. if(const char* generated = mf->GetDefinition("CMAKE_GENERATED_FILES"))
  1012. {
  1013. cmSystemTools::ExpandListArgument(generated, generatedFilesVec);
  1014. }
  1015. }
  1016. // Sort for efficient lookup.
  1017. std::set<cmStdString> generatedFiles;
  1018. for(std::vector<std::string>::iterator gfi = generatedFilesVec.begin();
  1019. gfi != generatedFilesVec.end(); ++gfi)
  1020. {
  1021. generatedFiles.insert(*gfi);
  1022. }
  1023. // for each language we need to scan, scan it
  1024. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1025. std::vector<std::string> langs;
  1026. cmSystemTools::ExpandListArgument(langStr, langs);
  1027. for (std::vector<std::string>::iterator li =
  1028. langs.begin(); li != langs.end(); ++li)
  1029. {
  1030. // construct the checker
  1031. std::string lang = li->c_str();
  1032. // Get the set of include directories.
  1033. std::vector<std::string> includes;
  1034. if(haveDirectoryInfo)
  1035. {
  1036. std::string includePathVar = "CMAKE_";
  1037. includePathVar += lang;
  1038. includePathVar += "_INCLUDE_PATH";
  1039. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  1040. {
  1041. cmSystemTools::ExpandListArgument(includePath, includes);
  1042. }
  1043. }
  1044. // Get the include file regular expression.
  1045. std::string includeRegexScan = "^.*$";
  1046. std::string includeRegexComplain = "^$";
  1047. if(haveDirectoryInfo)
  1048. {
  1049. std::string scanRegexVar = "CMAKE_";
  1050. scanRegexVar += lang;
  1051. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  1052. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  1053. {
  1054. includeRegexScan = scanRegex;
  1055. }
  1056. std::string complainRegexVar = "CMAKE_";
  1057. complainRegexVar += lang;
  1058. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  1059. if(const char* complainRegex = mf->GetDefinition(complainRegexVar.c_str()))
  1060. {
  1061. includeRegexComplain = complainRegex;
  1062. }
  1063. }
  1064. // Create the scanner for this language
  1065. cmDepends *scanner = 0;
  1066. if(lang == "C" || lang == "CXX" || lang == "RC")
  1067. {
  1068. std::string includeCacheFileName = dir;
  1069. includeCacheFileName += "/includecache.";
  1070. includeCacheFileName += lang;
  1071. // TODO: Handle RC (resource files) dependencies correctly.
  1072. scanner = new cmDependsC(includes,
  1073. includeRegexScan.c_str(),
  1074. includeRegexComplain.c_str(),
  1075. generatedFiles, includeCacheFileName);
  1076. }
  1077. #ifdef CMAKE_BUILD_WITH_CMAKE
  1078. else if(lang == "Fortran")
  1079. {
  1080. scanner = new cmDependsFortran(includes);
  1081. }
  1082. else if(lang == "Java")
  1083. {
  1084. scanner = new cmDependsJava();
  1085. }
  1086. #endif
  1087. if (scanner)
  1088. {
  1089. scanner->SetFileComparison(m_GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1090. // for each file we need to scan
  1091. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  1092. srcLang += lang;
  1093. const char *srcStr = mf->GetSafeDefinition(srcLang.c_str());
  1094. std::vector<std::string> srcs;
  1095. cmSystemTools::ExpandListArgument(srcStr, srcs);
  1096. for (std::vector<std::string>::iterator si =
  1097. srcs.begin(); si != srcs.end(); ++si)
  1098. {
  1099. std::string &src = *si;
  1100. ++si;
  1101. // make sure the object file is relative to home output
  1102. std::string obj = *si;
  1103. obj = lg->Convert(obj.c_str(),HOME_OUTPUT,MAKEFILE);
  1104. scanner->Write(src.c_str(),obj.c_str(),ruleFileStream, internalRuleFileStream);
  1105. }
  1106. // free the scanner for this language
  1107. delete scanner;
  1108. }
  1109. }
  1110. // dependencies were generated, so touch the mark file
  1111. ruleFileNameFull += ".mark";
  1112. std::ofstream fmark(ruleFileNameFull.c_str());
  1113. fmark << "Dependencies updated>" << std::endl;
  1114. return true;
  1115. }
  1116. //----------------------------------------------------------------------------
  1117. void cmLocalUnixMakefileGenerator3
  1118. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1119. {
  1120. this->WriteDisclaimer(ruleFileStream);
  1121. this->WriteMakeVariables(ruleFileStream);
  1122. // Write the main entry point target. This must be the VERY first
  1123. // target so that make with no arguments will run it.
  1124. {
  1125. // Just depend on the all target to drive the build.
  1126. std::vector<std::string> depends;
  1127. std::vector<std::string> no_commands;
  1128. depends.push_back("all");
  1129. // Write the rule.
  1130. this->WriteMakeRule(ruleFileStream,
  1131. "Default target executed when no arguments are "
  1132. "given to make.",
  1133. "default_target",
  1134. depends,
  1135. no_commands, true);
  1136. }
  1137. // Write special "test" target to run ctest.
  1138. if(m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  1139. {
  1140. std::string ctest;
  1141. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  1142. {
  1143. // We are building CMake itself. Use the ctest that comes with
  1144. // this version of CMake instead of the one used to build it.
  1145. ctest = m_ExecutableOutputPath;
  1146. ctest += "ctest";
  1147. ctest += cmSystemTools::GetExecutableExtension();
  1148. ctest = this->Convert(ctest.c_str(),START_OUTPUT,SHELL);
  1149. ctest += " --force-new-ctest-process";
  1150. }
  1151. else
  1152. {
  1153. // We are building another project. Use the ctest that comes with
  1154. // the CMake building it.
  1155. ctest = m_Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  1156. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1157. ctest += "/";
  1158. ctest += "ctest";
  1159. ctest += cmSystemTools::GetExecutableExtension();
  1160. ctest = this->ConvertToOutputForExisting(ctest.c_str());
  1161. }
  1162. std::vector<std::string> no_depends;
  1163. std::vector<std::string> commands;
  1164. this->AppendEcho(commands, "Running tests...");
  1165. ctest += " $(ARGS)";
  1166. commands.push_back(ctest);
  1167. this->WriteMakeRule(ruleFileStream,
  1168. "Special rule to drive testing with ctest.",
  1169. "test", no_depends, commands, true);
  1170. }
  1171. // Write special "install" target to run cmake_install.cmake script.
  1172. {
  1173. std::vector<std::string> depends;
  1174. depends.push_back("preinstall");
  1175. std::vector<std::string> commands;
  1176. std::string cmd;
  1177. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  1178. {
  1179. // We are building CMake itself. We cannot use the original
  1180. // executable to install over itself.
  1181. cmd = m_ExecutableOutputPath;
  1182. cmd += "cmake";
  1183. cmd = this->Convert(cmd.c_str(),START_OUTPUT,SHELL);
  1184. }
  1185. else
  1186. {
  1187. cmd = "$(CMAKE_COMMAND)";
  1188. }
  1189. cmd += " -P cmake_install.cmake";
  1190. commands.push_back(cmd);
  1191. this->WriteMakeRule(ruleFileStream,
  1192. "Special rule to run installation script.",
  1193. "install", depends, commands, true);
  1194. commands.clear();
  1195. depends.clear();
  1196. const char* noall =
  1197. m_Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1198. if(!noall || cmSystemTools::IsOff(noall))
  1199. {
  1200. // Drive the build before installing.
  1201. depends.push_back("all");
  1202. }
  1203. else
  1204. {
  1205. // At least make sure the build system is up to date.
  1206. depends.push_back("cmake_check_build_system");
  1207. }
  1208. commands.push_back(this->GetRecursiveMakeCall
  1209. ("CMakeFiles/Makefile2", "preinstall"));
  1210. this->WriteMakeRule(ruleFileStream,
  1211. "Prepare targets for installation.",
  1212. "preinstall", depends, commands, true);
  1213. }
  1214. // Write special "rebuild_cache" target to re-run cmake.
  1215. {
  1216. std::vector<std::string> no_depends;
  1217. std::vector<std::string> commands;
  1218. this->AppendEcho(commands, "Running CMake to regenerate build system...");
  1219. commands.push_back(
  1220. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1221. this->WriteMakeRule(ruleFileStream,
  1222. "Special rule to re-run CMake using make.",
  1223. "rebuild_cache",
  1224. no_depends,
  1225. commands, true);
  1226. }
  1227. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1228. // Otherwise default to the interactive command-line interface.
  1229. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1230. {
  1231. std::vector<std::string> no_depends;
  1232. std::vector<std::string> commands;
  1233. this->AppendEcho(commands, "Running CMake cache editor...");
  1234. commands.push_back(
  1235. "$(CMAKE_EDIT_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1236. this->WriteMakeRule(ruleFileStream,
  1237. "Special rule to re-run CMake cache editor using make.",
  1238. "edit_cache",
  1239. no_depends,
  1240. commands, true);
  1241. }
  1242. else
  1243. {
  1244. std::vector<std::string> no_depends;
  1245. std::vector<std::string> commands;
  1246. this->AppendEcho(commands,
  1247. "Running interactive CMake command-line interface...");
  1248. commands.push_back(
  1249. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  1250. this->WriteMakeRule(ruleFileStream,
  1251. "Special rule to re-run CMake cache editor using make.",
  1252. "edit_cache",
  1253. no_depends,
  1254. commands, true);
  1255. }
  1256. this->WriteSpecialTargetsTop(ruleFileStream);
  1257. std::vector<std::string> depends;
  1258. std::vector<std::string> commands;
  1259. // Write the all rule.
  1260. std::string dir = m_Makefile->GetStartOutputDirectory();
  1261. dir += "/directorystart";
  1262. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1263. // if at the top the rule is called all
  1264. if (!m_Parent)
  1265. {
  1266. dir = "all";
  1267. depends.push_back("cmake_check_build_system");
  1268. }
  1269. commands.push_back
  1270. (this->GetRecursiveMakeCall("CMakeFiles/Makefile2",dir.c_str()));
  1271. this->CreateCDCommand(commands,
  1272. m_Makefile->GetHomeOutputDirectory(),
  1273. m_Makefile->GetStartOutputDirectory());
  1274. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1275. depends, commands, true);
  1276. // Write the clean rule.
  1277. dir = m_Makefile->GetStartOutputDirectory();
  1278. dir += "/clean";
  1279. dir = this->Convert(dir.c_str(),HOME_OUTPUT,MAKEFILE);
  1280. commands.clear();
  1281. depends.clear();
  1282. commands.push_back
  1283. (this->GetRecursiveMakeCall("CMakeFiles/Makefile2",dir.c_str()));
  1284. this->CreateCDCommand(commands,
  1285. m_Makefile->GetHomeOutputDirectory(),
  1286. m_Makefile->GetStartOutputDirectory());
  1287. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1288. depends, commands, true);
  1289. // write the depend rule, really a recompute depends rule
  1290. depends.clear();
  1291. commands.clear();
  1292. std::string cmakefileName = "CMakeFiles/Makefile.cmake";
  1293. this->Convert(cmakefileName.c_str(),HOME_OUTPUT,
  1294. cmLocalGenerator::MAKEFILE);
  1295. std::string runRule =
  1296. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1297. runRule += " --check-build-system ";
  1298. runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  1299. cmLocalGenerator::SHELL);
  1300. runRule += " 1";
  1301. commands.push_back(runRule);
  1302. this->WriteMakeRule(ruleFileStream, "clear depends",
  1303. "depend",
  1304. depends, commands, true);
  1305. }
  1306. //----------------------------------------------------------------------------
  1307. void cmLocalUnixMakefileGenerator3::CheckDependencies(cmMakefile* mf,
  1308. bool verbose,
  1309. bool clear)
  1310. {
  1311. // Get the list of target files to check
  1312. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1313. if(!infoDef)
  1314. {
  1315. return;
  1316. }
  1317. std::vector<std::string> files;
  1318. cmSystemTools::ExpandListArgument(infoDef, files);
  1319. // For each info file run the check
  1320. cmDependsC checker;
  1321. checker.SetVerbose(verbose);
  1322. checker.SetFileComparison(m_GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1323. for(std::vector<std::string>::iterator l = files.begin();
  1324. l != files.end(); ++l)
  1325. {
  1326. // either clear or check the files
  1327. std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
  1328. std::string internalDependFile = dir + "/depend.internal";
  1329. std::string dependFile = dir + "/depend.make";
  1330. if (clear)
  1331. {
  1332. checker.Clear(internalDependFile.c_str());
  1333. checker.Clear(dependFile.c_str());
  1334. }
  1335. else
  1336. {
  1337. checker.Check(dependFile.c_str(), internalDependFile.c_str());
  1338. }
  1339. }
  1340. }
  1341. void cmLocalUnixMakefileGenerator3
  1342. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1343. {
  1344. // now write all the language stuff
  1345. // Set the set of files to check for dependency integrity.
  1346. std::set<cmStdString> checkSetLangs;
  1347. std::map<cmStdString,cmLocalUnixMakefileGenerator3::IntegrityCheckSet>&
  1348. checkSet = this->GetIntegrityCheckSet()[target.GetName()];
  1349. for(std::map<cmStdString,
  1350. cmLocalUnixMakefileGenerator3::IntegrityCheckSet>::const_iterator
  1351. l = checkSet.begin(); l != checkSet.end(); ++l)
  1352. {
  1353. checkSetLangs.insert(l->first);
  1354. }
  1355. // list the languages
  1356. cmakefileStream
  1357. << "# The set of files whose dependency integrity should be checked:\n";
  1358. cmakefileStream
  1359. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  1360. for(std::set<cmStdString>::iterator
  1361. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1362. {
  1363. cmakefileStream << " \"" << l->c_str() << "\"\n";
  1364. }
  1365. cmakefileStream << " )\n";
  1366. // now list the files for each language
  1367. for(std::set<cmStdString>::iterator
  1368. l = checkSetLangs.begin(); l != checkSetLangs.end(); ++l)
  1369. {
  1370. cmakefileStream
  1371. << "SET(CMAKE_DEPENDS_CHECK_" << l->c_str() << "\n";
  1372. // get the check set for this local gen and language
  1373. cmLocalUnixMakefileGenerator3::IntegrityCheckSet iCheckSet =
  1374. checkSet[*l];
  1375. // for each file
  1376. for(cmLocalUnixMakefileGenerator3::IntegrityCheckSet::const_iterator
  1377. csIter = iCheckSet.begin();
  1378. csIter != iCheckSet.end(); ++csIter)
  1379. {
  1380. cmakefileStream << " \"" << (*csIter)->GetFullPath() << "\"\n";
  1381. // Get the full path name of the object file.
  1382. std::string obj = m_Makefile->GetStartOutputDirectory();
  1383. obj += "/";
  1384. obj += this->GetObjectFileName(target, **csIter);
  1385. cmakefileStream << " \"" <<
  1386. this->Convert(obj.c_str(),
  1387. cmLocalGenerator::FULL).c_str() << "\"\n";
  1388. }
  1389. cmakefileStream << " )\n";
  1390. }
  1391. }
  1392. //----------------------------------------------------------------------------
  1393. std::string
  1394. cmLocalUnixMakefileGenerator3
  1395. ::GetObjectFileName(cmTarget& target,
  1396. const cmSourceFile& source,
  1397. std::string* nameWithoutTargetDir)
  1398. {
  1399. // If the full path to the source file includes this directory,
  1400. // we want to use the relative path for the filename of the
  1401. // object file. Otherwise, we will use just the filename
  1402. // portion.
  1403. std::string objectName;
  1404. if((cmSystemTools::GetFilenamePath(
  1405. source.GetFullPath()).find(
  1406. m_Makefile->GetCurrentDirectory()) == 0)
  1407. || (cmSystemTools::GetFilenamePath(
  1408. source.GetFullPath()).find(
  1409. m_Makefile->GetStartOutputDirectory()) == 0))
  1410. {
  1411. objectName = source.GetSourceName();
  1412. }
  1413. else
  1414. {
  1415. objectName = cmSystemTools::GetFilenameName(source.GetSourceName());
  1416. }
  1417. // Append the object file extension.
  1418. objectName +=
  1419. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  1420. source.GetSourceExtension().c_str());
  1421. // Convert to a safe name.
  1422. objectName = this->CreateSafeUniqueObjectFileName(objectName.c_str());
  1423. // Prepend the target directory.
  1424. std::string obj = this->GetTargetDirectory(target);
  1425. obj += "/";
  1426. obj += objectName;
  1427. if(nameWithoutTargetDir)
  1428. {
  1429. *nameWithoutTargetDir = objectName;
  1430. }
  1431. return obj;
  1432. }
  1433. //----------------------------------------------------------------------------
  1434. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1435. {
  1436. os
  1437. << "# CMAKE generated file: DO NOT EDIT!\n"
  1438. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  1439. << " Generator, CMake Version "
  1440. << cmMakefile::GetMajorVersion() << "."
  1441. << cmMakefile::GetMinorVersion() << "\n\n";
  1442. }
  1443. //----------------------------------------------------------------------------
  1444. std::string
  1445. cmLocalUnixMakefileGenerator3
  1446. ::GetRecursiveMakeCall(const char *Makefile, const char* tgt)
  1447. {
  1448. // Call make on the given file.
  1449. std::string cmd;
  1450. cmd += "$(MAKE) -f ";
  1451. cmd += Makefile;
  1452. cmd += " ";
  1453. // Passg down verbosity level.
  1454. if(this->GetMakeSilentFlag().size())
  1455. {
  1456. cmd += this->GetMakeSilentFlag();
  1457. cmd += " ";
  1458. }
  1459. // Most unix makes will pass the command line flags to make down to
  1460. // sub-invoked makes via an environment variable. However, some
  1461. // makes do not support that, so you have to pass the flags
  1462. // explicitly.
  1463. if(this->GetPassMakeflags())
  1464. {
  1465. cmd += "-$(MAKEFLAGS) ";
  1466. }
  1467. // Add the target.
  1468. if (tgt && tgt[0] != '\0')
  1469. {
  1470. std::string tgt2 = this->Convert(tgt,HOME_OUTPUT,MAKEFILE);
  1471. tgt2 = this->ConvertToMakeTarget(tgt2.c_str());
  1472. cmd += tgt2;
  1473. }
  1474. return cmd;
  1475. }
  1476. //----------------------------------------------------------------------------
  1477. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1478. {
  1479. os
  1480. << "#======================================"
  1481. << "=======================================\n";
  1482. }
  1483. //----------------------------------------------------------------------------
  1484. void
  1485. cmLocalUnixMakefileGenerator3
  1486. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1487. {
  1488. // Write the given string to the stream with escaping to get it back
  1489. // into CMake through the lexical scanner.
  1490. os << "\"";
  1491. for(const char* c = s; *c; ++c)
  1492. {
  1493. if(*c == '\\')
  1494. {
  1495. os << "\\\\";
  1496. }
  1497. else if(*c == '"')
  1498. {
  1499. os << "\\\"";
  1500. }
  1501. else
  1502. {
  1503. os << *c;
  1504. }
  1505. }
  1506. os << "\"";
  1507. }
  1508. //----------------------------------------------------------------------------
  1509. std::string
  1510. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1511. {
  1512. // Split the path into its components.
  1513. std::vector<std::string> components;
  1514. cmSystemTools::SplitPath(p, components);
  1515. // Return an empty path if there are no components.
  1516. if(components.empty())
  1517. {
  1518. return "\"\"";
  1519. }
  1520. // Choose a slash direction and fix root component.
  1521. const char* slash = "/";
  1522. #if defined(_WIN32) && !defined(__CYGWIN__)
  1523. if(!cmSystemTools::GetForceUnixPaths())
  1524. {
  1525. slash = "\\";
  1526. for(std::string::iterator i = components[0].begin();
  1527. i != components[0].end(); ++i)
  1528. {
  1529. if(*i == '/')
  1530. {
  1531. *i = '\\';
  1532. }
  1533. }
  1534. }
  1535. #endif
  1536. // Begin the quoted result with the root component.
  1537. std::string result = "\"";
  1538. result += components[0];
  1539. // Now add the rest of the components separated by the proper slash
  1540. // direction for this platform.
  1541. bool first = true;
  1542. for(unsigned int i=1; i < components.size(); ++i)
  1543. {
  1544. // Only the last component can be empty to avoid double slashes.
  1545. if(components[i].length() > 0 || (i == (components.size()-1)))
  1546. {
  1547. if(!first)
  1548. {
  1549. result += slash;
  1550. }
  1551. result += components[i];
  1552. first = false;
  1553. }
  1554. }
  1555. // Close the quoted result.
  1556. result += "\"";
  1557. return result;
  1558. }
  1559. //----------------------------------------------------------------------------
  1560. std::string
  1561. cmLocalUnixMakefileGenerator3::GetTargetDirectory(cmTarget& target)
  1562. {
  1563. std::string dir = "CMakeFiles/";
  1564. dir += target.GetName();
  1565. dir += ".dir";
  1566. return dir;
  1567. }
  1568. void cmLocalUnixMakefileGenerator3
  1569. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  1570. const char *retDir)
  1571. {
  1572. // do we need to cd?
  1573. if (!strcmp(tgtDir,retDir))
  1574. {
  1575. return;
  1576. }
  1577. if(!m_UnixCD)
  1578. {
  1579. // On Windows we must perform each step separately and then change
  1580. // back because the shell keeps the working directory between
  1581. // commands.
  1582. std::string cmd = "cd ";
  1583. cmd += this->ConvertToOutputForExisting(tgtDir);
  1584. commands.insert(commands.begin(),cmd);
  1585. // Change back to the starting directory. Any trailing slash must be
  1586. // removed to avoid problems with Borland Make.
  1587. std::string back = retDir;
  1588. if(back.size() && back[back.size()-1] == '/')
  1589. {
  1590. back = back.substr(0, back.size()-1);
  1591. }
  1592. cmd = "cd ";
  1593. cmd += this->ConvertToOutputForExisting(back.c_str());
  1594. commands.push_back(cmd);
  1595. }
  1596. else
  1597. {
  1598. // On UNIX we must construct a single shell command to change
  1599. // directory and build because make resets the directory between
  1600. // each command.
  1601. std::vector<std::string>::iterator i = commands.begin();
  1602. for (; i != commands.end(); ++i)
  1603. {
  1604. std::string cmd = "cd ";
  1605. cmd += this->ConvertToOutputForExisting(tgtDir);
  1606. cmd += " && ";
  1607. cmd += *i;
  1608. *i = cmd;
  1609. }
  1610. }
  1611. }
  1612. //----------------------------------------------------------------------------
  1613. const char*
  1614. cmLocalUnixMakefileGenerator3
  1615. ::GetSourceFileLanguage(const cmSourceFile& source)
  1616. {
  1617. // Identify the language of the source file.
  1618. return (m_GlobalGenerator
  1619. ->GetLanguageFromExtension(source.GetSourceExtension().c_str()));
  1620. }