cmLocalUnixMakefileGenerator3.cxx 76 KB

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