cmLocalUnixMakefileGenerator3.cxx 76 KB

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