cmLocalUnixMakefileGenerator3.cxx 73 KB

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