cmLocalUnixMakefileGenerator3.cxx 57 KB

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