cmLocalUnixMakefileGenerator3.cxx 62 KB

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