cmLocalUnixMakefileGenerator.cxx 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSystemTools.h"
  17. #include "cmSourceFile.h"
  18. #include "cmMakeDepend.h"
  19. #include "cmCacheManager.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include <cmsys/RegularExpression.hxx>
  22. #include <stdio.h>
  23. cmLocalUnixMakefileGenerator::cmLocalUnixMakefileGenerator()
  24. {
  25. m_WindowsShell = false;
  26. m_IncludeDirective = "include";
  27. m_MakefileVariableSize = 0;
  28. m_IgnoreLibPrefix = false;
  29. }
  30. cmLocalUnixMakefileGenerator::~cmLocalUnixMakefileGenerator()
  31. {
  32. }
  33. void cmLocalUnixMakefileGenerator::Generate(bool fromTheTop)
  34. {
  35. // suppoirt override in output directories
  36. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  37. {
  38. m_LibraryOutputPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  39. if(m_LibraryOutputPath.size())
  40. {
  41. if(m_LibraryOutputPath[m_LibraryOutputPath.size() -1] != '/')
  42. {
  43. m_LibraryOutputPath += "/";
  44. }
  45. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  46. {
  47. cmSystemTools::Error("Error failed create "
  48. "LIBRARY_OUTPUT_PATH directory:",
  49. m_LibraryOutputPath.c_str());
  50. }
  51. m_Makefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  52. }
  53. }
  54. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  55. {
  56. m_ExecutableOutputPath =
  57. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  58. if(m_ExecutableOutputPath.size())
  59. {
  60. if(m_ExecutableOutputPath[m_ExecutableOutputPath.size() -1] != '/')
  61. {
  62. m_ExecutableOutputPath += "/";
  63. }
  64. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  65. {
  66. cmSystemTools::Error("Error failed to create "
  67. "EXECUTABLE_OUTPUT_PATH directory:",
  68. m_ExecutableOutputPath.c_str());
  69. }
  70. m_Makefile->AddLinkDirectory(m_ExecutableOutputPath.c_str());
  71. }
  72. }
  73. if (!fromTheTop)
  74. {
  75. // Generate depends
  76. cmMakeDepend md;
  77. md.SetMakefile(m_Makefile);
  78. md.GenerateMakefileDependencies();
  79. this->ProcessDepends(md);
  80. }
  81. // output the makefile fragment
  82. std::string dest = m_Makefile->GetStartOutputDirectory();
  83. dest += "/Makefile";
  84. this->OutputMakefile(dest.c_str(), !fromTheTop);
  85. }
  86. void
  87. cmLocalUnixMakefileGenerator::AddDependenciesToSourceFile(cmDependInformation const *info,
  88. cmSourceFile *i,
  89. std::set<cmDependInformation const*> *visited)
  90. {
  91. // add info to the visited set
  92. visited->insert(info);
  93. // add this dependency and the recurse
  94. // now recurse with info's dependencies
  95. for(cmDependInformation::DependencySet::const_iterator d =
  96. info->m_DependencySet.begin();
  97. d != info->m_DependencySet.end(); ++d)
  98. {
  99. if (visited->find(*d) == visited->end())
  100. {
  101. if((*d)->m_FullPath != "")
  102. {
  103. i->GetDepends().push_back((*d)->m_FullPath);
  104. }
  105. this->AddDependenciesToSourceFile(*d,i,visited);
  106. }
  107. }
  108. }
  109. void cmLocalUnixMakefileGenerator::ProcessDepends(const cmMakeDepend &md)
  110. {
  111. // Now create cmDependInformation objects for files in the directory
  112. cmTargets &tgts = m_Makefile->GetTargets();
  113. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  114. {
  115. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  116. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  117. i != classes.end(); ++i)
  118. {
  119. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  120. {
  121. // get the depends
  122. const cmDependInformation *info =
  123. md.GetDependInformationForSourceFile(*(*i));
  124. // Delete any hints from the source file's dependencies.
  125. (*i)->GetDepends().erase((*i)->GetDepends().begin(), (*i)->GetDepends().end());
  126. // Now add the real dependencies for the file.
  127. if (info)
  128. {
  129. // create visited set
  130. std::set<cmDependInformation const*> visited;
  131. this->AddDependenciesToSourceFile(info,*i, &visited);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. // This is where CMakeTargets.make is generated
  138. void cmLocalUnixMakefileGenerator::OutputMakefile(const char* file,
  139. bool withDepends)
  140. {
  141. // Create sub directories fro aux source directories
  142. std::vector<std::string>& auxSourceDirs =
  143. m_Makefile->GetAuxSourceDirectories();
  144. if( auxSourceDirs.size() )
  145. {
  146. // For the case when this is running as a remote build
  147. // on unix, make the directory
  148. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  149. i != auxSourceDirs.end(); ++i)
  150. {
  151. if(i->c_str()[0] != '/')
  152. {
  153. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  154. if(dir.size() && dir[dir.size()-1] != '/')
  155. {
  156. dir += "/";
  157. }
  158. dir += *i;
  159. cmSystemTools::MakeDirectory(dir.c_str());
  160. }
  161. else
  162. {
  163. cmSystemTools::MakeDirectory(i->c_str());
  164. }
  165. }
  166. }
  167. // Create a stream that writes to a temporary file
  168. // then does a copy at the end. This is to allow users
  169. // to hit control-c during the make of the makefile
  170. cmGeneratedFileStream tempFile(file);
  171. tempFile.SetAlwaysCopy(true);
  172. std::ostream& fout = tempFile.GetStream();
  173. if(!fout)
  174. {
  175. cmSystemTools::Error("Error can not open for write: ", file);
  176. return;
  177. }
  178. fout << "# CMAKE generated Makefile, DO NOT EDIT!\n"
  179. << "# Generated by \"" << m_GlobalGenerator->GetName() << "\""
  180. << " Generator, CMake Version "
  181. << cmMakefile::GetMajorVersion() << "."
  182. << cmMakefile::GetMinorVersion() << "\n"
  183. << "# Generated from the following files:\n# "
  184. << m_Makefile->GetHomeOutputDirectory() << "/CMakeCache.txt\n";
  185. std::vector<std::string> lfiles = m_Makefile->GetListFiles();
  186. // sort the array
  187. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  188. // remove duplicates
  189. std::vector<std::string>::iterator new_end =
  190. std::unique(lfiles.begin(), lfiles.end());
  191. lfiles.erase(new_end, lfiles.end());
  192. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  193. i != lfiles.end(); ++i)
  194. {
  195. fout << "# " << i->c_str() << "\n";
  196. }
  197. fout << "\n\n";
  198. if(!m_Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))
  199. {
  200. fout << "# Suppresses display of executed commands\n";
  201. fout << ".SILENT:\n";
  202. }
  203. fout << "# disable some common implicit rules to speed things up\n";
  204. fout << ".SUFFIXES:\n";
  205. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  206. // create a make variable with all of the sources for this Makefile
  207. // for depend purposes.
  208. fout << "CMAKE_MAKEFILE_SOURCES = ";
  209. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  210. i != lfiles.end(); ++i)
  211. {
  212. fout << " " << cmSystemTools::ConvertToOutputPath(i->c_str());
  213. }
  214. // Add the cache to the list
  215. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  216. cacheFile += "/CMakeCache.txt";
  217. fout << " " << cmSystemTools::ConvertToOutputPath(cacheFile.c_str());
  218. fout << "\n\n\n";
  219. this->OutputMakeVariables(fout);
  220. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  221. checkCache += "/cmake.check_cache";
  222. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  223. // Set up the default target as the VERY first target, so that make with no arguments will run it
  224. this->
  225. OutputMakeRule(fout,
  226. "Default target executed when no arguments are given to make, first make sure cmake.depends exists, cmake.check_depends is up-to-date, check the sources, then build the all target",
  227. "default_target",
  228. checkCache.c_str(),
  229. "$(MAKE) $(MAKESILENT) cmake.depends",
  230. "$(MAKE) $(MAKESILENT) cmake.check_depends",
  231. "$(MAKE) $(MAKESILENT) -f cmake.check_depends",
  232. "$(MAKE) $(MAKESILENT) all");
  233. this->OutputTargetRules(fout);
  234. this->OutputDependLibs(fout);
  235. this->OutputTargets(fout);
  236. this->OutputSubDirectoryRules(fout);
  237. std::string dependName = m_Makefile->GetStartOutputDirectory();
  238. dependName += "/cmake.depends";
  239. if(withDepends)
  240. {
  241. std::ofstream dependout(dependName.c_str());
  242. if(!dependout)
  243. {
  244. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  245. return;
  246. }
  247. dependout << "# .o dependencies in this directory." << std::endl;
  248. std::string checkDepend = m_Makefile->GetStartOutputDirectory();
  249. checkDepend += "/cmake.check_depends";
  250. std::ofstream checkdependout(checkDepend.c_str());
  251. if(!checkdependout)
  252. {
  253. cmSystemTools::Error("Error can not open for write: ", checkDepend.c_str());
  254. return;
  255. }
  256. checkdependout << "# This file is used as a tag file, that all sources depend on. If a source changes, then the rule to rebuild this file will cause cmake.depends to be rebuilt." << std::endl;
  257. // if there were any depends output, then output the check depends
  258. // information inot checkdependout
  259. if(this->OutputObjectDepends(dependout))
  260. {
  261. this->OutputCheckDepends(checkdependout);
  262. }
  263. else
  264. {
  265. checkdependout << "all:\n\t@echo cmake.depends is up-to-date\n";
  266. }
  267. }
  268. this->OutputCustomRules(fout);
  269. this->OutputMakeRules(fout);
  270. this->OutputInstallRules(fout);
  271. // only add the depend include if the depend file exists
  272. if(cmSystemTools::FileExists(dependName.c_str()))
  273. {
  274. fout << m_IncludeDirective << " cmake.depends\n";
  275. }
  276. }
  277. #if defined(_WIN32) && ! defined(__CYGWIN__)
  278. std::string cmLocalUnixMakefileGenerator::GetOutputExtension(const char* s)
  279. {
  280. std::string sourceExtension = s;
  281. if(sourceExtension == "def")
  282. {
  283. return "";
  284. }
  285. if(sourceExtension == "ico" || sourceExtension == "rc2")
  286. {
  287. return "";
  288. }
  289. if(sourceExtension == "rc")
  290. {
  291. return ".res";
  292. }
  293. return ".obj";
  294. }
  295. #else
  296. std::string cmLocalUnixMakefileGenerator::GetOutputExtension(const char*)
  297. {
  298. return ".o";
  299. }
  300. #endif
  301. std::string cmLocalUnixMakefileGenerator::GetFullTargetName(const char* n,
  302. const cmTarget& t)
  303. {
  304. const char* targetPrefix = t.GetProperty("PREFIX");
  305. const char* targetSuffix = t.GetProperty("SUFFIX");
  306. const char* prefixVar = 0;
  307. const char* suffixVar = 0;
  308. switch(t.GetType())
  309. {
  310. case cmTarget::STATIC_LIBRARY:
  311. prefixVar = "CMAKE_STATIC_LIBRARY_PREFIX";
  312. suffixVar = "CMAKE_STATIC_LIBRARY_SUFFIX";
  313. break;
  314. case cmTarget::SHARED_LIBRARY:
  315. prefixVar = "CMAKE_SHARED_LIBRARY_PREFIX";
  316. suffixVar = "CMAKE_SHARED_LIBRARY_SUFFIX";
  317. break;
  318. case cmTarget::MODULE_LIBRARY:
  319. prefixVar = "CMAKE_SHARED_MODULE_PREFIX";
  320. suffixVar = "CMAKE_SHARED_MODULE_SUFFIX";
  321. break;
  322. case cmTarget::EXECUTABLE:
  323. case cmTarget::WIN32_EXECUTABLE:
  324. targetSuffix = cmSystemTools::GetExecutableExtension();
  325. case cmTarget::UTILITY:
  326. case cmTarget::INSTALL_FILES:
  327. case cmTarget::INSTALL_PROGRAMS:
  328. break;
  329. }
  330. // if there is no prefix on the target use the cmake definition
  331. if(!targetPrefix && prefixVar)
  332. {
  333. targetPrefix = this->GetSafeDefinition(prefixVar);
  334. }
  335. // if there is no suffix on the target use the cmake definition
  336. if(!targetSuffix && suffixVar)
  337. {
  338. targetSuffix = this->GetSafeDefinition(suffixVar);
  339. }
  340. std::string name = targetPrefix?targetPrefix:"";
  341. name += n;
  342. name += targetSuffix?targetSuffix:"";
  343. return name;
  344. }
  345. // Output the rules for any targets
  346. void cmLocalUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  347. {
  348. // for each target add to the list of targets
  349. fout << "TARGETS = ";
  350. const cmTargets &tgts = m_Makefile->GetTargets();
  351. // list libraries first
  352. for(cmTargets::const_iterator l = tgts.begin();
  353. l != tgts.end(); l++)
  354. {
  355. if (l->second.IsInAll())
  356. {
  357. if((l->second.GetType() == cmTarget::STATIC_LIBRARY) ||
  358. (l->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  359. (l->second.GetType() == cmTarget::MODULE_LIBRARY))
  360. {
  361. std::string path = m_LibraryOutputPath;
  362. path += this->GetFullTargetName(l->first.c_str(), l->second);
  363. fout << " \\\n"
  364. << cmSystemTools::ConvertToOutputPath(path.c_str());
  365. }
  366. }
  367. }
  368. // executables
  369. for(cmTargets::const_iterator l = tgts.begin();
  370. l != tgts.end(); l++)
  371. {
  372. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  373. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  374. l->second.IsInAll())
  375. {
  376. std::string path = m_ExecutableOutputPath;
  377. path += this->GetFullTargetName(l->first.c_str(), l->second);
  378. fout << " \\\n" << cmSystemTools::ConvertToOutputPath(path.c_str());
  379. }
  380. }
  381. // list utilities last
  382. for(cmTargets::const_iterator l = tgts.begin();
  383. l != tgts.end(); l++)
  384. {
  385. if (l->second.GetType() == cmTarget::UTILITY &&
  386. l->second.IsInAll())
  387. {
  388. fout << " \\\n" << l->first.c_str();
  389. }
  390. }
  391. fout << "\n\n";
  392. // get the classes from the source lists then add them to the groups
  393. for(cmTargets::const_iterator l = tgts.begin();
  394. l != tgts.end(); l++)
  395. {
  396. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  397. if (classes.begin() != classes.end())
  398. {
  399. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS") << " = ";
  400. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  401. i != classes.end(); i++)
  402. {
  403. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  404. !(*i)->GetCustomCommand())
  405. {
  406. std::string outExt(
  407. this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  408. if(outExt.size())
  409. {
  410. fout << "\\\n"
  411. << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  412. << outExt.c_str() << " ";
  413. }
  414. }
  415. }
  416. fout << "\n\n";
  417. fout << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS_QUOTED") << " = ";
  418. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  419. i != classes.end(); i++)
  420. {
  421. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  422. !(*i)->GetCustomCommand())
  423. {
  424. std::string outExt(this->GetOutputExtension((*i)->GetSourceExtension().c_str()));
  425. if(outExt.size())
  426. {
  427. fout << "\\\n\"" << cmSystemTools::ConvertToOutputPath((*i)->GetSourceName().c_str())
  428. << outExt.c_str() << "\" ";
  429. }
  430. }
  431. }
  432. fout << "\n\n";
  433. }
  434. }
  435. fout << "CLEAN_OBJECT_FILES = ";
  436. for(cmTargets::const_iterator l = tgts.begin();
  437. l != tgts.end(); l++)
  438. {
  439. std::vector<cmSourceFile*> classes = l->second.GetSourceFiles();
  440. if (classes.begin() != classes.end())
  441. {
  442. fout << "$(" << this->CreateMakeVariable(l->first.c_str(), "_SRC_OBJS")
  443. << ") ";
  444. }
  445. }
  446. fout << "\n\n";
  447. const char * qt_files = m_Makefile->GetDefinition("GENERATED_QT_FILES");
  448. if (qt_files != NULL &&
  449. strlen(m_Makefile->GetDefinition("GENERATED_QT_FILES"))>0)
  450. {
  451. fout << "GENERATED_QT_FILES = ";
  452. fout << qt_files;
  453. fout << "\n\n";
  454. }
  455. }
  456. /**
  457. * Output the linking rules on a command line. For executables,
  458. * targetLibrary should be a NULL pointer. For libraries, it should point
  459. * to the name of the library. This will not link a library against itself.
  460. */
  461. void cmLocalUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  462. const char* targetLibrary,
  463. const cmTarget &tgt)
  464. {
  465. // Try to emit each search path once
  466. std::set<std::string> emitted;
  467. // Embed runtime search paths if possible and if required.
  468. bool outputRuntime = true;
  469. std::string runtimeFlag;
  470. std::string runtimeSep;
  471. std::vector<std::string> runtimeDirs;
  472. bool cxx = tgt.HasCxx();
  473. if(!cxx )
  474. {
  475. // if linking a c executable use the C runtime flag as cc
  476. // may not be the same program that creates shared libaries
  477. // and may have different flags
  478. runtimeFlag = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_FLAG");
  479. runtimeSep = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_FLAG_SEP");
  480. }
  481. else
  482. {
  483. runtimeFlag = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG");
  484. runtimeSep = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP");
  485. }
  486. // concatenate all paths or no?
  487. bool runtimeConcatenate = ( runtimeSep!="" );
  488. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  489. {
  490. outputRuntime = false;
  491. }
  492. // Some search paths should never be emitted
  493. emitted.insert("");
  494. emitted.insert("/usr/lib");
  495. std::string libPathFlag = m_Makefile->GetDefinition("CMAKE_LIBRARY_PATH_FLAG");
  496. std::string libLinkFlag = this->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  497. // collect all the flags needed for linking libraries
  498. std::string linkLibs;
  499. // Flags to link an executable to shared libraries.
  500. if( tgt.GetType() == cmTarget::EXECUTABLE ||
  501. tgt.GetType() == cmTarget::WIN32_EXECUTABLE )
  502. {
  503. if(cxx)
  504. {
  505. linkLibs = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS");
  506. }
  507. else
  508. {
  509. linkLibs = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_LINK_FLAGS");
  510. }
  511. linkLibs += " ";
  512. }
  513. const std::vector<std::string>& libdirs = tgt.GetLinkDirectories();
  514. for(std::vector<std::string>::const_iterator libDir = libdirs.begin();
  515. libDir != libdirs.end(); ++libDir)
  516. {
  517. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  518. if(emitted.insert(libpath).second)
  519. {
  520. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  521. if((pos == std::string::npos || pos > 0)
  522. && libDir->find("${") == std::string::npos)
  523. {
  524. linkLibs += libPathFlag;
  525. if(outputRuntime)
  526. {
  527. runtimeDirs.push_back( libpath );
  528. }
  529. }
  530. linkLibs += libpath;
  531. linkLibs += " ";
  532. }
  533. }
  534. std::string linkSuffix = this->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  535. std::string regexp = ".*\\";
  536. regexp += linkSuffix;
  537. regexp += "$";
  538. cmsys::RegularExpression hasSuffix(regexp.c_str());
  539. std::string librariesLinked;
  540. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  541. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  542. lib != libs.end(); ++lib)
  543. {
  544. // Don't link the library against itself!
  545. if(targetLibrary && (lib->first == targetLibrary)) continue;
  546. // don't look at debug libraries
  547. if (lib->second == cmTarget::DEBUG) continue;
  548. // skip zero size library entries, this may happen
  549. // if a variable expands to nothing.
  550. if (lib->first.size() == 0) continue;
  551. // if it is a full path break it into -L and -l
  552. cmsys::RegularExpression reg("([ \t]*\\-l)|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)");
  553. if(lib->first.find('/') != std::string::npos
  554. && !reg.find(lib->first))
  555. {
  556. std::string dir, file;
  557. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  558. dir, file);
  559. std::string libpath = this->ConvertToOutputForExisting(dir.c_str());
  560. if(emitted.insert(libpath).second)
  561. {
  562. linkLibs += libPathFlag;
  563. linkLibs += libpath;
  564. linkLibs += " ";
  565. if(outputRuntime)
  566. {
  567. runtimeDirs.push_back( libpath );
  568. }
  569. }
  570. cmsys::RegularExpression libname("^lib([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  571. cmsys::RegularExpression libname_noprefix("([^/]*)(\\.so|\\.lib|\\.dll|\\.sl|\\.a|\\.dylib).*");
  572. std::cout << "file is " << file << "\n";
  573. if(libname.find(file))
  574. {
  575. // Library had "lib" prefix.
  576. librariesLinked += libLinkFlag;
  577. file = libname.match(1);
  578. // if ignore libprefix is on,
  579. // then add the lib prefix back into the name
  580. if(m_IgnoreLibPrefix)
  581. {
  582. std::cout << "m_IgnoreLibPrefix\n";
  583. file = "lib" + file;
  584. }
  585. librariesLinked += file;
  586. if(linkSuffix.size() && !hasSuffix.find(file))
  587. {
  588. librariesLinked += linkSuffix;
  589. }
  590. librariesLinked += " ";
  591. }
  592. else if(libname_noprefix.find(file))
  593. {
  594. // Library had no "lib" prefix.
  595. librariesLinked += libLinkFlag;
  596. file = libname_noprefix.match(1);
  597. librariesLinked += file;
  598. if(linkSuffix.size() && !hasSuffix.find(file))
  599. {
  600. librariesLinked += linkSuffix;
  601. }
  602. librariesLinked += " ";
  603. }
  604. else
  605. {
  606. // Error parsing the library name. Just use the full path.
  607. // The linker will give an error if it is invalid.
  608. librariesLinked += lib->first;
  609. librariesLinked += " ";
  610. }
  611. }
  612. // not a full path, so add -l name
  613. else
  614. {
  615. if(!reg.find(lib->first))
  616. {
  617. librariesLinked += libLinkFlag;
  618. }
  619. librariesLinked += lib->first;
  620. if(linkSuffix.size() && !hasSuffix.find(lib->first))
  621. {
  622. librariesLinked += linkSuffix;
  623. }
  624. librariesLinked += " ";
  625. }
  626. }
  627. linkLibs += librariesLinked;
  628. fout << linkLibs;
  629. if(outputRuntime && runtimeDirs.size()>0)
  630. {
  631. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  632. // a "-R a -R b -R c" type link line
  633. fout << runtimeFlag;
  634. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  635. fout << *itr;
  636. ++itr;
  637. for( ; itr != runtimeDirs.end(); ++itr )
  638. {
  639. if(runtimeConcatenate)
  640. {
  641. fout << runtimeSep << *itr;
  642. }
  643. else
  644. {
  645. fout << " " << runtimeFlag << *itr;
  646. }
  647. }
  648. fout << " ";
  649. }
  650. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  651. {
  652. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  653. }
  654. }
  655. std::string cmLocalUnixMakefileGenerator::CreatePreBuildRules(
  656. const cmTarget &target, const char* /* targetName */)
  657. {
  658. std::string customRuleCode = "";
  659. bool initNext = false;
  660. for (std::vector<cmCustomCommand>::const_iterator cr =
  661. target.GetPreBuildCommands().begin();
  662. cr != target.GetPreBuildCommands().end(); ++cr)
  663. {
  664. cmCustomCommand cc(*cr);
  665. cc.ExpandVariables(*m_Makefile);
  666. if(initNext)
  667. {
  668. customRuleCode += "\n\t";
  669. }
  670. else
  671. {
  672. initNext = true;
  673. }
  674. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  675. customRuleCode += command + " " + cc.GetArguments();
  676. }
  677. return customRuleCode;
  678. }
  679. std::string cmLocalUnixMakefileGenerator::CreatePreLinkRules(
  680. const cmTarget &target, const char* /* targetName */)
  681. {
  682. std::string customRuleCode = "";
  683. bool initNext = false;
  684. for (std::vector<cmCustomCommand>::const_iterator cr =
  685. target.GetPreLinkCommands().begin();
  686. cr != target.GetPreLinkCommands().end(); ++cr)
  687. {
  688. cmCustomCommand cc(*cr);
  689. cc.ExpandVariables(*m_Makefile);
  690. if(initNext)
  691. {
  692. customRuleCode += "\n\t";
  693. }
  694. else
  695. {
  696. initNext = true;
  697. }
  698. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  699. customRuleCode += command + " " + cc.GetArguments();
  700. }
  701. return customRuleCode;
  702. }
  703. std::string cmLocalUnixMakefileGenerator::CreatePostBuildRules(
  704. const cmTarget &target, const char* /* targetName */)
  705. {
  706. std::string customRuleCode = "";
  707. bool initNext = false;
  708. for (std::vector<cmCustomCommand>::const_iterator cr =
  709. target.GetPostBuildCommands().begin();
  710. cr != target.GetPostBuildCommands().end(); ++cr)
  711. {
  712. cmCustomCommand cc(*cr);
  713. cc.ExpandVariables(*m_Makefile);
  714. if(initNext)
  715. {
  716. customRuleCode += "\n\t";
  717. }
  718. else
  719. {
  720. initNext = true;
  721. }
  722. std::string command = cmSystemTools::ConvertToOutputPath(cc.GetCommand().c_str());
  723. customRuleCode += command + " " + cc.GetArguments();
  724. }
  725. return customRuleCode;
  726. }
  727. struct RuleVariables
  728. {
  729. const char* replace;
  730. const char* lookup;
  731. };
  732. static RuleVariables ruleReplaceVars[] =
  733. {
  734. {"<CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS"},
  735. {"<CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS"},
  736. {"<CMAKE_SHARED_MODULE_C_FLAGS>", "CMAKE_SHARED_MODULE_C_FLAGS"},
  737. {"<CMAKE_SHARED_MODULE_CXX_FLAGS>", "CMAKE_SHARED_MODULE_CXX_FLAGS"},
  738. {"<CMAKE_SHARED_LIBRARY_C_FLAGS>", "CMAKE_SHARED_LIBRARY_C_FLAGS"},
  739. {"<CMAKE_SHARED_LIBRARY_CXX_FLAGS>", "CMAKE_SHARED_LIBRARY_CXX_FLAGS"},
  740. {"<CMAKE_CXX_LINK_FLAGS>", "CMAKE_CXX_LINK_FLAGS"},
  741. {"<CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS>", "CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS"},
  742. {"<CMAKE_SHARED_MODULE_CREATE_C_FLAGS>", "CMAKE_SHARED_MODULE_CREATE_C_FLAGS"},
  743. {"<CMAKE_C_LINK_FLAGS>", "CMAKE_C_LINK_FLAGS"},
  744. {"<CMAKE_AR>", "CMAKE_AR"},
  745. {"<CMAKE_RANLIB>", "CMAKE_RANLIB"},
  746. {0, 0}
  747. };
  748. void
  749. cmLocalUnixMakefileGenerator::ExpandRuleVariables(std::string& s,
  750. const char* objects,
  751. const char* target,
  752. const char* linkLibs,
  753. const char* source,
  754. const char* object,
  755. const char* flags,
  756. const char* objectsquoted,
  757. const char* targetBase,
  758. const char* linkFlags)
  759. {
  760. std::string cxxcompiler = this->ConvertToOutputForExisting(
  761. this->GetSafeDefinition("CMAKE_CXX_COMPILER"));
  762. std::string ccompiler = this->ConvertToOutputForExisting(
  763. this->GetSafeDefinition("CMAKE_C_COMPILER"));
  764. cmSystemTools::ReplaceString(s, "<CMAKE_CXX_COMPILER>", cxxcompiler.c_str());
  765. cmSystemTools::ReplaceString(s, "<CMAKE_C_COMPILER>", ccompiler.c_str());
  766. if(linkFlags)
  767. {
  768. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  769. }
  770. if(flags)
  771. {
  772. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  773. }
  774. if(source)
  775. {
  776. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  777. }
  778. if(object)
  779. {
  780. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  781. }
  782. if(objects)
  783. {
  784. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  785. }
  786. if(objectsquoted)
  787. {
  788. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  789. }
  790. if(target)
  791. {
  792. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  793. }
  794. if(targetBase)
  795. {
  796. // special case for quoted paths with spaces
  797. // if you see <TARGET_BASE>.lib then put the .lib inside
  798. // the quotes, same for .dll
  799. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  800. {
  801. std::string base = targetBase;
  802. base[base.size()-1] = '.';
  803. std::string baseLib = base + "lib\"";
  804. std::string baseDll = base + "dll\"";
  805. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.lib", baseLib.c_str());
  806. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.dll", baseDll.c_str());
  807. }
  808. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  809. }
  810. if(linkLibs)
  811. {
  812. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  813. }
  814. RuleVariables* rv = ruleReplaceVars;
  815. while(rv->replace)
  816. {
  817. cmSystemTools::ReplaceString(s, rv->replace,
  818. this->GetSafeDefinition(rv->lookup));
  819. rv++;
  820. }
  821. }
  822. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  823. const char* name,
  824. const cmTarget &t,
  825. const char* prefix,
  826. const char* suffix,
  827. const char* createVariable,
  828. const char* comment,
  829. const char* linkFlags
  830. )
  831. {
  832. // create the library name
  833. std::string targetNameBase = prefix;
  834. targetNameBase += name;
  835. std::string targetName = prefix;
  836. targetName += name;
  837. targetName += suffix;
  838. // create the target full path name
  839. std::string targetFullPath = m_LibraryOutputPath + targetName;
  840. std::string targetBaseFullPath = m_LibraryOutputPath + targetNameBase;
  841. targetBaseFullPath =
  842. cmSystemTools::ConvertToOutputPath(targetBaseFullPath.c_str());
  843. targetFullPath = cmSystemTools::ConvertToOutputPath(targetFullPath.c_str());
  844. // get the objects that are used to link this library
  845. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  846. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ") ";
  847. // create a variable with the objects that this library depends on
  848. std::string depend = objs + " $("
  849. + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  850. // collect up the build rules
  851. std::vector<std::string> rules;
  852. std::string command = "$(RM) " + targetFullPath;
  853. rules.push_back(command);
  854. rules.push_back(m_Makefile->GetDefinition(createVariable));
  855. // expand multi-command semi-colon separated lists
  856. // of commands into separate commands
  857. std::vector<std::string> commands;
  858. // collect custom commands for this target and add them to the list
  859. std::string customCommands = this->CreatePreBuildRules(t, name);
  860. if(customCommands.size() > 0)
  861. {
  862. commands.push_back(customCommands);
  863. }
  864. // collect custom commands for this target and add them to the list
  865. customCommands = this->CreatePreLinkRules(t, name);
  866. if(customCommands.size() > 0)
  867. {
  868. commands.push_back(customCommands);
  869. }
  870. cmSystemTools::ExpandList(rules, commands);
  871. // collect custom commands for this target and add them to the list
  872. customCommands = this->CreatePostBuildRules(t, name);
  873. if(customCommands.size() > 0)
  874. {
  875. commands.push_back(customCommands);
  876. }
  877. // collect up the link libraries
  878. cmOStringStream linklibs;
  879. this->OutputLinkLibraries(linklibs, name, t);
  880. for(std::vector<std::string>::iterator i = commands.begin();
  881. i != commands.end(); ++i)
  882. {
  883. this->ExpandRuleVariables(*i,
  884. objs.c_str(),
  885. targetFullPath.c_str(),
  886. linklibs.str().c_str(),
  887. 0, 0, 0, objsQuoted.c_str(),
  888. targetBaseFullPath.c_str(),
  889. linkFlags);
  890. }
  891. this->OutputMakeRule(fout, comment,
  892. targetFullPath.c_str(),
  893. depend.c_str(),
  894. commands);
  895. }
  896. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  897. const char* name,
  898. const cmTarget &t)
  899. {
  900. const char* createRule;
  901. if(t.HasCxx())
  902. {
  903. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  904. }
  905. else
  906. {
  907. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  908. }
  909. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  910. buildType = cmSystemTools::UpperCase(buildType);
  911. std::string linkFlags = this->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  912. linkFlags += " ";
  913. if(buildType.size())
  914. {
  915. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  916. build += buildType;
  917. linkFlags += this->GetSafeDefinition(build.c_str());
  918. linkFlags += " ";
  919. }
  920. #ifdef _WIN32
  921. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  922. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  923. i != sources.end(); ++i)
  924. {
  925. if((*i)->GetSourceExtension() == "def")
  926. {
  927. linkFlags += this->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  928. linkFlags += cmSystemTools::ConvertToOutputPath((*i)->GetFullPath().c_str());
  929. linkFlags += " ";
  930. }
  931. }
  932. #endif
  933. const char* targetPrefix = t.GetProperty("PREFIX");
  934. if(!targetPrefix)
  935. {
  936. targetPrefix = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  937. }
  938. const char* targetSuffix = t.GetProperty("SUFFIX");
  939. if(!targetSuffix)
  940. {
  941. targetSuffix = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  942. }
  943. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  944. if(targetLinkFlags)
  945. {
  946. linkFlags += targetLinkFlags;
  947. linkFlags += " ";
  948. }
  949. this->OutputLibraryRule(fout, name, t,
  950. targetPrefix,
  951. targetSuffix,
  952. createRule,
  953. "shared library",
  954. linkFlags.c_str());
  955. }
  956. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  957. const char* name,
  958. const cmTarget &t)
  959. {
  960. const char* createRule;
  961. if(t.HasCxx())
  962. {
  963. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  964. }
  965. else
  966. {
  967. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  968. }
  969. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  970. buildType = cmSystemTools::UpperCase(buildType);
  971. std::string linkFlags = this->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  972. linkFlags += " ";
  973. if(buildType.size())
  974. {
  975. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  976. build += buildType;
  977. linkFlags += this->GetSafeDefinition(build.c_str());
  978. linkFlags += " ";
  979. }
  980. const char* targetPrefix = t.GetProperty("PREFIX");
  981. if(!targetPrefix)
  982. {
  983. targetPrefix = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  984. }
  985. const char* targetSuffix = t.GetProperty("SUFFIX");
  986. if(!targetSuffix)
  987. {
  988. targetSuffix = this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  989. }
  990. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  991. if(targetLinkFlags)
  992. {
  993. linkFlags += targetLinkFlags;
  994. linkFlags += " ";
  995. }
  996. this->OutputLibraryRule(fout, name, t,
  997. targetPrefix,
  998. targetSuffix,
  999. createRule,
  1000. "shared module",
  1001. linkFlags.c_str());
  1002. }
  1003. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  1004. const char* name,
  1005. const cmTarget &t)
  1006. {
  1007. const char* createRule;
  1008. if(t.HasCxx())
  1009. {
  1010. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  1011. }
  1012. else
  1013. {
  1014. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  1015. }
  1016. const char* targetPrefix = t.GetProperty("PREFIX");
  1017. if(!targetPrefix)
  1018. {
  1019. targetPrefix = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1020. }
  1021. const char* targetSuffix = t.GetProperty("SUFFIX");
  1022. if(!targetSuffix)
  1023. {
  1024. targetSuffix = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1025. }
  1026. std::string linkFlags;
  1027. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1028. if(targetLinkFlags)
  1029. {
  1030. linkFlags += targetLinkFlags;
  1031. linkFlags += " ";
  1032. }
  1033. this->OutputLibraryRule(fout, name, t,
  1034. targetPrefix,
  1035. targetSuffix,
  1036. createRule,
  1037. "static library",
  1038. linkFlags.c_str());
  1039. }
  1040. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  1041. const char* name,
  1042. const cmTarget &t)
  1043. {
  1044. std::string linkFlags;
  1045. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1046. buildType = cmSystemTools::UpperCase(buildType);
  1047. std::string flags;
  1048. // Construct the full path to the executable that will be generated.
  1049. std::string target = m_ExecutableOutputPath;
  1050. if(target.length() == 0)
  1051. {
  1052. target = m_Makefile->GetCurrentOutputDirectory();
  1053. if(target.size() && target[target.size()-1] != '/')
  1054. {
  1055. target += "/";
  1056. }
  1057. }
  1058. target += name;
  1059. target += cmSystemTools::GetExecutableExtension();
  1060. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  1061. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  1062. std::string depend = "$(";
  1063. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  1064. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1065. std::vector<std::string> rules;
  1066. linkFlags += this->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1067. linkFlags += " ";
  1068. if(buildType.size())
  1069. {
  1070. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1071. build += buildType;
  1072. linkFlags += this->GetSafeDefinition(build.c_str());
  1073. linkFlags += " ";
  1074. }
  1075. if(t.HasCxx())
  1076. {
  1077. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  1078. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  1079. flags += " ";
  1080. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  1081. flags += " ";
  1082. }
  1083. else
  1084. {
  1085. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  1086. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  1087. flags += " ";
  1088. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  1089. flags += " ";
  1090. }
  1091. cmOStringStream linklibs;
  1092. this->OutputLinkLibraries(linklibs, 0, t);
  1093. std::string comment = "executable";
  1094. std::vector<std::string> commands;
  1095. std::string customCommands = this->CreatePreBuildRules(t, name);
  1096. if(customCommands.size() > 0)
  1097. {
  1098. commands.push_back(customCommands.c_str());
  1099. }
  1100. customCommands = this->CreatePreLinkRules(t, name);
  1101. if(customCommands.size() > 0)
  1102. {
  1103. commands.push_back(customCommands.c_str());
  1104. }
  1105. cmSystemTools::ExpandList(rules, commands);
  1106. customCommands = this->CreatePostBuildRules(t, name);
  1107. if(customCommands.size() > 0)
  1108. {
  1109. commands.push_back(customCommands.c_str());
  1110. }
  1111. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1112. {
  1113. linkFlags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  1114. linkFlags += " ";
  1115. }
  1116. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  1117. {
  1118. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1119. linkFlags += " ";
  1120. }
  1121. else
  1122. {
  1123. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1124. linkFlags += " ";
  1125. }
  1126. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1127. if(targetLinkFlags)
  1128. {
  1129. linkFlags += targetLinkFlags;
  1130. linkFlags += " ";
  1131. }
  1132. for(std::vector<std::string>::iterator i = commands.begin();
  1133. i != commands.end(); ++i)
  1134. {
  1135. this->ExpandRuleVariables(*i,
  1136. objs.c_str(),
  1137. target.c_str(),
  1138. linklibs.str().c_str(),
  1139. 0,
  1140. 0,
  1141. flags.c_str(),
  1142. 0,
  1143. 0,
  1144. linkFlags.c_str());
  1145. }
  1146. this->OutputMakeRule(fout,
  1147. comment.c_str(),
  1148. target.c_str(),
  1149. depend.c_str(),
  1150. commands);
  1151. // If there is no executable output path, add a rule with the
  1152. // relative path to the executable. This is necessary for
  1153. // try-compile to work in this case.
  1154. depend = target;
  1155. target = name;
  1156. target += cmSystemTools::GetExecutableExtension();
  1157. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  1158. commands.resize(0);
  1159. this->OutputMakeRule(fout,
  1160. comment.c_str(),
  1161. target.c_str(),
  1162. depend.c_str(),
  1163. commands);
  1164. }
  1165. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1166. const char* name,
  1167. const cmTarget &t)
  1168. {
  1169. const char* cc = 0;
  1170. std::string customCommands = this->CreatePreBuildRules(t, name);
  1171. std::string customCommands2 = this->CreatePreLinkRules(t, name);
  1172. if(customCommands2.size() > 0)
  1173. {
  1174. customCommands += customCommands2;
  1175. }
  1176. customCommands2 = this->CreatePostBuildRules(t, name);
  1177. if(customCommands2.size() > 0)
  1178. {
  1179. customCommands += customCommands2;
  1180. }
  1181. if(customCommands.size() > 0)
  1182. {
  1183. cc = customCommands.c_str();
  1184. }
  1185. std::string comment = "Utility";
  1186. std::string depends;
  1187. depends = "$(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1188. std::string replaceVars;
  1189. const std::vector<cmCustomCommand> &ccs = t.GetPostBuildCommands();
  1190. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1191. i != ccs.end(); ++i)
  1192. {
  1193. const std::vector<std::string> & dep = i->GetDepends();
  1194. for(std::vector<std::string>::const_iterator d = dep.begin();
  1195. d != dep.end(); ++d)
  1196. {
  1197. depends += " \\\n";
  1198. replaceVars = *d;
  1199. m_Makefile->ExpandVariablesInString(replaceVars);
  1200. depends += cmSystemTools::ConvertToOutputPath(replaceVars.c_str());
  1201. }
  1202. }
  1203. this->OutputMakeRule(fout, comment.c_str(), name,
  1204. depends.c_str(), cc);
  1205. }
  1206. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1207. {
  1208. // for each target
  1209. const cmTargets &tgts = m_Makefile->GetTargets();
  1210. for(cmTargets::const_iterator l = tgts.begin();
  1211. l != tgts.end(); l++)
  1212. {
  1213. switch(l->second.GetType())
  1214. {
  1215. case cmTarget::STATIC_LIBRARY:
  1216. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1217. break;
  1218. case cmTarget::SHARED_LIBRARY:
  1219. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1220. break;
  1221. case cmTarget::MODULE_LIBRARY:
  1222. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1223. break;
  1224. case cmTarget::EXECUTABLE:
  1225. case cmTarget::WIN32_EXECUTABLE:
  1226. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1227. break;
  1228. case cmTarget::UTILITY:
  1229. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1230. break;
  1231. // This is handled by the OutputCustomRules method
  1232. case cmTarget::INSTALL_FILES:
  1233. // This is handled by the OutputInstallRules method
  1234. case cmTarget::INSTALL_PROGRAMS:
  1235. // This is handled by the OutputInstallRules method
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. // For each target that is an executable or shared library, generate
  1241. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1242. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1243. {
  1244. // Build a set of libraries that will be linked into any target in
  1245. // this directory.
  1246. std::set<std::string> used;
  1247. // for each target
  1248. const cmTargets &tgts = m_Makefile->GetTargets();
  1249. for(cmTargets::const_iterator l = tgts.begin();
  1250. l != tgts.end(); l++)
  1251. {
  1252. // Each dependency should only be emitted once per target.
  1253. std::set<std::string> emitted;
  1254. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1255. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1256. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1257. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1258. || (l->second.GetType() == cmTarget::UTILITY)
  1259. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  1260. {
  1261. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1262. // A library should not depend on itself!
  1263. emitted.insert(l->first);
  1264. // for static libraries do not depend on other libraries
  1265. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1266. {
  1267. // Now, look at all link libraries specific to this target.
  1268. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1269. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1270. lib != tlibs.end(); ++lib)
  1271. {
  1272. // Record that this library was used.
  1273. used.insert(lib->first);
  1274. // Don't emit the same library twice for this target.
  1275. if(emitted.insert(lib->first).second)
  1276. {
  1277. // Output this dependency.
  1278. this->OutputLibDepend(fout, lib->first.c_str());
  1279. }
  1280. }
  1281. }
  1282. // for all targets depend on utilities
  1283. // Now, look at all utilities specific to this target.
  1284. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1285. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1286. util != tutils.end(); ++util)
  1287. {
  1288. // Record that this utility was used.
  1289. used.insert(*util);
  1290. // Don't emit the same utility twice for this target.
  1291. if(emitted.insert(*util).second)
  1292. {
  1293. // Output this dependency.
  1294. this->OutputExeDepend(fout, util->c_str());
  1295. }
  1296. }
  1297. fout << "\n";
  1298. }
  1299. }
  1300. fout << "\n";
  1301. // Loop over the libraries used and make sure there is a rule to
  1302. // build them in this makefile. If the library is in another
  1303. // directory, add a rule to jump to that directory and make sure it
  1304. // exists.
  1305. for(std::set<std::string>::const_iterator lib = used.begin();
  1306. lib != used.end(); ++lib)
  1307. {
  1308. // loop over the list of directories that the libraries might
  1309. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1310. // be stored in the cache
  1311. std::string libPath = *lib + "_CMAKE_PATH";
  1312. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1313. // if cache and not the current directory add a rule, to
  1314. // jump into the directory and build for the first time
  1315. if(cacheValue &&
  1316. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1317. {
  1318. // add the correct extension
  1319. std::string ltname = *lib+"_LIBRARY_TYPE";
  1320. const char* libType
  1321. = m_Makefile->GetDefinition(ltname.c_str());
  1322. // if it was a library..
  1323. if (libType)
  1324. {
  1325. std::string library;
  1326. std::string libpath = cacheValue;
  1327. if(libType && std::string(libType) == "SHARED")
  1328. {
  1329. library = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1330. library += *lib;
  1331. library += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1332. }
  1333. else if(libType && std::string(libType) == "MODULE")
  1334. {
  1335. library = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1336. library += *lib;
  1337. library += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1338. }
  1339. else if(libType && std::string(libType) == "STATIC")
  1340. {
  1341. library = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1342. library += *lib;
  1343. library += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1344. }
  1345. else
  1346. {
  1347. cmSystemTools::Error("Unknown library type!");
  1348. return;
  1349. }
  1350. if(m_LibraryOutputPath.size())
  1351. {
  1352. libpath = m_LibraryOutputPath;
  1353. }
  1354. else
  1355. {
  1356. libpath += "/";
  1357. }
  1358. libpath += library;
  1359. // put out a rule to build the library if it does not exist
  1360. this->OutputBuildTargetInDir(fout,
  1361. cacheValue,
  1362. library.c_str(),
  1363. libpath.c_str(),
  1364. m_Makefile->
  1365. GetDefinition("LIBRARY_OUTPUT_PATH")
  1366. );
  1367. }
  1368. // something other than a library...
  1369. else
  1370. {
  1371. std::string exepath = cacheValue;
  1372. if(m_ExecutableOutputPath.size())
  1373. {
  1374. exepath = m_ExecutableOutputPath;
  1375. }
  1376. else
  1377. {
  1378. exepath += "/";
  1379. }
  1380. std::string fullName = lib->c_str();
  1381. fullName += cmSystemTools::GetExecutableExtension();
  1382. exepath += fullName;
  1383. this->OutputBuildTargetInDir(fout,
  1384. cacheValue,
  1385. fullName.c_str(),
  1386. exepath.c_str(),
  1387. m_Makefile->
  1388. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  1389. );
  1390. }
  1391. }
  1392. }
  1393. }
  1394. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1395. const char* path,
  1396. const char* library,
  1397. const char* fullpath,
  1398. const char* libOutPath)
  1399. {
  1400. const char* makeTarget = library;
  1401. std::string currentDir =
  1402. cmSystemTools::ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  1403. std::string wpath = this->ConvertToOutputForExisting(path);
  1404. std::string wfullpath = this->ConvertToOutputForExisting(fullpath);
  1405. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  1406. {
  1407. makeTarget = wfullpath.c_str();
  1408. }
  1409. fout << wfullpath
  1410. << ":\n\tcd " << wpath << "\n"
  1411. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1412. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1413. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1414. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  1415. << "\n\tcd " << currentDir << "\n";
  1416. }
  1417. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1418. const char* path,
  1419. const char* library,
  1420. const char* fullpath,
  1421. const char* outputPath)
  1422. {
  1423. if(m_WindowsShell)
  1424. {
  1425. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath, outputPath);
  1426. return;
  1427. }
  1428. const char* makeTarget = library;
  1429. if(outputPath && strcmp( outputPath, "" ) != 0)
  1430. {
  1431. makeTarget = fullpath;
  1432. }
  1433. fout << this->ConvertToOutputForExisting(fullpath)
  1434. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1435. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1436. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1437. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1438. << "; $(MAKE) $(MAKESILENT) "
  1439. << cmSystemTools::ConvertToOutputPath(makeTarget) << "\n\n";
  1440. }
  1441. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1442. {
  1443. if (strcmp(path1, path2) == 0)
  1444. {
  1445. return true;
  1446. }
  1447. #if defined(_WIN32) || defined(__APPLE__)
  1448. return
  1449. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1450. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1451. #else
  1452. return false;
  1453. #endif
  1454. }
  1455. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1456. const char* name)
  1457. {
  1458. std::string libPath = name;
  1459. libPath += "_CMAKE_PATH";
  1460. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1461. if(cacheValue )
  1462. {
  1463. // if there is a cache value, then this is a library that cmake
  1464. // knows how to build, so we can depend on it
  1465. std::string libpath;
  1466. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1467. {
  1468. // if the library is not in the current directory, then get the full
  1469. // path to it
  1470. if(m_LibraryOutputPath.size())
  1471. {
  1472. libpath = m_LibraryOutputPath;
  1473. }
  1474. else
  1475. {
  1476. libpath = cacheValue;
  1477. libpath += "/";
  1478. }
  1479. }
  1480. else
  1481. {
  1482. // library is in current Makefile so use lib as a prefix
  1483. libpath = m_LibraryOutputPath;
  1484. }
  1485. // add the correct extension
  1486. std::string ltname = name;
  1487. ltname += "_LIBRARY_TYPE";
  1488. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1489. if(libType && std::string(libType) == "SHARED")
  1490. {
  1491. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1492. libpath += name;
  1493. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1494. }
  1495. else if (libType && std::string(libType) == "MODULE")
  1496. {
  1497. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1498. libpath += name;
  1499. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1500. }
  1501. else if (libType && std::string(libType) == "STATIC")
  1502. {
  1503. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1504. libpath += name;
  1505. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1506. }
  1507. fout << cmSystemTools::ConvertToOutputPath(libpath.c_str()) << " ";
  1508. }
  1509. }
  1510. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1511. const char* name)
  1512. {
  1513. std::string exePath = name;
  1514. exePath += "_CMAKE_PATH";
  1515. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1516. if(cacheValue )
  1517. {
  1518. // if there is a cache value, then this is a executable/utility that cmake
  1519. // knows how to build, so we can depend on it
  1520. std::string exepath;
  1521. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1522. {
  1523. // if the exe/utility is not in the current directory, then get the full
  1524. // path to it
  1525. if(m_ExecutableOutputPath.size())
  1526. {
  1527. exepath = m_ExecutableOutputPath;
  1528. }
  1529. else
  1530. {
  1531. exepath = cacheValue;
  1532. exepath += "/";
  1533. }
  1534. }
  1535. else
  1536. {
  1537. // library is in current Makefile
  1538. exepath = m_ExecutableOutputPath;
  1539. }
  1540. // add the library name
  1541. exepath += name;
  1542. // add the correct extension
  1543. exepath += cmSystemTools::GetExecutableExtension();
  1544. fout << cmSystemTools::ConvertToOutputPath(exepath.c_str()) << " ";
  1545. }
  1546. // if it isn't in the cache, it might still be a utility target
  1547. // so check for that
  1548. else
  1549. {
  1550. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1551. if (targets.find(name) != targets.end())
  1552. {
  1553. fout << name << " ";
  1554. }
  1555. }
  1556. }
  1557. // fix up names of directories so they can be used
  1558. // as targets in makefiles.
  1559. inline std::string FixDirectoryName(const char* dir)
  1560. {
  1561. std::string s = dir;
  1562. // replace ../ with 3 under bars
  1563. size_t pos = s.find("../");
  1564. if(pos != std::string::npos)
  1565. {
  1566. s.replace(pos, 3, "___");
  1567. }
  1568. // replace / directory separators with a single under bar
  1569. pos = s.find("/");
  1570. while(pos != std::string::npos)
  1571. {
  1572. s.replace(pos, 1, "_");
  1573. pos = s.find("/");
  1574. }
  1575. return s;
  1576. }
  1577. void cmLocalUnixMakefileGenerator::BuildInSubDirectoryWindows(std::ostream& fout,
  1578. const char* directory,
  1579. const char* target1,
  1580. const char* target2,
  1581. bool silent)
  1582. {
  1583. if(target1)
  1584. {
  1585. std::string dir = this->ConvertToOutputForExisting(directory);
  1586. if(dir[0] == '\"')
  1587. {
  1588. dir = dir.substr(1, dir.size()-2);
  1589. }
  1590. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1591. << " "
  1592. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1593. if (!silent)
  1594. {
  1595. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1596. }
  1597. fout << "\tcd " << dir << "\n"
  1598. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1599. }
  1600. if(target2)
  1601. {
  1602. if (!silent)
  1603. {
  1604. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1605. }
  1606. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1607. }
  1608. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  1609. fout << "\tcd " << this->ConvertToOutputForExisting(currentDir.c_str()) << "\n\n";
  1610. }
  1611. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1612. const char* dir,
  1613. const char* target1,
  1614. const char* target2,
  1615. bool silent)
  1616. {
  1617. if(m_WindowsShell)
  1618. {
  1619. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1620. return;
  1621. }
  1622. std::string directory = cmSystemTools::ConvertToOutputPath(dir);
  1623. if(target1)
  1624. {
  1625. fout << "\t@if test ! -d " << directory
  1626. << "; then $(MAKE) rebuild_cache; fi\n";
  1627. if (!silent)
  1628. {
  1629. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1630. }
  1631. fout << "\t@cd " << directory
  1632. << "; $(MAKE) " << target1 << "\n";
  1633. }
  1634. if(target2)
  1635. {
  1636. if (!silent)
  1637. {
  1638. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1639. }
  1640. fout << "\t@cd " << directory
  1641. << "; $(MAKE) " << target2 << "\n";
  1642. }
  1643. fout << "\n";
  1644. }
  1645. void
  1646. cmLocalUnixMakefileGenerator::
  1647. OutputSubDirectoryVars(std::ostream& fout,
  1648. const char* var,
  1649. const char* target,
  1650. const char* target1,
  1651. const char* target2,
  1652. const char* depend,
  1653. const std::vector<std::string>& SubDirectories,
  1654. bool silent)
  1655. {
  1656. if(!depend)
  1657. {
  1658. depend = "";
  1659. }
  1660. if( SubDirectories.size() == 0)
  1661. {
  1662. return;
  1663. }
  1664. fout << "# Variable for making " << target << " in subdirectories.\n";
  1665. fout << var << " = \\\n";
  1666. unsigned int ii;
  1667. for(ii =0; ii < SubDirectories.size(); ii++)
  1668. {
  1669. std::string subdir = FixDirectoryName(SubDirectories[ii].c_str());
  1670. fout << target << "_" << subdir.c_str();
  1671. if(ii == SubDirectories.size()-1)
  1672. {
  1673. fout << " \n\n";
  1674. }
  1675. else
  1676. {
  1677. fout << " \\\n";
  1678. }
  1679. }
  1680. fout << "# Targets for making " << target << " in subdirectories.\n";
  1681. std::string last = "";
  1682. for(unsigned int cc =0; cc < SubDirectories.size(); cc++)
  1683. {
  1684. std::string subdir = FixDirectoryName(SubDirectories[cc].c_str());
  1685. fout << target << "_" << subdir.c_str() << ": " << depend;
  1686. // Make each subdirectory depend on previous one. This forces
  1687. // parallel builds (make -j 2) to build in same order as a single
  1688. // threaded build to avoid dependency problems.
  1689. if(cc > 0)
  1690. {
  1691. fout << " " << target << "_" << last.c_str();
  1692. }
  1693. fout << "\n";
  1694. last = subdir;
  1695. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1696. dir += "/";
  1697. dir += SubDirectories[cc];
  1698. this->BuildInSubDirectory(fout, dir.c_str(),
  1699. target1, target2, silent);
  1700. }
  1701. fout << "\n\n";
  1702. }
  1703. // output rules for decending into sub directories
  1704. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1705. {
  1706. // Output Sub directory build rules
  1707. const std::vector<std::string>& SubDirectories
  1708. = m_Makefile->GetSubDirectories();
  1709. if( SubDirectories.size() == 0)
  1710. {
  1711. return;
  1712. }
  1713. this->OutputSubDirectoryVars(fout,
  1714. "SUBDIR_BUILD",
  1715. "default_target",
  1716. "default_target",
  1717. 0, "$(TARGETS)",
  1718. SubDirectories,
  1719. false);
  1720. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1721. "clean",
  1722. 0, 0,
  1723. SubDirectories);
  1724. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1725. "depend",
  1726. 0, 0,
  1727. SubDirectories);
  1728. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1729. "install",
  1730. 0, 0,
  1731. SubDirectories);
  1732. }
  1733. // Output the depend information for all the classes
  1734. // in the makefile. These would have been generated
  1735. // by the class cmMakeDepend GenerateMakefile
  1736. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1737. {
  1738. bool ret = false;
  1739. // Iterate over every target.
  1740. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1741. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1742. target != targets.end(); ++target)
  1743. {
  1744. // Iterate over every source for this target.
  1745. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1746. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1747. source != sources.end(); ++source)
  1748. {
  1749. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1750. {
  1751. if(!(*source)->GetDepends().empty())
  1752. {
  1753. // Iterate through all the dependencies for this source.
  1754. for(std::vector<std::string>::const_iterator dep =
  1755. (*source)->GetDepends().begin();
  1756. dep != (*source)->GetDepends().end(); ++dep)
  1757. {
  1758. std::string s = (*source)->GetSourceName();
  1759. s += this->GetOutputExtension((*source)->GetSourceExtension().c_str());
  1760. fout << cmSystemTools::ConvertToOutputPath(s.c_str()) << " : "
  1761. << cmSystemTools::ConvertToOutputPath(dep->c_str()) << "\n";
  1762. ret = true;
  1763. }
  1764. fout << "\n\n";
  1765. }
  1766. }
  1767. }
  1768. }
  1769. return ret;
  1770. }
  1771. // Output the depend information for all the classes
  1772. // in the makefile. These would have been generated
  1773. // by the class cmMakeDepend GenerateMakefile
  1774. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1775. {
  1776. std::set<std::string> emittedLowerPath;
  1777. std::set<std::string> emitted;
  1778. // Iterate over every target.
  1779. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1780. fout << "# Suppresses display of executed commands\n";
  1781. fout << ".SILENT:\n";
  1782. fout << "# disable some common implicit rules to speed things up\n";
  1783. fout << ".SUFFIXES:\n";
  1784. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1785. this->OutputMakeVariables(fout);
  1786. fout << "default:\n";
  1787. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1788. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1789. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1790. target != targets.end(); ++target)
  1791. {
  1792. // Iterate over every source for this target.
  1793. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1794. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1795. source != sources.end(); ++source)
  1796. {
  1797. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1798. {
  1799. if(!(*source)->GetDepends().empty())
  1800. {
  1801. for(std::vector<std::string>::const_iterator dep =
  1802. (*source)->GetDepends().begin();
  1803. dep != (*source)->GetDepends().end(); ++dep)
  1804. {
  1805. // do not call CollapseFullPath on dep here, because it already
  1806. // has been done because m_FullPath on cmDependInformation
  1807. // always is it called. If it is called here, network builds are
  1808. // very slow because of the number of stats
  1809. std::string dependfile = cmSystemTools::ConvertToOutputPath(dep->c_str());
  1810. // use the lower path function to create uniqe names
  1811. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1812. if(emittedLowerPath.insert(lowerpath).second)
  1813. {
  1814. emitted.insert(dependfile);
  1815. fout << "all:: " << dependfile << "\n";
  1816. }
  1817. }
  1818. }
  1819. }
  1820. }
  1821. }
  1822. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1823. std::set<std::string>::iterator i;
  1824. for(i = emitted.begin(); i != emitted.end(); ++i)
  1825. {
  1826. fout << "cmake.depends:: " << *i <<
  1827. "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1828. }
  1829. fout << "\n\n";
  1830. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1831. for(std::set<std::string>::iterator it = emitted.begin();
  1832. it != emitted.end(); ++it)
  1833. {
  1834. fout << *it << ":\n"
  1835. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1836. }
  1837. }
  1838. // Output each custom rule in the following format:
  1839. // output: source depends...
  1840. // (tab) command...
  1841. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1842. {
  1843. // we cannot provide multiple rules for a single output
  1844. // so we will keep track of outputs to make sure we don't write
  1845. // two rules. First found wins
  1846. std::set<std::string> processedOutputs;
  1847. // first output all custom rules
  1848. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  1849. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1850. i != sources.end(); ++i)
  1851. {
  1852. if ((*i)->GetCustomCommand())
  1853. {
  1854. cmCustomCommand *c = (*i)->GetCustomCommand();
  1855. // escape spaces and convert to native slashes path for
  1856. // the command
  1857. std::string comment = c->GetComment();
  1858. std::string command = c->GetCommand();
  1859. cmSystemTools::ReplaceString(command, "/./", "/");
  1860. command = cmSystemTools::ConvertToOutputPath(command.c_str());
  1861. command += " ";
  1862. // now add the arguments
  1863. command += c->GetArguments();
  1864. std::string depends;
  1865. // Collect out all the dependencies for this rule.
  1866. for(std::vector<std::string>::const_iterator d =
  1867. c->GetDepends().begin();
  1868. d != c->GetDepends().end(); ++d)
  1869. {
  1870. std::string dep = *d;
  1871. m_Makefile->ExpandVariablesInString(dep);
  1872. // watch for target dependencies,
  1873. std::string libPath = dep + "_CMAKE_PATH";
  1874. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1875. if (cacheValue)
  1876. {
  1877. libPath = cacheValue;
  1878. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  1879. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  1880. {
  1881. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1882. }
  1883. libPath += "/";
  1884. libPath += dep;
  1885. libPath += cmSystemTools::GetExecutableExtension();
  1886. dep = libPath;
  1887. }
  1888. cmSystemTools::ReplaceString(dep, "/./", "/");
  1889. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  1890. dep = cmSystemTools::ConvertToOutputPath(dep.c_str());
  1891. depends += " ";
  1892. depends += dep;
  1893. }
  1894. // output rule
  1895. if (processedOutputs.find(c->GetOutput()) == processedOutputs.end())
  1896. {
  1897. this->OutputMakeRule(fout,
  1898. (comment.size()?comment.c_str():"Custom command"),
  1899. c->GetOutput().c_str(),
  1900. depends.c_str(),
  1901. command.c_str());
  1902. processedOutputs.insert(c->GetOutput());
  1903. }
  1904. else
  1905. {
  1906. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  1907. c->GetOutput().c_str());
  1908. }
  1909. }
  1910. }
  1911. }
  1912. std::string
  1913. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  1914. {
  1915. std::string ret = cmSystemTools::ConvertToOutputPath(p);
  1916. // if there are spaces in the path, then get the short path version
  1917. // if there is one
  1918. if(ret.find(' ') != std::string::npos)
  1919. {
  1920. if(cmSystemTools::FileExists(p))
  1921. {
  1922. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  1923. {
  1924. ret = p;
  1925. }
  1926. }
  1927. }
  1928. return ret;
  1929. }
  1930. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1931. {
  1932. const char* variables =
  1933. "# the standard shell for make\n"
  1934. "SHELL = /bin/sh\n"
  1935. "\n";
  1936. if(!m_WindowsShell)
  1937. {
  1938. fout << variables;
  1939. }
  1940. else
  1941. {
  1942. fout <<
  1943. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  1944. "NULL=\n"
  1945. "!ELSE \n"
  1946. "NULL=nul\n"
  1947. "!ENDIF \n";
  1948. }
  1949. if(m_MakeSilentFlag.size())
  1950. {
  1951. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  1952. }
  1953. std::string cmakecommand = this->ConvertToOutputForExisting(
  1954. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  1955. fout << "CMAKE_COMMAND = "
  1956. << cmakecommand
  1957. << "\n";
  1958. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  1959. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1960. {
  1961. fout << "CMAKE_EDIT_COMMAND = "
  1962. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1963. << "\n";
  1964. }
  1965. fout << "CMAKE_CURRENT_SOURCE = " <<
  1966. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory())
  1967. << "\n";
  1968. fout << "CMAKE_CURRENT_BINARY = " <<
  1969. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory())
  1970. << "\n";
  1971. fout << "CMAKE_SOURCE_DIR = " <<
  1972. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory())
  1973. << "\n";
  1974. fout << "CMAKE_BINARY_DIR = " <<
  1975. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory())
  1976. << "\n";
  1977. // Output Include paths
  1978. fout << "INCLUDE_FLAGS = ";
  1979. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1980. std::vector<std::string>::iterator i;
  1981. fout << "-I" <<
  1982. this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory()) << " ";
  1983. for(i = includes.begin(); i != includes.end(); ++i)
  1984. {
  1985. std::string include = *i;
  1986. // Don't output a -I for the standard include path "/usr/include".
  1987. // This can cause problems with certain standard library
  1988. // implementations because the wrong headers may be found first.
  1989. if(include != "/usr/include")
  1990. {
  1991. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  1992. }
  1993. }
  1994. fout << m_Makefile->GetDefineFlags();
  1995. fout << "\n\n";
  1996. }
  1997. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1998. {
  1999. const char* root
  2000. = m_Makefile->GetDefinition("CMAKE_ROOT");
  2001. fout << "INSTALL = \"" << root << "/Templates/install-sh\" -c\n";
  2002. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  2003. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  2004. const cmTargets &tgts = m_Makefile->GetTargets();
  2005. fout << "install: $(SUBDIR_INSTALL)\n";
  2006. fout << "\t@echo \"Installing ...\"\n";
  2007. const char* prefix
  2008. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  2009. if (!prefix)
  2010. {
  2011. prefix = "/usr/local";
  2012. }
  2013. for(cmTargets::const_iterator l = tgts.begin();
  2014. l != tgts.end(); l++)
  2015. {
  2016. if (l->second.GetInstallPath() != "")
  2017. {
  2018. // first make the directories for each target
  2019. fout << "\t@if [ ! -d \"$(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  2020. "\" ] ; then \\\n";
  2021. fout << "\t @echo \"Making directory \\\"$(DESTDIR)" << prefix
  2022. << l->second.GetInstallPath() << "\\\" \"; \\\n";
  2023. fout << "\t mkdir -p \"$(DESTDIR)" << prefix << l->second.GetInstallPath()
  2024. << "\"; \\\n";
  2025. fout << "\t chmod 755 \"$(DESTDIR)" << prefix << l->second.GetInstallPath()
  2026. << "\"; \\\n";
  2027. fout << "\t else true; \\\n";
  2028. fout << "\t fi\n";
  2029. std::string fname;
  2030. // now install the target
  2031. switch (l->second.GetType())
  2032. {
  2033. case cmTarget::STATIC_LIBRARY:
  2034. case cmTarget::SHARED_LIBRARY:
  2035. case cmTarget::MODULE_LIBRARY:
  2036. fname = m_LibraryOutputPath;
  2037. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  2038. fout << "\t$(INSTALL_DATA) " << cmSystemTools::ConvertToOutputPath(fname.c_str())
  2039. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2040. break;
  2041. case cmTarget::WIN32_EXECUTABLE:
  2042. case cmTarget::EXECUTABLE:
  2043. fname = m_ExecutableOutputPath;
  2044. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  2045. fout << "\t$(INSTALL_PROGRAM) " << cmSystemTools::ConvertToOutputPath(fname.c_str())
  2046. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2047. break;
  2048. case cmTarget::INSTALL_FILES:
  2049. {
  2050. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2051. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2052. sourcePath += "/";
  2053. binaryPath += "/";
  2054. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2055. std::vector<std::string>::const_iterator i;
  2056. for (i = sf.begin(); i != sf.end(); ++i)
  2057. {
  2058. std::string f = *i;
  2059. if(f.substr(0, sourcePath.length()) == sourcePath)
  2060. {
  2061. f = f.substr(sourcePath.length());
  2062. }
  2063. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2064. {
  2065. f = f.substr(binaryPath.length());
  2066. }
  2067. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2068. // avoid using install-sh to install install-sh
  2069. // does not work on windows....
  2070. if(*i == "install-sh")
  2071. {
  2072. fout << "\tcp ";
  2073. }
  2074. else
  2075. {
  2076. fout << "\t$(INSTALL_DATA) ";
  2077. }
  2078. fout << cmSystemTools::ConvertToOutputPath(i->c_str())
  2079. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2080. }
  2081. }
  2082. break;
  2083. case cmTarget::INSTALL_PROGRAMS:
  2084. {
  2085. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2086. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2087. sourcePath += "/";
  2088. binaryPath += "/";
  2089. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2090. std::vector<std::string>::const_iterator i;
  2091. for (i = sf.begin(); i != sf.end(); ++i)
  2092. {
  2093. std::string f = *i;
  2094. if(f.substr(0, sourcePath.length()) == sourcePath)
  2095. {
  2096. f = f.substr(sourcePath.length());
  2097. }
  2098. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2099. {
  2100. f = f.substr(binaryPath.length());
  2101. }
  2102. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2103. // avoid using install-sh to install install-sh
  2104. // does not work on windows....
  2105. if(*i == "install-sh")
  2106. {
  2107. fout << "\t @cp ";
  2108. }
  2109. else
  2110. {
  2111. fout << "\t @$(INSTALL_PROGRAM) ";
  2112. }
  2113. fout << cmSystemTools::ConvertToOutputPath(i->c_str())
  2114. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2115. }
  2116. }
  2117. break;
  2118. case cmTarget::UTILITY:
  2119. default:
  2120. break;
  2121. }
  2122. }
  2123. }
  2124. }
  2125. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  2126. {
  2127. this->OutputMakeRule(fout,
  2128. "default build rule",
  2129. "all",
  2130. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  2131. 0);
  2132. this->OutputMakeRule(fout,
  2133. "clean generated files",
  2134. "clean",
  2135. "$(SUBDIR_CLEAN)",
  2136. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  2137. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  2138. // collect up all the sources
  2139. std::string allsources;
  2140. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2141. for(std::map<cmStdString,cmTarget>::const_iterator target = targets.begin();
  2142. target != targets.end(); ++target)
  2143. {
  2144. // Iterate over every source for this target.
  2145. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2146. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2147. source != sources.end(); ++source)
  2148. {
  2149. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2150. {
  2151. allsources += " \\\n";
  2152. allsources += cmSystemTools::ConvertToOutputPath((*source)->GetFullPath().c_str());
  2153. }
  2154. }
  2155. }
  2156. this->OutputMakeRule(fout,
  2157. "dependencies.",
  2158. "cmake.depends",
  2159. "$(CMAKE_MAKEFILE_SOURCES)",
  2160. "$(CMAKE_COMMAND) "
  2161. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2162. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  2163. );
  2164. this->OutputMakeRule(fout,
  2165. "dependencies",
  2166. "cmake.check_depends",
  2167. allsources.c_str(),
  2168. "$(CMAKE_COMMAND) "
  2169. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2170. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  2171. );
  2172. this->OutputMakeRule(fout,
  2173. "dependencies",
  2174. "depend",
  2175. "$(SUBDIR_DEPEND)",
  2176. "$(CMAKE_COMMAND) "
  2177. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2178. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2179. this->OutputMakeRule(fout,
  2180. "dependencies",
  2181. "dependlocal",
  2182. 0,
  2183. "$(CMAKE_COMMAND) "
  2184. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2185. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2186. this->OutputMakeRule(fout,
  2187. "CMakeCache.txt",
  2188. "rebuild_cache",
  2189. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2190. "$(CMAKE_COMMAND) "
  2191. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2192. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2193. checkCache += "/cmake.check_cache";
  2194. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  2195. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2196. CMakeCache += "/CMakeCache.txt";
  2197. CMakeCache = cmSystemTools::ConvertToOutputPath(CMakeCache.c_str());
  2198. this->OutputMakeRule(fout,
  2199. "CMakeCache.txt because out-of-date:",
  2200. checkCache.c_str(),
  2201. CMakeCache.c_str(),
  2202. "$(CMAKE_COMMAND) "
  2203. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2204. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2205. // called edit_cache
  2206. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2207. {
  2208. this->OutputMakeRule(fout,
  2209. "edit CMakeCache.txt",
  2210. "edit_cache",
  2211. 0,
  2212. "$(CMAKE_EDIT_COMMAND) "
  2213. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2214. }
  2215. this->OutputMakeRule(fout,
  2216. "CMakeCache.txt",
  2217. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2218. 0,
  2219. "$(CMAKE_COMMAND) "
  2220. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2221. this->OutputMakeRule(fout,
  2222. "Rule to keep make from removing Makefiles "
  2223. "if control-C is hit during a run of cmake.",
  2224. ".PRECIOUS",
  2225. "Makefile cmake.depends",
  2226. 0);
  2227. this->OutputSourceObjectBuildRules(fout);
  2228. // find ctest
  2229. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2230. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2231. ctest += "/";
  2232. ctest += "ctest";
  2233. ctest += cmSystemTools::GetExecutableExtension();
  2234. if(!cmSystemTools::FileExists(ctest.c_str()))
  2235. {
  2236. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2237. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2238. ctest += "/Debug/";
  2239. ctest += "ctest";
  2240. ctest += cmSystemTools::GetExecutableExtension();
  2241. }
  2242. if(!cmSystemTools::FileExists(ctest.c_str()))
  2243. {
  2244. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2245. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2246. ctest += "/Release/";
  2247. ctest += "ctest";
  2248. ctest += cmSystemTools::GetExecutableExtension();
  2249. }
  2250. if (!cmSystemTools::FileExists(ctest.c_str()))
  2251. {
  2252. if ( !m_Makefile->GetDefinition("PROJECT_NAME") ||
  2253. strcmp(m_Makefile->GetDefinition("PROJECT_NAME"), "CMake") )
  2254. {
  2255. return;
  2256. }
  2257. ctest = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2258. ctest += "/ctest";
  2259. }
  2260. fout << "ARGS=\n";
  2261. std::string cmd = this->ConvertToOutputForExisting(ctest.c_str());
  2262. cmd += " $(ARGS)";
  2263. this->OutputMakeRule(fout,
  2264. "tests",
  2265. "test",
  2266. "",
  2267. cmd.c_str());
  2268. }
  2269. void
  2270. cmLocalUnixMakefileGenerator::
  2271. OutputBuildObjectFromSource(std::ostream& fout,
  2272. const char* shortName,
  2273. const cmSourceFile& source,
  2274. const char* extraCompileFlags,
  2275. bool shared)
  2276. {
  2277. // Header files shouldn't have build rules.
  2278. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2279. {
  2280. return;
  2281. }
  2282. std::string comment = "object file";
  2283. std::string objectFile = std::string(shortName) +
  2284. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2285. objectFile = cmSystemTools::ConvertToOutputPath(objectFile.c_str());
  2286. cmSystemTools::FileFormat format =
  2287. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2288. std::vector<std::string> rules;
  2289. std::string flags;
  2290. if(extraCompileFlags)
  2291. {
  2292. flags += extraCompileFlags;
  2293. }
  2294. std::string sourceFile =
  2295. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  2296. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2297. buildType = cmSystemTools::UpperCase(buildType);
  2298. switch(format)
  2299. {
  2300. case cmSystemTools::C_FILE_FORMAT:
  2301. {
  2302. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2303. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  2304. flags += " ";
  2305. if(buildType.size())
  2306. {
  2307. std::string build = "CMAKE_C_FLAGS_";
  2308. build += buildType;
  2309. flags += this->GetSafeDefinition(build.c_str());
  2310. flags += " ";
  2311. }
  2312. if(shared)
  2313. {
  2314. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2315. flags += " ";
  2316. }
  2317. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2318. {
  2319. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2320. flags += " ";
  2321. }
  2322. break;
  2323. }
  2324. case cmSystemTools::CXX_FILE_FORMAT:
  2325. {
  2326. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2327. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2328. flags += " ";
  2329. if(buildType.size())
  2330. {
  2331. std::string build = "CMAKE_CXX_FLAGS_";
  2332. build += buildType;
  2333. flags += this->GetSafeDefinition(build.c_str());
  2334. flags += " ";
  2335. }
  2336. if(shared)
  2337. {
  2338. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2339. flags += " ";
  2340. }
  2341. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2342. {
  2343. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2344. flags += " ";
  2345. }
  2346. break;
  2347. }
  2348. case cmSystemTools::HEADER_FILE_FORMAT:
  2349. return;
  2350. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2351. return;
  2352. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2353. {
  2354. flags = " $(INCLUDE_FLAGS) ";
  2355. // use rc rule here if it is defined
  2356. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2357. if(rule)
  2358. {
  2359. rules.push_back(rule);
  2360. }
  2361. }
  2362. break;
  2363. case cmSystemTools::NO_FILE_FORMAT:
  2364. case cmSystemTools::JAVA_FILE_FORMAT:
  2365. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2366. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2367. case cmSystemTools::MODULE_FILE_FORMAT:
  2368. case cmSystemTools::OBJECT_FILE_FORMAT:
  2369. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2370. cmSystemTools::Error("Unexpected file type ",
  2371. sourceFile.c_str());
  2372. break;
  2373. }
  2374. flags += "$(INCLUDE_FLAGS) ";
  2375. // expand multi-command semi-colon separated lists
  2376. // of commands into separate commands
  2377. std::vector<std::string> commands;
  2378. cmSystemTools::ExpandList(rules, commands);
  2379. for(std::vector<std::string>::iterator i = commands.begin();
  2380. i != commands.end(); ++i)
  2381. {
  2382. this->ExpandRuleVariables(*i,
  2383. 0, // no objects
  2384. 0, // no target
  2385. 0, // no link libs
  2386. sourceFile.c_str(),
  2387. objectFile.c_str(),
  2388. flags.c_str());
  2389. }
  2390. std::vector<std::string> sourceAndDeps;
  2391. sourceAndDeps.push_back(sourceFile);
  2392. // Check for extra object-file dependencies.
  2393. std::vector<std::string> depends;
  2394. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2395. if(additionalDeps)
  2396. {
  2397. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2398. for(std::vector<std::string>::iterator i = depends.begin();
  2399. i != depends.end(); ++i)
  2400. {
  2401. sourceAndDeps.push_back(cmSystemTools::ConvertToOutputPath(i->c_str()));
  2402. }
  2403. }
  2404. this->OutputMakeRule(fout,
  2405. comment.c_str(),
  2406. objectFile.c_str(),
  2407. sourceAndDeps,
  2408. commands);
  2409. }
  2410. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2411. {
  2412. fout << "# Rules to build " << this->GetOutputExtension("")
  2413. << " files from their sources:\n";
  2414. std::set<std::string> rules;
  2415. // Iterate over every target.
  2416. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2417. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2418. target != targets.end(); ++target)
  2419. {
  2420. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2421. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2422. std::string exportsDef = "";
  2423. if(shared)
  2424. {
  2425. std::string export_symbol;
  2426. if (const char* custom_export_name = target->second.GetProperty("DEFINE_SYMBOL"))
  2427. {
  2428. export_symbol = custom_export_name;
  2429. }
  2430. else
  2431. {
  2432. std::string in = target->first + "_EXPORTS";
  2433. export_symbol = cmSystemTools::MakeCindentifier(in.c_str());
  2434. }
  2435. exportsDef = "-D"+ export_symbol;
  2436. }
  2437. // Iterate over every source for this target.
  2438. const std::vector<cmSourceFile*>& sources =
  2439. target->second.GetSourceFiles();
  2440. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2441. source != sources.end(); ++source)
  2442. {
  2443. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  2444. !(*source)->GetCustomCommand())
  2445. {
  2446. std::string shortName;
  2447. std::string sourceName;
  2448. // If the full path to the source file includes this
  2449. // directory, we want to use the relative path for the
  2450. // filename of the object file. Otherwise, we will use just
  2451. // the filename portion.
  2452. if((cmSystemTools::GetFilenamePath(
  2453. (*source)->GetFullPath()).find(
  2454. m_Makefile->GetCurrentDirectory()) == 0)
  2455. || (cmSystemTools::GetFilenamePath(
  2456. (*source)->GetFullPath()).find(
  2457. m_Makefile->GetCurrentOutputDirectory()) == 0))
  2458. {
  2459. sourceName = (*source)->GetSourceName()+"."+
  2460. (*source)->GetSourceExtension();
  2461. shortName = (*source)->GetSourceName();
  2462. // The path may be relative. See if a directory needs to be
  2463. // created for the output file. This is a ugly, and perhaps
  2464. // should be moved elsewhere.
  2465. std::string relPath =
  2466. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2467. if(relPath != "")
  2468. {
  2469. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2470. outPath += "/"+relPath;
  2471. cmSystemTools::MakeDirectory(outPath.c_str());
  2472. }
  2473. }
  2474. else
  2475. {
  2476. sourceName = (*source)->GetFullPath();
  2477. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2478. }
  2479. std::string shortNameWithExt = shortName +
  2480. (*source)->GetSourceExtension();
  2481. // Only output a rule for each .o once.
  2482. std::string compileFlags = exportsDef;
  2483. compileFlags += " ";
  2484. if(rules.find(shortNameWithExt) == rules.end())
  2485. {
  2486. if((*source)->GetProperty("COMPILE_FLAGS"))
  2487. {
  2488. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2489. compileFlags += " ";
  2490. }
  2491. this->OutputBuildObjectFromSource(fout,
  2492. shortName.c_str(),
  2493. *(*source),
  2494. compileFlags.c_str(),
  2495. shared);
  2496. rules.insert(shortNameWithExt);
  2497. }
  2498. }
  2499. }
  2500. }
  2501. }
  2502. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2503. const char* comment,
  2504. const char* target,
  2505. const char* depends,
  2506. const char* command,
  2507. const char* command2,
  2508. const char* command3,
  2509. const char* command4)
  2510. {
  2511. std::vector<std::string> commands;
  2512. if(command)
  2513. {
  2514. commands.push_back(command);
  2515. }
  2516. if(command2)
  2517. {
  2518. commands.push_back(command2);
  2519. }
  2520. if(command3)
  2521. {
  2522. commands.push_back(command3);
  2523. }
  2524. if(command4)
  2525. {
  2526. commands.push_back(command4);
  2527. }
  2528. this->OutputMakeRule(fout, comment, target, depends, commands);
  2529. }
  2530. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2531. const char* comment,
  2532. const char* target,
  2533. const char* depends,
  2534. const std::vector<std::string>& commands)
  2535. {
  2536. std::vector<std::string> depend;
  2537. if(depends)
  2538. {
  2539. depend.push_back(depends);
  2540. }
  2541. this->OutputMakeRule(fout, comment, target, depend, commands);
  2542. }
  2543. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2544. const char* comment,
  2545. const char* target,
  2546. const std::vector<std::string>& depends,
  2547. const std::vector<std::string>& commands)
  2548. {
  2549. if(!target)
  2550. {
  2551. cmSystemTools::Error("no target for OutputMakeRule");
  2552. return;
  2553. }
  2554. std::string replace;
  2555. if(comment)
  2556. {
  2557. replace = comment;
  2558. m_Makefile->ExpandVariablesInString(replace);
  2559. fout << "#---------------------------------------------------------\n";
  2560. fout << "# " << replace;
  2561. fout << "\n#\n";
  2562. }
  2563. fout << "\n";
  2564. replace = target;
  2565. m_Makefile->ExpandVariablesInString(replace);
  2566. fout << cmSystemTools::ConvertToOutputPath(replace.c_str()) << ":";
  2567. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2568. dep != depends.end(); ++dep)
  2569. {
  2570. replace = *dep;
  2571. m_Makefile->ExpandVariablesInString(replace);
  2572. fout << " " << replace.c_str();
  2573. }
  2574. fout << "\n";
  2575. int count = 0;
  2576. for (std::vector<std::string>::const_iterator i = commands.begin();
  2577. i != commands.end(); ++i)
  2578. {
  2579. replace = *i;
  2580. m_Makefile->ExpandVariablesInString(replace);
  2581. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2582. replace[0] != '-' && replace.find("echo") != 0
  2583. && replace.find("$(MAKE)") != 0)
  2584. {
  2585. std::string echostring = "Building ";
  2586. echostring += comment;
  2587. echostring += " ";
  2588. echostring += target;
  2589. echostring += "...";
  2590. // for unix we want to quote the output of echo
  2591. // for nmake and borland, the echo should not be quoted
  2592. if(strcmp(m_GlobalGenerator->GetName(), "Unix Makefiles") == 0)
  2593. {
  2594. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  2595. cmSystemTools::ReplaceString(echostring, " \t", " ");
  2596. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\t@echo \"");
  2597. fout << "\t@echo \"" << echostring.c_str() << "\"\n";
  2598. }
  2599. else
  2600. {
  2601. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\t@echo ");
  2602. fout << "\t@echo " << echostring.c_str() << "\n";
  2603. }
  2604. }
  2605. fout << "\t" << replace.c_str() << "\n";
  2606. count++;
  2607. }
  2608. fout << "\n";
  2609. }
  2610. const char* cmLocalUnixMakefileGenerator::GetSafeDefinition(const char* def)
  2611. {
  2612. const char* ret = m_Makefile->GetDefinition(def);
  2613. if(!ret)
  2614. {
  2615. return "";
  2616. }
  2617. return ret;
  2618. }
  2619. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2620. {
  2621. #ifdef _WIN32
  2622. return cmSystemTools::LowerCase(path);
  2623. #else
  2624. return std::string(path);
  2625. #endif
  2626. }
  2627. std::string
  2628. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* sin, const char* s2in)
  2629. {
  2630. std::string s = sin;
  2631. std::string s2 = s2in;
  2632. std::string unmodified = s;
  2633. unmodified += s2;
  2634. // if there is no restriction on the length of make variables
  2635. // and there are no "." charactors in the string, then return the
  2636. // unmodified combination.
  2637. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2638. {
  2639. return unmodified;
  2640. }
  2641. // see if the variable has been defined before and return
  2642. // the modified version of the variable
  2643. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2644. if(i != m_MakeVariableMap.end())
  2645. {
  2646. return i->second;
  2647. }
  2648. // start with the unmodified variable
  2649. std::string ret = unmodified;
  2650. // if this there is no value for m_MakefileVariableSize then
  2651. // the string must have bad characters in it
  2652. if(!m_MakefileVariableSize)
  2653. {
  2654. cmSystemTools::ReplaceString(ret, ".", "_");
  2655. int ni = 0;
  2656. char buffer[5];
  2657. // make sure the _ version is not already used, if
  2658. // it is used then add number to the end of the variable
  2659. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2660. {
  2661. ++ni;
  2662. sprintf(buffer, "%04d", ni);
  2663. ret = unmodified + buffer;
  2664. }
  2665. m_ShortMakeVariableMap[ret] = "1";
  2666. m_MakeVariableMap[unmodified] = ret;
  2667. return ret;
  2668. }
  2669. // if the string is greater the 32 chars it is an invalid vairable name
  2670. // for borland make
  2671. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2672. {
  2673. int keep = m_MakefileVariableSize - 8;
  2674. int size = keep + 3;
  2675. std::string str1 = s;
  2676. std::string str2 = s2;
  2677. // we must shorten the combined string by 4 charactors
  2678. // keep no more than 24 charactors from the second string
  2679. if(static_cast<int>(str2.size()) > keep)
  2680. {
  2681. str2 = str2.substr(0, keep);
  2682. }
  2683. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2684. {
  2685. str1 = str1.substr(0, size - str2.size());
  2686. }
  2687. char buffer[5];
  2688. int ni = 0;
  2689. sprintf(buffer, "%04d", ni);
  2690. ret = str1 + str2 + buffer;
  2691. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2692. {
  2693. ++ni;
  2694. sprintf(buffer, "%04d", ni);
  2695. ret = str1 + str2 + buffer;
  2696. }
  2697. if(ni == 1000)
  2698. {
  2699. cmSystemTools::Error("Borland makefile variable length too long");
  2700. return unmodified;
  2701. }
  2702. // once an unused variable is found
  2703. m_ShortMakeVariableMap[ret] = "1";
  2704. }
  2705. // always make an entry into the unmodified to variable map
  2706. m_MakeVariableMap[unmodified] = ret;
  2707. return ret;
  2708. }