cmLocalUnixMakefileGenerator3.cxx 70 KB

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