cmLocalUnixMakefileGenerator3.cxx 73 KB

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