cmLocalUnixMakefileGenerator.cxx 107 KB

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