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