1
0

cmLocalUnixMakefileGenerator.cxx 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  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. // Add a target with the canonical name (no prefix, suffix or path).
  1191. this->OutputMakeRule(fout, comment, name, tgt.c_str(), 0);
  1192. }
  1193. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  1194. const char* name,
  1195. const cmTarget &t)
  1196. {
  1197. const char* createRule;
  1198. if(t.HasCxx())
  1199. {
  1200. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  1201. }
  1202. else
  1203. {
  1204. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  1205. }
  1206. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1207. buildType = cmSystemTools::UpperCase(buildType);
  1208. std::string linkFlags = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1209. linkFlags += " ";
  1210. if(buildType.size())
  1211. {
  1212. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  1213. build += buildType;
  1214. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1215. linkFlags += " ";
  1216. }
  1217. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1218. {
  1219. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  1220. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1221. i != sources.end(); ++i)
  1222. {
  1223. if((*i)->GetSourceExtension() == "def")
  1224. {
  1225. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1226. linkFlags += this->ConvertToRelativeOutputPath((*i)->GetFullPath().c_str());
  1227. linkFlags += " ";
  1228. }
  1229. }
  1230. }
  1231. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1232. if(targetLinkFlags)
  1233. {
  1234. linkFlags += targetLinkFlags;
  1235. linkFlags += " ";
  1236. }
  1237. this->OutputLibraryRule(fout, name, t,
  1238. createRule,
  1239. "shared library",
  1240. linkFlags.c_str());
  1241. }
  1242. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  1243. const char* name,
  1244. const cmTarget &t)
  1245. {
  1246. const char* createRule;
  1247. if(t.HasCxx())
  1248. {
  1249. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  1250. }
  1251. else
  1252. {
  1253. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  1254. }
  1255. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1256. buildType = cmSystemTools::UpperCase(buildType);
  1257. std::string linkFlags = m_Makefile->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1258. linkFlags += " ";
  1259. if(buildType.size())
  1260. {
  1261. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  1262. build += buildType;
  1263. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1264. linkFlags += " ";
  1265. }
  1266. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1267. if(targetLinkFlags)
  1268. {
  1269. linkFlags += targetLinkFlags;
  1270. linkFlags += " ";
  1271. }
  1272. this->OutputLibraryRule(fout, name, t,
  1273. createRule,
  1274. "shared module",
  1275. linkFlags.c_str());
  1276. }
  1277. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  1278. const char* name,
  1279. const cmTarget &t)
  1280. {
  1281. const char* createRule;
  1282. if(t.HasCxx())
  1283. {
  1284. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  1285. }
  1286. else
  1287. {
  1288. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  1289. }
  1290. std::string linkFlags;
  1291. const char* targetLinkFlags = t.GetProperty("STATIC_LIBRARY_FLAGS");
  1292. if(targetLinkFlags)
  1293. {
  1294. linkFlags += targetLinkFlags;
  1295. linkFlags += " ";
  1296. }
  1297. this->OutputLibraryRule(fout, name, t,
  1298. createRule,
  1299. "static library",
  1300. linkFlags.c_str());
  1301. }
  1302. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  1303. const char* name,
  1304. const cmTarget &t)
  1305. {
  1306. std::string linkFlags;
  1307. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1308. buildType = cmSystemTools::UpperCase(buildType);
  1309. std::string flags;
  1310. // Construct the full path to the executable that will be generated.
  1311. std::string target = m_ExecutableOutputPath;
  1312. if(target.length() == 0)
  1313. {
  1314. target = m_Makefile->GetCurrentOutputDirectory();
  1315. if(target.size() && target[target.size()-1] != '/')
  1316. {
  1317. target += "/";
  1318. }
  1319. }
  1320. #ifdef __APPLE__
  1321. if ( t.GetPropertyAsBool("MACOSX_BUNDLE") )
  1322. {
  1323. // Make bundle directories
  1324. target += name;
  1325. target += ".app/Contents/MacOS/";
  1326. }
  1327. #endif
  1328. target += name;
  1329. target += cmSystemTools::GetExecutableExtension();
  1330. target = this->ConvertToRelativeOutputPath(target.c_str());
  1331. bool needsLocalTarget = false;
  1332. if(m_UseRelativePaths)
  1333. {
  1334. cmSystemTools::ConvertToUnixSlashes(target);
  1335. std::string tgt = this->ConvertToMakeTarget(target.c_str());
  1336. if(tgt.find('/', 0) != tgt.npos)
  1337. {
  1338. needsLocalTarget = true;
  1339. }
  1340. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  1341. }
  1342. else
  1343. {
  1344. needsLocalTarget = true;
  1345. }
  1346. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS") + ") ";
  1347. std::string depend = "$(";
  1348. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  1349. + ") $(" + this->CreateMakeVariable(name, "_EXTERNAL_OBJS")
  1350. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1351. std::vector<std::string> rules;
  1352. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1353. linkFlags += " ";
  1354. if(buildType.size())
  1355. {
  1356. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1357. build += buildType;
  1358. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1359. linkFlags += " ";
  1360. }
  1361. if(t.HasCxx())
  1362. {
  1363. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  1364. flags += m_Makefile->GetSafeDefinition("CMAKE_CXX_FLAGS");
  1365. flags += " ";
  1366. flags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  1367. flags += " ";
  1368. }
  1369. else
  1370. {
  1371. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  1372. flags += m_Makefile->GetSafeDefinition("CMAKE_C_FLAGS");
  1373. flags += " ";
  1374. flags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  1375. flags += " ";
  1376. }
  1377. cmOStringStream linklibs;
  1378. this->OutputLinkLibraries(linklibs, 0, t);
  1379. std::string comment = "executable";
  1380. std::vector<std::string> commands;
  1381. std::string customCommands = this->CreatePreBuildRules(t, name);
  1382. if(customCommands.size() > 0)
  1383. {
  1384. commands.push_back(customCommands.c_str());
  1385. }
  1386. customCommands = this->CreatePreLinkRules(t, name);
  1387. if(customCommands.size() > 0)
  1388. {
  1389. commands.push_back(customCommands.c_str());
  1390. }
  1391. cmSystemTools::ExpandList(rules, commands);
  1392. customCommands = this->CreatePostBuildRules(t, name);
  1393. if(customCommands.size() > 0)
  1394. {
  1395. commands.push_back(customCommands.c_str());
  1396. }
  1397. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1398. {
  1399. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  1400. linkFlags += " ";
  1401. }
  1402. if ( t.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1403. {
  1404. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1405. linkFlags += " ";
  1406. }
  1407. else
  1408. {
  1409. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1410. linkFlags += " ";
  1411. }
  1412. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1413. if(targetLinkFlags)
  1414. {
  1415. linkFlags += targetLinkFlags;
  1416. linkFlags += " ";
  1417. }
  1418. for(std::vector<std::string>::iterator i = commands.begin();
  1419. i != commands.end(); ++i)
  1420. {
  1421. this->ExpandRuleVariables(*i,
  1422. objs.c_str(),
  1423. target.c_str(),
  1424. linklibs.str().c_str(),
  1425. 0,
  1426. 0,
  1427. flags.c_str(),
  1428. 0,
  1429. 0,
  1430. 0,
  1431. linkFlags.c_str());
  1432. }
  1433. this->OutputMakeRule(fout,
  1434. comment.c_str(),
  1435. target.c_str(),
  1436. depend.c_str(),
  1437. commands);
  1438. // If there is no executable output path, add a rule with the
  1439. // relative path to the executable. This is necessary for
  1440. // try-compile to work in this case.
  1441. if(needsLocalTarget)
  1442. {
  1443. depend = target;
  1444. target = name;
  1445. target += cmSystemTools::GetExecutableExtension();
  1446. target = this->ConvertToRelativeOutputPath(target.c_str());
  1447. commands.resize(0);
  1448. this->OutputMakeRule(fout,
  1449. comment.c_str(),
  1450. target.c_str(),
  1451. depend.c_str(),
  1452. commands);
  1453. }
  1454. // Add a target with the canonical name (no prefix, suffix or path).
  1455. // Note that on some platforms the "local target" added above will
  1456. // actually be the canonical name and will have set "target"
  1457. // correctly. Do not duplicate this target.
  1458. if(target != name)
  1459. {
  1460. this->OutputMakeRule(fout, comment.c_str(), name, target.c_str(), 0);
  1461. }
  1462. }
  1463. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1464. const char* name,
  1465. const cmTarget &t)
  1466. {
  1467. const char* cc = 0;
  1468. std::string customCommands = this->CreatePreBuildRules(t, name);
  1469. std::string customCommands2 = this->CreatePreLinkRules(t, name);
  1470. if(customCommands2.size() > 0)
  1471. {
  1472. customCommands += customCommands2;
  1473. }
  1474. customCommands2 = this->CreatePostBuildRules(t, name);
  1475. if(customCommands2.size() > 0)
  1476. {
  1477. customCommands += customCommands2;
  1478. }
  1479. if(customCommands.size() > 0)
  1480. {
  1481. cc = customCommands.c_str();
  1482. }
  1483. std::string comment = "Utility";
  1484. std::string depends;
  1485. depends = "$(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1486. std::string replaceVars;
  1487. const std::vector<cmCustomCommand> &ccs = t.GetPostBuildCommands();
  1488. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1489. i != ccs.end(); ++i)
  1490. {
  1491. const std::vector<std::string> & dep = i->GetDepends();
  1492. for(std::vector<std::string>::const_iterator d = dep.begin();
  1493. d != dep.end(); ++d)
  1494. {
  1495. depends += " \\\n";
  1496. replaceVars = *d;
  1497. m_Makefile->ExpandVariablesInString(replaceVars);
  1498. depends += this->ConvertToMakeTarget(this->ConvertToRelativeOutputPath(replaceVars.c_str()).c_str());
  1499. }
  1500. }
  1501. this->OutputMakeRule(fout, comment.c_str(), name,
  1502. depends.c_str(), cc);
  1503. }
  1504. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1505. {
  1506. // for each target
  1507. const cmTargets &tgts = m_Makefile->GetTargets();
  1508. for(cmTargets::const_iterator l = tgts.begin();
  1509. l != tgts.end(); l++)
  1510. {
  1511. switch(l->second.GetType())
  1512. {
  1513. case cmTarget::STATIC_LIBRARY:
  1514. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1515. break;
  1516. case cmTarget::SHARED_LIBRARY:
  1517. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1518. break;
  1519. case cmTarget::MODULE_LIBRARY:
  1520. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1521. break;
  1522. case cmTarget::EXECUTABLE:
  1523. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1524. break;
  1525. case cmTarget::UTILITY:
  1526. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1527. break;
  1528. // This is handled by the OutputCustomRules method
  1529. case cmTarget::INSTALL_FILES:
  1530. // This is handled by the OutputInstallRules method
  1531. case cmTarget::INSTALL_PROGRAMS:
  1532. // This is handled by the OutputInstallRules method
  1533. break;
  1534. }
  1535. }
  1536. }
  1537. // For each target that is an executable or shared library, generate
  1538. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1539. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1540. {
  1541. // Build a set of libraries that will be linked into any target in
  1542. // this directory.
  1543. std::set<std::string> used;
  1544. // for each target
  1545. const cmTargets &tgts = m_Makefile->GetTargets();
  1546. for(cmTargets::const_iterator l = tgts.begin();
  1547. l != tgts.end(); l++)
  1548. {
  1549. // Each dependency should only be emitted once per target.
  1550. std::set<std::string> emitted;
  1551. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1552. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1553. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1554. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1555. || (l->second.GetType() == cmTarget::UTILITY))
  1556. {
  1557. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1558. // A library should not depend on itself!
  1559. emitted.insert(l->first);
  1560. // for static libraries do not depend on other libraries
  1561. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1562. {
  1563. // Now, look at all link libraries specific to this target.
  1564. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1565. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1566. lib != tlibs.end(); ++lib)
  1567. {
  1568. // Record that this library was used.
  1569. used.insert(lib->first);
  1570. // Don't emit the same library twice for this target.
  1571. if(emitted.insert(lib->first).second)
  1572. {
  1573. // Output this dependency.
  1574. this->OutputLibDepend(fout, lib->first.c_str());
  1575. }
  1576. }
  1577. }
  1578. // for all targets depend on utilities
  1579. // Now, look at all utilities specific to this target.
  1580. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1581. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1582. util != tutils.end(); ++util)
  1583. {
  1584. // Record that this utility was used.
  1585. used.insert(*util);
  1586. // Don't emit the same utility twice for this target.
  1587. if(emitted.insert(*util).second)
  1588. {
  1589. // Output this dependency.
  1590. std::string utilType = *util + "_LIBRARY_TYPE";
  1591. const char* libType = m_Makefile->GetDefinition(utilType.c_str());
  1592. if ( libType )
  1593. {
  1594. this->OutputLibDepend(fout, util->c_str());
  1595. }
  1596. else
  1597. {
  1598. this->OutputExeDepend(fout, util->c_str());
  1599. }
  1600. }
  1601. }
  1602. fout << "\n";
  1603. }
  1604. }
  1605. fout << "\n";
  1606. // Loop over the libraries used and make sure there is a rule to
  1607. // build them in this makefile. If the library is in another
  1608. // directory, add a rule to jump to that directory and make sure it
  1609. // exists.
  1610. for(std::set<std::string>::const_iterator lib = used.begin();
  1611. lib != used.end(); ++lib)
  1612. {
  1613. // loop over the list of directories that the libraries might
  1614. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1615. // be stored in the cache
  1616. std::string libPath = *lib + "_CMAKE_PATH";
  1617. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1618. // if cache and not the current directory add a rule, to
  1619. // jump into the directory and build for the first time
  1620. if(cacheValue && *cacheValue &&
  1621. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1622. {
  1623. // add the correct extension
  1624. std::string ltname = *lib+"_LIBRARY_TYPE";
  1625. const char* libType
  1626. = m_Makefile->GetDefinition(ltname.c_str());
  1627. // if it was a library..
  1628. if (libType)
  1629. {
  1630. std::string library;
  1631. std::string libpath = cacheValue;
  1632. if(libType && std::string(libType) == "SHARED")
  1633. {
  1634. library = m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1635. library += *lib;
  1636. library += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1637. }
  1638. else if(libType && std::string(libType) == "MODULE")
  1639. {
  1640. library = m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1641. library += *lib;
  1642. library += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1643. }
  1644. else if(libType && std::string(libType) == "STATIC")
  1645. {
  1646. library = m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1647. library += *lib;
  1648. library += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1649. }
  1650. else
  1651. {
  1652. cmSystemTools::Error("Unknown library type!");
  1653. return;
  1654. }
  1655. if(m_LibraryOutputPath.size())
  1656. {
  1657. libpath = m_LibraryOutputPath;
  1658. }
  1659. else
  1660. {
  1661. libpath += "/";
  1662. }
  1663. libpath += library;
  1664. // put out a rule to build the library if it does not exist
  1665. this->OutputBuildTargetInDir(fout,
  1666. cacheValue,
  1667. library.c_str(),
  1668. libpath.c_str());
  1669. }
  1670. // something other than a library...
  1671. else
  1672. {
  1673. std::string exepath = cacheValue;
  1674. if(m_ExecutableOutputPath.size())
  1675. {
  1676. exepath = m_ExecutableOutputPath;
  1677. }
  1678. else
  1679. {
  1680. exepath += "/";
  1681. }
  1682. std::string fullName = lib->c_str();
  1683. fullName += cmSystemTools::GetExecutableExtension();
  1684. exepath += fullName;
  1685. this->OutputBuildTargetInDir(fout,
  1686. cacheValue,
  1687. fullName.c_str(),
  1688. exepath.c_str());
  1689. }
  1690. }
  1691. }
  1692. }
  1693. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1694. const char* path,
  1695. const char* library,
  1696. const char* fullpath)
  1697. {
  1698. std::string jumpBack;
  1699. if(m_UseRelativePaths)
  1700. {
  1701. cmSystemTools::Message("using relative paths??");
  1702. jumpBack = cmSystemTools::RelativePath(cmSystemTools::GetProgramPath(path).c_str(),
  1703. m_Makefile->GetCurrentOutputDirectory());
  1704. }
  1705. else
  1706. {
  1707. jumpBack = m_Makefile->GetCurrentOutputDirectory();
  1708. }
  1709. jumpBack = this->ConvertToOutputForExisting(jumpBack.c_str());
  1710. std::string wpath = this->ConvertToOutputForExisting(path);
  1711. std::string wfullpath = this->ConvertToRelativeOutputPath(fullpath);
  1712. fout << wfullpath
  1713. << ":\n\tcd " << wpath << "\n"
  1714. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1715. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1716. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1717. << "\t$(MAKE) $(MAKESILENT) " << library
  1718. << "\n\tcd " << jumpBack << "\n";
  1719. }
  1720. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1721. const char* path,
  1722. const char* library,
  1723. const char* fullpath)
  1724. {
  1725. if(m_WindowsShell)
  1726. {
  1727. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath);
  1728. return;
  1729. }
  1730. fout << this->ConvertToOutputForExisting(fullpath)
  1731. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1732. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1733. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1734. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1735. << "; $(MAKE) $(MAKESILENT) "
  1736. << library << "\n\n";
  1737. }
  1738. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1739. {
  1740. if (strcmp(path1, path2) == 0)
  1741. {
  1742. return true;
  1743. }
  1744. #if defined(_WIN32) || defined(__APPLE__)
  1745. return
  1746. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1747. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1748. #else
  1749. return false;
  1750. #endif
  1751. }
  1752. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1753. const char* name)
  1754. {
  1755. std::string libPath = name;
  1756. libPath += "_CMAKE_PATH";
  1757. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1758. if( cacheValue && *cacheValue )
  1759. {
  1760. // if there is a cache value, then this is a library that cmake
  1761. // knows how to build, so we can depend on it
  1762. std::string libpath;
  1763. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1764. {
  1765. // if the library is not in the current directory, then get the full
  1766. // path to it
  1767. if(m_LibraryOutputPath.size())
  1768. {
  1769. libpath = m_LibraryOutputPath;
  1770. }
  1771. else
  1772. {
  1773. libpath = cacheValue;
  1774. libpath += "/";
  1775. }
  1776. }
  1777. else
  1778. {
  1779. // library is in current Makefile so use lib as a prefix
  1780. libpath = m_LibraryOutputPath;
  1781. }
  1782. // add the correct extension
  1783. std::string ltname = name;
  1784. ltname += "_LIBRARY_TYPE";
  1785. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1786. if(libType && std::string(libType) == "SHARED")
  1787. {
  1788. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1789. libpath += name;
  1790. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1791. }
  1792. else if (libType && std::string(libType) == "MODULE")
  1793. {
  1794. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1795. libpath += name;
  1796. libpath += m_Makefile->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1797. }
  1798. else if (libType && std::string(libType) == "STATIC")
  1799. {
  1800. libpath += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1801. libpath += name;
  1802. libpath += m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1803. }
  1804. fout << this->ConvertToRelativeOutputPath(libpath.c_str()) << " ";
  1805. }
  1806. else
  1807. {
  1808. if(cmSystemTools::FileExists(name))
  1809. {
  1810. std::string nameStr = name;
  1811. // if it starts with / or \ or ?:/ or ?:\ then it must be a full path
  1812. if( (nameStr.size() && (nameStr[0] == '/' || nameStr[0] == '\\')) ||
  1813. ((nameStr.size() > 3) && (nameStr[1] == ':') && (nameStr[2] == '/' || nameStr[2] == '\\')))
  1814. {
  1815. fout << this->ConvertToRelativeOutputPath(name) << " ";
  1816. }
  1817. }
  1818. }
  1819. }
  1820. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1821. const char* name)
  1822. {
  1823. std::string exePath = name;
  1824. exePath += "_CMAKE_PATH";
  1825. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1826. if( cacheValue && *cacheValue )
  1827. {
  1828. // if there is a cache value, then this is a executable/utility that cmake
  1829. // knows how to build, so we can depend on it
  1830. std::string exepath;
  1831. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1832. {
  1833. // if the exe/utility is not in the current directory, then get the full
  1834. // path to it
  1835. if(m_ExecutableOutputPath.size())
  1836. {
  1837. exepath = m_ExecutableOutputPath;
  1838. }
  1839. else
  1840. {
  1841. exepath = cacheValue;
  1842. exepath += "/";
  1843. }
  1844. }
  1845. else
  1846. {
  1847. // library is in current Makefile
  1848. exepath = m_ExecutableOutputPath;
  1849. }
  1850. // add the library name
  1851. exepath += name;
  1852. // add the correct extension
  1853. exepath += cmSystemTools::GetExecutableExtension();
  1854. fout << this->ConvertToRelativeOutputPath(exepath.c_str()) << " ";
  1855. }
  1856. // if it isn't in the cache, it might still be a utility target
  1857. // so check for that
  1858. else
  1859. {
  1860. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1861. if (targets.find(name) != targets.end())
  1862. {
  1863. fout << name << " ";
  1864. }
  1865. }
  1866. }
  1867. // fix up names of directories so they can be used
  1868. // as targets in makefiles.
  1869. inline std::string FixDirectoryName(const char* dir)
  1870. {
  1871. std::string s = dir;
  1872. // replace ../ with 3 under bars
  1873. size_t pos = s.find("../");
  1874. if(pos != std::string::npos)
  1875. {
  1876. s.replace(pos, 3, "___");
  1877. }
  1878. // replace / directory separators with a single under bar
  1879. pos = s.find("/");
  1880. while(pos != std::string::npos)
  1881. {
  1882. s.replace(pos, 1, "_");
  1883. pos = s.find("/");
  1884. }
  1885. return s;
  1886. }
  1887. void cmLocalUnixMakefileGenerator::
  1888. BuildInSubDirectoryWindows(std::ostream& fout,
  1889. const char* directory,
  1890. const char* target1,
  1891. const char* target2,
  1892. bool silent)
  1893. {
  1894. std::string dir;
  1895. if(target1)
  1896. {
  1897. dir = this->ConvertToOutputForExisting(directory);
  1898. if(dir[0] == '\"')
  1899. {
  1900. dir = dir.substr(1, dir.size()-2);
  1901. }
  1902. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1903. << " "
  1904. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1905. if (!silent)
  1906. {
  1907. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1908. }
  1909. fout << "\tcd " << dir << "\n"
  1910. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1911. }
  1912. if(target2)
  1913. {
  1914. if (!silent)
  1915. {
  1916. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1917. }
  1918. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1919. }
  1920. std::string currentDir;
  1921. if(m_UseRelativePaths)
  1922. {
  1923. currentDir = dir;
  1924. cmSystemTools::ConvertToUnixSlashes(currentDir);
  1925. std::string cdback = "..";
  1926. unsigned int i = 0;
  1927. if(currentDir.size() > 2 && currentDir[0] == '.' && currentDir[1] == '/')
  1928. {
  1929. // start past ./ if it starts with ./
  1930. i = 2;
  1931. }
  1932. for(; i < currentDir.size(); ++i)
  1933. {
  1934. if(currentDir[i] == '/')
  1935. {
  1936. cdback += "/..";
  1937. }
  1938. }
  1939. fout << "\tcd " << this->ConvertToOutputForExisting(cdback.c_str()) << "\n\n";
  1940. }
  1941. else
  1942. {
  1943. currentDir = m_Makefile->GetCurrentOutputDirectory();
  1944. fout << "\tcd " << this->ConvertToOutputForExisting(currentDir.c_str()) << "\n\n";
  1945. }
  1946. }
  1947. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1948. const char* dir,
  1949. const char* target1,
  1950. const char* target2,
  1951. bool silent)
  1952. {
  1953. if(m_WindowsShell)
  1954. {
  1955. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1956. return;
  1957. }
  1958. std::string directory = this->ConvertToRelativeOutputPath(dir);
  1959. if(target1)
  1960. {
  1961. fout << "\t@if test ! -d " << directory
  1962. << "; then $(MAKE) rebuild_cache; fi\n";
  1963. if (!silent)
  1964. {
  1965. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1966. }
  1967. fout << "\t@cd " << directory
  1968. << "; $(MAKE) " << target1 << "\n";
  1969. }
  1970. if(target2)
  1971. {
  1972. if (!silent)
  1973. {
  1974. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1975. }
  1976. fout << "\t@cd " << directory
  1977. << "; $(MAKE) " << target2 << "\n";
  1978. }
  1979. fout << "\n";
  1980. }
  1981. void
  1982. cmLocalUnixMakefileGenerator::
  1983. OutputSubDirectoryVars(std::ostream& fout,
  1984. const char* var,
  1985. const char* target,
  1986. const char* target1,
  1987. const char* target2,
  1988. const char* depend,
  1989. const std::vector<std::pair<cmStdString, bool> >& SubDirectories,
  1990. bool silent, int order)
  1991. {
  1992. if(!depend)
  1993. {
  1994. depend = "";
  1995. }
  1996. if( SubDirectories.size() == 0)
  1997. {
  1998. return;
  1999. }
  2000. fout << "# Variable for making " << target << " in subdirectories.\n";
  2001. fout << var << " = ";
  2002. unsigned int ii;
  2003. // make sure all the pre-order subdirectories are fist
  2004. // other than that keep the same order that the user specified
  2005. std::vector<std::pair<cmStdString, bool> > orderedDirs;
  2006. // collect pre-order first
  2007. for(ii =0; ii < SubDirectories.size(); ii++)
  2008. {
  2009. if(m_Makefile->IsDirectoryPreOrder(SubDirectories[ii].first.c_str()))
  2010. {
  2011. orderedDirs.push_back(SubDirectories[ii]);
  2012. }
  2013. }
  2014. // now collect post order dirs
  2015. for(ii =0; ii < SubDirectories.size(); ii++)
  2016. {
  2017. if(!m_Makefile->IsDirectoryPreOrder(SubDirectories[ii].first.c_str()))
  2018. {
  2019. orderedDirs.push_back(SubDirectories[ii]);
  2020. }
  2021. }
  2022. for(ii =0; ii < orderedDirs.size(); ii++)
  2023. {
  2024. if(!orderedDirs[ii].second)
  2025. {
  2026. continue;
  2027. }
  2028. if(order == 1 && m_Makefile->IsDirectoryPreOrder(orderedDirs[ii].first.c_str()))
  2029. {
  2030. continue;
  2031. }
  2032. if(order == 2 && !m_Makefile->IsDirectoryPreOrder(orderedDirs[ii].first.c_str()))
  2033. {
  2034. continue;
  2035. }
  2036. fout << " \\\n";
  2037. std::string subdir = FixDirectoryName(orderedDirs[ii].first.c_str());
  2038. fout << target << "_" << subdir.c_str();
  2039. }
  2040. fout << " \n\n";
  2041. fout << "# Targets for making " << target << " in subdirectories.\n";
  2042. std::string last = "";
  2043. for(unsigned int cc =0; cc < orderedDirs.size(); cc++)
  2044. {
  2045. if(!orderedDirs[cc].second)
  2046. {
  2047. continue;
  2048. }
  2049. std::string subdir = FixDirectoryName(orderedDirs[cc].first.c_str());
  2050. if(order == 1 && m_Makefile->IsDirectoryPreOrder(orderedDirs[cc].first.c_str()))
  2051. {
  2052. last = subdir;
  2053. continue;
  2054. }
  2055. if(order == 2 && !m_Makefile->IsDirectoryPreOrder(orderedDirs[cc].first.c_str()))
  2056. {
  2057. last = subdir;
  2058. continue;
  2059. }
  2060. fout << target << "_" << subdir.c_str() << ": " << depend;
  2061. // Make each subdirectory depend on previous one. This forces
  2062. // parallel builds (make -j 2) to build in same order as a single
  2063. // threaded build to avoid dependency problems.
  2064. if(cc > 0)
  2065. {
  2066. fout << " " << target << "_" << last.c_str();
  2067. }
  2068. fout << "\n";
  2069. last = subdir;
  2070. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  2071. dir += "/";
  2072. dir += orderedDirs[cc].first;
  2073. this->BuildInSubDirectory(fout, dir.c_str(),
  2074. target1, target2, silent);
  2075. }
  2076. fout << "\n\n";
  2077. }
  2078. // output rules for decending into sub directories
  2079. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  2080. {
  2081. // Output Sub directory build rules
  2082. const std::vector<std::pair<cmStdString, bool> >& SubDirectories
  2083. = m_Makefile->GetSubDirectories();
  2084. if( SubDirectories.size() == 0)
  2085. {
  2086. return;
  2087. }
  2088. this->OutputSubDirectoryVars(fout,
  2089. "SUBDIR_BUILD",
  2090. "default_target",
  2091. "default_target",
  2092. 0, "$(TARGETS)",
  2093. SubDirectories,
  2094. false, 1);
  2095. this->OutputSubDirectoryVars(fout,
  2096. "SUBDIR_PREORDER_BUILD",
  2097. "default_target",
  2098. "default_target",
  2099. 0, 0,
  2100. SubDirectories,
  2101. false, 2);
  2102. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  2103. "clean",
  2104. 0, 0,
  2105. SubDirectories);
  2106. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  2107. "depend",
  2108. 0, 0,
  2109. SubDirectories);
  2110. }
  2111. // Output the depend information for all the classes
  2112. // in the makefile. These would have been generated
  2113. // by the class cmMakeDepend GenerateMakefile
  2114. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  2115. {
  2116. bool ret = false;
  2117. // Iterate over every target.
  2118. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2119. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2120. target != targets.end(); ++target)
  2121. {
  2122. // Iterate over every source for this target.
  2123. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2124. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2125. source != sources.end(); ++source)
  2126. {
  2127. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2128. {
  2129. if(!(*source)->GetDepends().empty())
  2130. {
  2131. // Iterate through all the dependencies for this source.
  2132. for(std::vector<std::string>::const_iterator dep =
  2133. (*source)->GetDepends().begin();
  2134. dep != (*source)->GetDepends().end(); ++dep)
  2135. {
  2136. std::string s = (*source)->GetSourceName();
  2137. s += this->GetOutputExtension((*source)->GetSourceExtension().c_str());
  2138. fout << this->ConvertToRelativeOutputPath(s.c_str()) << " : "
  2139. << this->ConvertToRelativeOutputPath(dep->c_str()) << "\n";
  2140. ret = true;
  2141. }
  2142. fout << "\n\n";
  2143. }
  2144. }
  2145. }
  2146. }
  2147. return ret;
  2148. }
  2149. // Output the depend information for all the classes
  2150. // in the makefile. These would have been generated
  2151. // by the class cmMakeDepend GenerateMakefile
  2152. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  2153. {
  2154. std::set<std::string> emittedLowerPath;
  2155. std::set<std::string> emitted;
  2156. // Iterate over every target.
  2157. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2158. fout << "# Suppresses display of executed commands\n";
  2159. fout << ".SILENT:\n";
  2160. fout << "# disable some common implicit rules to speed things up\n";
  2161. fout << ".SUFFIXES:\n";
  2162. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  2163. this->OutputMakeVariables(fout);
  2164. fout << "default:\n";
  2165. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  2166. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  2167. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2168. target != targets.end(); ++target)
  2169. {
  2170. // Iterate over every source for this target.
  2171. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2172. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2173. source != sources.end(); ++source)
  2174. {
  2175. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2176. {
  2177. if(!(*source)->GetDepends().empty())
  2178. {
  2179. for(std::vector<std::string>::const_iterator dep =
  2180. (*source)->GetDepends().begin();
  2181. dep != (*source)->GetDepends().end(); ++dep)
  2182. {
  2183. // do not call CollapseFullPath on dep here, because it already
  2184. // has been done because m_FullPath on cmDependInformation
  2185. // always is it called. If it is called here, network builds are
  2186. // very slow because of the number of stats
  2187. std::string dependfile = this->ConvertToRelativeOutputPath(dep->c_str());
  2188. // use the lower path function to create uniqe names
  2189. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  2190. if(emittedLowerPath.insert(lowerpath).second)
  2191. {
  2192. emitted.insert(dependfile);
  2193. fout << "all: " << dependfile << "\n";
  2194. }
  2195. }
  2196. }
  2197. }
  2198. }
  2199. }
  2200. fout << "\n\n# if any of these files changes run make dependlocal\n";
  2201. std::set<std::string>::iterator i;
  2202. for(i = emitted.begin(); i != emitted.end(); ++i)
  2203. {
  2204. fout << "cmake.depends: " << *i << "\n";
  2205. }
  2206. fout << "cmake.depends: \n"
  2207. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  2208. fout << "\n\n";
  2209. fout << "# if a .h file is removed then run make dependlocal\n\n";
  2210. for(std::set<std::string>::iterator it = emitted.begin();
  2211. it != emitted.end(); ++it)
  2212. {
  2213. fout << *it << ":\n"
  2214. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  2215. }
  2216. }
  2217. // Output each custom rule in the following format:
  2218. // output: source depends...
  2219. // (tab) command...
  2220. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  2221. {
  2222. // we cannot provide multiple rules for a single output
  2223. // so we will keep track of outputs to make sure we don't write
  2224. // two rules. First found wins
  2225. std::set<std::string> processedOutputs;
  2226. // first output all custom rules
  2227. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  2228. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  2229. i != sources.end(); ++i)
  2230. {
  2231. if ((*i)->GetCustomCommand())
  2232. {
  2233. cmCustomCommand *c = (*i)->GetCustomCommand();
  2234. // escape spaces and convert to native slashes path for
  2235. // the command
  2236. std::string comment = c->GetComment();
  2237. std::string command = c->GetCommand();
  2238. cmSystemTools::ReplaceString(command, "/./", "/");
  2239. command = this->ConvertToRelativeOutputPath(command.c_str());
  2240. command += " ";
  2241. // now add the arguments
  2242. command += c->GetArguments();
  2243. std::vector<std::string> depends;
  2244. // Collect out all the dependencies for this rule.
  2245. for(std::vector<std::string>::const_iterator d =
  2246. c->GetDepends().begin();
  2247. d != c->GetDepends().end(); ++d)
  2248. {
  2249. std::string dep = *d;
  2250. m_Makefile->ExpandVariablesInString(dep);
  2251. // watch for target dependencies,
  2252. std::string libPath = dep + "_CMAKE_PATH";
  2253. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  2254. if ( cacheValue && *cacheValue )
  2255. {
  2256. libPath = cacheValue;
  2257. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  2258. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  2259. {
  2260. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2261. }
  2262. libPath += "/";
  2263. libPath += dep;
  2264. libPath += cmSystemTools::GetExecutableExtension();
  2265. dep = libPath;
  2266. }
  2267. cmSystemTools::ReplaceString(dep, "/./", "/");
  2268. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  2269. dep = this->ConvertToRelativeOutputPath(dep.c_str());
  2270. depends.push_back(dep.c_str());
  2271. }
  2272. // output rule
  2273. if (processedOutputs.find(c->GetOutput()) == processedOutputs.end())
  2274. {
  2275. this->OutputMakeRule(fout,
  2276. (comment.size()?comment.c_str():"Custom command"),
  2277. c->GetOutput().c_str(),
  2278. depends,
  2279. command.c_str());
  2280. processedOutputs.insert(c->GetOutput());
  2281. }
  2282. else
  2283. {
  2284. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  2285. c->GetOutput().c_str());
  2286. }
  2287. }
  2288. }
  2289. }
  2290. std::string
  2291. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  2292. {
  2293. std::string ret = this->ConvertToRelativeOutputPath(p);
  2294. // if there are spaces in the path, then get the short path version
  2295. // if there is one
  2296. if(ret.find(' ') != std::string::npos)
  2297. {
  2298. if(cmSystemTools::FileExists(p))
  2299. {
  2300. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  2301. {
  2302. ret = this->ConvertToRelativeOutputPath(p);
  2303. }
  2304. }
  2305. }
  2306. return ret;
  2307. }
  2308. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  2309. {
  2310. const char* variables =
  2311. "# the standard shell for make\n"
  2312. "SHELL = /bin/sh\n"
  2313. "\n";
  2314. if(!m_WindowsShell)
  2315. {
  2316. fout << variables;
  2317. }
  2318. else
  2319. {
  2320. fout <<
  2321. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  2322. "NULL=\n"
  2323. "!ELSE \n"
  2324. "NULL=nul\n"
  2325. "!ENDIF \n";
  2326. }
  2327. if(m_MakeSilentFlag.size())
  2328. {
  2329. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  2330. }
  2331. std::string cmakecommand = this->ConvertToOutputForExisting(
  2332. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  2333. fout << "CMAKE_COMMAND = "
  2334. << cmakecommand
  2335. << "\n";
  2336. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  2337. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2338. {
  2339. fout << "CMAKE_EDIT_COMMAND = "
  2340. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2341. << "\n";
  2342. }
  2343. fout << "CMAKE_CURRENT_SOURCE = " <<
  2344. this->ConvertToRelativeOutputPath(m_Makefile->GetStartDirectory())
  2345. << "\n";
  2346. fout << "CMAKE_CURRENT_BINARY = " <<
  2347. this->ConvertToRelativeOutputPath(m_Makefile->GetStartOutputDirectory())
  2348. << "\n";
  2349. fout << "CMAKE_SOURCE_DIR = " <<
  2350. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeDirectory())
  2351. << "\n";
  2352. fout << "CMAKE_BINARY_DIR = " <<
  2353. this->ConvertToRelativeOutputPath(m_Makefile->GetHomeOutputDirectory())
  2354. << "\n";
  2355. // Output Include paths
  2356. fout << "INCLUDE_FLAGS = ";
  2357. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  2358. std::vector<std::string>::iterator i;
  2359. std::map<cmStdString, cmStdString> implicitIncludes;
  2360. // CMake versions below 2.0 would add the source tree to the -I path
  2361. // automatically. Preserve compatibility.
  2362. bool includeSourceDir = false;
  2363. const char* versionValue =
  2364. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  2365. if(versionValue)
  2366. {
  2367. int major = 0;
  2368. int minor = 0;
  2369. if(sscanf(versionValue, "%d.%d", &major, &minor) == 2 && major < 2)
  2370. {
  2371. includeSourceDir = true;
  2372. }
  2373. }
  2374. const char* vtkSourceDir =
  2375. m_Makefile->GetDefinition("VTK_SOURCE_DIR");
  2376. if(vtkSourceDir)
  2377. {
  2378. // Special hack for VTK 4.0 - 4.4.
  2379. const char* vtk_major = m_Makefile->GetDefinition("VTK_MAJOR_VERSION");
  2380. const char* vtk_minor = m_Makefile->GetDefinition("VTK_MINOR_VERSION");
  2381. vtk_major = vtk_major? vtk_major : "4";
  2382. vtk_minor = vtk_minor? vtk_minor : "4";
  2383. int major = 0;
  2384. int minor = 0;
  2385. if(sscanf(vtk_major, "%d", &major) && sscanf(vtk_minor, "%d", &minor) &&
  2386. major == 4 && minor <= 4)
  2387. {
  2388. includeSourceDir = true;
  2389. }
  2390. }
  2391. if(includeSourceDir)
  2392. {
  2393. fout << "-I"
  2394. << this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory())
  2395. << " ";
  2396. }
  2397. implicitIncludes["/usr/include"] = "/usr/include";
  2398. if(m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  2399. {
  2400. std::string arg = m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES");
  2401. std::vector<std::string> implicitIncludeVec;
  2402. cmSystemTools::ExpandListArgument(arg, implicitIncludeVec);
  2403. for(unsigned int k =0; k < implicitIncludeVec.size(); k++)
  2404. {
  2405. implicitIncludes[implicitIncludeVec[k]] = implicitIncludeVec[k];
  2406. }
  2407. }
  2408. for(i = includes.begin(); i != includes.end(); ++i)
  2409. {
  2410. std::string include = *i;
  2411. // Don't output a -I for the standard include path "/usr/include".
  2412. // This can cause problems with certain standard library
  2413. // implementations because the wrong headers may be found first.
  2414. if(implicitIncludes.find(include) == implicitIncludes.end())
  2415. {
  2416. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  2417. }
  2418. }
  2419. fout << m_Makefile->GetDefineFlags();
  2420. fout << "\n\n";
  2421. }
  2422. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  2423. {
  2424. this->OutputMakeRule(fout,
  2425. "default build rule",
  2426. "all",
  2427. "cmake.depends $(SUBDIR_PREORDER_BUILD) $(TARGETS) $(SUBDIR_BUILD)",
  2428. 0);
  2429. this->OutputMakeRule(fout,
  2430. "clean generated files",
  2431. "clean",
  2432. "$(SUBDIR_CLEAN)",
  2433. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  2434. " $(TARGETS) $(TARGET_EXTRAS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES) $(ADDITIONAL_MAKE_CLEAN_FILES)");
  2435. // collect up all the sources
  2436. std::vector<std::string> allsources;
  2437. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2438. for(std::map<cmStdString,cmTarget>::const_iterator target = targets.begin();
  2439. target != targets.end(); ++target)
  2440. {
  2441. // Iterate over every source for this target.
  2442. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2443. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2444. source != sources.end(); ++source)
  2445. {
  2446. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2447. {
  2448. allsources.push_back(this->ConvertToRelativeOutputPath((*source)->GetFullPath().c_str()));
  2449. }
  2450. }
  2451. }
  2452. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2453. checkCache += "/cmake.check_cache";
  2454. checkCache = this->ConvertToRelativeOutputPath(checkCache.c_str());
  2455. std::vector<std::string> cmake_depends;
  2456. cmake_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2457. this->OutputMakeRule(fout,
  2458. "dependencies.",
  2459. "cmake.depends",
  2460. cmake_depends,
  2461. "$(CMAKE_COMMAND) "
  2462. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2463. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2464. this->OutputMakeRule(fout,
  2465. "dependencies",
  2466. "cmake.check_depends",
  2467. allsources,
  2468. "$(CMAKE_COMMAND) "
  2469. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2470. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2471. this->OutputMakeRule(fout,
  2472. "dependencies",
  2473. "depend",
  2474. "$(SUBDIR_DEPEND)",
  2475. "$(CMAKE_COMMAND) "
  2476. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2477. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2478. this->OutputMakeRule(fout,
  2479. "dependencies",
  2480. "dependlocal",
  2481. 0,
  2482. "$(CMAKE_COMMAND) "
  2483. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2484. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2485. this->OutputMakeRule(fout,
  2486. "CMakeCache.txt",
  2487. "rebuild_cache",
  2488. 0,
  2489. "$(CMAKE_COMMAND) "
  2490. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2491. std::vector<std::string> check_cache_depends;
  2492. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2493. CMakeCache += "/CMakeCache.txt";
  2494. CMakeCache = this->ConvertToRelativeOutputPath(CMakeCache.c_str());
  2495. check_cache_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2496. this->OutputMakeRule(fout,
  2497. "CMakeCache.txt because out-of-date:",
  2498. checkCache.c_str(),
  2499. check_cache_depends,
  2500. "$(CMAKE_COMMAND) "
  2501. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2502. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2503. // called edit_cache
  2504. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2505. {
  2506. this->OutputMakeRule(fout,
  2507. "edit CMakeCache.txt",
  2508. "edit_cache",
  2509. 0,
  2510. "$(CMAKE_EDIT_COMMAND) "
  2511. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2512. }
  2513. else
  2514. {
  2515. this->OutputMakeRule(fout,
  2516. "edit CMakeCache.txt",
  2517. "edit_cache",
  2518. 0,
  2519. "$(CMAKE_COMMAND) "
  2520. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  2521. }
  2522. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  2523. cacheFile += "/CMakeCache.txt";
  2524. cacheFile = this->ConvertToRelativeOutputPath(cacheFile.c_str());
  2525. this->OutputMakeRule(fout,
  2526. "CMakeCache.txt",
  2527. cacheFile.c_str(),
  2528. 0,
  2529. "$(CMAKE_COMMAND) "
  2530. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2531. this->OutputMakeRule(fout,
  2532. "Rule to keep make from removing Makefiles "
  2533. "if control-C is hit during a run of cmake.",
  2534. ".PRECIOUS",
  2535. "Makefile cmake.depends",
  2536. 0);
  2537. this->OutputSourceObjectBuildRules(fout);
  2538. // find ctest
  2539. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2540. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2541. ctest += "/";
  2542. ctest += "ctest";
  2543. ctest += cmSystemTools::GetExecutableExtension();
  2544. if(!cmSystemTools::FileExists(ctest.c_str()))
  2545. {
  2546. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2547. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2548. ctest += "/Debug/";
  2549. ctest += "ctest";
  2550. ctest += cmSystemTools::GetExecutableExtension();
  2551. }
  2552. if(!cmSystemTools::FileExists(ctest.c_str()))
  2553. {
  2554. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2555. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2556. ctest += "/Release/";
  2557. ctest += "ctest";
  2558. ctest += cmSystemTools::GetExecutableExtension();
  2559. }
  2560. if (!cmSystemTools::FileExists(ctest.c_str()))
  2561. {
  2562. if ( !m_Makefile->GetDefinition("PROJECT_NAME") ||
  2563. strcmp(m_Makefile->GetDefinition("PROJECT_NAME"), "CMake") )
  2564. {
  2565. return;
  2566. }
  2567. ctest = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2568. ctest += "/ctest";
  2569. }
  2570. if (m_Makefile->IsOn("CMAKE_TESTING_ENABLED"))
  2571. {
  2572. fout << "ARGS=\n";
  2573. std::string cmd = this->ConvertToOutputForExisting(ctest.c_str());
  2574. cmd += " $(ARGS)";
  2575. this->OutputMakeRule(fout,
  2576. "tests",
  2577. "test",
  2578. "",
  2579. cmd.c_str());
  2580. }
  2581. if(m_Makefile->GetDefinition("CMake_BINARY_DIR"))
  2582. {
  2583. // We are building CMake itself. We cannot use the original
  2584. // executable to install over itself.
  2585. std::string rule = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2586. rule += "/cmake";
  2587. rule = cmSystemTools::ConvertToOutputPath(rule.c_str());
  2588. rule += " -P cmake_install.cmake";
  2589. this->OutputMakeRule(fout, "installation", "install", "", rule.c_str());
  2590. }
  2591. else
  2592. {
  2593. this->OutputMakeRule(fout, "installation", "install", "",
  2594. "$(CMAKE_COMMAND) -P cmake_install.cmake");
  2595. }
  2596. }
  2597. void
  2598. cmLocalUnixMakefileGenerator::
  2599. OutputBuildObjectFromSource(std::ostream& fout,
  2600. const char* shortName,
  2601. const cmSourceFile& source,
  2602. const char* extraCompileFlags,
  2603. bool shared)
  2604. {
  2605. // Header files shouldn't have build rules.
  2606. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2607. {
  2608. return;
  2609. }
  2610. std::string comment = "object file";
  2611. std::string objectFile = std::string(shortName) +
  2612. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2613. objectFile = this->CreateSafeUniqueObjectFileName(objectFile.c_str());
  2614. objectFile = this->ConvertToRelativeOutputPath(objectFile.c_str());
  2615. cmSystemTools::FileFormat format =
  2616. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2617. std::vector<std::string> rules;
  2618. std::string flags;
  2619. if(extraCompileFlags)
  2620. {
  2621. flags += extraCompileFlags;
  2622. }
  2623. std::string sourceFile =
  2624. this->ConvertToRelativeOutputPath(source.GetFullPath().c_str());
  2625. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2626. buildType = cmSystemTools::UpperCase(buildType);
  2627. switch(format)
  2628. {
  2629. case cmSystemTools::C_FILE_FORMAT:
  2630. {
  2631. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2632. flags += m_Makefile->GetSafeDefinition("CMAKE_C_FLAGS");
  2633. flags += " ";
  2634. if(buildType.size())
  2635. {
  2636. std::string build = "CMAKE_C_FLAGS_";
  2637. build += buildType;
  2638. flags += m_Makefile->GetSafeDefinition(build.c_str());
  2639. flags += " ";
  2640. }
  2641. if(shared)
  2642. {
  2643. flags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2644. flags += " ";
  2645. }
  2646. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2647. {
  2648. flags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2649. flags += " ";
  2650. }
  2651. break;
  2652. }
  2653. case cmSystemTools::CXX_FILE_FORMAT:
  2654. {
  2655. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2656. flags += m_Makefile->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2657. flags += " ";
  2658. if(buildType.size())
  2659. {
  2660. std::string build = "CMAKE_CXX_FLAGS_";
  2661. build += buildType;
  2662. flags += m_Makefile->GetSafeDefinition(build.c_str());
  2663. flags += " ";
  2664. }
  2665. if(shared)
  2666. {
  2667. flags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2668. flags += " ";
  2669. }
  2670. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2671. {
  2672. flags += m_Makefile->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2673. flags += " ";
  2674. }
  2675. break;
  2676. }
  2677. case cmSystemTools::HEADER_FILE_FORMAT:
  2678. return;
  2679. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2680. return;
  2681. case cmSystemTools::OBJECT_FILE_FORMAT:
  2682. return;
  2683. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2684. {
  2685. // use rc rule here if it is defined
  2686. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2687. if(rule)
  2688. {
  2689. rules.push_back(rule);
  2690. }
  2691. }
  2692. break;
  2693. case cmSystemTools::NO_FILE_FORMAT:
  2694. case cmSystemTools::JAVA_FILE_FORMAT:
  2695. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2696. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2697. case cmSystemTools::MODULE_FILE_FORMAT:
  2698. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2699. cmSystemTools::Error("Unexpected file type ",
  2700. sourceFile.c_str());
  2701. break;
  2702. }
  2703. flags += "$(INCLUDE_FLAGS) ";
  2704. // expand multi-command semi-colon separated lists
  2705. // of commands into separate commands
  2706. std::vector<std::string> commands;
  2707. cmSystemTools::ExpandList(rules, commands);
  2708. for(std::vector<std::string>::iterator i = commands.begin();
  2709. i != commands.end(); ++i)
  2710. {
  2711. this->ExpandRuleVariables(*i,
  2712. 0, // no objects
  2713. 0, // no target
  2714. 0, // no link libs
  2715. sourceFile.c_str(),
  2716. objectFile.c_str(),
  2717. flags.c_str());
  2718. }
  2719. std::vector<std::string> sourceAndDeps;
  2720. sourceAndDeps.push_back(sourceFile);
  2721. // Check for extra object-file dependencies.
  2722. std::vector<std::string> depends;
  2723. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2724. if(additionalDeps)
  2725. {
  2726. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2727. for(std::vector<std::string>::iterator i = depends.begin();
  2728. i != depends.end(); ++i)
  2729. {
  2730. sourceAndDeps.push_back(this->ConvertToRelativeOutputPath(i->c_str()));
  2731. }
  2732. }
  2733. this->OutputMakeRule(fout,
  2734. comment.c_str(),
  2735. objectFile.c_str(),
  2736. sourceAndDeps,
  2737. commands);
  2738. }
  2739. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2740. {
  2741. fout << "# Rules to build " << this->GetOutputExtension("")
  2742. << " files from their sources:\n";
  2743. std::set<std::string> rules;
  2744. // Iterate over every target.
  2745. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2746. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2747. target != targets.end(); ++target)
  2748. {
  2749. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2750. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2751. std::string exportsDef = "";
  2752. if(shared)
  2753. {
  2754. std::string export_symbol;
  2755. if (const char* custom_export_name = target->second.GetProperty("DEFINE_SYMBOL"))
  2756. {
  2757. export_symbol = custom_export_name;
  2758. }
  2759. else
  2760. {
  2761. std::string in = target->first + "_EXPORTS";
  2762. export_symbol = cmSystemTools::MakeCindentifier(in.c_str());
  2763. }
  2764. exportsDef = "-D"+ export_symbol;
  2765. }
  2766. // Iterate over every source for this target.
  2767. const std::vector<cmSourceFile*>& sources =
  2768. target->second.GetSourceFiles();
  2769. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2770. source != sources.end(); ++source)
  2771. {
  2772. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  2773. !(*source)->GetCustomCommand())
  2774. {
  2775. std::string shortName;
  2776. std::string sourceName;
  2777. // If the full path to the source file includes this
  2778. // directory, we want to use the relative path for the
  2779. // filename of the object file. Otherwise, we will use just
  2780. // the filename portion.
  2781. if((cmSystemTools::GetFilenamePath(
  2782. (*source)->GetFullPath()).find(
  2783. m_Makefile->GetCurrentDirectory()) == 0)
  2784. || (cmSystemTools::GetFilenamePath(
  2785. (*source)->GetFullPath()).find(
  2786. m_Makefile->GetCurrentOutputDirectory()) == 0))
  2787. {
  2788. sourceName = (*source)->GetSourceName()+"."+
  2789. (*source)->GetSourceExtension();
  2790. shortName = (*source)->GetSourceName();
  2791. // The path may be relative. See if a directory needs to be
  2792. // created for the output file. This is a ugly, and perhaps
  2793. // should be moved elsewhere.
  2794. std::string relPath =
  2795. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2796. if(relPath != "")
  2797. {
  2798. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2799. outPath += "/"+relPath;
  2800. cmSystemTools::MakeDirectory(outPath.c_str());
  2801. }
  2802. }
  2803. else
  2804. {
  2805. sourceName = (*source)->GetFullPath();
  2806. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2807. }
  2808. std::string shortNameWithExt = shortName +
  2809. (*source)->GetSourceExtension();
  2810. // Only output a rule for each .o once.
  2811. std::string compileFlags = exportsDef;
  2812. compileFlags += " ";
  2813. if(rules.find(shortNameWithExt) == rules.end())
  2814. {
  2815. if((*source)->GetProperty("COMPILE_FLAGS"))
  2816. {
  2817. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2818. compileFlags += " ";
  2819. }
  2820. this->OutputBuildObjectFromSource(fout,
  2821. shortName.c_str(),
  2822. *(*source),
  2823. compileFlags.c_str(),
  2824. shared);
  2825. rules.insert(shortNameWithExt);
  2826. }
  2827. }
  2828. }
  2829. }
  2830. }
  2831. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2832. const char* comment,
  2833. const char* target,
  2834. const char* depends,
  2835. const char* command,
  2836. const char* command2,
  2837. const char* command3,
  2838. const char* command4)
  2839. {
  2840. std::vector<std::string> commands;
  2841. if(command)
  2842. {
  2843. commands.push_back(command);
  2844. }
  2845. if(command2)
  2846. {
  2847. commands.push_back(command2);
  2848. }
  2849. if(command3)
  2850. {
  2851. commands.push_back(command3);
  2852. }
  2853. if(command4)
  2854. {
  2855. commands.push_back(command4);
  2856. }
  2857. this->OutputMakeRule(fout, comment, target, depends, commands);
  2858. }
  2859. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2860. const char* comment,
  2861. const char* target,
  2862. const char* depends,
  2863. const std::vector<std::string>& commands)
  2864. {
  2865. std::vector<std::string> depend;
  2866. if(depends)
  2867. {
  2868. depend.push_back(depends);
  2869. }
  2870. this->OutputMakeRule(fout, comment, target, depend, commands);
  2871. }
  2872. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2873. const char* comment,
  2874. const char* target,
  2875. const std::vector<std::string>& depends,
  2876. const char* command)
  2877. {
  2878. std::vector<std::string> commands;
  2879. if(command)
  2880. {
  2881. commands.push_back(command);
  2882. }
  2883. this->OutputMakeRule(fout, comment, target, depends, commands);
  2884. }
  2885. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2886. const char* comment,
  2887. const char* target,
  2888. const std::vector<std::string>& depends,
  2889. const std::vector<std::string>& commands)
  2890. {
  2891. if(!target)
  2892. {
  2893. cmSystemTools::Error("no target for OutputMakeRule");
  2894. return;
  2895. }
  2896. std::string replace;
  2897. if(comment)
  2898. {
  2899. replace = comment;
  2900. m_Makefile->ExpandVariablesInString(replace);
  2901. fout << "#---------------------------------------------------------\n";
  2902. fout << "# " << replace;
  2903. fout << "\n#\n";
  2904. }
  2905. fout << "\n";
  2906. replace = target;
  2907. m_Makefile->ExpandVariablesInString(replace);
  2908. std::string tgt = this->ConvertToRelativeOutputPath(replace.c_str());
  2909. tgt = this->ConvertToMakeTarget(tgt.c_str());
  2910. if(depends.empty())
  2911. {
  2912. fout << tgt.c_str() << ":\n";
  2913. }
  2914. else
  2915. {
  2916. // Split dependencies into multiple rule lines. This allows for
  2917. // very long dependency lists.
  2918. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2919. dep != depends.end(); ++dep)
  2920. {
  2921. replace = *dep;
  2922. m_Makefile->ExpandVariablesInString(replace);
  2923. replace = this->ConvertToMakeTarget(replace.c_str());
  2924. fout << tgt.c_str() << ": " << replace.c_str() << "\n";
  2925. }
  2926. }
  2927. int count = 0;
  2928. for (std::vector<std::string>::const_iterator i = commands.begin();
  2929. i != commands.end(); ++i)
  2930. {
  2931. replace = *i;
  2932. m_Makefile->ExpandVariablesInString(replace);
  2933. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2934. replace[0] != '-' && replace.find("echo") != 0
  2935. && replace.find("$(MAKE)") != 0)
  2936. {
  2937. std::string echostring = "Building ";
  2938. echostring += comment;
  2939. echostring += " ";
  2940. echostring += target;
  2941. echostring += "...";
  2942. this->OutputEcho(fout,echostring.c_str());
  2943. }
  2944. fout << "\t" << replace.c_str() << "\n";
  2945. count++;
  2946. }
  2947. fout << "\n";
  2948. }
  2949. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2950. {
  2951. #ifdef _WIN32
  2952. return cmSystemTools::LowerCase(path);
  2953. #else
  2954. return std::string(path);
  2955. #endif
  2956. }
  2957. std::string&
  2958. cmLocalUnixMakefileGenerator::CreateSafeUniqueObjectFileName(const char* sin)
  2959. {
  2960. if ( m_Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES") )
  2961. {
  2962. std::map<cmStdString,cmStdString>::iterator it = m_UniqueObjectNamesMap.find(sin);
  2963. if ( it == m_UniqueObjectNamesMap.end() )
  2964. {
  2965. std::string ssin = sin;
  2966. bool done;
  2967. int cc = 0;
  2968. char rpstr[100];
  2969. sprintf(rpstr, "_p_");
  2970. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2971. std::string sssin = sin;
  2972. do
  2973. {
  2974. done = true;
  2975. for ( it = m_UniqueObjectNamesMap.begin();
  2976. it != m_UniqueObjectNamesMap.end();
  2977. ++ it )
  2978. {
  2979. if ( it->second == ssin )
  2980. {
  2981. done = false;
  2982. }
  2983. }
  2984. if ( done )
  2985. {
  2986. break;
  2987. }
  2988. sssin = ssin;
  2989. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2990. sprintf(rpstr, "_p%d_", cc++);
  2991. }
  2992. while ( !done );
  2993. m_UniqueObjectNamesMap[sin] = ssin;
  2994. }
  2995. }
  2996. else
  2997. {
  2998. m_UniqueObjectNamesMap[sin] = sin;
  2999. }
  3000. return m_UniqueObjectNamesMap[sin];
  3001. }
  3002. std::string
  3003. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* sin, const char* s2in)
  3004. {
  3005. std::string s = sin;
  3006. std::string s2 = s2in;
  3007. std::string unmodified = s;
  3008. unmodified += s2;
  3009. // if there is no restriction on the length of make variables
  3010. // and there are no "." charactors in the string, then return the
  3011. // unmodified combination.
  3012. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  3013. {
  3014. return unmodified;
  3015. }
  3016. // see if the variable has been defined before and return
  3017. // the modified version of the variable
  3018. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  3019. if(i != m_MakeVariableMap.end())
  3020. {
  3021. return i->second;
  3022. }
  3023. // start with the unmodified variable
  3024. std::string ret = unmodified;
  3025. // if this there is no value for m_MakefileVariableSize then
  3026. // the string must have bad characters in it
  3027. if(!m_MakefileVariableSize)
  3028. {
  3029. cmSystemTools::ReplaceString(ret, ".", "_");
  3030. int ni = 0;
  3031. char buffer[5];
  3032. // make sure the _ version is not already used, if
  3033. // it is used then add number to the end of the variable
  3034. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  3035. {
  3036. ++ni;
  3037. sprintf(buffer, "%04d", ni);
  3038. ret = unmodified + buffer;
  3039. }
  3040. m_ShortMakeVariableMap[ret] = "1";
  3041. m_MakeVariableMap[unmodified] = ret;
  3042. return ret;
  3043. }
  3044. // if the string is greater the 32 chars it is an invalid vairable name
  3045. // for borland make
  3046. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  3047. {
  3048. int keep = m_MakefileVariableSize - 8;
  3049. int size = keep + 3;
  3050. std::string str1 = s;
  3051. std::string str2 = s2;
  3052. // we must shorten the combined string by 4 charactors
  3053. // keep no more than 24 charactors from the second string
  3054. if(static_cast<int>(str2.size()) > keep)
  3055. {
  3056. str2 = str2.substr(0, keep);
  3057. }
  3058. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  3059. {
  3060. str1 = str1.substr(0, size - str2.size());
  3061. }
  3062. char buffer[5];
  3063. int ni = 0;
  3064. sprintf(buffer, "%04d", ni);
  3065. ret = str1 + str2 + buffer;
  3066. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  3067. {
  3068. ++ni;
  3069. sprintf(buffer, "%04d", ni);
  3070. ret = str1 + str2 + buffer;
  3071. }
  3072. if(ni == 1000)
  3073. {
  3074. cmSystemTools::Error("Borland makefile variable length too long");
  3075. return unmodified;
  3076. }
  3077. // once an unused variable is found
  3078. m_ShortMakeVariableMap[ret] = "1";
  3079. }
  3080. // always make an entry into the unmodified to variable map
  3081. m_MakeVariableMap[unmodified] = ret;
  3082. return ret;
  3083. }
  3084. void cmLocalUnixMakefileGenerator::GetLibraryNames(const char* n,
  3085. const cmTarget& t,
  3086. std::string& name,
  3087. std::string& soName,
  3088. std::string& realName,
  3089. std::string& baseName)
  3090. {
  3091. // Check for library version properties.
  3092. const char* version = t.GetProperty("VERSION");
  3093. const char* soversion = t.GetProperty("SOVERSION");
  3094. if((t.GetType() != cmTarget::SHARED_LIBRARY &&
  3095. t.GetType() != cmTarget::MODULE_LIBRARY) ||
  3096. !m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  3097. {
  3098. // Versioning is supported only for shared libraries and modules,
  3099. // and then only when the platform supports an soname flag.
  3100. version = 0;
  3101. soversion = 0;
  3102. }
  3103. if(version && !soversion)
  3104. {
  3105. // The soversion must be set if the library version is set. Use
  3106. // the library version as the soversion.
  3107. soversion = version;
  3108. }
  3109. // The library name.
  3110. name = this->GetFullTargetName(n, t);
  3111. // The library's soname.
  3112. soName = name;
  3113. if(soversion)
  3114. {
  3115. soName += ".";
  3116. soName += soversion;
  3117. }
  3118. // The library's real name on disk.
  3119. realName = name;
  3120. if(version)
  3121. {
  3122. realName += ".";
  3123. realName += version;
  3124. }
  3125. else if(soversion)
  3126. {
  3127. realName += ".";
  3128. realName += soversion;
  3129. }
  3130. // The library name without extension.
  3131. baseName = this->GetBaseTargetName(n, t);
  3132. }
  3133. std::string cmLocalUnixMakefileGenerator::ConvertToMakeTarget(const char* tgt)
  3134. {
  3135. // Make targets should not have a leading './' for a file in the
  3136. // directory containing the makefile.
  3137. std::string ret = tgt;
  3138. if(ret.size() > 2 &&
  3139. (ret[0] == '.') &&
  3140. ( (ret[1] == '/') || ret[1] == '\\'))
  3141. {
  3142. std::string upath = ret;
  3143. cmSystemTools::ConvertToUnixSlashes(upath);
  3144. if(upath.find(2, '/') == upath.npos)
  3145. {
  3146. ret = ret.substr(2, ret.size()-2);
  3147. }
  3148. }
  3149. return ret;
  3150. }