cmLocalUnixMakefileGenerator3.cxx 70 KB

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