cmLocalUnixMakefileGenerator3.cxx 76 KB

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