cmLocalUnixMakefileGenerator3.cxx 73 KB

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