cmLocalUnixMakefileGenerator.cxx 109 KB

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