cmLocalUnixMakefileGenerator3.cxx 74 KB

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