cmLocalUnixMakefileGenerator3.cxx 77 KB

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