cmLocalUnixMakefileGenerator.cxx 106 KB

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