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