cmLocalUnixMakefileGenerator.cxx 112 KB

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