1
0

cmLocalUnixMakefileGenerator.cxx 105 KB

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