cmLocalUnixMakefileGenerator3.cxx 75 KB

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