cmLocalUnixMakefileGenerator.cxx 106 KB

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