cmLocalUnixMakefileGenerator3.cxx 72 KB

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