cmLocalUnixMakefileGenerator3.cxx 73 KB

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