cmLocalUnixMakefileGenerator3.cxx 71 KB

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