cmLocalUnixMakefileGenerator.cxx 111 KB

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