cmLocalUnixMakefileGenerator.cxx 106 KB

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