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