cmLocalUnixMakefileGenerator3.cxx 72 KB

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