1
0

cmLocalUnixMakefileGenerator3.cxx 73 KB

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