cmLocalUnixMakefileGenerator3.cxx 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  669. std::vector<std::string> depends;
  670. depends.push_back(".hpux_make_needs_suffix_list");
  671. this->WriteMakeRule(makefileStream, 0,
  672. ".SUFFIXES", depends, no_commands, false);
  673. cmGlobalUnixMakefileGenerator3* gg =
  674. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  675. // Write special target to silence make output. This must be after
  676. // the default target in case VERBOSE is set (which changes the
  677. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  678. // "VERBOSE=1" to be added as a make variable which will change the
  679. // name of this special target. This gives a make-time choice to
  680. // the user.
  681. if((this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  682. || (gg->GetForceVerboseMakefiles()))
  683. {
  684. makefileStream
  685. << "# Produce verbose output by default.\n"
  686. << "VERBOSE = 1\n"
  687. << "\n";
  688. }
  689. if(this->SilentNoColon)
  690. {
  691. makefileStream << "$(VERBOSE).SILENT\n";
  692. }
  693. else
  694. {
  695. this->WriteMakeRule(makefileStream,
  696. "Suppress display of executed commands.",
  697. "$(VERBOSE).SILENT",
  698. no_depends,
  699. no_commands, false);
  700. }
  701. // Work-around for makes that drop rules that have no dependencies
  702. // or commands.
  703. std::string hack = gg->GetEmptyRuleHackDepends();
  704. if(!hack.empty())
  705. {
  706. no_depends.push_back(hack);
  707. }
  708. std::string hack_cmd = gg->GetEmptyRuleHackCommand();
  709. if(!hack_cmd.empty())
  710. {
  711. no_commands.push_back(hack_cmd);
  712. }
  713. // Special symbolic target that never exists to force dependers to
  714. // run their rules.
  715. this->WriteMakeRule
  716. (makefileStream,
  717. "A target that is always out of date.",
  718. "cmake_force", no_depends, no_commands, true);
  719. // Variables for reference by other rules.
  720. this->WriteMakeVariables(makefileStream);
  721. }
  722. //----------------------------------------------------------------------------
  723. void cmLocalUnixMakefileGenerator3
  724. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  725. {
  726. this->WriteDivider(makefileStream);
  727. makefileStream
  728. << "# Special targets to cleanup operation of make.\n"
  729. << "\n";
  730. // Write special "cmake_check_build_system" target to run cmake with
  731. // the --check-build-system flag.
  732. {
  733. // Build command to run CMake to check if anything needs regenerating.
  734. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  735. cmakefileName += "Makefile.cmake";
  736. std::string runRule =
  737. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  738. runRule += " --check-build-system ";
  739. runRule += this->Convert(cmakefileName.c_str(),NONE,SHELL);
  740. runRule += " 0";
  741. std::vector<std::string> no_depends;
  742. std::vector<std::string> commands;
  743. commands.push_back(runRule);
  744. if(this->Parent)
  745. {
  746. this->CreateCDCommand(commands,
  747. this->Makefile->GetHomeOutputDirectory(),
  748. this->Makefile->GetStartOutputDirectory());
  749. }
  750. this->WriteMakeRule(makefileStream,
  751. "Special rule to run CMake to check the build system "
  752. "integrity.\n"
  753. "No rule that depends on this can have "
  754. "commands that come from listfiles\n"
  755. "because they might be regenerated.",
  756. "cmake_check_build_system",
  757. no_depends,
  758. commands, true);
  759. }
  760. }
  761. //----------------------------------------------------------------------------
  762. void
  763. cmLocalUnixMakefileGenerator3
  764. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  765. const char* realTarget,
  766. const char* helpTarget)
  767. {
  768. // A rule is only needed if the names are different.
  769. if(strcmp(realTarget, helpTarget) != 0)
  770. {
  771. // The helper target depends on the real target.
  772. std::vector<std::string> depends;
  773. depends.push_back(realTarget);
  774. // There are no commands.
  775. std::vector<std::string> no_commands;
  776. // Write the rule.
  777. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  778. helpTarget, depends, no_commands, true);
  779. }
  780. }
  781. //----------------------------------------------------------------------------
  782. std::string
  783. cmLocalUnixMakefileGenerator3
  784. ::GetRelativeTargetDirectory(cmTarget const& target)
  785. {
  786. std::string dir = this->HomeRelativeOutputPath;
  787. dir += this->GetTargetDirectory(target);
  788. return this->Convert(dir.c_str(),NONE,UNCHANGED);
  789. }
  790. //----------------------------------------------------------------------------
  791. void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
  792. const char* newFlags)
  793. {
  794. if(this->WatcomWMake && newFlags && *newFlags)
  795. {
  796. std::string newf = newFlags;
  797. if(newf.find("\\\"") != newf.npos)
  798. {
  799. cmSystemTools::ReplaceString(newf, "\\\"", "\"");
  800. this->cmLocalGenerator::AppendFlags(flags, newf.c_str());
  801. return;
  802. }
  803. }
  804. this->cmLocalGenerator::AppendFlags(flags, newFlags);
  805. }
  806. //----------------------------------------------------------------------------
  807. void
  808. cmLocalUnixMakefileGenerator3
  809. ::AppendRuleDepend(std::vector<std::string>& depends,
  810. const char* ruleFileName)
  811. {
  812. // Add a dependency on the rule file itself unless an option to skip
  813. // it is specifically enabled by the user or project.
  814. const char* nodep =
  815. this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  816. if(!nodep || cmSystemTools::IsOff(nodep))
  817. {
  818. depends.push_back(ruleFileName);
  819. }
  820. }
  821. //----------------------------------------------------------------------------
  822. void
  823. cmLocalUnixMakefileGenerator3
  824. ::AppendCustomDepends(std::vector<std::string>& depends,
  825. const std::vector<cmCustomCommand>& ccs)
  826. {
  827. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  828. i != ccs.end(); ++i)
  829. {
  830. this->AppendCustomDepend(depends, *i);
  831. }
  832. }
  833. //----------------------------------------------------------------------------
  834. void
  835. cmLocalUnixMakefileGenerator3
  836. ::AppendCustomDepend(std::vector<std::string>& depends,
  837. const cmCustomCommand& cc)
  838. {
  839. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  840. d != cc.GetDepends().end(); ++d)
  841. {
  842. // Lookup the real name of the dependency in case it is a CMake target.
  843. std::string dep = this->GetRealDependency
  844. (d->c_str(), this->ConfigurationName.c_str());
  845. depends.push_back(dep);
  846. }
  847. }
  848. //----------------------------------------------------------------------------
  849. void
  850. cmLocalUnixMakefileGenerator3
  851. ::AppendCustomCommands(std::vector<std::string>& commands,
  852. const std::vector<cmCustomCommand>& ccs)
  853. {
  854. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  855. i != ccs.end(); ++i)
  856. {
  857. this->AppendCustomCommand(commands, *i, true);
  858. }
  859. }
  860. //----------------------------------------------------------------------------
  861. void
  862. cmLocalUnixMakefileGenerator3
  863. ::AppendCustomCommand(std::vector<std::string>& commands,
  864. const cmCustomCommand& cc, bool echo_comment)
  865. {
  866. // Optionally create a command to display the custom command's
  867. // comment text. This is used for pre-build, pre-link, and
  868. // post-build command comments. Custom build step commands have
  869. // their comments generated elsewhere.
  870. if(echo_comment)
  871. {
  872. const char* comment = cc.GetComment();
  873. if(comment && *comment)
  874. {
  875. this->AppendEcho(commands, comment,
  876. cmLocalUnixMakefileGenerator3::EchoGenerate);
  877. }
  878. }
  879. // if the command specified a working directory use it.
  880. const char* dir = this->Makefile->GetStartOutputDirectory();
  881. const char* workingDir = cc.GetWorkingDirectory();
  882. if(workingDir)
  883. {
  884. dir = workingDir;
  885. }
  886. bool escapeOldStyle = cc.GetEscapeOldStyle();
  887. bool escapeAllowMakeVars = cc.GetEscapeAllowMakeVars();
  888. // Add each command line to the set of commands.
  889. std::vector<std::string> commands1;
  890. for(cmCustomCommandLines::const_iterator cl = cc.GetCommandLines().begin();
  891. cl != cc.GetCommandLines().end(); ++cl)
  892. {
  893. // Build the command line in a single string.
  894. const cmCustomCommandLine& commandLine = *cl;
  895. std::string cmd = GetRealLocation(commandLine[0].c_str(),
  896. this->ConfigurationName.c_str());
  897. if (cmd.size())
  898. {
  899. cmSystemTools::ReplaceString(cmd, "/./", "/");
  900. // Convert the command to a relative path only if the current
  901. // working directory will be the start-output directory.
  902. bool had_slash = cmd.find("/") != cmd.npos;
  903. if(!workingDir)
  904. {
  905. cmd = this->Convert(cmd.c_str(),START_OUTPUT);
  906. }
  907. bool has_slash = cmd.find("/") != cmd.npos;
  908. if(had_slash && !has_slash)
  909. {
  910. // This command was specified as a path to a file in the
  911. // current directory. Add a leading "./" so it can run
  912. // without the current directory being in the search path.
  913. cmd = "./" + cmd;
  914. }
  915. cmd = this->Convert(cmd.c_str(),NONE,SHELL);
  916. for(unsigned int j=1; j < commandLine.size(); ++j)
  917. {
  918. cmd += " ";
  919. if(escapeOldStyle)
  920. {
  921. cmd += this->EscapeForShellOldStyle(commandLine[j].c_str());
  922. }
  923. else
  924. {
  925. cmd += this->EscapeForShell(commandLine[j].c_str(),
  926. escapeAllowMakeVars);
  927. }
  928. }
  929. if(this->BorlandMakeCurlyHack)
  930. {
  931. // Borland Make has a very strange bug. If the first curly
  932. // brace anywhere in the command string is a left curly, it
  933. // must be written {{} instead of just {. Otherwise some
  934. // curly braces are removed. The hack can be skipped if the
  935. // first curly brace is the last character.
  936. std::string::size_type lcurly = cmd.find("{");
  937. if(lcurly != cmd.npos && lcurly < (cmd.size()-1))
  938. {
  939. std::string::size_type rcurly = cmd.find("}");
  940. if(rcurly == cmd.npos || rcurly > lcurly)
  941. {
  942. // The first curly is a left curly. Use the hack.
  943. std::string hack_cmd = cmd.substr(0, lcurly);
  944. hack_cmd += "{{}";
  945. hack_cmd += cmd.substr(lcurly+1);
  946. cmd = hack_cmd;
  947. }
  948. }
  949. }
  950. commands1.push_back(cmd);
  951. }
  952. }
  953. // Setup the proper working directory for the commands.
  954. this->CreateCDCommand(commands1, dir,
  955. this->Makefile->GetHomeOutputDirectory());
  956. // push back the custom commands
  957. commands.insert(commands.end(), commands1.begin(), commands1.end());
  958. }
  959. //----------------------------------------------------------------------------
  960. void
  961. cmLocalUnixMakefileGenerator3
  962. ::AppendCleanCommand(std::vector<std::string>& commands,
  963. const std::vector<std::string>& files,
  964. cmTarget& target, const char* filename)
  965. {
  966. if(!files.empty())
  967. {
  968. std::string cleanfile = this->Makefile->GetCurrentOutputDirectory();
  969. cleanfile += "/";
  970. cleanfile += this->GetTargetDirectory(target);
  971. cleanfile += "/cmake_clean";
  972. if(filename)
  973. {
  974. cleanfile += "_";
  975. cleanfile += filename;
  976. }
  977. cleanfile += ".cmake";
  978. std::string cleanfilePath = this->Convert(cleanfile.c_str(), FULL);
  979. std::ofstream fout(cleanfilePath.c_str());
  980. if(!fout)
  981. {
  982. cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
  983. }
  984. fout << "FILE(REMOVE_RECURSE\n";
  985. std::string remove = "$(CMAKE_COMMAND) -P ";
  986. remove += this->Convert(cleanfile.c_str(), START_OUTPUT, SHELL);
  987. for(std::vector<std::string>::const_iterator f = files.begin();
  988. f != files.end(); ++f)
  989. {
  990. fout << "\"" << this->Convert(f->c_str(),START_OUTPUT,UNCHANGED)
  991. << "\"\n";
  992. }
  993. fout << ")\n";
  994. commands.push_back(remove);
  995. // For the main clean rule add per-language cleaning.
  996. if(!filename)
  997. {
  998. // Get the set of source languages in the target.
  999. std::set<cmStdString> languages;
  1000. target.GetLanguages(languages);
  1001. fout << "\n"
  1002. << "# Per-language clean rules from dependency scanning.\n"
  1003. << "FOREACH(lang";
  1004. for(std::set<cmStdString>::const_iterator l = languages.begin();
  1005. l != languages.end(); ++l)
  1006. {
  1007. fout << " " << *l;
  1008. }
  1009. fout << ")\n"
  1010. << " INCLUDE(" << this->GetTargetDirectory(target)
  1011. << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
  1012. << "ENDFOREACH(lang)\n";
  1013. }
  1014. }
  1015. }
  1016. //----------------------------------------------------------------------------
  1017. void
  1018. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  1019. const char* text,
  1020. EchoColor color)
  1021. {
  1022. // Choose the color for the text.
  1023. std::string color_name;
  1024. #ifdef CMAKE_BUILD_WITH_CMAKE
  1025. if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
  1026. {
  1027. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  1028. // This color set is readable on both black and white backgrounds.
  1029. switch(color)
  1030. {
  1031. case EchoNormal:
  1032. break;
  1033. case EchoDepend:
  1034. color_name = "--magenta --bold ";
  1035. break;
  1036. case EchoBuild:
  1037. color_name = "--green ";
  1038. break;
  1039. case EchoLink:
  1040. color_name = "--red --bold ";
  1041. break;
  1042. case EchoGenerate:
  1043. color_name = "--blue --bold ";
  1044. break;
  1045. case EchoGlobal:
  1046. color_name = "--cyan ";
  1047. break;
  1048. }
  1049. }
  1050. #else
  1051. (void)color;
  1052. #endif
  1053. // Echo one line at a time.
  1054. std::string line;
  1055. line.reserve(200);
  1056. for(const char* c = text;; ++c)
  1057. {
  1058. if(*c == '\n' || *c == '\0')
  1059. {
  1060. // Avoid writing a blank last line on end-of-string.
  1061. if(*c != '\0' || !line.empty())
  1062. {
  1063. // Add a command to echo this line.
  1064. std::string cmd;
  1065. if(color_name.empty())
  1066. {
  1067. // Use the native echo command.
  1068. cmd = this->NativeEchoCommand;
  1069. cmd += this->EscapeForShell(line.c_str(), false,
  1070. this->NativeEchoWindows);
  1071. }
  1072. else
  1073. {
  1074. // Use cmake to echo the text in color.
  1075. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  1076. cmd += color_name;
  1077. cmd += this->EscapeForShell(line.c_str());
  1078. }
  1079. commands.push_back(cmd);
  1080. }
  1081. // Reset the line to emtpy.
  1082. line = "";
  1083. // Terminate on end-of-string.
  1084. if(*c == '\0')
  1085. {
  1086. return;
  1087. }
  1088. }
  1089. else if(*c != '\r')
  1090. {
  1091. // Append this character to the current line.
  1092. line += *c;
  1093. }
  1094. }
  1095. }
  1096. //----------------------------------------------------------------------------
  1097. std::string
  1098. cmLocalUnixMakefileGenerator3
  1099. ::CreateMakeVariable(const char* sin, const char* s2in)
  1100. {
  1101. std::string s = sin;
  1102. std::string s2 = s2in;
  1103. std::string unmodified = s;
  1104. unmodified += s2;
  1105. // if there is no restriction on the length of make variables
  1106. // and there are no "." charactors in the string, then return the
  1107. // unmodified combination.
  1108. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  1109. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  1110. {
  1111. return unmodified;
  1112. }
  1113. // see if the variable has been defined before and return
  1114. // the modified version of the variable
  1115. std::map<cmStdString, cmStdString>::iterator i =
  1116. this->MakeVariableMap.find(unmodified);
  1117. if(i != this->MakeVariableMap.end())
  1118. {
  1119. return i->second;
  1120. }
  1121. // start with the unmodified variable
  1122. std::string ret = unmodified;
  1123. // if this there is no value for this->MakefileVariableSize then
  1124. // the string must have bad characters in it
  1125. if(!this->MakefileVariableSize)
  1126. {
  1127. cmSystemTools::ReplaceString(ret, ".", "_");
  1128. cmSystemTools::ReplaceString(ret, "-", "__");
  1129. int ni = 0;
  1130. char buffer[5];
  1131. // make sure the _ version is not already used, if
  1132. // it is used then add number to the end of the variable
  1133. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1134. {
  1135. ++ni;
  1136. sprintf(buffer, "%04d", ni);
  1137. ret = unmodified + buffer;
  1138. }
  1139. this->ShortMakeVariableMap[ret] = "1";
  1140. this->MakeVariableMap[unmodified] = ret;
  1141. return ret;
  1142. }
  1143. // if the string is greater the 32 chars it is an invalid vairable name
  1144. // for borland make
  1145. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1146. {
  1147. int keep = this->MakefileVariableSize - 8;
  1148. int size = keep + 3;
  1149. std::string str1 = s;
  1150. std::string str2 = s2;
  1151. // we must shorten the combined string by 4 charactors
  1152. // keep no more than 24 charactors from the second string
  1153. if(static_cast<int>(str2.size()) > keep)
  1154. {
  1155. str2 = str2.substr(0, keep);
  1156. }
  1157. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1158. {
  1159. str1 = str1.substr(0, size - str2.size());
  1160. }
  1161. char buffer[5];
  1162. int ni = 0;
  1163. sprintf(buffer, "%04d", ni);
  1164. ret = str1 + str2 + buffer;
  1165. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1166. {
  1167. ++ni;
  1168. sprintf(buffer, "%04d", ni);
  1169. ret = str1 + str2 + buffer;
  1170. }
  1171. if(ni == 1000)
  1172. {
  1173. cmSystemTools::Error("Borland makefile variable length too long");
  1174. return unmodified;
  1175. }
  1176. // once an unused variable is found
  1177. this->ShortMakeVariableMap[ret] = "1";
  1178. }
  1179. // always make an entry into the unmodified to variable map
  1180. this->MakeVariableMap[unmodified] = ret;
  1181. return ret;
  1182. }
  1183. //----------------------------------------------------------------------------
  1184. bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
  1185. bool verbose,
  1186. bool color)
  1187. {
  1188. // read in the target info file
  1189. if(!this->Makefile->ReadListFile(0, tgtInfo) ||
  1190. cmSystemTools::GetErrorOccuredFlag())
  1191. {
  1192. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1193. }
  1194. // Check if any multiple output pairs have a missing file.
  1195. this->CheckMultipleOutputs(verbose);
  1196. std::string dir = cmSystemTools::GetFilenamePath(tgtInfo);
  1197. std::string internalDependFile = dir + "/depend.internal";
  1198. std::string dependFile = dir + "/depend.make";
  1199. // If the target DependInfo.cmake file has changed since the last
  1200. // time dependencies were scanned then force rescanning. This may
  1201. // happen when a new source file is added and CMake regenerates the
  1202. // project but no other sources were touched.
  1203. bool needRescan = false;
  1204. cmFileTimeComparison* ftc =
  1205. this->GlobalGenerator->GetCMakeInstance()->GetFileComparison();
  1206. {
  1207. int result;
  1208. if(!ftc->FileTimeCompare(internalDependFile.c_str(), tgtInfo, &result) ||
  1209. result < 0)
  1210. {
  1211. if(verbose)
  1212. {
  1213. cmOStringStream msg;
  1214. msg << "Dependee \"" << tgtInfo
  1215. << "\" is newer than depender \""
  1216. << internalDependFile << "\"." << std::endl;
  1217. cmSystemTools::Stdout(msg.str().c_str());
  1218. }
  1219. needRescan = true;
  1220. }
  1221. }
  1222. // Check the implicit dependencies to see if they are up to date.
  1223. // The build.make file may have explicit dependencies for the object
  1224. // files but these will not affect the scanning process so they need
  1225. // not be considered.
  1226. cmDependsC checker;
  1227. checker.SetVerbose(verbose);
  1228. checker.SetFileComparison(ftc);
  1229. if(needRescan ||
  1230. !checker.Check(dependFile.c_str(), internalDependFile.c_str()))
  1231. {
  1232. // The dependencies must be regenerated.
  1233. std::string targetName = cmSystemTools::GetFilenameName(dir);
  1234. targetName = targetName.substr(0, targetName.length()-4);
  1235. std::string message = "Scanning dependencies of target ";
  1236. message += targetName;
  1237. #ifdef CMAKE_BUILD_WITH_CMAKE
  1238. cmSystemTools::MakefileColorEcho(
  1239. cmsysTerminal_Color_ForegroundMagenta |
  1240. cmsysTerminal_Color_ForegroundBold,
  1241. message.c_str(), true, color);
  1242. #else
  1243. fprintf(stdout, "%s\n", message.c_str());
  1244. #endif
  1245. return this->ScanDependencies(dir.c_str());
  1246. }
  1247. else
  1248. {
  1249. // The dependencies are already up-to-date.
  1250. return true;
  1251. }
  1252. }
  1253. //----------------------------------------------------------------------------
  1254. bool
  1255. cmLocalUnixMakefileGenerator3
  1256. ::ScanDependencies(const char* targetDir)
  1257. {
  1258. // Read the directory information file.
  1259. cmMakefile* mf = this->Makefile;
  1260. bool haveDirectoryInfo = false;
  1261. std::string dirInfoFile = this->Makefile->GetStartOutputDirectory();
  1262. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1263. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1264. if(mf->ReadListFile(0, dirInfoFile.c_str()) &&
  1265. !cmSystemTools::GetErrorOccuredFlag())
  1266. {
  1267. haveDirectoryInfo = true;
  1268. }
  1269. // Lookup useful directory information.
  1270. if(haveDirectoryInfo)
  1271. {
  1272. // Test whether we need to force Unix paths.
  1273. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1274. {
  1275. if(!cmSystemTools::IsOff(force))
  1276. {
  1277. cmSystemTools::SetForceUnixPaths(true);
  1278. }
  1279. }
  1280. // Setup relative path top directories.
  1281. this->RelativePathsConfigured = true;
  1282. if(const char* relativePathTopSource =
  1283. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE"))
  1284. {
  1285. this->RelativePathTopSource = relativePathTopSource;
  1286. }
  1287. if(const char* relativePathTopBinary =
  1288. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY"))
  1289. {
  1290. this->RelativePathTopBinary = relativePathTopBinary;
  1291. }
  1292. }
  1293. else
  1294. {
  1295. cmSystemTools::Error("Directory Information file not found");
  1296. }
  1297. // create the file stream for the depends file
  1298. std::string dir = targetDir;
  1299. // Open the rule file. This should be copy-if-different because the
  1300. // rules may depend on this file itself.
  1301. std::string ruleFileNameFull = dir;
  1302. ruleFileNameFull += "/depend.make";
  1303. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  1304. ruleFileStream.SetCopyIfDifferent(true);
  1305. if(!ruleFileStream)
  1306. {
  1307. return false;
  1308. }
  1309. std::string internalRuleFileNameFull = dir;
  1310. internalRuleFileNameFull += "/depend.internal";
  1311. cmGeneratedFileStream
  1312. internalRuleFileStream(internalRuleFileNameFull.c_str());
  1313. internalRuleFileStream.SetCopyIfDifferent(true);
  1314. if(!internalRuleFileStream)
  1315. {
  1316. return false;
  1317. }
  1318. this->WriteDisclaimer(ruleFileStream);
  1319. this->WriteDisclaimer(internalRuleFileStream);
  1320. // for each language we need to scan, scan it
  1321. const char *langStr = mf->GetSafeDefinition("CMAKE_DEPENDS_LANGUAGES");
  1322. std::vector<std::string> langs;
  1323. cmSystemTools::ExpandListArgument(langStr, langs);
  1324. for (std::vector<std::string>::iterator li =
  1325. langs.begin(); li != langs.end(); ++li)
  1326. {
  1327. // construct the checker
  1328. std::string lang = li->c_str();
  1329. // Get the set of include directories.
  1330. std::vector<std::string> includes;
  1331. if(haveDirectoryInfo)
  1332. {
  1333. std::string includePathVar = "CMAKE_";
  1334. includePathVar += lang;
  1335. includePathVar += "_INCLUDE_PATH";
  1336. if(const char* includePath = mf->GetDefinition(includePathVar.c_str()))
  1337. {
  1338. cmSystemTools::ExpandListArgument(includePath, includes);
  1339. }
  1340. }
  1341. // Get the include file regular expression.
  1342. std::string includeRegexScan = "^.*$";
  1343. std::string includeRegexComplain = "^$";
  1344. if(haveDirectoryInfo)
  1345. {
  1346. std::string scanRegexVar = "CMAKE_";
  1347. scanRegexVar += lang;
  1348. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  1349. if(const char* scanRegex = mf->GetDefinition(scanRegexVar.c_str()))
  1350. {
  1351. includeRegexScan = scanRegex;
  1352. }
  1353. std::string complainRegexVar = "CMAKE_";
  1354. complainRegexVar += lang;
  1355. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  1356. if(const char* complainRegex =
  1357. mf->GetDefinition(complainRegexVar.c_str()))
  1358. {
  1359. includeRegexComplain = complainRegex;
  1360. }
  1361. }
  1362. // Create the scanner for this language
  1363. cmDepends *scanner = 0;
  1364. if(lang == "C" || lang == "CXX" || lang == "RC")
  1365. {
  1366. std::string includeCacheFileName = dir;
  1367. includeCacheFileName += "/";
  1368. includeCacheFileName += lang;
  1369. includeCacheFileName += ".includecache";
  1370. // TODO: Handle RC (resource files) dependencies correctly.
  1371. scanner = new cmDependsC(includes,
  1372. includeRegexScan.c_str(),
  1373. includeRegexComplain.c_str(),
  1374. includeCacheFileName);
  1375. }
  1376. #ifdef CMAKE_BUILD_WITH_CMAKE
  1377. else if(lang == "Fortran")
  1378. {
  1379. std::vector<std::string> defines;
  1380. if(const char* c_defines =
  1381. mf->GetDefinition("CMAKE_TARGET_DEFINITIONS"))
  1382. {
  1383. cmSystemTools::ExpandListArgument(c_defines, defines);
  1384. }
  1385. scanner = new cmDependsFortran(includes, defines);
  1386. }
  1387. else if(lang == "Java")
  1388. {
  1389. scanner = new cmDependsJava();
  1390. }
  1391. #endif
  1392. if (scanner)
  1393. {
  1394. scanner->SetLocalGenerator(this);
  1395. scanner->SetFileComparison
  1396. (this->GlobalGenerator->GetCMakeInstance()->GetFileComparison());
  1397. scanner->SetLanguage(lang.c_str());
  1398. scanner->SetTargetDirectory(dir.c_str());
  1399. scanner->Write(ruleFileStream, internalRuleFileStream);
  1400. // free the scanner for this language
  1401. delete scanner;
  1402. }
  1403. }
  1404. return true;
  1405. }
  1406. //----------------------------------------------------------------------------
  1407. void cmLocalUnixMakefileGenerator3::CheckMultipleOutputs(bool verbose)
  1408. {
  1409. cmMakefile* mf = this->Makefile;
  1410. // Get the string listing the multiple output pairs.
  1411. const char* pairs_string = mf->GetDefinition("CMAKE_MULTIPLE_OUTPUT_PAIRS");
  1412. if(!pairs_string)
  1413. {
  1414. return;
  1415. }
  1416. // Convert the string to a list and preserve empty entries.
  1417. std::vector<std::string> pairs;
  1418. cmSystemTools::ExpandListArgument(pairs_string, pairs, true);
  1419. for(std::vector<std::string>::const_iterator i = pairs.begin();
  1420. i != pairs.end(); ++i)
  1421. {
  1422. const std::string& depender = *i;
  1423. if(++i != pairs.end())
  1424. {
  1425. const std::string& dependee = *i;
  1426. // If the depender is missing then delete the dependee to make
  1427. // sure both will be regenerated.
  1428. if(cmSystemTools::FileExists(dependee.c_str()) &&
  1429. !cmSystemTools::FileExists(depender.c_str()))
  1430. {
  1431. if(verbose)
  1432. {
  1433. cmOStringStream msg;
  1434. msg << "Deleting primary custom command output \"" << dependee
  1435. << "\" because another output \""
  1436. << depender << "\" does not exist." << std::endl;
  1437. cmSystemTools::Stdout(msg.str().c_str());
  1438. }
  1439. cmSystemTools::RemoveFile(dependee.c_str());
  1440. }
  1441. }
  1442. }
  1443. }
  1444. //----------------------------------------------------------------------------
  1445. void cmLocalUnixMakefileGenerator3
  1446. ::WriteLocalAllRules(std::ostream& ruleFileStream)
  1447. {
  1448. this->WriteDisclaimer(ruleFileStream);
  1449. // Write the main entry point target. This must be the VERY first
  1450. // target so that make with no arguments will run it.
  1451. {
  1452. // Just depend on the all target to drive the build.
  1453. std::vector<std::string> depends;
  1454. std::vector<std::string> no_commands;
  1455. depends.push_back("all");
  1456. // Write the rule.
  1457. this->WriteMakeRule(ruleFileStream,
  1458. "Default target executed when no arguments are "
  1459. "given to make.",
  1460. "default_target",
  1461. depends,
  1462. no_commands, true);
  1463. }
  1464. this->WriteSpecialTargetsTop(ruleFileStream);
  1465. // Include the progress variables for the target.
  1466. // Write all global targets
  1467. this->WriteDivider(ruleFileStream);
  1468. ruleFileStream
  1469. << "# Targets provided globally by CMake.\n"
  1470. << "\n";
  1471. cmTargets* targets = &(this->Makefile->GetTargets());
  1472. cmTargets::iterator glIt;
  1473. for ( glIt = targets->begin(); glIt != targets->end(); ++ glIt )
  1474. {
  1475. if ( glIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  1476. {
  1477. std::string targetString = "Special rule for the target " + glIt->first;
  1478. std::vector<std::string> commands;
  1479. std::vector<std::string> depends;
  1480. const char* text = glIt->second.GetProperty("EchoString");
  1481. if ( !text )
  1482. {
  1483. text = "Running external command ...";
  1484. }
  1485. std::set<cmStdString>::const_iterator dit;
  1486. for ( dit = glIt->second.GetUtilities().begin();
  1487. dit != glIt->second.GetUtilities().end();
  1488. ++ dit )
  1489. {
  1490. depends.push_back(dit->c_str());
  1491. }
  1492. this->AppendEcho(commands, text,
  1493. cmLocalUnixMakefileGenerator3::EchoGlobal);
  1494. // Global targets store their rules in pre- and post-build commands.
  1495. this->AppendCustomDepends(depends,
  1496. glIt->second.GetPreBuildCommands());
  1497. this->AppendCustomDepends(depends,
  1498. glIt->second.GetPostBuildCommands());
  1499. this->AppendCustomCommands(commands,
  1500. glIt->second.GetPreBuildCommands());
  1501. this->AppendCustomCommands(commands,
  1502. glIt->second.GetPostBuildCommands());
  1503. std::string targetName = glIt->second.GetName();
  1504. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1505. targetName.c_str(), depends, commands, true);
  1506. // Provide a "/fast" version of the target.
  1507. depends.clear();
  1508. if((targetName == "install")
  1509. || (targetName == "install_local")
  1510. || (targetName == "install_strip"))
  1511. {
  1512. // Provide a fast install target that does not depend on all
  1513. // but has the same command.
  1514. depends.push_back("preinstall/fast");
  1515. }
  1516. else
  1517. {
  1518. // Just forward to the real target so at least it will work.
  1519. depends.push_back(targetName);
  1520. commands.clear();
  1521. }
  1522. targetName += "/fast";
  1523. this->WriteMakeRule(ruleFileStream, targetString.c_str(),
  1524. targetName.c_str(), depends, commands, true);
  1525. }
  1526. }
  1527. std::vector<std::string> depends;
  1528. std::vector<std::string> commands;
  1529. // Write the all rule.
  1530. std::string dir;
  1531. std::string recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1532. recursiveTarget += "/all";
  1533. depends.push_back("cmake_check_build_system");
  1534. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1535. progressDir += cmake::GetCMakeFilesDirectory();
  1536. {
  1537. cmOStringStream progCmd;
  1538. progCmd <<
  1539. "$(CMAKE_COMMAND) -E cmake_progress_start ";
  1540. progCmd << this->Convert(progressDir.c_str(),
  1541. cmLocalGenerator::FULL,
  1542. cmLocalGenerator::SHELL);
  1543. std::string progressFile = cmake::GetCMakeFilesDirectory();
  1544. progressFile += "/progress.make";
  1545. std::string progressFileNameFull =
  1546. this->ConvertToFullPath(progressFile.c_str());
  1547. progCmd << " " << this->Convert(progressFileNameFull.c_str(),
  1548. cmLocalGenerator::FULL,
  1549. cmLocalGenerator::SHELL);
  1550. commands.push_back(progCmd.str());
  1551. }
  1552. std::string mf2Dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1553. mf2Dir += "Makefile2";
  1554. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1555. recursiveTarget.c_str()));
  1556. this->CreateCDCommand(commands,
  1557. this->Makefile->GetHomeOutputDirectory(),
  1558. this->Makefile->GetStartOutputDirectory());
  1559. {
  1560. cmOStringStream progCmd;
  1561. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_start "; // # 0
  1562. progCmd << this->Convert(progressDir.c_str(),
  1563. cmLocalGenerator::FULL,
  1564. cmLocalGenerator::SHELL);
  1565. progCmd << " 0";
  1566. commands.push_back(progCmd.str());
  1567. }
  1568. this->WriteMakeRule(ruleFileStream, "The main all target", "all",
  1569. depends, commands, true);
  1570. // Write the clean rule.
  1571. recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1572. recursiveTarget += "/clean";
  1573. commands.clear();
  1574. depends.clear();
  1575. commands.push_back(this->GetRecursiveMakeCall(mf2Dir.c_str(),
  1576. recursiveTarget.c_str()));
  1577. this->CreateCDCommand(commands,
  1578. this->Makefile->GetHomeOutputDirectory(),
  1579. this->Makefile->GetStartOutputDirectory());
  1580. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean",
  1581. depends, commands, true);
  1582. commands.clear();
  1583. depends.clear();
  1584. depends.push_back("clean");
  1585. this->WriteMakeRule(ruleFileStream, "The main clean target", "clean/fast",
  1586. depends, commands, true);
  1587. // Write the preinstall rule.
  1588. recursiveTarget = this->Makefile->GetStartOutputDirectory();
  1589. recursiveTarget += "/preinstall";
  1590. commands.clear();
  1591. depends.clear();
  1592. const char* noall =
  1593. this->Makefile->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1594. if(!noall || cmSystemTools::IsOff(noall))
  1595. {
  1596. // Drive the build before installing.
  1597. depends.push_back("all");
  1598. }
  1599. else
  1600. {
  1601. // At least make sure the build system is up to date.
  1602. depends.push_back("cmake_check_build_system");
  1603. }
  1604. commands.push_back
  1605. (this->GetRecursiveMakeCall(mf2Dir.c_str(), recursiveTarget.c_str()));
  1606. this->CreateCDCommand(commands,
  1607. this->Makefile->GetHomeOutputDirectory(),
  1608. this->Makefile->GetStartOutputDirectory());
  1609. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1610. "preinstall", depends, commands, true);
  1611. depends.clear();
  1612. this->WriteMakeRule(ruleFileStream, "Prepare targets for installation.",
  1613. "preinstall/fast", depends, commands, true);
  1614. // write the depend rule, really a recompute depends rule
  1615. depends.clear();
  1616. commands.clear();
  1617. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  1618. cmakefileName += "Makefile.cmake";
  1619. std::string runRule =
  1620. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  1621. runRule += " --check-build-system ";
  1622. runRule += this->Convert(cmakefileName.c_str(),cmLocalGenerator::NONE,
  1623. cmLocalGenerator::SHELL);
  1624. runRule += " 1";
  1625. commands.push_back(runRule);
  1626. this->CreateCDCommand(commands,
  1627. this->Makefile->GetHomeOutputDirectory(),
  1628. this->Makefile->GetStartOutputDirectory());
  1629. this->WriteMakeRule(ruleFileStream, "clear depends",
  1630. "depend",
  1631. depends, commands, true);
  1632. }
  1633. //----------------------------------------------------------------------------
  1634. void cmLocalUnixMakefileGenerator3::ClearDependencies(cmMakefile* mf,
  1635. bool verbose)
  1636. {
  1637. // Get the list of target files to check
  1638. const char* infoDef = mf->GetDefinition("CMAKE_DEPEND_INFO_FILES");
  1639. if(!infoDef)
  1640. {
  1641. return;
  1642. }
  1643. std::vector<std::string> files;
  1644. cmSystemTools::ExpandListArgument(infoDef, files);
  1645. // Each depend information file corresponds to a target. Clear the
  1646. // dependencies for that target.
  1647. cmDepends clearer;
  1648. clearer.SetVerbose(verbose);
  1649. for(std::vector<std::string>::iterator l = files.begin();
  1650. l != files.end(); ++l)
  1651. {
  1652. std::string dir = cmSystemTools::GetFilenamePath(l->c_str());
  1653. // Clear the implicit dependency makefile.
  1654. std::string dependFile = dir + "/depend.make";
  1655. clearer.Clear(dependFile.c_str());
  1656. // Remove the internal dependency check file to force
  1657. // regeneration.
  1658. std::string internalDependFile = dir + "/depend.internal";
  1659. cmSystemTools::RemoveFile(internalDependFile.c_str());
  1660. }
  1661. }
  1662. void cmLocalUnixMakefileGenerator3
  1663. ::WriteDependLanguageInfo(std::ostream& cmakefileStream, cmTarget &target)
  1664. {
  1665. ImplicitDependLanguageMap const& implicitLangs =
  1666. this->GetImplicitDepends(target);
  1667. // list the languages
  1668. cmakefileStream
  1669. << "# The set of languages for which implicit dependencies are needed:\n";
  1670. cmakefileStream
  1671. << "SET(CMAKE_DEPENDS_LANGUAGES\n";
  1672. for(ImplicitDependLanguageMap::const_iterator
  1673. l = implicitLangs.begin(); l != implicitLangs.end(); ++l)
  1674. {
  1675. cmakefileStream << " \"" << l->first.c_str() << "\"\n";
  1676. }
  1677. cmakefileStream << " )\n";
  1678. // now list the files for each language
  1679. cmakefileStream
  1680. << "# The set of files for implicit dependencies of each language:\n";
  1681. for(ImplicitDependLanguageMap::const_iterator
  1682. l = implicitLangs.begin(); l != implicitLangs.end(); ++l)
  1683. {
  1684. cmakefileStream
  1685. << "SET(CMAKE_DEPENDS_CHECK_" << l->first.c_str() << "\n";
  1686. ImplicitDependFileMap const& implicitPairs = l->second;
  1687. // for each file pair
  1688. for(ImplicitDependFileMap::const_iterator pi = implicitPairs.begin();
  1689. pi != implicitPairs.end(); ++pi)
  1690. {
  1691. cmakefileStream << " \"" << pi->second << "\" ";
  1692. cmakefileStream << "\"" << pi->first << "\"\n";
  1693. }
  1694. cmakefileStream << " )\n";
  1695. // Tell the dependency scanner what compiler is used.
  1696. std::string cidVar = "CMAKE_";
  1697. cidVar += l->first;
  1698. cidVar += "_COMPILER_ID";
  1699. const char* cid = this->Makefile->GetDefinition(cidVar.c_str());
  1700. if(cid && *cid)
  1701. {
  1702. cmakefileStream
  1703. << "SET(CMAKE_" << l->first.c_str() << "_COMPILER_ID \""
  1704. << cid << "\")\n";
  1705. }
  1706. }
  1707. // Build a list of preprocessor definitions for the target.
  1708. std::vector<std::string> defines;
  1709. {
  1710. std::string defPropName = "COMPILE_DEFINITIONS_";
  1711. defPropName += cmSystemTools::UpperCase(this->ConfigurationName);
  1712. if(const char* ddefs = this->Makefile->GetProperty("COMPILE_DEFINITIONS"))
  1713. {
  1714. cmSystemTools::ExpandListArgument(ddefs, defines);
  1715. }
  1716. if(const char* cdefs = target.GetProperty("COMPILE_DEFINITIONS"))
  1717. {
  1718. cmSystemTools::ExpandListArgument(cdefs, defines);
  1719. }
  1720. if(const char* dcdefs = this->Makefile->GetProperty(defPropName.c_str()))
  1721. {
  1722. cmSystemTools::ExpandListArgument(dcdefs, defines);
  1723. }
  1724. if(const char* ccdefs = target.GetProperty(defPropName.c_str()))
  1725. {
  1726. cmSystemTools::ExpandListArgument(ccdefs, defines);
  1727. }
  1728. }
  1729. if(!defines.empty())
  1730. {
  1731. cmakefileStream
  1732. << "\n"
  1733. << "# Preprocessor definitions for this target.\n"
  1734. << "SET(CMAKE_TARGET_DEFINITIONS\n";
  1735. for(std::vector<std::string>::const_iterator di = defines.begin();
  1736. di != defines.end(); ++di)
  1737. {
  1738. cmakefileStream
  1739. << " " << this->EscapeForCMake(di->c_str()) << "\n";
  1740. }
  1741. cmakefileStream
  1742. << " )\n";
  1743. }
  1744. }
  1745. //----------------------------------------------------------------------------
  1746. std::string
  1747. cmLocalUnixMakefileGenerator3
  1748. ::GetObjectFileName(cmTarget& target,
  1749. const cmSourceFile& source,
  1750. std::string* nameWithoutTargetDir,
  1751. bool* hasSourceExtension)
  1752. {
  1753. // Make sure we never hit this old case.
  1754. if(source.GetProperty("MACOSX_PACKAGE_LOCATION"))
  1755. {
  1756. std::string msg = "MACOSX_PACKAGE_LOCATION set on source file: ";
  1757. msg += source.GetFullPath();
  1758. this->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR,
  1759. msg.c_str());
  1760. }
  1761. // Start with the target directory.
  1762. std::string obj = this->GetTargetDirectory(target);
  1763. obj += "/";
  1764. // Get the object file name without the target directory.
  1765. std::string::size_type dir_len = 0;
  1766. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  1767. dir_len += 1;
  1768. dir_len += obj.size();
  1769. std::string objectName =
  1770. this->GetObjectFileNameWithoutTarget(source, dir_len,
  1771. hasSourceExtension);
  1772. if(nameWithoutTargetDir)
  1773. {
  1774. *nameWithoutTargetDir = objectName;
  1775. }
  1776. // Append the object name to the target directory.
  1777. obj += objectName;
  1778. return obj;
  1779. }
  1780. //----------------------------------------------------------------------------
  1781. void cmLocalUnixMakefileGenerator3::WriteDisclaimer(std::ostream& os)
  1782. {
  1783. os
  1784. << "# CMAKE generated file: DO NOT EDIT!\n"
  1785. << "# Generated by \"" << this->GlobalGenerator->GetName() << "\""
  1786. << " Generator, CMake Version "
  1787. << cmVersion::GetMajorVersion() << "."
  1788. << cmVersion::GetMinorVersion() << "\n\n";
  1789. }
  1790. //----------------------------------------------------------------------------
  1791. std::string
  1792. cmLocalUnixMakefileGenerator3
  1793. ::GetRecursiveMakeCall(const char *makefile, const char* tgt)
  1794. {
  1795. // Call make on the given file.
  1796. std::string cmd;
  1797. cmd += "$(MAKE) -f ";
  1798. cmd += this->Convert(makefile,NONE,SHELL);
  1799. cmd += " ";
  1800. // Passg down verbosity level.
  1801. if(this->GetMakeSilentFlag().size())
  1802. {
  1803. cmd += this->GetMakeSilentFlag();
  1804. cmd += " ";
  1805. }
  1806. // Most unix makes will pass the command line flags to make down to
  1807. // sub-invoked makes via an environment variable. However, some
  1808. // makes do not support that, so you have to pass the flags
  1809. // explicitly.
  1810. if(this->GetPassMakeflags())
  1811. {
  1812. cmd += "-$(MAKEFLAGS) ";
  1813. }
  1814. // Add the target.
  1815. if (tgt && tgt[0] != '\0')
  1816. {
  1817. // The make target is always relative to the top of the build tree.
  1818. std::string tgt2 = this->Convert(tgt, HOME_OUTPUT);
  1819. // The target may have been written with windows paths.
  1820. cmSystemTools::ConvertToOutputSlashes(tgt2);
  1821. // Escape one extra time if the make tool requires it.
  1822. if(this->MakeCommandEscapeTargetTwice)
  1823. {
  1824. tgt2 = this->EscapeForShell(tgt2.c_str(), true, false);
  1825. }
  1826. // The target name is now a string that should be passed verbatim
  1827. // on the command line.
  1828. cmd += this->EscapeForShell(tgt2.c_str(), true, false);
  1829. }
  1830. return cmd;
  1831. }
  1832. //----------------------------------------------------------------------------
  1833. void cmLocalUnixMakefileGenerator3::WriteDivider(std::ostream& os)
  1834. {
  1835. os
  1836. << "#======================================"
  1837. << "=======================================\n";
  1838. }
  1839. //----------------------------------------------------------------------------
  1840. void
  1841. cmLocalUnixMakefileGenerator3
  1842. ::WriteCMakeArgument(std::ostream& os, const char* s)
  1843. {
  1844. // Write the given string to the stream with escaping to get it back
  1845. // into CMake through the lexical scanner.
  1846. os << "\"";
  1847. for(const char* c = s; *c; ++c)
  1848. {
  1849. if(*c == '\\')
  1850. {
  1851. os << "\\\\";
  1852. }
  1853. else if(*c == '"')
  1854. {
  1855. os << "\\\"";
  1856. }
  1857. else
  1858. {
  1859. os << *c;
  1860. }
  1861. }
  1862. os << "\"";
  1863. }
  1864. //----------------------------------------------------------------------------
  1865. std::string
  1866. cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(const char* p)
  1867. {
  1868. // Split the path into its components.
  1869. std::vector<std::string> components;
  1870. cmSystemTools::SplitPath(p, components);
  1871. // Return an empty path if there are no components.
  1872. if(components.empty())
  1873. {
  1874. return "\"\"";
  1875. }
  1876. // Choose a slash direction and fix root component.
  1877. const char* slash = "/";
  1878. #if defined(_WIN32) && !defined(__CYGWIN__)
  1879. if(!cmSystemTools::GetForceUnixPaths())
  1880. {
  1881. slash = "\\";
  1882. for(std::string::iterator i = components[0].begin();
  1883. i != components[0].end(); ++i)
  1884. {
  1885. if(*i == '/')
  1886. {
  1887. *i = '\\';
  1888. }
  1889. }
  1890. }
  1891. #endif
  1892. // Begin the quoted result with the root component.
  1893. std::string result = "\"";
  1894. result += components[0];
  1895. // Now add the rest of the components separated by the proper slash
  1896. // direction for this platform.
  1897. bool first = true;
  1898. for(unsigned int i=1; i < components.size(); ++i)
  1899. {
  1900. // Only the last component can be empty to avoid double slashes.
  1901. if(components[i].length() > 0 || (i == (components.size()-1)))
  1902. {
  1903. if(!first)
  1904. {
  1905. result += slash;
  1906. }
  1907. result += components[i];
  1908. first = false;
  1909. }
  1910. }
  1911. // Close the quoted result.
  1912. result += "\"";
  1913. return result;
  1914. }
  1915. //----------------------------------------------------------------------------
  1916. std::string
  1917. cmLocalUnixMakefileGenerator3
  1918. ::GetTargetDirectory(cmTarget const& target) const
  1919. {
  1920. std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
  1921. dir += target.GetName();
  1922. dir += ".dir";
  1923. return dir;
  1924. }
  1925. //----------------------------------------------------------------------------
  1926. cmLocalUnixMakefileGenerator3::ImplicitDependLanguageMap const&
  1927. cmLocalUnixMakefileGenerator3::GetImplicitDepends(cmTarget const& tgt)
  1928. {
  1929. return this->ImplicitDepends[tgt.GetName()];
  1930. }
  1931. //----------------------------------------------------------------------------
  1932. void
  1933. cmLocalUnixMakefileGenerator3::AddImplicitDepends(cmTarget const& tgt,
  1934. const char* lang,
  1935. const char* obj,
  1936. const char* src)
  1937. {
  1938. this->ImplicitDepends[tgt.GetName()][lang][obj] = src;
  1939. }
  1940. //----------------------------------------------------------------------------
  1941. void cmLocalUnixMakefileGenerator3
  1942. ::CreateCDCommand(std::vector<std::string>& commands, const char *tgtDir,
  1943. const char *retDir)
  1944. {
  1945. // do we need to cd?
  1946. if (!strcmp(tgtDir,retDir))
  1947. {
  1948. return;
  1949. }
  1950. if(!this->UnixCD)
  1951. {
  1952. // On Windows we must perform each step separately and then change
  1953. // back because the shell keeps the working directory between
  1954. // commands.
  1955. std::string cmd = "cd ";
  1956. cmd += this->ConvertToOutputForExisting(tgtDir);
  1957. commands.insert(commands.begin(),cmd);
  1958. // Change back to the starting directory. Any trailing slash must be
  1959. // removed to avoid problems with Borland Make.
  1960. std::string back = retDir;
  1961. if(back.size() && back[back.size()-1] == '/')
  1962. {
  1963. back = back.substr(0, back.size()-1);
  1964. }
  1965. cmd = "cd ";
  1966. cmd += this->ConvertToOutputForExisting(back.c_str());
  1967. commands.push_back(cmd);
  1968. }
  1969. else
  1970. {
  1971. // On UNIX we must construct a single shell command to change
  1972. // directory and build because make resets the directory between
  1973. // each command.
  1974. std::vector<std::string>::iterator i = commands.begin();
  1975. for (; i != commands.end(); ++i)
  1976. {
  1977. std::string cmd = "cd ";
  1978. cmd += this->ConvertToOutputForExisting(tgtDir);
  1979. cmd += " && ";
  1980. cmd += *i;
  1981. *i = cmd;
  1982. }
  1983. }
  1984. }
  1985. void cmLocalUnixMakefileGenerator3
  1986. ::GetTargetObjectFileDirectories(cmTarget* target,
  1987. std::vector<std::string>& dirs)
  1988. {
  1989. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1990. dir += "/";
  1991. dir += this->GetTargetDirectory(*target);
  1992. dirs.push_back(dir);
  1993. }