cmLocalUnixMakefileGenerator3.cxx 69 KB

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