cmLocalUnixMakefileGenerator3.cxx 57 KB

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