cmLocalUnixMakefileGenerator.cxx 104 KB

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