cmLocalUnixMakefileGenerator.cxx 112 KB

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