cmLocalUnixMakefileGenerator3.cxx 55 KB

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