cmLocalUnixMakefileGenerator3.cxx 73 KB

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