cmLocalUnixMakefileGenerator3.cxx 77 KB

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