1
0

cmLocalUnixMakefileGenerator.cxx 108 KB

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