cmLocalUnixMakefileGenerator3.cxx 75 KB

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