cmLocalUnixMakefileGenerator3.cxx 77 KB

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