cmLocalUnixMakefileGenerator3.cxx 70 KB

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