cmLocalUnixMakefileGenerator3.cxx 69 KB

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