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, 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() == cmTarget::INTERFACE_LIBRARY
  110. || t->second->Target->IsImported())
  111. {
  112. continue;
  113. }
  114. cmsys::auto_ptr<cmMakefileTargetGenerator> tg(
  115. cmMakefileTargetGenerator::New(t->second));
  116. if (tg.get())
  117. {
  118. tg->WriteRuleFiles();
  119. gg->RecordTargetProgress(tg.get());
  120. }
  121. }
  122. // write the local Makefile
  123. this->WriteLocalMakefile();
  124. // Write the cmake file with information for this directory.
  125. this->WriteDirectoryInformationFile();
  126. }
  127. void cmLocalUnixMakefileGenerator3::ComputeHomeRelativeOutputPath()
  128. {
  129. // Compute the path to use when referencing the current output
  130. // directory from the top output directory.
  131. this->HomeRelativeOutputPath =
  132. this->Convert(this->Makefile->GetCurrentBinaryDirectory(), HOME_OUTPUT);
  133. if(this->HomeRelativeOutputPath == ".")
  134. {
  135. this->HomeRelativeOutputPath = "";
  136. }
  137. if(!this->HomeRelativeOutputPath.empty())
  138. {
  139. this->HomeRelativeOutputPath += "/";
  140. }
  141. }
  142. //----------------------------------------------------------------------------
  143. void cmLocalUnixMakefileGenerator3::ComputeObjectFilenames(
  144. std::map<cmSourceFile const*, std::string>& mapping,
  145. cmGeneratorTarget const* gt)
  146. {
  147. for(std::map<cmSourceFile const*, std::string>::iterator
  148. si = mapping.begin(); si != mapping.end(); ++si)
  149. {
  150. cmSourceFile const* sf = si->first;
  151. si->second = this->GetObjectFileNameWithoutTarget(*sf,
  152. gt->ObjectDirectory);
  153. }
  154. }
  155. //----------------------------------------------------------------------------
  156. void cmLocalUnixMakefileGenerator3::
  157. GetLocalObjectFiles(std::map<std::string, LocalObjectInfo> &localObjectFiles)
  158. {
  159. std::set<std::string> emitted;
  160. cmGeneratorTargetsType targets = this->GetGeneratorTargets();
  161. for(cmGeneratorTargetsType::iterator ti = targets.begin();
  162. ti != targets.end(); ++ti)
  163. {
  164. cmGeneratorTarget* gt = ti->second;
  165. if (gt->GetType() == cmTarget::INTERFACE_LIBRARY)
  166. {
  167. continue;
  168. }
  169. std::vector<cmSourceFile const*> objectSources;
  170. gt->GetObjectSources(objectSources, this->Makefile
  171. ->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  172. // Compute full path to object file directory for this target.
  173. std::string dir;
  174. dir += gt->Makefile->GetCurrentBinaryDirectory();
  175. dir += "/";
  176. dir += this->GetTargetDirectory(*gt->Target);
  177. dir += "/";
  178. // Compute the name of each object file.
  179. for(std::vector<cmSourceFile const*>::iterator
  180. si = objectSources.begin();
  181. si != objectSources.end(); ++si)
  182. {
  183. cmSourceFile const* sf = *si;
  184. bool hasSourceExtension = true;
  185. std::string objectName = this->GetObjectFileNameWithoutTarget(*sf,
  186. dir,
  187. &hasSourceExtension);
  188. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  189. {
  190. objectName = cmSystemTools::GetFilenameName(objectName);
  191. }
  192. LocalObjectInfo& info = localObjectFiles[objectName];
  193. info.HasSourceExtension = hasSourceExtension;
  194. info.push_back(LocalObjectEntry(gt->Target, sf->GetLanguage()));
  195. }
  196. }
  197. }
  198. //----------------------------------------------------------------------------
  199. void cmLocalUnixMakefileGenerator3::GetIndividualFileTargets
  200. (std::vector<std::string>& targets)
  201. {
  202. std::map<std::string, LocalObjectInfo> localObjectFiles;
  203. this->GetLocalObjectFiles(localObjectFiles);
  204. for (std::map<std::string, LocalObjectInfo>::iterator lo =
  205. localObjectFiles.begin();
  206. lo != localObjectFiles.end(); ++lo)
  207. {
  208. targets.push_back(lo->first);
  209. std::string::size_type dot_pos = lo->first.rfind(".");
  210. std::string base = lo->first.substr(0, dot_pos);
  211. if(lo->second.HasPreprocessRule)
  212. {
  213. targets.push_back(base + ".i");
  214. }
  215. if(lo->second.HasAssembleRule)
  216. {
  217. targets.push_back(base + ".s");
  218. }
  219. }
  220. }
  221. //----------------------------------------------------------------------------
  222. void cmLocalUnixMakefileGenerator3::WriteLocalMakefile()
  223. {
  224. // generate the includes
  225. std::string ruleFileName = "Makefile";
  226. // Open the rule file. This should be copy-if-different because the
  227. // rules may depend on this file itself.
  228. std::string ruleFileNameFull = this->ConvertToFullPath(ruleFileName);
  229. cmGeneratedFileStream ruleFileStream(ruleFileNameFull.c_str());
  230. if(!ruleFileStream)
  231. {
  232. return;
  233. }
  234. // always write the top makefile
  235. if (!this->GetMakefile()->IsRootMakefile())
  236. {
  237. ruleFileStream.SetCopyIfDifferent(true);
  238. }
  239. // write the all rules
  240. this->WriteLocalAllRules(ruleFileStream);
  241. // only write local targets unless at the top Keep track of targets already
  242. // listed.
  243. std::set<std::string> emittedTargets;
  244. if (!this->GetMakefile()->IsRootMakefile())
  245. {
  246. // write our targets, and while doing it collect up the object
  247. // file rules
  248. this->WriteLocalMakefileTargets(ruleFileStream,emittedTargets);
  249. }
  250. else
  251. {
  252. cmGlobalUnixMakefileGenerator3 *gg =
  253. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  254. gg->WriteConvenienceRules(ruleFileStream,emittedTargets);
  255. }
  256. bool do_preprocess_rules =
  257. this->GetCreatePreprocessedSourceRules();
  258. bool do_assembly_rules =
  259. this->GetCreateAssemblySourceRules();
  260. std::map<std::string, LocalObjectInfo> localObjectFiles;
  261. this->GetLocalObjectFiles(localObjectFiles);
  262. // now write out the object rules
  263. // for each object file name
  264. for (std::map<std::string, LocalObjectInfo>::iterator lo =
  265. localObjectFiles.begin();
  266. lo != localObjectFiles.end(); ++lo)
  267. {
  268. // Add a convenience rule for building the object file.
  269. this->WriteObjectConvenienceRule(ruleFileStream,
  270. "target to build an object file",
  271. lo->first.c_str(), lo->second);
  272. // Check whether preprocessing and assembly rules make sense.
  273. // They make sense only for C and C++ sources.
  274. bool lang_has_preprocessor = false;
  275. bool lang_has_assembly = false;
  276. for(std::vector<LocalObjectEntry>::const_iterator ei =
  277. lo->second.begin(); ei != lo->second.end(); ++ei)
  278. {
  279. if(ei->Language == "C" ||
  280. ei->Language == "CXX" ||
  281. ei->Language == "Fortran")
  282. {
  283. // Right now, C, C++ and Fortran have both a preprocessor and the
  284. // ability to generate assembly code
  285. lang_has_preprocessor = true;
  286. lang_has_assembly = true;
  287. break;
  288. }
  289. }
  290. // Add convenience rules for preprocessed and assembly files.
  291. if(lang_has_preprocessor && do_preprocess_rules)
  292. {
  293. std::string::size_type dot_pos = lo->first.rfind(".");
  294. std::string base = lo->first.substr(0, dot_pos);
  295. this->WriteObjectConvenienceRule(
  296. ruleFileStream, "target to preprocess a source file",
  297. (base + ".i").c_str(), lo->second);
  298. lo->second.HasPreprocessRule = true;
  299. }
  300. if(lang_has_assembly && do_assembly_rules)
  301. {
  302. std::string::size_type dot_pos = lo->first.rfind(".");
  303. std::string base = lo->first.substr(0, dot_pos);
  304. this->WriteObjectConvenienceRule(
  305. ruleFileStream, "target to generate assembly for a file",
  306. (base + ".s").c_str(), lo->second);
  307. lo->second.HasAssembleRule = true;
  308. }
  309. }
  310. // add a help target as long as there isn;t a real target named help
  311. if(emittedTargets.insert("help").second)
  312. {
  313. cmGlobalUnixMakefileGenerator3 *gg =
  314. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  315. gg->WriteHelpRule(ruleFileStream,this);
  316. }
  317. this->WriteSpecialTargetsBottom(ruleFileStream);
  318. }
  319. //----------------------------------------------------------------------------
  320. void
  321. cmLocalUnixMakefileGenerator3
  322. ::WriteObjectConvenienceRule(std::ostream& ruleFileStream,
  323. const char* comment, const char* output,
  324. LocalObjectInfo const& info)
  325. {
  326. // If the rule includes the source file extension then create a
  327. // version that has the extension removed. The help should include
  328. // only the version without source extension.
  329. bool inHelp = true;
  330. if(info.HasSourceExtension)
  331. {
  332. // Remove the last extension. This should be kept.
  333. std::string outBase1 = output;
  334. std::string outExt1 = cmSplitExtension(outBase1, outBase1);
  335. // Now remove the source extension and put back the last
  336. // extension.
  337. std::string outNoExt;
  338. cmSplitExtension(outBase1, outNoExt);
  339. outNoExt += outExt1;
  340. // Add a rule to drive the rule below.
  341. std::vector<std::string> depends;
  342. depends.push_back(output);
  343. std::vector<std::string> no_commands;
  344. this->WriteMakeRule(ruleFileStream, 0,
  345. outNoExt, depends, no_commands, true, true);
  346. inHelp = false;
  347. }
  348. // Recursively make the rule for each target using the object file.
  349. std::vector<std::string> commands;
  350. for(std::vector<LocalObjectEntry>::const_iterator t = info.begin();
  351. t != info.end(); ++t)
  352. {
  353. std::string tgtMakefileName =
  354. this->GetRelativeTargetDirectory(*(t->Target));
  355. std::string targetName = tgtMakefileName;
  356. tgtMakefileName += "/build.make";
  357. targetName += "/";
  358. targetName += output;
  359. commands.push_back(
  360. this->GetRecursiveMakeCall(tgtMakefileName.c_str(), targetName)
  361. );
  362. }
  363. this->CreateCDCommand(commands,
  364. this->GetBinaryDirectory(),
  365. cmLocalGenerator::START_OUTPUT);
  366. // Write the rule to the makefile.
  367. std::vector<std::string> no_depends;
  368. this->WriteMakeRule(ruleFileStream, comment,
  369. output, no_depends, commands, true, inHelp);
  370. }
  371. //----------------------------------------------------------------------------
  372. void cmLocalUnixMakefileGenerator3
  373. ::WriteLocalMakefileTargets(std::ostream& ruleFileStream,
  374. std::set<std::string> &emitted)
  375. {
  376. std::vector<std::string> depends;
  377. std::vector<std::string> commands;
  378. // for each target we just provide a rule to cd up to the top and do a make
  379. // on the target
  380. cmGeneratorTargetsType targets = this->GetGeneratorTargets();
  381. std::string localName;
  382. for(cmGeneratorTargetsType::iterator t = targets.begin();
  383. t != targets.end(); ++t)
  384. {
  385. if((t->second->GetType() == cmTarget::EXECUTABLE) ||
  386. (t->second->GetType() == cmTarget::STATIC_LIBRARY) ||
  387. (t->second->GetType() == cmTarget::SHARED_LIBRARY) ||
  388. (t->second->GetType() == cmTarget::MODULE_LIBRARY) ||
  389. (t->second->GetType() == cmTarget::OBJECT_LIBRARY) ||
  390. (t->second->GetType() == cmTarget::UTILITY))
  391. {
  392. if (t->second->Target->IsImported())
  393. {
  394. continue;
  395. }
  396. emitted.insert(t->second->GetName());
  397. // for subdirs add a rule to build this specific target by name.
  398. localName = this->GetRelativeTargetDirectory(*t->second->Target);
  399. localName += "/rule";
  400. commands.clear();
  401. depends.clear();
  402. // Build the target for this pass.
  403. std::string makefile2 = cmake::GetCMakeFilesDirectoryPostSlash();
  404. makefile2 += "Makefile2";
  405. commands.push_back(this->GetRecursiveMakeCall
  406. (makefile2.c_str(),localName));
  407. this->CreateCDCommand(commands,
  408. this->GetBinaryDirectory(),
  409. cmLocalGenerator::START_OUTPUT);
  410. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  411. localName, depends, commands, true);
  412. // Add a target with the canonical name (no prefix, suffix or path).
  413. if(localName != t->second->GetName())
  414. {
  415. commands.clear();
  416. depends.push_back(localName);
  417. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  418. t->second->GetName(), depends, commands, true);
  419. }
  420. // Add a fast rule to build the target
  421. std::string makefileName =
  422. this->GetRelativeTargetDirectory(*t->second->Target);
  423. makefileName += "/build.make";
  424. // make sure the makefile name is suitable for a makefile
  425. std::string makeTargetName =
  426. this->GetRelativeTargetDirectory(*t->second->Target);
  427. makeTargetName += "/build";
  428. localName = t->second->GetName();
  429. localName += "/fast";
  430. depends.clear();
  431. commands.clear();
  432. commands.push_back(this->GetRecursiveMakeCall
  433. (makefileName.c_str(), makeTargetName));
  434. this->CreateCDCommand(commands,
  435. this->GetBinaryDirectory(),
  436. cmLocalGenerator::START_OUTPUT);
  437. this->WriteMakeRule(ruleFileStream, "fast build rule for target.",
  438. localName, depends, commands, true);
  439. // Add a local name for the rule to relink the target before
  440. // installation.
  441. if(t->second->NeedRelinkBeforeInstall(this->ConfigName))
  442. {
  443. makeTargetName = this->GetRelativeTargetDirectory(*t->second->Target);
  444. makeTargetName += "/preinstall";
  445. localName = t->second->GetName();
  446. localName += "/preinstall";
  447. depends.clear();
  448. commands.clear();
  449. commands.push_back(this->GetRecursiveMakeCall
  450. (makefile2.c_str(), makeTargetName));
  451. this->CreateCDCommand(commands,
  452. this->GetBinaryDirectory(),
  453. cmLocalGenerator::START_OUTPUT);
  454. this->WriteMakeRule(ruleFileStream,
  455. "Manual pre-install relink rule for target.",
  456. localName, depends, commands, true);
  457. }
  458. }
  459. }
  460. }
  461. //----------------------------------------------------------------------------
  462. void cmLocalUnixMakefileGenerator3::WriteDirectoryInformationFile()
  463. {
  464. std::string infoFileName = this->Makefile->GetCurrentBinaryDirectory();
  465. infoFileName += cmake::GetCMakeFilesDirectory();
  466. infoFileName += "/CMakeDirectoryInformation.cmake";
  467. // Open the output file.
  468. cmGeneratedFileStream infoFileStream(infoFileName.c_str());
  469. if(!infoFileStream)
  470. {
  471. return;
  472. }
  473. infoFileStream.SetCopyIfDifferent(true);
  474. // Write the do not edit header.
  475. this->WriteDisclaimer(infoFileStream);
  476. // Setup relative path conversion tops.
  477. infoFileStream
  478. << "# Relative path conversion top directories.\n"
  479. << "set(CMAKE_RELATIVE_PATH_TOP_SOURCE \""
  480. << this->StateSnapshot.GetDirectory().GetRelativePathTopSource()
  481. << "\")\n"
  482. << "set(CMAKE_RELATIVE_PATH_TOP_BINARY \""
  483. << this->StateSnapshot.GetDirectory().GetRelativePathTopBinary()
  484. << "\")\n"
  485. << "\n";
  486. // Tell the dependency scanner to use unix paths if necessary.
  487. if(cmSystemTools::GetForceUnixPaths())
  488. {
  489. infoFileStream
  490. << "# Force unix paths in dependencies.\n"
  491. << "set(CMAKE_FORCE_UNIX_PATHS 1)\n"
  492. << "\n";
  493. }
  494. // Store the include regular expressions for this directory.
  495. infoFileStream
  496. << "\n"
  497. << "# The C and CXX include file regular expressions for "
  498. << "this directory.\n";
  499. infoFileStream
  500. << "set(CMAKE_C_INCLUDE_REGEX_SCAN ";
  501. this->WriteCMakeArgument(infoFileStream,
  502. this->Makefile->GetIncludeRegularExpression());
  503. infoFileStream
  504. << ")\n";
  505. infoFileStream
  506. << "set(CMAKE_C_INCLUDE_REGEX_COMPLAIN ";
  507. this->WriteCMakeArgument(infoFileStream,
  508. this->Makefile->GetComplainRegularExpression());
  509. infoFileStream
  510. << ")\n";
  511. infoFileStream
  512. << "set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN})\n";
  513. infoFileStream
  514. << "set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN "
  515. "${CMAKE_C_INCLUDE_REGEX_COMPLAIN})\n";
  516. }
  517. //----------------------------------------------------------------------------
  518. std::string
  519. cmLocalUnixMakefileGenerator3
  520. ::ConvertToFullPath(const std::string& localPath)
  521. {
  522. std::string dir = this->Makefile->GetCurrentBinaryDirectory();
  523. dir += "/";
  524. dir += localPath;
  525. return dir;
  526. }
  527. const std::string &cmLocalUnixMakefileGenerator3::GetHomeRelativeOutputPath()
  528. {
  529. return this->HomeRelativeOutputPath;
  530. }
  531. //----------------------------------------------------------------------------
  532. void
  533. cmLocalUnixMakefileGenerator3
  534. ::WriteMakeRule(std::ostream& os,
  535. const char* comment,
  536. const std::string& target,
  537. const std::vector<std::string>& depends,
  538. const std::vector<std::string>& commands,
  539. bool symbolic,
  540. bool in_help)
  541. {
  542. // Make sure there is a target.
  543. if(target.empty())
  544. {
  545. cmSystemTools::Error("No target for WriteMakeRule! called with comment: ",
  546. comment);
  547. return;
  548. }
  549. std::string replace;
  550. // Write the comment describing the rule in the makefile.
  551. if(comment)
  552. {
  553. replace = comment;
  554. std::string::size_type lpos = 0;
  555. std::string::size_type rpos;
  556. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  557. {
  558. os << "# " << replace.substr(lpos, rpos-lpos) << "\n";
  559. lpos = rpos+1;
  560. }
  561. os << "# " << replace.substr(lpos) << "\n";
  562. }
  563. // Construct the left hand side of the rule.
  564. std::string tgt = this->Convert(target, HOME_OUTPUT, MAKERULE);
  565. const char* space = "";
  566. if(tgt.size() == 1)
  567. {
  568. // Add a space before the ":" to avoid drive letter confusion on
  569. // Windows.
  570. space = " ";
  571. }
  572. // Mark the rule as symbolic if requested.
  573. if(symbolic)
  574. {
  575. if(const char* sym =
  576. this->Makefile->GetDefinition("CMAKE_MAKE_SYMBOLIC_RULE"))
  577. {
  578. os << cmMakeSafe(tgt) << space << ": " << sym << "\n";
  579. }
  580. }
  581. // Write the rule.
  582. if(depends.empty())
  583. {
  584. // No dependencies. The commands will always run.
  585. os << cmMakeSafe(tgt) << space << ":\n";
  586. }
  587. else
  588. {
  589. // Split dependencies into multiple rule lines. This allows for
  590. // very long dependency lists even on older make implementations.
  591. for(std::vector<std::string>::const_iterator dep = depends.begin();
  592. dep != depends.end(); ++dep)
  593. {
  594. replace = *dep;
  595. replace = this->Convert(replace,HOME_OUTPUT,MAKERULE);
  596. os << cmMakeSafe(tgt) << space << ": " << cmMakeSafe(replace) << "\n";
  597. }
  598. }
  599. // Write the list of commands.
  600. os << cmWrap("\t", commands, "", "\n") << "\n";
  601. if(symbolic && !this->IsWatcomWMake())
  602. {
  603. os << ".PHONY : " << cmMakeSafe(tgt) << "\n";
  604. }
  605. os << "\n";
  606. // Add the output to the local help if requested.
  607. if(in_help)
  608. {
  609. this->LocalHelp.push_back(target);
  610. }
  611. }
  612. //----------------------------------------------------------------------------
  613. std::string
  614. cmLocalUnixMakefileGenerator3
  615. ::ConvertShellCommand(std::string const& cmd, RelativeRoot root)
  616. {
  617. if(this->IsWatcomWMake() &&
  618. cmSystemTools::FileIsFullPath(cmd.c_str()) &&
  619. cmd.find_first_of("( )") != cmd.npos)
  620. {
  621. // On Watcom WMake use the windows short path for the command
  622. // name. This is needed to avoid funny quoting problems on
  623. // lines with shell redirection operators.
  624. std::string scmd;
  625. if(cmSystemTools::GetShortPath(cmd, scmd))
  626. {
  627. return this->Convert(scmd, NONE, SHELL);
  628. }
  629. }
  630. return this->Convert(cmd, root, SHELL);
  631. }
  632. //----------------------------------------------------------------------------
  633. void
  634. cmLocalUnixMakefileGenerator3
  635. ::WriteMakeVariables(std::ostream& makefileStream)
  636. {
  637. this->WriteDivider(makefileStream);
  638. makefileStream
  639. << "# Set environment variables for the build.\n"
  640. << "\n";
  641. cmGlobalUnixMakefileGenerator3* gg =
  642. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  643. if(gg->DefineWindowsNULL)
  644. {
  645. makefileStream
  646. << "!IF \"$(OS)\" == \"Windows_NT\"\n"
  647. << "NULL=\n"
  648. << "!ELSE\n"
  649. << "NULL=nul\n"
  650. << "!ENDIF\n";
  651. }
  652. if(this->IsWindowsShell())
  653. {
  654. makefileStream
  655. << "SHELL = cmd.exe\n"
  656. << "\n";
  657. }
  658. else
  659. {
  660. #if !defined(__VMS)
  661. makefileStream
  662. << "# The shell in which to execute make rules.\n"
  663. << "SHELL = /bin/sh\n"
  664. << "\n";
  665. #endif
  666. }
  667. makefileStream
  668. << "# The CMake executable.\n"
  669. << "CMAKE_COMMAND = "
  670. << this->ConvertShellCommand(cmSystemTools::GetCMakeCommand(), FULL)
  671. << "\n"
  672. << "\n";
  673. makefileStream
  674. << "# The command to remove a file.\n"
  675. << "RM = "
  676. << this->ConvertShellCommand(cmSystemTools::GetCMakeCommand(), FULL)
  677. << " -E remove -f\n"
  678. << "\n";
  679. makefileStream
  680. << "# Escaping for special characters.\n"
  681. << "EQUALS = =\n"
  682. << "\n";
  683. makefileStream
  684. << "# The top-level source directory on which CMake was run.\n"
  685. << "CMAKE_SOURCE_DIR = "
  686. << this->Convert(this->GetSourceDirectory(), FULL, SHELL)
  687. << "\n"
  688. << "\n";
  689. makefileStream
  690. << "# The top-level build directory on which CMake was run.\n"
  691. << "CMAKE_BINARY_DIR = "
  692. << this->Convert(this->GetBinaryDirectory(), FULL, SHELL)
  693. << "\n"
  694. << "\n";
  695. }
  696. //----------------------------------------------------------------------------
  697. void
  698. cmLocalUnixMakefileGenerator3
  699. ::WriteSpecialTargetsTop(std::ostream& makefileStream)
  700. {
  701. this->WriteDivider(makefileStream);
  702. makefileStream
  703. << "# Special targets provided by cmake.\n"
  704. << "\n";
  705. std::vector<std::string> no_commands;
  706. std::vector<std::string> no_depends;
  707. // Special target to cleanup operation of make tool.
  708. // This should be the first target except for the default_target in
  709. // the interface Makefile.
  710. this->WriteMakeRule(
  711. makefileStream, "Disable implicit rules so canonical targets will work.",
  712. ".SUFFIXES", no_depends, no_commands, false);
  713. if(!this->IsNMake()
  714. && !this->IsWatcomWMake() && !this->BorlandMakeCurlyHack)
  715. {
  716. // turn off RCS and SCCS automatic stuff from gmake
  717. makefileStream
  718. << "# Remove some rules from gmake that .SUFFIXES does not remove.\n"
  719. << "SUFFIXES =\n\n";
  720. }
  721. // Add a fake suffix to keep HP happy. Must be max 32 chars for SGI make.
  722. std::vector<std::string> depends;
  723. depends.push_back(".hpux_make_needs_suffix_list");
  724. this->WriteMakeRule(makefileStream, 0,
  725. ".SUFFIXES", depends, no_commands, false);
  726. if(this->IsWatcomWMake())
  727. {
  728. // Switch on WMake feature, if an error or interrupt occurs during
  729. // makefile processing, the current target being made may be deleted
  730. // without prompting (the same as command line -e option).
  731. makefileStream <<
  732. "\n"
  733. ".ERASE\n"
  734. "\n"
  735. ;
  736. }
  737. if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  738. {
  739. makefileStream
  740. << "# Produce verbose output by default.\n"
  741. << "VERBOSE = 1\n"
  742. << "\n";
  743. }
  744. if(this->IsWatcomWMake())
  745. {
  746. makefileStream <<
  747. "!ifndef VERBOSE\n"
  748. ".SILENT\n"
  749. "!endif\n"
  750. "\n"
  751. ;
  752. }
  753. else
  754. {
  755. // Write special target to silence make output. This must be after
  756. // the default target in case VERBOSE is set (which changes the
  757. // name). The setting of CMAKE_VERBOSE_MAKEFILE to ON will cause a
  758. // "VERBOSE=1" to be added as a make variable which will change the
  759. // name of this special target. This gives a make-time choice to
  760. // the user.
  761. this->WriteMakeRule(makefileStream,
  762. "Suppress display of executed commands.",
  763. "$(VERBOSE).SILENT",
  764. no_depends,
  765. no_commands, false);
  766. }
  767. // Work-around for makes that drop rules that have no dependencies
  768. // or commands.
  769. cmGlobalUnixMakefileGenerator3* gg =
  770. static_cast<cmGlobalUnixMakefileGenerator3*>(this->GlobalGenerator);
  771. std::string hack = gg->GetEmptyRuleHackDepends();
  772. if(!hack.empty())
  773. {
  774. no_depends.push_back(hack);
  775. }
  776. std::string hack_cmd = gg->GetEmptyRuleHackCommand();
  777. if(!hack_cmd.empty())
  778. {
  779. no_commands.push_back(hack_cmd);
  780. }
  781. // Special symbolic target that never exists to force dependers to
  782. // run their rules.
  783. this->WriteMakeRule
  784. (makefileStream,
  785. "A target that is always out of date.",
  786. "cmake_force", no_depends, no_commands, true);
  787. // Variables for reference by other rules.
  788. this->WriteMakeVariables(makefileStream);
  789. }
  790. //----------------------------------------------------------------------------
  791. void cmLocalUnixMakefileGenerator3
  792. ::WriteSpecialTargetsBottom(std::ostream& makefileStream)
  793. {
  794. this->WriteDivider(makefileStream);
  795. makefileStream
  796. << "# Special targets to cleanup operation of make.\n"
  797. << "\n";
  798. // Write special "cmake_check_build_system" target to run cmake with
  799. // the --check-build-system flag.
  800. {
  801. // Build command to run CMake to check if anything needs regenerating.
  802. std::string cmakefileName = cmake::GetCMakeFilesDirectoryPostSlash();
  803. cmakefileName += "Makefile.cmake";
  804. std::string runRule =
  805. "$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)";
  806. runRule += " --check-build-system ";
  807. runRule += this->Convert(cmakefileName,NONE,SHELL);
  808. runRule += " 0";
  809. std::vector<std::string> no_depends;
  810. std::vector<std::string> commands;
  811. commands.push_back(runRule);
  812. if(!this->GetMakefile()->IsRootMakefile())
  813. {
  814. this->CreateCDCommand(commands,
  815. this->GetBinaryDirectory(),
  816. cmLocalGenerator::START_OUTPUT);
  817. }
  818. this->WriteMakeRule(makefileStream,
  819. "Special rule to run CMake to check the build system "
  820. "integrity.\n"
  821. "No rule that depends on this can have "
  822. "commands that come from listfiles\n"
  823. "because they might be regenerated.",
  824. "cmake_check_build_system",
  825. no_depends,
  826. commands, true);
  827. }
  828. }
  829. //----------------------------------------------------------------------------
  830. void
  831. cmLocalUnixMakefileGenerator3
  832. ::WriteConvenienceRule(std::ostream& ruleFileStream,
  833. const std::string& realTarget,
  834. const std::string& helpTarget)
  835. {
  836. // A rule is only needed if the names are different.
  837. if(realTarget != helpTarget)
  838. {
  839. // The helper target depends on the real target.
  840. std::vector<std::string> depends;
  841. depends.push_back(realTarget);
  842. // There are no commands.
  843. std::vector<std::string> no_commands;
  844. // Write the rule.
  845. this->WriteMakeRule(ruleFileStream, "Convenience name for target.",
  846. helpTarget, depends, no_commands, true);
  847. }
  848. }
  849. //----------------------------------------------------------------------------
  850. std::string
  851. cmLocalUnixMakefileGenerator3
  852. ::GetRelativeTargetDirectory(cmTarget const& target)
  853. {
  854. std::string dir = this->HomeRelativeOutputPath;
  855. dir += this->GetTargetDirectory(target);
  856. return this->Convert(dir,NONE,UNCHANGED);
  857. }
  858. //----------------------------------------------------------------------------
  859. void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
  860. const std::string& newFlags)
  861. {
  862. if(this->IsWatcomWMake() && !newFlags.empty())
  863. {
  864. std::string newf = newFlags;
  865. if(newf.find("\\\"") != newf.npos)
  866. {
  867. cmSystemTools::ReplaceString(newf, "\\\"", "\"");
  868. this->cmLocalGenerator::AppendFlags(flags, newf);
  869. return;
  870. }
  871. }
  872. this->cmLocalGenerator::AppendFlags(flags, newFlags);
  873. }
  874. //----------------------------------------------------------------------------
  875. void cmLocalUnixMakefileGenerator3::AppendFlags(std::string& flags,
  876. const char* newFlags)
  877. {
  878. this->cmLocalGenerator::AppendFlags(flags, newFlags);
  879. }
  880. //----------------------------------------------------------------------------
  881. void
  882. cmLocalUnixMakefileGenerator3
  883. ::AppendRuleDepend(std::vector<std::string>& depends,
  884. const char* ruleFileName)
  885. {
  886. // Add a dependency on the rule file itself unless an option to skip
  887. // it is specifically enabled by the user or project.
  888. const char* nodep =
  889. this->Makefile->GetDefinition("CMAKE_SKIP_RULE_DEPENDENCY");
  890. if(!nodep || cmSystemTools::IsOff(nodep))
  891. {
  892. depends.push_back(ruleFileName);
  893. }
  894. }
  895. //----------------------------------------------------------------------------
  896. void
  897. cmLocalUnixMakefileGenerator3
  898. ::AppendRuleDepends(std::vector<std::string>& depends,
  899. std::vector<std::string> const& ruleFiles)
  900. {
  901. // Add a dependency on the rule file itself unless an option to skip
  902. // it is specifically enabled by the user or project.
  903. if(!this->Makefile->IsOn("CMAKE_SKIP_RULE_DEPENDENCY"))
  904. {
  905. depends.insert(depends.end(), ruleFiles.begin(), ruleFiles.end());
  906. }
  907. }
  908. //----------------------------------------------------------------------------
  909. void
  910. cmLocalUnixMakefileGenerator3
  911. ::AppendCustomDepends(std::vector<std::string>& depends,
  912. const std::vector<cmCustomCommand>& ccs)
  913. {
  914. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  915. i != ccs.end(); ++i)
  916. {
  917. cmCustomCommandGenerator ccg(*i, this->ConfigName, this);
  918. this->AppendCustomDepend(depends, ccg);
  919. }
  920. }
  921. //----------------------------------------------------------------------------
  922. void
  923. cmLocalUnixMakefileGenerator3
  924. ::AppendCustomDepend(std::vector<std::string>& depends,
  925. cmCustomCommandGenerator const& ccg)
  926. {
  927. for(std::vector<std::string>::const_iterator d = ccg.GetDepends().begin();
  928. d != ccg.GetDepends().end(); ++d)
  929. {
  930. // Lookup the real name of the dependency in case it is a CMake target.
  931. std::string dep;
  932. if(this->GetRealDependency(*d, this->ConfigName,
  933. dep))
  934. {
  935. depends.push_back(dep);
  936. }
  937. }
  938. }
  939. //----------------------------------------------------------------------------
  940. void
  941. cmLocalUnixMakefileGenerator3
  942. ::AppendCustomCommands(std::vector<std::string>& commands,
  943. const std::vector<cmCustomCommand>& ccs,
  944. cmTarget* target,
  945. cmLocalGenerator::RelativeRoot relative)
  946. {
  947. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  948. i != ccs.end(); ++i)
  949. {
  950. cmCustomCommandGenerator ccg(*i, this->ConfigName, this);
  951. this->AppendCustomCommand(commands, ccg, target, true, relative);
  952. }
  953. }
  954. //----------------------------------------------------------------------------
  955. void
  956. cmLocalUnixMakefileGenerator3
  957. ::AppendCustomCommand(std::vector<std::string>& commands,
  958. cmCustomCommandGenerator const& ccg,
  959. cmTarget* target,
  960. bool echo_comment,
  961. cmLocalGenerator::RelativeRoot relative,
  962. std::ostream* content)
  963. {
  964. // Optionally create a command to display the custom command's
  965. // comment text. This is used for pre-build, pre-link, and
  966. // post-build command comments. Custom build step commands have
  967. // their comments generated elsewhere.
  968. if(echo_comment)
  969. {
  970. const char* comment = ccg.GetComment();
  971. if(comment && *comment)
  972. {
  973. this->AppendEcho(commands, comment,
  974. cmLocalUnixMakefileGenerator3::EchoGenerate);
  975. }
  976. }
  977. // if the command specified a working directory use it.
  978. std::string dir = this->Makefile->GetCurrentBinaryDirectory();
  979. std::string workingDir = ccg.GetWorkingDirectory();
  980. if(!workingDir.empty())
  981. {
  982. dir = workingDir;
  983. }
  984. if(content)
  985. {
  986. *content << dir;
  987. }
  988. // Add each command line to the set of commands.
  989. std::vector<std::string> commands1;
  990. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  991. {
  992. // Build the command line in a single string.
  993. std::string cmd = ccg.GetCommand(c);
  994. if (!cmd.empty())
  995. {
  996. // Use "call " before any invocations of .bat or .cmd files
  997. // invoked as custom commands in the WindowsShell.
  998. //
  999. bool useCall = false;
  1000. if (this->IsWindowsShell())
  1001. {
  1002. std::string suffix;
  1003. if (cmd.size() > 4)
  1004. {
  1005. suffix = cmSystemTools::LowerCase(cmd.substr(cmd.size()-4));
  1006. if (suffix == ".bat" || suffix == ".cmd")
  1007. {
  1008. useCall = true;
  1009. }
  1010. }
  1011. }
  1012. cmSystemTools::ReplaceString(cmd, "/./", "/");
  1013. // Convert the command to a relative path only if the current
  1014. // working directory will be the start-output directory.
  1015. bool had_slash = cmd.find("/") != cmd.npos;
  1016. if(workingDir.empty())
  1017. {
  1018. cmd = this->Convert(cmd,START_OUTPUT);
  1019. }
  1020. bool has_slash = cmd.find("/") != cmd.npos;
  1021. if(had_slash && !has_slash)
  1022. {
  1023. // This command was specified as a path to a file in the
  1024. // current directory. Add a leading "./" so it can run
  1025. // without the current directory being in the search path.
  1026. cmd = "./" + cmd;
  1027. }
  1028. std::string launcher =
  1029. this->MakeLauncher(ccg, target,
  1030. workingDir.empty()? START_OUTPUT : NONE);
  1031. cmd = launcher + this->ConvertShellCommand(cmd, NONE);
  1032. ccg.AppendArguments(c, cmd);
  1033. if(content)
  1034. {
  1035. // Rule content does not include the launcher.
  1036. *content << (cmd.c_str()+launcher.size());
  1037. }
  1038. if(this->BorlandMakeCurlyHack)
  1039. {
  1040. // Borland Make has a very strange bug. If the first curly
  1041. // brace anywhere in the command string is a left curly, it
  1042. // must be written {{} instead of just {. Otherwise some
  1043. // curly braces are removed. The hack can be skipped if the
  1044. // first curly brace is the last character.
  1045. std::string::size_type lcurly = cmd.find("{");
  1046. if(lcurly != cmd.npos && lcurly < (cmd.size()-1))
  1047. {
  1048. std::string::size_type rcurly = cmd.find("}");
  1049. if(rcurly == cmd.npos || rcurly > lcurly)
  1050. {
  1051. // The first curly is a left curly. Use the hack.
  1052. std::string hack_cmd = cmd.substr(0, lcurly);
  1053. hack_cmd += "{{}";
  1054. hack_cmd += cmd.substr(lcurly+1);
  1055. cmd = hack_cmd;
  1056. }
  1057. }
  1058. }
  1059. if (launcher.empty())
  1060. {
  1061. if (useCall)
  1062. {
  1063. cmd = "call " + cmd;
  1064. }
  1065. else if (this->IsNMake() && cmd[0]=='"')
  1066. {
  1067. cmd = "echo >nul && " + cmd;
  1068. }
  1069. }
  1070. commands1.push_back(cmd);
  1071. }
  1072. }
  1073. // Setup the proper working directory for the commands.
  1074. this->CreateCDCommand(commands1, dir.c_str(), relative);
  1075. // push back the custom commands
  1076. commands.insert(commands.end(), commands1.begin(), commands1.end());
  1077. }
  1078. //----------------------------------------------------------------------------
  1079. std::string
  1080. cmLocalUnixMakefileGenerator3::MakeLauncher(
  1081. cmCustomCommandGenerator const& ccg,
  1082. cmTarget* target, RelativeRoot relative)
  1083. {
  1084. // Short-circuit if there is no launcher.
  1085. const char* prop = "RULE_LAUNCH_CUSTOM";
  1086. const char* val = this->GetRuleLauncher(target, prop);
  1087. if(!(val && *val))
  1088. {
  1089. return "";
  1090. }
  1091. // Expand rules in the empty string. It may insert the launcher and
  1092. // perform replacements.
  1093. RuleVariables vars;
  1094. vars.RuleLauncher = prop;
  1095. vars.CMTarget = target;
  1096. std::string output;
  1097. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1098. if(!outputs.empty())
  1099. {
  1100. output = this->Convert(outputs[0], relative, SHELL);
  1101. }
  1102. vars.Output = output.c_str();
  1103. std::string launcher;
  1104. this->ExpandRuleVariables(launcher, vars);
  1105. if(!launcher.empty())
  1106. {
  1107. launcher += " ";
  1108. }
  1109. return launcher;
  1110. }
  1111. //----------------------------------------------------------------------------
  1112. void
  1113. cmLocalUnixMakefileGenerator3
  1114. ::AppendCleanCommand(std::vector<std::string>& commands,
  1115. const std::vector<std::string>& files,
  1116. cmTarget& target, const char* filename)
  1117. {
  1118. std::string cleanfile = this->Makefile->GetCurrentBinaryDirectory();
  1119. cleanfile += "/";
  1120. cleanfile += this->GetTargetDirectory(target);
  1121. cleanfile += "/cmake_clean";
  1122. if(filename)
  1123. {
  1124. cleanfile += "_";
  1125. cleanfile += filename;
  1126. }
  1127. cleanfile += ".cmake";
  1128. std::string cleanfilePath = this->Convert(cleanfile, FULL);
  1129. cmsys::ofstream fout(cleanfilePath.c_str());
  1130. if(!fout)
  1131. {
  1132. cmSystemTools::Error("Could not create ", cleanfilePath.c_str());
  1133. }
  1134. if(!files.empty())
  1135. {
  1136. fout << "file(REMOVE_RECURSE\n";
  1137. for(std::vector<std::string>::const_iterator f = files.begin();
  1138. f != files.end(); ++f)
  1139. {
  1140. std::string fc = this->Convert(*f,START_OUTPUT,UNCHANGED);
  1141. fout << " " << cmOutputConverter::EscapeForCMake(fc) << "\n";
  1142. }
  1143. fout << ")\n";
  1144. }
  1145. std::string remove = "$(CMAKE_COMMAND) -P ";
  1146. remove += this->Convert(cleanfile, START_OUTPUT, SHELL);
  1147. commands.push_back(remove);
  1148. // For the main clean rule add per-language cleaning.
  1149. if(!filename)
  1150. {
  1151. // Get the set of source languages in the target.
  1152. std::set<std::string> languages;
  1153. cmGeneratorTarget *gtgt =
  1154. this->GlobalGenerator->GetGeneratorTarget(&target);
  1155. gtgt->GetLanguages(languages,
  1156. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1157. fout << "\n"
  1158. << "# Per-language clean rules from dependency scanning.\n"
  1159. << "foreach(lang " << cmJoin(languages, " ") << ")\n"
  1160. << " include(" << this->GetTargetDirectory(target)
  1161. << "/cmake_clean_${lang}.cmake OPTIONAL)\n"
  1162. << "endforeach()\n";
  1163. }
  1164. }
  1165. //----------------------------------------------------------------------------
  1166. void
  1167. cmLocalUnixMakefileGenerator3::AppendEcho(std::vector<std::string>& commands,
  1168. std::string const& text,
  1169. EchoColor color,
  1170. EchoProgress const* progress)
  1171. {
  1172. // Choose the color for the text.
  1173. std::string color_name;
  1174. if(this->GlobalGenerator->GetToolSupportsColor() && this->ColorMakefile)
  1175. {
  1176. // See cmake::ExecuteEchoColor in cmake.cxx for these options.
  1177. // This color set is readable on both black and white backgrounds.
  1178. switch(color)
  1179. {
  1180. case EchoNormal:
  1181. break;
  1182. case EchoDepend:
  1183. color_name = "--magenta --bold ";
  1184. break;
  1185. case EchoBuild:
  1186. color_name = "--green ";
  1187. break;
  1188. case EchoLink:
  1189. color_name = "--green --bold ";
  1190. break;
  1191. case EchoGenerate:
  1192. color_name = "--blue --bold ";
  1193. break;
  1194. case EchoGlobal:
  1195. color_name = "--cyan ";
  1196. break;
  1197. }
  1198. }
  1199. // Echo one line at a time.
  1200. std::string line;
  1201. line.reserve(200);
  1202. for(const char* c = text.c_str();; ++c)
  1203. {
  1204. if(*c == '\n' || *c == '\0')
  1205. {
  1206. // Avoid writing a blank last line on end-of-string.
  1207. if(*c != '\0' || !line.empty())
  1208. {
  1209. // Add a command to echo this line.
  1210. std::string cmd;
  1211. if(color_name.empty() && !progress)
  1212. {
  1213. // Use the native echo command.
  1214. cmd = "@echo ";
  1215. cmd += this->EscapeForShell(line, false, true);
  1216. }
  1217. else
  1218. {
  1219. // Use cmake to echo the text in color.
  1220. cmd = "@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) ";
  1221. cmd += color_name;
  1222. if (progress)
  1223. {
  1224. cmd += "--progress-dir=";
  1225. cmd += this->Convert(progress->Dir,
  1226. cmLocalGenerator::FULL,
  1227. cmLocalGenerator::SHELL);
  1228. cmd += " ";
  1229. cmd += "--progress-num=";
  1230. cmd += progress->Arg;
  1231. cmd += " ";
  1232. }
  1233. cmd += this->EscapeForShell(line);
  1234. }
  1235. commands.push_back(cmd);
  1236. }
  1237. // Reset the line to emtpy.
  1238. line = "";
  1239. // Progress appears only on first line.
  1240. progress = 0;
  1241. // Terminate on end-of-string.
  1242. if(*c == '\0')
  1243. {
  1244. return;
  1245. }
  1246. }
  1247. else if(*c != '\r')
  1248. {
  1249. // Append this character to the current line.
  1250. line += *c;
  1251. }
  1252. }
  1253. }
  1254. //----------------------------------------------------------------------------
  1255. std::string
  1256. cmLocalUnixMakefileGenerator3
  1257. ::CreateMakeVariable(const std::string& sin, const std::string& s2in)
  1258. {
  1259. std::string s = sin;
  1260. std::string s2 = s2in;
  1261. std::string unmodified = s;
  1262. unmodified += s2;
  1263. // if there is no restriction on the length of make variables
  1264. // and there are no "." characters in the string, then return the
  1265. // unmodified combination.
  1266. if((!this->MakefileVariableSize && unmodified.find('.') == s.npos)
  1267. && (!this->MakefileVariableSize && unmodified.find('+') == s.npos)
  1268. && (!this->MakefileVariableSize && unmodified.find('-') == s.npos))
  1269. {
  1270. return unmodified;
  1271. }
  1272. // see if the variable has been defined before and return
  1273. // the modified version of the variable
  1274. std::map<std::string, std::string>::iterator i =
  1275. this->MakeVariableMap.find(unmodified);
  1276. if(i != this->MakeVariableMap.end())
  1277. {
  1278. return i->second;
  1279. }
  1280. // start with the unmodified variable
  1281. std::string ret = unmodified;
  1282. // if this there is no value for this->MakefileVariableSize then
  1283. // the string must have bad characters in it
  1284. if(!this->MakefileVariableSize)
  1285. {
  1286. cmSystemTools::ReplaceString(ret, ".", "_");
  1287. cmSystemTools::ReplaceString(ret, "-", "__");
  1288. cmSystemTools::ReplaceString(ret, "+", "___");
  1289. int ni = 0;
  1290. char buffer[5];
  1291. // make sure the _ version is not already used, if
  1292. // it is used then add number to the end of the variable
  1293. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1294. {
  1295. ++ni;
  1296. sprintf(buffer, "%04d", ni);
  1297. ret = unmodified + buffer;
  1298. }
  1299. this->ShortMakeVariableMap[ret] = "1";
  1300. this->MakeVariableMap[unmodified] = ret;
  1301. return ret;
  1302. }
  1303. // if the string is greater than 32 chars it is an invalid variable name
  1304. // for borland make
  1305. if(static_cast<int>(ret.size()) > this->MakefileVariableSize)
  1306. {
  1307. int keep = this->MakefileVariableSize - 8;
  1308. int size = keep + 3;
  1309. std::string str1 = s;
  1310. std::string str2 = s2;
  1311. // we must shorten the combined string by 4 characters
  1312. // keep no more than 24 characters from the second string
  1313. if(static_cast<int>(str2.size()) > keep)
  1314. {
  1315. str2 = str2.substr(0, keep);
  1316. }
  1317. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  1318. {
  1319. str1 = str1.substr(0, size - str2.size());
  1320. }
  1321. char buffer[5];
  1322. int ni = 0;
  1323. sprintf(buffer, "%04d", ni);
  1324. ret = str1 + str2 + buffer;
  1325. while(this->ShortMakeVariableMap.count(ret) && ni < 1000)
  1326. {
  1327. ++ni;
  1328. sprintf(buffer, "%04d", ni);
  1329. ret = str1 + str2 + buffer;
  1330. }
  1331. if(ni == 1000)
  1332. {
  1333. cmSystemTools::Error("Borland makefile variable length too long");
  1334. return unmodified;
  1335. }
  1336. // once an unused variable is found
  1337. this->ShortMakeVariableMap[ret] = "1";
  1338. }
  1339. // always make an entry into the unmodified to variable map
  1340. this->MakeVariableMap[unmodified] = ret;
  1341. return ret;
  1342. }
  1343. //----------------------------------------------------------------------------
  1344. bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
  1345. bool verbose,
  1346. bool color)
  1347. {
  1348. // read in the target info file
  1349. if(!this->Makefile->ReadListFile(tgtInfo) ||
  1350. cmSystemTools::GetErrorOccuredFlag())
  1351. {
  1352. cmSystemTools::Error("Target DependInfo.cmake file not found");
  1353. }
  1354. // Check if any multiple output pairs have a missing file.
  1355. this->CheckMultipleOutputs(verbose);
  1356. std::string dir = cmSystemTools::GetFilenamePath(tgtInfo);
  1357. std::string internalDependFile = dir + "/depend.internal";
  1358. std::string dependFile = dir + "/depend.make";
  1359. // If the target DependInfo.cmake file has changed since the last
  1360. // time dependencies were scanned then force rescanning. This may
  1361. // happen when a new source file is added and CMake regenerates the
  1362. // project but no other sources were touched.
  1363. bool needRescanDependInfo = false;
  1364. cmFileTimeComparison* ftc =
  1365. this->GlobalGenerator->GetCMakeInstance()->GetFileComparison();
  1366. {
  1367. int result;
  1368. if(!ftc->FileTimeCompare(internalDependFile.c_str(), tgtInfo, &result) ||
  1369. result < 0)
  1370. {
  1371. if(verbose)
  1372. {
  1373. std::ostringstream msg;
  1374. msg << "Dependee \"" << tgtInfo
  1375. << "\" is newer than depender \""
  1376. << internalDependFile << "\"." << std::endl;
  1377. cmSystemTools::Stdout(msg.str().c_str());
  1378. }
  1379. needRescanDependInfo = true;
  1380. }
  1381. }
  1382. // If the directory information is newer than depend.internal, include dirs
  1383. // may have changed. In this case discard all old dependencies.
  1384. bool needRescanDirInfo = false;
  1385. std::string dirInfoFile = this->Makefile->GetCurrentBinaryDirectory();
  1386. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1387. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1388. {
  1389. int result;
  1390. if(!ftc->FileTimeCompare(internalDependFile.c_str(),
  1391. dirInfoFile.c_str(), &result) || result < 0)
  1392. {
  1393. if(verbose)
  1394. {
  1395. std::ostringstream msg;
  1396. msg << "Dependee \"" << dirInfoFile
  1397. << "\" is newer than depender \""
  1398. << internalDependFile << "\"." << std::endl;
  1399. cmSystemTools::Stdout(msg.str().c_str());
  1400. }
  1401. needRescanDirInfo = true;
  1402. }
  1403. }
  1404. // Check the implicit dependencies to see if they are up to date.
  1405. // The build.make file may have explicit dependencies for the object
  1406. // files but these will not affect the scanning process so they need
  1407. // not be considered.
  1408. std::map<std::string, cmDepends::DependencyVector> validDependencies;
  1409. bool needRescanDependencies = false;
  1410. if (needRescanDirInfo == false)
  1411. {
  1412. cmDependsC checker;
  1413. checker.SetVerbose(verbose);
  1414. checker.SetFileComparison(ftc);
  1415. // cmDependsC::Check() fills the vector validDependencies() with the
  1416. // dependencies for those files where they are still valid, i.e. neither
  1417. // the files themselves nor any files they depend on have changed.
  1418. // We don't do that if the CMakeDirectoryInformation.cmake file has
  1419. // changed, because then potentially all dependencies have changed.
  1420. // This information is given later on to cmDependsC, which then only
  1421. // rescans the files where it did not get valid dependencies via this
  1422. // dependency vector. This means that in the normal case, when only
  1423. // few or one file have been edited, then also only this one file is
  1424. // actually scanned again, instead of all files for this target.
  1425. needRescanDependencies = !checker.Check(dependFile.c_str(),
  1426. internalDependFile.c_str(),
  1427. validDependencies);
  1428. }
  1429. if(needRescanDependInfo || needRescanDirInfo || needRescanDependencies)
  1430. {
  1431. // The dependencies must be regenerated.
  1432. std::string targetName = cmSystemTools::GetFilenameName(dir);
  1433. targetName = targetName.substr(0, targetName.length()-4);
  1434. std::string message = "Scanning dependencies of target ";
  1435. message += targetName;
  1436. cmSystemTools::MakefileColorEcho(
  1437. cmsysTerminal_Color_ForegroundMagenta |
  1438. cmsysTerminal_Color_ForegroundBold,
  1439. message.c_str(), true, color);
  1440. return this->ScanDependencies(dir.c_str(), validDependencies);
  1441. }
  1442. // The dependencies are already up-to-date.
  1443. return true;
  1444. }
  1445. //----------------------------------------------------------------------------
  1446. bool
  1447. cmLocalUnixMakefileGenerator3
  1448. ::ScanDependencies(const char* targetDir,
  1449. std::map<std::string, cmDepends::DependencyVector>& validDeps)
  1450. {
  1451. // Read the directory information file.
  1452. cmMakefile* mf = this->Makefile;
  1453. bool haveDirectoryInfo = false;
  1454. std::string dirInfoFile = this->Makefile->GetCurrentBinaryDirectory();
  1455. dirInfoFile += cmake::GetCMakeFilesDirectory();
  1456. dirInfoFile += "/CMakeDirectoryInformation.cmake";
  1457. if(mf->ReadListFile(dirInfoFile.c_str()) &&
  1458. !cmSystemTools::GetErrorOccuredFlag())
  1459. {
  1460. haveDirectoryInfo = true;
  1461. }
  1462. // Lookup useful directory information.
  1463. if(haveDirectoryInfo)
  1464. {
  1465. // Test whether we need to force Unix paths.
  1466. if(const char* force = mf->GetDefinition("CMAKE_FORCE_UNIX_PATHS"))
  1467. {
  1468. if(!cmSystemTools::IsOff(force))
  1469. {
  1470. cmSystemTools::SetForceUnixPaths(true);
  1471. }
  1472. }
  1473. // Setup relative path top directories.
  1474. if(const char* relativePathTopSource =
  1475. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_SOURCE"))
  1476. {
  1477. this->StateSnapshot.GetDirectory()
  1478. .SetRelativePathTopSource(relativePathTopSource);
  1479. }
  1480. if(const char* relativePathTopBinary =
  1481. mf->GetDefinition("CMAKE_RELATIVE_PATH_TOP_BINARY"))
  1482. {
  1483. this->StateSnapshot.GetDirectory()
  1484. .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->GetBinaryDirectory();
  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->GetBinaryDirectory(),
  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->GetBinaryDirectory(),
  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->GetBinaryDirectory(),
  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->GetBinaryDirectory(),
  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(cmMakeRange(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. }