cmLocalUnixMakefileGenerator3.cxx 72 KB

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