cmLocalUnixMakefileGenerator3.cxx 69 KB

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