cmLocalUnixMakefileGenerator.cxx 104 KB

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