cmLocalUnixMakefileGenerator.cxx 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158
  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 << " " << cmSystemTools::ConvertToOutputPath(i->c_str());
  208. }
  209. // Add the cache to the list
  210. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  211. cacheFile += "/CMakeCache.txt";
  212. fout << " " << cmSystemTools::ConvertToOutputPath(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 = cmSystemTools::ConvertToOutputPath(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 += cmSystemTools::ConvertToOutputPath(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. << cmSystemTools::ConvertToOutputPath(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" << cmSystemTools::ConvertToOutputPath(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. << cmSystemTools::ConvertToOutputPath((*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\"" << cmSystemTools::ConvertToOutputPath((*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. << cmSystemTools::ConvertToOutputPath(path.c_str());
  556. }
  557. if(targetNameReal != targetName &&
  558. targetNameReal != targetNameSO)
  559. {
  560. path = m_LibraryOutputPath;
  561. path += targetNameReal;
  562. fout << " \\\n"
  563. << cmSystemTools::ConvertToOutputPath(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 = cmSystemTools::ConvertToOutputPath(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 = cmSystemTools::ConvertToOutputPath(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 = cmSystemTools::ConvertToOutputPath(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_C_FLAG>", "CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"},
  886. {"<CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG>", "CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG"},
  887. {"<CMAKE_C_LINK_FLAGS>", "CMAKE_C_LINK_FLAGS"},
  888. {"<CMAKE_AR>", "CMAKE_AR"},
  889. {"<CMAKE_RANLIB>", "CMAKE_RANLIB"},
  890. {0, 0}
  891. };
  892. void
  893. cmLocalUnixMakefileGenerator::ExpandRuleVariables(std::string& s,
  894. const char* objects,
  895. const char* target,
  896. const char* linkLibs,
  897. const char* source,
  898. const char* object,
  899. const char* flags,
  900. const char* objectsquoted,
  901. const char* targetBase,
  902. const char* targetSOName,
  903. const char* linkFlags)
  904. {
  905. std::string cxxcompiler = this->ConvertToOutputForExisting(
  906. this->GetSafeDefinition("CMAKE_CXX_COMPILER"));
  907. std::string ccompiler = this->ConvertToOutputForExisting(
  908. this->GetSafeDefinition("CMAKE_C_COMPILER"));
  909. cmSystemTools::ReplaceString(s, "<CMAKE_CXX_COMPILER>", cxxcompiler.c_str());
  910. cmSystemTools::ReplaceString(s, "<CMAKE_C_COMPILER>", ccompiler.c_str());
  911. if(linkFlags)
  912. {
  913. cmSystemTools::ReplaceString(s, "<LINK_FLAGS>", linkFlags);
  914. }
  915. if(flags)
  916. {
  917. cmSystemTools::ReplaceString(s, "<FLAGS>", flags);
  918. }
  919. if(source)
  920. {
  921. cmSystemTools::ReplaceString(s, "<SOURCE>", source);
  922. }
  923. if(object)
  924. {
  925. cmSystemTools::ReplaceString(s, "<OBJECT>", object);
  926. }
  927. if(objects)
  928. {
  929. cmSystemTools::ReplaceString(s, "<OBJECTS>", objects);
  930. }
  931. if(objectsquoted)
  932. {
  933. cmSystemTools::ReplaceString(s, "<OBJECTS_QUOTED>", objectsquoted);
  934. }
  935. if(target)
  936. {
  937. std::string targetQuoted = target;
  938. if(targetQuoted.size() && targetQuoted[0] != '\"')
  939. {
  940. targetQuoted = '\"';
  941. targetQuoted += target;
  942. targetQuoted += '\"';
  943. }
  944. cmSystemTools::ReplaceString(s, "<TARGET_QUOTED>", targetQuoted.c_str());
  945. cmSystemTools::ReplaceString(s, "<TARGET>", target);
  946. }
  947. if(targetBase)
  948. {
  949. // special case for quoted paths with spaces
  950. // if you see <TARGET_BASE>.lib then put the .lib inside
  951. // the quotes, same for .dll
  952. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  953. {
  954. std::string base = targetBase;
  955. base[base.size()-1] = '.';
  956. std::string baseLib = base + "lib\"";
  957. std::string baseDll = base + "dll\"";
  958. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.lib", baseLib.c_str());
  959. cmSystemTools::ReplaceString(s, "<TARGET_BASE>.dll", baseDll.c_str());
  960. }
  961. cmSystemTools::ReplaceString(s, "<TARGET_BASE>", targetBase);
  962. }
  963. if(targetSOName)
  964. {
  965. if(m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  966. {
  967. cmSystemTools::ReplaceString(s, "<TARGET_SONAME>", targetSOName);
  968. }
  969. else
  970. {
  971. cmSystemTools::ReplaceString(s, "<TARGET_SONAME>", "");
  972. }
  973. }
  974. if(linkLibs)
  975. {
  976. cmSystemTools::ReplaceString(s, "<LINK_LIBRARIES>", linkLibs);
  977. }
  978. RuleVariables* rv = ruleReplaceVars;
  979. while(rv->replace)
  980. {
  981. cmSystemTools::ReplaceString(s, rv->replace,
  982. this->GetSafeDefinition(rv->lookup));
  983. rv++;
  984. }
  985. }
  986. void cmLocalUnixMakefileGenerator::OutputLibraryRule(std::ostream& fout,
  987. const char* name,
  988. const cmTarget &t,
  989. const char* createVariable,
  990. const char* comment,
  991. const char* linkFlags
  992. )
  993. {
  994. std::string targetName;
  995. std::string targetNameSO;
  996. std::string targetNameReal;
  997. std::string targetNameBase;
  998. this->GetLibraryNames(name, t,
  999. targetName, targetNameSO,
  1000. targetNameReal, targetNameBase);
  1001. // The full path versions of the names.
  1002. std::string targetFullPath = m_LibraryOutputPath + targetName;
  1003. std::string targetFullPathSO = m_LibraryOutputPath + targetNameSO;
  1004. std::string targetFullPathReal = m_LibraryOutputPath + targetNameReal;
  1005. std::string targetFullPathBase = m_LibraryOutputPath + targetNameBase;
  1006. targetFullPath = cmSystemTools::ConvertToOutputPath(targetFullPath.c_str());
  1007. targetFullPathSO = cmSystemTools::ConvertToOutputPath(targetFullPathSO.c_str());
  1008. targetFullPathReal = cmSystemTools::ConvertToOutputPath(targetFullPathReal.c_str());
  1009. targetFullPathBase = cmSystemTools::ConvertToOutputPath(targetFullPathBase.c_str());
  1010. // get the objects that are used to link this library
  1011. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  1012. std::string objsQuoted = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS_QUOTED") + ") ";
  1013. // create a variable with the objects that this library depends on
  1014. std::string depend =
  1015. objs + " $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1016. std::vector<std::string> commands;
  1017. std::string cmakecommand = this->ConvertToOutputForExisting(
  1018. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  1019. // Remove any existing files for this library.
  1020. std::string remove = cmakecommand;
  1021. remove += " -E remove -f ";
  1022. remove += targetFullPathReal;
  1023. if(targetFullPathSO != targetFullPathReal)
  1024. {
  1025. remove += " ";
  1026. remove += targetFullPathSO;
  1027. }
  1028. if(targetFullPath != targetFullPathSO &&
  1029. targetFullPath != targetFullPathReal)
  1030. {
  1031. remove += " ";
  1032. remove += targetFullPath;
  1033. }
  1034. commands.push_back(remove);
  1035. // collect custom commands for this target and add them to the list
  1036. std::string customCommands = this->CreatePreBuildRules(t, name);
  1037. if(customCommands.size() > 0)
  1038. {
  1039. commands.push_back(customCommands);
  1040. }
  1041. // collect custom commands for this target and add them to the list
  1042. customCommands = this->CreatePreLinkRules(t, name);
  1043. if(customCommands.size() > 0)
  1044. {
  1045. commands.push_back(customCommands);
  1046. }
  1047. // collect up the build rules
  1048. std::vector<std::string> rules;
  1049. rules.push_back(m_Makefile->GetDefinition(createVariable));
  1050. // expand multi-command semi-colon separated lists
  1051. // of commands into separate commands
  1052. cmSystemTools::ExpandList(rules, commands);
  1053. // Create necessary symlinks for library.
  1054. if(targetFullPath != targetFullPathReal)
  1055. {
  1056. std::string symlink = cmakecommand;
  1057. symlink += " -E cmake_symlink_library ";
  1058. symlink += targetFullPathReal;
  1059. symlink += " ";
  1060. symlink += targetFullPathSO;
  1061. symlink += " ";
  1062. symlink += targetFullPath;
  1063. commands.push_back(symlink);
  1064. }
  1065. // collect custom commands for this target and add them to the list
  1066. customCommands = this->CreatePostBuildRules(t, name);
  1067. if(customCommands.size() > 0)
  1068. {
  1069. commands.push_back(customCommands);
  1070. }
  1071. // collect up the link libraries
  1072. cmOStringStream linklibs;
  1073. this->OutputLinkLibraries(linklibs, name, t);
  1074. for(std::vector<std::string>::iterator i = commands.begin();
  1075. i != commands.end(); ++i)
  1076. {
  1077. this->ExpandRuleVariables(*i,
  1078. objs.c_str(),
  1079. targetFullPathReal.c_str(),
  1080. linklibs.str().c_str(),
  1081. 0, 0, 0, objsQuoted.c_str(),
  1082. targetFullPathBase.c_str(),
  1083. targetNameSO.c_str(),
  1084. linkFlags);
  1085. }
  1086. this->OutputMakeRule(fout, comment,
  1087. targetFullPath.c_str(),
  1088. depend.c_str(),
  1089. commands);
  1090. }
  1091. void cmLocalUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  1092. const char* name,
  1093. const cmTarget &t)
  1094. {
  1095. const char* createRule;
  1096. if(t.HasCxx())
  1097. {
  1098. createRule = "CMAKE_CXX_CREATE_SHARED_LIBRARY";
  1099. }
  1100. else
  1101. {
  1102. createRule = "CMAKE_C_CREATE_SHARED_LIBRARY";
  1103. }
  1104. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1105. buildType = cmSystemTools::UpperCase(buildType);
  1106. std::string linkFlags = this->GetSafeDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1107. linkFlags += " ";
  1108. if(buildType.size())
  1109. {
  1110. std::string build = "CMAKE_SHARED_LINKER_FLAGS_";
  1111. build += buildType;
  1112. linkFlags += this->GetSafeDefinition(build.c_str());
  1113. linkFlags += " ";
  1114. }
  1115. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1116. {
  1117. const std::vector<cmSourceFile*>& sources = t.GetSourceFiles();
  1118. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1119. i != sources.end(); ++i)
  1120. {
  1121. if((*i)->GetSourceExtension() == "def")
  1122. {
  1123. linkFlags += this->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1124. linkFlags += cmSystemTools::ConvertToOutputPath((*i)->GetFullPath().c_str());
  1125. linkFlags += " ";
  1126. }
  1127. }
  1128. }
  1129. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1130. if(targetLinkFlags)
  1131. {
  1132. linkFlags += targetLinkFlags;
  1133. linkFlags += " ";
  1134. }
  1135. this->OutputLibraryRule(fout, name, t,
  1136. createRule,
  1137. "shared library",
  1138. linkFlags.c_str());
  1139. }
  1140. void cmLocalUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  1141. const char* name,
  1142. const cmTarget &t)
  1143. {
  1144. const char* createRule;
  1145. if(t.HasCxx())
  1146. {
  1147. createRule = "CMAKE_CXX_CREATE_SHARED_MODULE";
  1148. }
  1149. else
  1150. {
  1151. createRule = "CMAKE_C_CREATE_SHARED_MODULE";
  1152. }
  1153. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1154. buildType = cmSystemTools::UpperCase(buildType);
  1155. std::string linkFlags = this->GetSafeDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1156. linkFlags += " ";
  1157. if(buildType.size())
  1158. {
  1159. std::string build = "CMAKE_MODULE_LINKER_FLAGS_";
  1160. build += buildType;
  1161. linkFlags += this->GetSafeDefinition(build.c_str());
  1162. linkFlags += " ";
  1163. }
  1164. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1165. if(targetLinkFlags)
  1166. {
  1167. linkFlags += targetLinkFlags;
  1168. linkFlags += " ";
  1169. }
  1170. this->OutputLibraryRule(fout, name, t,
  1171. createRule,
  1172. "shared module",
  1173. linkFlags.c_str());
  1174. }
  1175. void cmLocalUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  1176. const char* name,
  1177. const cmTarget &t)
  1178. {
  1179. const char* createRule;
  1180. if(t.HasCxx())
  1181. {
  1182. createRule = "CMAKE_CXX_CREATE_STATIC_LIBRARY";
  1183. }
  1184. else
  1185. {
  1186. createRule = "CMAKE_C_CREATE_STATIC_LIBRARY";
  1187. }
  1188. std::string linkFlags;
  1189. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1190. if(targetLinkFlags)
  1191. {
  1192. linkFlags += targetLinkFlags;
  1193. linkFlags += " ";
  1194. }
  1195. this->OutputLibraryRule(fout, name, t,
  1196. createRule,
  1197. "static library",
  1198. linkFlags.c_str());
  1199. }
  1200. void cmLocalUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  1201. const char* name,
  1202. const cmTarget &t)
  1203. {
  1204. std::string linkFlags;
  1205. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1206. buildType = cmSystemTools::UpperCase(buildType);
  1207. std::string flags;
  1208. // Construct the full path to the executable that will be generated.
  1209. std::string target = m_ExecutableOutputPath;
  1210. if(target.length() == 0)
  1211. {
  1212. target = m_Makefile->GetCurrentOutputDirectory();
  1213. if(target.size() && target[target.size()-1] != '/')
  1214. {
  1215. target += "/";
  1216. }
  1217. }
  1218. target += name;
  1219. target += cmSystemTools::GetExecutableExtension();
  1220. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  1221. std::string objs = "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  1222. std::string depend = "$(";
  1223. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  1224. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1225. std::vector<std::string> rules;
  1226. linkFlags += this->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1227. linkFlags += " ";
  1228. if(buildType.size())
  1229. {
  1230. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1231. build += buildType;
  1232. linkFlags += this->GetSafeDefinition(build.c_str());
  1233. linkFlags += " ";
  1234. }
  1235. if(t.HasCxx())
  1236. {
  1237. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_LINK_EXECUTABLE"));
  1238. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  1239. flags += " ";
  1240. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  1241. flags += " ";
  1242. }
  1243. else
  1244. {
  1245. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_LINK_EXECUTABLE"));
  1246. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  1247. flags += " ";
  1248. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  1249. flags += " ";
  1250. }
  1251. cmOStringStream linklibs;
  1252. this->OutputLinkLibraries(linklibs, 0, t);
  1253. std::string comment = "executable";
  1254. std::vector<std::string> commands;
  1255. std::string customCommands = this->CreatePreBuildRules(t, name);
  1256. if(customCommands.size() > 0)
  1257. {
  1258. commands.push_back(customCommands.c_str());
  1259. }
  1260. customCommands = this->CreatePreLinkRules(t, name);
  1261. if(customCommands.size() > 0)
  1262. {
  1263. commands.push_back(customCommands.c_str());
  1264. }
  1265. cmSystemTools::ExpandList(rules, commands);
  1266. customCommands = this->CreatePostBuildRules(t, name);
  1267. if(customCommands.size() > 0)
  1268. {
  1269. commands.push_back(customCommands.c_str());
  1270. }
  1271. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1272. {
  1273. linkFlags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  1274. linkFlags += " ";
  1275. }
  1276. if(t.GetType() == cmTarget::WIN32_EXECUTABLE)
  1277. {
  1278. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1279. linkFlags += " ";
  1280. }
  1281. else
  1282. {
  1283. linkFlags += this->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1284. linkFlags += " ";
  1285. }
  1286. const char* targetLinkFlags = t.GetProperty("LINK_FLAGS");
  1287. if(targetLinkFlags)
  1288. {
  1289. linkFlags += targetLinkFlags;
  1290. linkFlags += " ";
  1291. }
  1292. for(std::vector<std::string>::iterator i = commands.begin();
  1293. i != commands.end(); ++i)
  1294. {
  1295. this->ExpandRuleVariables(*i,
  1296. objs.c_str(),
  1297. target.c_str(),
  1298. linklibs.str().c_str(),
  1299. 0,
  1300. 0,
  1301. flags.c_str(),
  1302. 0,
  1303. 0,
  1304. 0,
  1305. linkFlags.c_str());
  1306. }
  1307. this->OutputMakeRule(fout,
  1308. comment.c_str(),
  1309. target.c_str(),
  1310. depend.c_str(),
  1311. commands);
  1312. // If there is no executable output path, add a rule with the
  1313. // relative path to the executable. This is necessary for
  1314. // try-compile to work in this case.
  1315. depend = target;
  1316. target = name;
  1317. target += cmSystemTools::GetExecutableExtension();
  1318. target = cmSystemTools::ConvertToOutputPath(target.c_str());
  1319. commands.resize(0);
  1320. this->OutputMakeRule(fout,
  1321. comment.c_str(),
  1322. target.c_str(),
  1323. depend.c_str(),
  1324. commands);
  1325. }
  1326. void cmLocalUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  1327. const char* name,
  1328. const cmTarget &t)
  1329. {
  1330. const char* cc = 0;
  1331. std::string customCommands = this->CreatePreBuildRules(t, name);
  1332. std::string customCommands2 = this->CreatePreLinkRules(t, name);
  1333. if(customCommands2.size() > 0)
  1334. {
  1335. customCommands += customCommands2;
  1336. }
  1337. customCommands2 = this->CreatePostBuildRules(t, name);
  1338. if(customCommands2.size() > 0)
  1339. {
  1340. customCommands += customCommands2;
  1341. }
  1342. if(customCommands.size() > 0)
  1343. {
  1344. cc = customCommands.c_str();
  1345. }
  1346. std::string comment = "Utility";
  1347. std::string depends;
  1348. depends = "$(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  1349. std::string replaceVars;
  1350. const std::vector<cmCustomCommand> &ccs = t.GetPostBuildCommands();
  1351. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  1352. i != ccs.end(); ++i)
  1353. {
  1354. const std::vector<std::string> & dep = i->GetDepends();
  1355. for(std::vector<std::string>::const_iterator d = dep.begin();
  1356. d != dep.end(); ++d)
  1357. {
  1358. depends += " \\\n";
  1359. replaceVars = *d;
  1360. m_Makefile->ExpandVariablesInString(replaceVars);
  1361. depends += cmSystemTools::ConvertToOutputPath(replaceVars.c_str());
  1362. }
  1363. }
  1364. this->OutputMakeRule(fout, comment.c_str(), name,
  1365. depends.c_str(), cc);
  1366. }
  1367. void cmLocalUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  1368. {
  1369. // for each target
  1370. const cmTargets &tgts = m_Makefile->GetTargets();
  1371. for(cmTargets::const_iterator l = tgts.begin();
  1372. l != tgts.end(); l++)
  1373. {
  1374. switch(l->second.GetType())
  1375. {
  1376. case cmTarget::STATIC_LIBRARY:
  1377. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  1378. break;
  1379. case cmTarget::SHARED_LIBRARY:
  1380. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  1381. break;
  1382. case cmTarget::MODULE_LIBRARY:
  1383. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  1384. break;
  1385. case cmTarget::EXECUTABLE:
  1386. case cmTarget::WIN32_EXECUTABLE:
  1387. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  1388. break;
  1389. case cmTarget::UTILITY:
  1390. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  1391. break;
  1392. // This is handled by the OutputCustomRules method
  1393. case cmTarget::INSTALL_FILES:
  1394. // This is handled by the OutputInstallRules method
  1395. case cmTarget::INSTALL_PROGRAMS:
  1396. // This is handled by the OutputInstallRules method
  1397. break;
  1398. }
  1399. }
  1400. }
  1401. // For each target that is an executable or shared library, generate
  1402. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  1403. void cmLocalUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  1404. {
  1405. // Build a set of libraries that will be linked into any target in
  1406. // this directory.
  1407. std::set<std::string> used;
  1408. // for each target
  1409. const cmTargets &tgts = m_Makefile->GetTargets();
  1410. for(cmTargets::const_iterator l = tgts.begin();
  1411. l != tgts.end(); l++)
  1412. {
  1413. // Each dependency should only be emitted once per target.
  1414. std::set<std::string> emitted;
  1415. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  1416. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  1417. || (l->second.GetType() == cmTarget::STATIC_LIBRARY)
  1418. || (l->second.GetType() == cmTarget::EXECUTABLE)
  1419. || (l->second.GetType() == cmTarget::UTILITY)
  1420. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  1421. {
  1422. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  1423. // A library should not depend on itself!
  1424. emitted.insert(l->first);
  1425. // for static libraries do not depend on other libraries
  1426. if(l->second.GetType() != cmTarget::STATIC_LIBRARY)
  1427. {
  1428. // Now, look at all link libraries specific to this target.
  1429. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  1430. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  1431. lib != tlibs.end(); ++lib)
  1432. {
  1433. // Record that this library was used.
  1434. used.insert(lib->first);
  1435. // Don't emit the same library twice for this target.
  1436. if(emitted.insert(lib->first).second)
  1437. {
  1438. // Output this dependency.
  1439. this->OutputLibDepend(fout, lib->first.c_str());
  1440. }
  1441. }
  1442. }
  1443. // for all targets depend on utilities
  1444. // Now, look at all utilities specific to this target.
  1445. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  1446. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1447. util != tutils.end(); ++util)
  1448. {
  1449. // Record that this utility was used.
  1450. used.insert(*util);
  1451. // Don't emit the same utility twice for this target.
  1452. if(emitted.insert(*util).second)
  1453. {
  1454. // Output this dependency.
  1455. this->OutputExeDepend(fout, util->c_str());
  1456. }
  1457. }
  1458. fout << "\n";
  1459. }
  1460. }
  1461. fout << "\n";
  1462. // Loop over the libraries used and make sure there is a rule to
  1463. // build them in this makefile. If the library is in another
  1464. // directory, add a rule to jump to that directory and make sure it
  1465. // exists.
  1466. for(std::set<std::string>::const_iterator lib = used.begin();
  1467. lib != used.end(); ++lib)
  1468. {
  1469. // loop over the list of directories that the libraries might
  1470. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  1471. // be stored in the cache
  1472. std::string libPath = *lib + "_CMAKE_PATH";
  1473. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1474. // if cache and not the current directory add a rule, to
  1475. // jump into the directory and build for the first time
  1476. if(cacheValue && *cacheValue &&
  1477. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  1478. {
  1479. // add the correct extension
  1480. std::string ltname = *lib+"_LIBRARY_TYPE";
  1481. const char* libType
  1482. = m_Makefile->GetDefinition(ltname.c_str());
  1483. // if it was a library..
  1484. if (libType)
  1485. {
  1486. std::string library;
  1487. std::string libpath = cacheValue;
  1488. if(libType && std::string(libType) == "SHARED")
  1489. {
  1490. library = this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1491. library += *lib;
  1492. library += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1493. }
  1494. else if(libType && std::string(libType) == "MODULE")
  1495. {
  1496. library = this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1497. library += *lib;
  1498. library += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1499. }
  1500. else if(libType && std::string(libType) == "STATIC")
  1501. {
  1502. library = this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1503. library += *lib;
  1504. library += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1505. }
  1506. else
  1507. {
  1508. cmSystemTools::Error("Unknown library type!");
  1509. return;
  1510. }
  1511. if(m_LibraryOutputPath.size())
  1512. {
  1513. libpath = m_LibraryOutputPath;
  1514. }
  1515. else
  1516. {
  1517. libpath += "/";
  1518. }
  1519. libpath += library;
  1520. // put out a rule to build the library if it does not exist
  1521. this->OutputBuildTargetInDir(fout,
  1522. cacheValue,
  1523. library.c_str(),
  1524. libpath.c_str(),
  1525. m_Makefile->
  1526. GetDefinition("LIBRARY_OUTPUT_PATH")
  1527. );
  1528. }
  1529. // something other than a library...
  1530. else
  1531. {
  1532. std::string exepath = cacheValue;
  1533. if(m_ExecutableOutputPath.size())
  1534. {
  1535. exepath = m_ExecutableOutputPath;
  1536. }
  1537. else
  1538. {
  1539. exepath += "/";
  1540. }
  1541. std::string fullName = lib->c_str();
  1542. fullName += cmSystemTools::GetExecutableExtension();
  1543. exepath += fullName;
  1544. this->OutputBuildTargetInDir(fout,
  1545. cacheValue,
  1546. fullName.c_str(),
  1547. exepath.c_str(),
  1548. m_Makefile->
  1549. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  1550. );
  1551. }
  1552. }
  1553. }
  1554. }
  1555. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDirWindows(std::ostream& fout,
  1556. const char* path,
  1557. const char* library,
  1558. const char* fullpath,
  1559. const char* libOutPath)
  1560. {
  1561. const char* makeTarget = library;
  1562. std::string currentDir =
  1563. cmSystemTools::ConvertToOutputPath(m_Makefile->GetCurrentOutputDirectory());
  1564. std::string wpath = this->ConvertToOutputForExisting(path);
  1565. std::string wfullpath = this->ConvertToOutputForExisting(fullpath);
  1566. if(libOutPath && strcmp( libOutPath, "" ) != 0)
  1567. {
  1568. makeTarget = wfullpath.c_str();
  1569. }
  1570. fout << wfullpath
  1571. << ":\n\tcd " << wpath << "\n"
  1572. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.depends\n"
  1573. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) cmake.check_depends\n"
  1574. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) -f cmake.check_depends\n"
  1575. << "\t$(MAKE) $(MAKESILENT) " << makeTarget
  1576. << "\n\tcd " << currentDir << "\n";
  1577. }
  1578. void cmLocalUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  1579. const char* path,
  1580. const char* library,
  1581. const char* fullpath,
  1582. const char* outputPath)
  1583. {
  1584. if(m_WindowsShell)
  1585. {
  1586. this->OutputBuildTargetInDirWindows(fout, path, library, fullpath, outputPath);
  1587. return;
  1588. }
  1589. const char* makeTarget = library;
  1590. if(outputPath && strcmp( outputPath, "" ) != 0)
  1591. {
  1592. makeTarget = fullpath;
  1593. }
  1594. fout << this->ConvertToOutputForExisting(fullpath)
  1595. << ":\n\tcd " << this->ConvertToOutputForExisting(path)
  1596. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  1597. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  1598. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  1599. << "; $(MAKE) $(MAKESILENT) "
  1600. << cmSystemTools::ConvertToOutputPath(makeTarget) << "\n\n";
  1601. }
  1602. bool cmLocalUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  1603. {
  1604. if (strcmp(path1, path2) == 0)
  1605. {
  1606. return true;
  1607. }
  1608. #if defined(_WIN32) || defined(__APPLE__)
  1609. return
  1610. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path1)) ==
  1611. cmSystemTools::LowerCase(this->ConvertToOutputForExisting(path2));
  1612. #else
  1613. return false;
  1614. #endif
  1615. }
  1616. void cmLocalUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  1617. const char* name)
  1618. {
  1619. std::string libPath = name;
  1620. libPath += "_CMAKE_PATH";
  1621. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  1622. if( cacheValue && *cacheValue )
  1623. {
  1624. // if there is a cache value, then this is a library that cmake
  1625. // knows how to build, so we can depend on it
  1626. std::string libpath;
  1627. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1628. {
  1629. // if the library is not in the current directory, then get the full
  1630. // path to it
  1631. if(m_LibraryOutputPath.size())
  1632. {
  1633. libpath = m_LibraryOutputPath;
  1634. }
  1635. else
  1636. {
  1637. libpath = cacheValue;
  1638. libpath += "/";
  1639. }
  1640. }
  1641. else
  1642. {
  1643. // library is in current Makefile so use lib as a prefix
  1644. libpath = m_LibraryOutputPath;
  1645. }
  1646. // add the correct extension
  1647. std::string ltname = name;
  1648. ltname += "_LIBRARY_TYPE";
  1649. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1650. if(libType && std::string(libType) == "SHARED")
  1651. {
  1652. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_PREFIX");
  1653. libpath += name;
  1654. libpath += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1655. }
  1656. else if (libType && std::string(libType) == "MODULE")
  1657. {
  1658. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_PREFIX");
  1659. libpath += name;
  1660. libpath += this->GetSafeDefinition("CMAKE_SHARED_MODULE_SUFFIX");
  1661. }
  1662. else if (libType && std::string(libType) == "STATIC")
  1663. {
  1664. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1665. libpath += name;
  1666. libpath += this->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1667. }
  1668. fout << cmSystemTools::ConvertToOutputPath(libpath.c_str()) << " ";
  1669. }
  1670. }
  1671. void cmLocalUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1672. const char* name)
  1673. {
  1674. std::string exePath = name;
  1675. exePath += "_CMAKE_PATH";
  1676. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1677. if( cacheValue && *cacheValue )
  1678. {
  1679. // if there is a cache value, then this is a executable/utility that cmake
  1680. // knows how to build, so we can depend on it
  1681. std::string exepath;
  1682. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1683. {
  1684. // if the exe/utility is not in the current directory, then get the full
  1685. // path to it
  1686. if(m_ExecutableOutputPath.size())
  1687. {
  1688. exepath = m_ExecutableOutputPath;
  1689. }
  1690. else
  1691. {
  1692. exepath = cacheValue;
  1693. exepath += "/";
  1694. }
  1695. }
  1696. else
  1697. {
  1698. // library is in current Makefile
  1699. exepath = m_ExecutableOutputPath;
  1700. }
  1701. // add the library name
  1702. exepath += name;
  1703. // add the correct extension
  1704. exepath += cmSystemTools::GetExecutableExtension();
  1705. fout << cmSystemTools::ConvertToOutputPath(exepath.c_str()) << " ";
  1706. }
  1707. // if it isn't in the cache, it might still be a utility target
  1708. // so check for that
  1709. else
  1710. {
  1711. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1712. if (targets.find(name) != targets.end())
  1713. {
  1714. fout << name << " ";
  1715. }
  1716. }
  1717. }
  1718. // fix up names of directories so they can be used
  1719. // as targets in makefiles.
  1720. inline std::string FixDirectoryName(const char* dir)
  1721. {
  1722. std::string s = dir;
  1723. // replace ../ with 3 under bars
  1724. size_t pos = s.find("../");
  1725. if(pos != std::string::npos)
  1726. {
  1727. s.replace(pos, 3, "___");
  1728. }
  1729. // replace / directory separators with a single under bar
  1730. pos = s.find("/");
  1731. while(pos != std::string::npos)
  1732. {
  1733. s.replace(pos, 1, "_");
  1734. pos = s.find("/");
  1735. }
  1736. return s;
  1737. }
  1738. void cmLocalUnixMakefileGenerator::
  1739. BuildInSubDirectoryWindows(std::ostream& fout,
  1740. const char* directory,
  1741. const char* target1,
  1742. const char* target2,
  1743. bool silent)
  1744. {
  1745. if(target1)
  1746. {
  1747. std::string dir = this->ConvertToOutputForExisting(directory);
  1748. if(dir[0] == '\"')
  1749. {
  1750. dir = dir.substr(1, dir.size()-2);
  1751. }
  1752. fout << "\tif not exist \"" << dir << "\\$(NULL)\""
  1753. << " "
  1754. << "$(MAKE) $(MAKESILENT) rebuild_cache\n";
  1755. if (!silent)
  1756. {
  1757. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1758. }
  1759. fout << "\tcd " << dir << "\n"
  1760. << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target1 << "\n";
  1761. }
  1762. if(target2)
  1763. {
  1764. if (!silent)
  1765. {
  1766. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1767. }
  1768. fout << "\t$(MAKE) -$(MAKEFLAGS) $(MAKESILENT) " << target2 << "\n";
  1769. }
  1770. std::string currentDir = m_Makefile->GetCurrentOutputDirectory();
  1771. fout << "\tcd " << this->ConvertToOutputForExisting(currentDir.c_str()) << "\n\n";
  1772. }
  1773. void cmLocalUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1774. const char* dir,
  1775. const char* target1,
  1776. const char* target2,
  1777. bool silent)
  1778. {
  1779. if(m_WindowsShell)
  1780. {
  1781. this->BuildInSubDirectoryWindows(fout, dir, target1, target2, silent);
  1782. return;
  1783. }
  1784. std::string directory = cmSystemTools::ConvertToOutputPath(dir);
  1785. if(target1)
  1786. {
  1787. fout << "\t@if test ! -d " << directory
  1788. << "; then $(MAKE) rebuild_cache; fi\n";
  1789. if (!silent)
  1790. {
  1791. fout << "\t@echo " << directory << ": building " << target1 << "\n";
  1792. }
  1793. fout << "\t@cd " << directory
  1794. << "; $(MAKE) " << target1 << "\n";
  1795. }
  1796. if(target2)
  1797. {
  1798. if (!silent)
  1799. {
  1800. fout << "\t@echo " << directory << ": building " << target2 << "\n";
  1801. }
  1802. fout << "\t@cd " << directory
  1803. << "; $(MAKE) " << target2 << "\n";
  1804. }
  1805. fout << "\n";
  1806. }
  1807. void
  1808. cmLocalUnixMakefileGenerator::
  1809. OutputSubDirectoryVars(std::ostream& fout,
  1810. const char* var,
  1811. const char* target,
  1812. const char* target1,
  1813. const char* target2,
  1814. const char* depend,
  1815. const std::vector<std::string>& SubDirectories,
  1816. bool silent)
  1817. {
  1818. if(!depend)
  1819. {
  1820. depend = "";
  1821. }
  1822. if( SubDirectories.size() == 0)
  1823. {
  1824. return;
  1825. }
  1826. fout << "# Variable for making " << target << " in subdirectories.\n";
  1827. fout << var << " = \\\n";
  1828. unsigned int ii;
  1829. for(ii =0; ii < SubDirectories.size(); ii++)
  1830. {
  1831. std::string subdir = FixDirectoryName(SubDirectories[ii].c_str());
  1832. fout << target << "_" << subdir.c_str();
  1833. if(ii == SubDirectories.size()-1)
  1834. {
  1835. fout << " \n\n";
  1836. }
  1837. else
  1838. {
  1839. fout << " \\\n";
  1840. }
  1841. }
  1842. fout << "# Targets for making " << target << " in subdirectories.\n";
  1843. std::string last = "";
  1844. for(unsigned int cc =0; cc < SubDirectories.size(); cc++)
  1845. {
  1846. std::string subdir = FixDirectoryName(SubDirectories[cc].c_str());
  1847. fout << target << "_" << subdir.c_str() << ": " << depend;
  1848. // Make each subdirectory depend on previous one. This forces
  1849. // parallel builds (make -j 2) to build in same order as a single
  1850. // threaded build to avoid dependency problems.
  1851. if(cc > 0)
  1852. {
  1853. fout << " " << target << "_" << last.c_str();
  1854. }
  1855. fout << "\n";
  1856. last = subdir;
  1857. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1858. dir += "/";
  1859. dir += SubDirectories[cc];
  1860. this->BuildInSubDirectory(fout, dir.c_str(),
  1861. target1, target2, silent);
  1862. }
  1863. fout << "\n\n";
  1864. }
  1865. // output rules for decending into sub directories
  1866. void cmLocalUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1867. {
  1868. // Output Sub directory build rules
  1869. const std::vector<std::string>& SubDirectories
  1870. = m_Makefile->GetSubDirectories();
  1871. if( SubDirectories.size() == 0)
  1872. {
  1873. return;
  1874. }
  1875. this->OutputSubDirectoryVars(fout,
  1876. "SUBDIR_BUILD",
  1877. "default_target",
  1878. "default_target",
  1879. 0, "$(TARGETS)",
  1880. SubDirectories,
  1881. false);
  1882. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1883. "clean",
  1884. 0, 0,
  1885. SubDirectories);
  1886. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1887. "depend",
  1888. 0, 0,
  1889. SubDirectories);
  1890. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1891. "install",
  1892. 0, 0,
  1893. SubDirectories);
  1894. }
  1895. // Output the depend information for all the classes
  1896. // in the makefile. These would have been generated
  1897. // by the class cmMakeDepend GenerateMakefile
  1898. bool cmLocalUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1899. {
  1900. bool ret = false;
  1901. // Iterate over every target.
  1902. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1903. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1904. target != targets.end(); ++target)
  1905. {
  1906. // Iterate over every source for this target.
  1907. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1908. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1909. source != sources.end(); ++source)
  1910. {
  1911. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1912. {
  1913. if(!(*source)->GetDepends().empty())
  1914. {
  1915. // Iterate through all the dependencies for this source.
  1916. for(std::vector<std::string>::const_iterator dep =
  1917. (*source)->GetDepends().begin();
  1918. dep != (*source)->GetDepends().end(); ++dep)
  1919. {
  1920. std::string s = (*source)->GetSourceName();
  1921. s += this->GetOutputExtension((*source)->GetSourceExtension().c_str());
  1922. fout << cmSystemTools::ConvertToOutputPath(s.c_str()) << " : "
  1923. << cmSystemTools::ConvertToOutputPath(dep->c_str()) << "\n";
  1924. ret = true;
  1925. }
  1926. fout << "\n\n";
  1927. }
  1928. }
  1929. }
  1930. }
  1931. return ret;
  1932. }
  1933. // Output the depend information for all the classes
  1934. // in the makefile. These would have been generated
  1935. // by the class cmMakeDepend GenerateMakefile
  1936. void cmLocalUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1937. {
  1938. std::set<std::string> emittedLowerPath;
  1939. std::set<std::string> emitted;
  1940. // Iterate over every target.
  1941. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1942. fout << "# Suppresses display of executed commands\n";
  1943. fout << ".SILENT:\n";
  1944. fout << "# disable some common implicit rules to speed things up\n";
  1945. fout << ".SUFFIXES:\n";
  1946. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1947. this->OutputMakeVariables(fout);
  1948. fout << "default:\n";
  1949. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1950. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1951. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1952. target != targets.end(); ++target)
  1953. {
  1954. // Iterate over every source for this target.
  1955. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1956. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1957. source != sources.end(); ++source)
  1958. {
  1959. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1960. {
  1961. if(!(*source)->GetDepends().empty())
  1962. {
  1963. for(std::vector<std::string>::const_iterator dep =
  1964. (*source)->GetDepends().begin();
  1965. dep != (*source)->GetDepends().end(); ++dep)
  1966. {
  1967. // do not call CollapseFullPath on dep here, because it already
  1968. // has been done because m_FullPath on cmDependInformation
  1969. // always is it called. If it is called here, network builds are
  1970. // very slow because of the number of stats
  1971. std::string dependfile = cmSystemTools::ConvertToOutputPath(dep->c_str());
  1972. // use the lower path function to create uniqe names
  1973. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1974. if(emittedLowerPath.insert(lowerpath).second)
  1975. {
  1976. emitted.insert(dependfile);
  1977. fout << "all: " << dependfile << "\n";
  1978. }
  1979. }
  1980. }
  1981. }
  1982. }
  1983. }
  1984. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1985. std::set<std::string>::iterator i;
  1986. for(i = emitted.begin(); i != emitted.end(); ++i)
  1987. {
  1988. fout << "cmake.depends: " << *i << "\n";
  1989. }
  1990. fout << "cmake.depends: \n"
  1991. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1992. fout << "\n\n";
  1993. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1994. for(std::set<std::string>::iterator it = emitted.begin();
  1995. it != emitted.end(); ++it)
  1996. {
  1997. fout << *it << ":\n"
  1998. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1999. }
  2000. }
  2001. // Output each custom rule in the following format:
  2002. // output: source depends...
  2003. // (tab) command...
  2004. void cmLocalUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  2005. {
  2006. // we cannot provide multiple rules for a single output
  2007. // so we will keep track of outputs to make sure we don't write
  2008. // two rules. First found wins
  2009. std::set<std::string> processedOutputs;
  2010. // first output all custom rules
  2011. const std::vector<cmSourceFile*>& sources = m_Makefile->GetSourceFiles();
  2012. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  2013. i != sources.end(); ++i)
  2014. {
  2015. if ((*i)->GetCustomCommand())
  2016. {
  2017. cmCustomCommand *c = (*i)->GetCustomCommand();
  2018. // escape spaces and convert to native slashes path for
  2019. // the command
  2020. std::string comment = c->GetComment();
  2021. std::string command = c->GetCommand();
  2022. cmSystemTools::ReplaceString(command, "/./", "/");
  2023. command = cmSystemTools::ConvertToOutputPath(command.c_str());
  2024. command += " ";
  2025. // now add the arguments
  2026. command += c->GetArguments();
  2027. std::string depends;
  2028. // Collect out all the dependencies for this rule.
  2029. for(std::vector<std::string>::const_iterator d =
  2030. c->GetDepends().begin();
  2031. d != c->GetDepends().end(); ++d)
  2032. {
  2033. std::string dep = *d;
  2034. m_Makefile->ExpandVariablesInString(dep);
  2035. // watch for target dependencies,
  2036. std::string libPath = dep + "_CMAKE_PATH";
  2037. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  2038. if ( cacheValue && *cacheValue )
  2039. {
  2040. libPath = cacheValue;
  2041. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH") &&
  2042. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")[0] != '\0')
  2043. {
  2044. libPath = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2045. }
  2046. libPath += "/";
  2047. libPath += dep;
  2048. libPath += cmSystemTools::GetExecutableExtension();
  2049. dep = libPath;
  2050. }
  2051. cmSystemTools::ReplaceString(dep, "/./", "/");
  2052. cmSystemTools::ReplaceString(dep, "/$(IntDir)/", "/");
  2053. dep = cmSystemTools::ConvertToOutputPath(dep.c_str());
  2054. depends += " ";
  2055. depends += dep;
  2056. }
  2057. // output rule
  2058. if (processedOutputs.find(c->GetOutput()) == processedOutputs.end())
  2059. {
  2060. this->OutputMakeRule(fout,
  2061. (comment.size()?comment.c_str():"Custom command"),
  2062. c->GetOutput().c_str(),
  2063. depends.c_str(),
  2064. command.c_str());
  2065. processedOutputs.insert(c->GetOutput());
  2066. }
  2067. else
  2068. {
  2069. cmSystemTools::Error("An output was found with multiple rules on how to build it for output: ",
  2070. c->GetOutput().c_str());
  2071. }
  2072. }
  2073. }
  2074. }
  2075. std::string
  2076. cmLocalUnixMakefileGenerator::ConvertToOutputForExisting(const char* p)
  2077. {
  2078. std::string ret = cmSystemTools::ConvertToOutputPath(p);
  2079. // if there are spaces in the path, then get the short path version
  2080. // if there is one
  2081. if(ret.find(' ') != std::string::npos)
  2082. {
  2083. if(cmSystemTools::FileExists(p))
  2084. {
  2085. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  2086. {
  2087. ret = cmSystemTools::ConvertToOutputPath(p);
  2088. }
  2089. }
  2090. }
  2091. return ret;
  2092. }
  2093. void cmLocalUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  2094. {
  2095. const char* variables =
  2096. "# the standard shell for make\n"
  2097. "SHELL = /bin/sh\n"
  2098. "\n";
  2099. if(!m_WindowsShell)
  2100. {
  2101. fout << variables;
  2102. }
  2103. else
  2104. {
  2105. fout <<
  2106. "!IF \"$(OS)\" == \"Windows_NT\"\n"
  2107. "NULL=\n"
  2108. "!ELSE \n"
  2109. "NULL=nul\n"
  2110. "!ENDIF \n";
  2111. }
  2112. if(m_MakeSilentFlag.size())
  2113. {
  2114. fout << "MAKESILENT = " << m_MakeSilentFlag << "\n";
  2115. }
  2116. std::string cmakecommand = this->ConvertToOutputForExisting(
  2117. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  2118. fout << "CMAKE_COMMAND = "
  2119. << cmakecommand
  2120. << "\n";
  2121. fout << "RM = " << cmakecommand.c_str() << " -E remove -f\n";
  2122. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2123. {
  2124. fout << "CMAKE_EDIT_COMMAND = "
  2125. << this->ConvertToOutputForExisting(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2126. << "\n";
  2127. }
  2128. fout << "CMAKE_CURRENT_SOURCE = " <<
  2129. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartDirectory())
  2130. << "\n";
  2131. fout << "CMAKE_CURRENT_BINARY = " <<
  2132. cmSystemTools::ConvertToOutputPath(m_Makefile->GetStartOutputDirectory())
  2133. << "\n";
  2134. fout << "CMAKE_SOURCE_DIR = " <<
  2135. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeDirectory())
  2136. << "\n";
  2137. fout << "CMAKE_BINARY_DIR = " <<
  2138. cmSystemTools::ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory())
  2139. << "\n";
  2140. // Output Include paths
  2141. fout << "INCLUDE_FLAGS = ";
  2142. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  2143. std::vector<std::string>::iterator i;
  2144. fout << "-I" <<
  2145. this->ConvertToOutputForExisting(m_Makefile->GetStartDirectory()) << " ";
  2146. for(i = includes.begin(); i != includes.end(); ++i)
  2147. {
  2148. std::string include = *i;
  2149. // Don't output a -I for the standard include path "/usr/include".
  2150. // This can cause problems with certain standard library
  2151. // implementations because the wrong headers may be found first.
  2152. if(include != "/usr/include")
  2153. {
  2154. fout << "-I" << this->ConvertToOutputForExisting(i->c_str()) << " ";
  2155. }
  2156. }
  2157. fout << m_Makefile->GetDefineFlags();
  2158. fout << "\n\n";
  2159. }
  2160. void cmLocalUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  2161. {
  2162. // Install is not currently supported on windows
  2163. if(m_WindowsShell)
  2164. {
  2165. return;
  2166. }
  2167. std::string cmakecommand = this->ConvertToOutputForExisting(
  2168. m_Makefile->GetDefinition("CMAKE_COMMAND"));
  2169. const char* root
  2170. = m_Makefile->GetDefinition("CMAKE_ROOT");
  2171. fout << "INSTALL = \"" << root << "/Templates/install-sh\" -c\n";
  2172. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  2173. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  2174. const cmTargets &tgts = m_Makefile->GetTargets();
  2175. fout << "install: $(SUBDIR_INSTALL)\n";
  2176. fout << "\t@echo \"Installing ...\"\n";
  2177. const char* prefix
  2178. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  2179. if (!prefix)
  2180. {
  2181. prefix = "/usr/local";
  2182. }
  2183. for(cmTargets::const_iterator l = tgts.begin();
  2184. l != tgts.end(); l++)
  2185. {
  2186. if (l->second.GetInstallPath() != "")
  2187. {
  2188. // first make the directories for each target
  2189. fout << "\t@if [ ! -d \"$(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  2190. "\" ] ; then \\\n";
  2191. fout << "\t echo \"Making directory \\\"$(DESTDIR)" << prefix
  2192. << l->second.GetInstallPath() << "\\\" \"; \\\n";
  2193. fout << "\t mkdir -p \"$(DESTDIR)" << prefix << l->second.GetInstallPath()
  2194. << "\"; \\\n";
  2195. fout << "\t chmod 755 \"$(DESTDIR)" << prefix << l->second.GetInstallPath()
  2196. << "\"; \\\n";
  2197. fout << "\t else true; \\\n";
  2198. fout << "\t fi\n";
  2199. std::string fname;
  2200. // now install the target
  2201. switch (l->second.GetType())
  2202. {
  2203. case cmTarget::STATIC_LIBRARY:
  2204. case cmTarget::SHARED_LIBRARY:
  2205. case cmTarget::MODULE_LIBRARY:
  2206. {
  2207. std::string targetName;
  2208. std::string targetNameSO;
  2209. std::string targetNameReal;
  2210. std::string targetNameBase;
  2211. this->GetLibraryNames(l->first.c_str(), l->second,
  2212. targetName, targetNameSO,
  2213. targetNameReal, targetNameBase);
  2214. std::string installName = "$(DESTDIR)";
  2215. installName += prefix;
  2216. installName += l->second.GetInstallPath();
  2217. installName += "/";
  2218. std::string installNameSO = installName;
  2219. std::string installNameReal = installName;
  2220. installName += targetName;
  2221. installNameSO += targetNameSO;
  2222. installNameReal += targetNameReal;
  2223. fname = m_LibraryOutputPath;
  2224. fname += targetNameReal;
  2225. fout << "\t$(INSTALL_DATA) " << cmSystemTools::ConvertToOutputPath(fname.c_str())
  2226. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2227. fout << "\t" << cmakecommand << " -E cmake_symlink_library \""
  2228. << installNameReal << "\" \"" << installNameSO << "\" \"" << installName
  2229. << "\"\n";
  2230. }; break;
  2231. case cmTarget::WIN32_EXECUTABLE:
  2232. case cmTarget::EXECUTABLE:
  2233. fname = m_ExecutableOutputPath;
  2234. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  2235. fout << "\t$(INSTALL_PROGRAM) " << cmSystemTools::ConvertToOutputPath(fname.c_str())
  2236. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2237. break;
  2238. case cmTarget::INSTALL_FILES:
  2239. {
  2240. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2241. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2242. sourcePath += "/";
  2243. binaryPath += "/";
  2244. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2245. std::vector<std::string>::const_iterator i;
  2246. for (i = sf.begin(); i != sf.end(); ++i)
  2247. {
  2248. std::string f = *i;
  2249. if(f.substr(0, sourcePath.length()) == sourcePath)
  2250. {
  2251. f = f.substr(sourcePath.length());
  2252. }
  2253. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2254. {
  2255. f = f.substr(binaryPath.length());
  2256. }
  2257. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2258. // avoid using install-sh to install install-sh
  2259. // does not work on windows....
  2260. if(*i == "install-sh")
  2261. {
  2262. fout << "\tcp ";
  2263. }
  2264. else
  2265. {
  2266. fout << "\t$(INSTALL_DATA) ";
  2267. }
  2268. fout << cmSystemTools::ConvertToOutputPath(i->c_str())
  2269. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2270. }
  2271. }
  2272. break;
  2273. case cmTarget::INSTALL_PROGRAMS:
  2274. {
  2275. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  2276. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  2277. sourcePath += "/";
  2278. binaryPath += "/";
  2279. const std::vector<std::string> &sf = l->second.GetSourceLists();
  2280. std::vector<std::string>::const_iterator i;
  2281. for (i = sf.begin(); i != sf.end(); ++i)
  2282. {
  2283. std::string f = *i;
  2284. if(f.substr(0, sourcePath.length()) == sourcePath)
  2285. {
  2286. f = f.substr(sourcePath.length());
  2287. }
  2288. else if(f.substr(0, binaryPath.length()) == binaryPath)
  2289. {
  2290. f = f.substr(binaryPath.length());
  2291. }
  2292. fout << "\t@echo \"Installing " << f.c_str() << " \"\n";
  2293. // avoid using install-sh to install install-sh
  2294. // does not work on windows....
  2295. if(*i == "install-sh")
  2296. {
  2297. fout << "\t @cp ";
  2298. }
  2299. else
  2300. {
  2301. fout << "\t @$(INSTALL_PROGRAM) ";
  2302. }
  2303. fout << cmSystemTools::ConvertToOutputPath(i->c_str())
  2304. << " \"$(DESTDIR)" << prefix << l->second.GetInstallPath() << "\"\n";
  2305. }
  2306. }
  2307. break;
  2308. case cmTarget::UTILITY:
  2309. default:
  2310. break;
  2311. }
  2312. }
  2313. }
  2314. }
  2315. void cmLocalUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  2316. {
  2317. this->OutputMakeRule(fout,
  2318. "default build rule",
  2319. "all",
  2320. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  2321. 0);
  2322. this->OutputMakeRule(fout,
  2323. "clean generated files",
  2324. "clean",
  2325. "$(SUBDIR_CLEAN)",
  2326. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  2327. " $(TARGETS) $(TARGET_EXTRAS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  2328. // collect up all the sources
  2329. std::vector<std::string> allsources;
  2330. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2331. for(std::map<cmStdString,cmTarget>::const_iterator target = targets.begin();
  2332. target != targets.end(); ++target)
  2333. {
  2334. // Iterate over every source for this target.
  2335. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  2336. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2337. source != sources.end(); ++source)
  2338. {
  2339. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  2340. {
  2341. allsources.push_back(cmSystemTools::ConvertToOutputPath((*source)->GetFullPath().c_str()));
  2342. }
  2343. }
  2344. }
  2345. std::string checkCache = m_Makefile->GetHomeOutputDirectory();
  2346. checkCache += "/cmake.check_cache";
  2347. checkCache = cmSystemTools::ConvertToOutputPath(checkCache.c_str());
  2348. std::vector<std::string> cmake_depends;
  2349. cmake_depends.push_back(checkCache);
  2350. cmake_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2351. this->OutputMakeRule(fout,
  2352. "dependencies.",
  2353. "cmake.depends",
  2354. cmake_depends,
  2355. "$(CMAKE_COMMAND) "
  2356. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2357. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2358. this->OutputMakeRule(fout,
  2359. "dependencies",
  2360. "cmake.check_depends",
  2361. allsources,
  2362. "$(CMAKE_COMMAND) "
  2363. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2364. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2365. this->OutputMakeRule(fout,
  2366. "dependencies",
  2367. "depend",
  2368. "$(SUBDIR_DEPEND)",
  2369. "$(CMAKE_COMMAND) "
  2370. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2371. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2372. this->OutputMakeRule(fout,
  2373. "dependencies",
  2374. "dependlocal",
  2375. 0,
  2376. "$(CMAKE_COMMAND) "
  2377. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  2378. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2379. this->OutputMakeRule(fout,
  2380. "CMakeCache.txt",
  2381. "rebuild_cache",
  2382. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2383. "$(CMAKE_COMMAND) "
  2384. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2385. std::vector<std::string> check_cache_depends;
  2386. std::string CMakeCache = m_Makefile->GetHomeOutputDirectory();
  2387. CMakeCache += "/CMakeCache.txt";
  2388. CMakeCache = cmSystemTools::ConvertToOutputPath(CMakeCache.c_str());
  2389. check_cache_depends.push_back(CMakeCache);
  2390. check_cache_depends.push_back("$(CMAKE_MAKEFILE_SOURCES)");
  2391. this->OutputMakeRule(fout,
  2392. "CMakeCache.txt because out-of-date:",
  2393. checkCache.c_str(),
  2394. check_cache_depends,
  2395. "$(CMAKE_COMMAND) "
  2396. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2397. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  2398. // called edit_cache
  2399. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  2400. {
  2401. this->OutputMakeRule(fout,
  2402. "edit CMakeCache.txt",
  2403. "edit_cache",
  2404. 0,
  2405. "$(CMAKE_EDIT_COMMAND) "
  2406. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2407. }
  2408. else
  2409. {
  2410. this->OutputMakeRule(fout,
  2411. "edit CMakeCache.txt",
  2412. "edit_cache",
  2413. 0,
  2414. "$(CMAKE_COMMAND) "
  2415. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -i");
  2416. }
  2417. this->OutputMakeRule(fout,
  2418. "CMakeCache.txt",
  2419. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  2420. 0,
  2421. "$(CMAKE_COMMAND) "
  2422. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  2423. this->OutputMakeRule(fout,
  2424. "Rule to keep make from removing Makefiles "
  2425. "if control-C is hit during a run of cmake.",
  2426. ".PRECIOUS",
  2427. "Makefile cmake.depends",
  2428. 0);
  2429. this->OutputSourceObjectBuildRules(fout);
  2430. // find ctest
  2431. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2432. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2433. ctest += "/";
  2434. ctest += "ctest";
  2435. ctest += cmSystemTools::GetExecutableExtension();
  2436. if(!cmSystemTools::FileExists(ctest.c_str()))
  2437. {
  2438. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2439. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2440. ctest += "/Debug/";
  2441. ctest += "ctest";
  2442. ctest += cmSystemTools::GetExecutableExtension();
  2443. }
  2444. if(!cmSystemTools::FileExists(ctest.c_str()))
  2445. {
  2446. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  2447. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  2448. ctest += "/Release/";
  2449. ctest += "ctest";
  2450. ctest += cmSystemTools::GetExecutableExtension();
  2451. }
  2452. if (!cmSystemTools::FileExists(ctest.c_str()))
  2453. {
  2454. if ( !m_Makefile->GetDefinition("PROJECT_NAME") ||
  2455. strcmp(m_Makefile->GetDefinition("PROJECT_NAME"), "CMake") )
  2456. {
  2457. return;
  2458. }
  2459. ctest = m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  2460. ctest += "/ctest";
  2461. }
  2462. fout << "ARGS=\n";
  2463. std::string cmd = this->ConvertToOutputForExisting(ctest.c_str());
  2464. cmd += " $(ARGS)";
  2465. this->OutputMakeRule(fout,
  2466. "tests",
  2467. "test",
  2468. "",
  2469. cmd.c_str());
  2470. }
  2471. void
  2472. cmLocalUnixMakefileGenerator::
  2473. OutputBuildObjectFromSource(std::ostream& fout,
  2474. const char* shortName,
  2475. const cmSourceFile& source,
  2476. const char* extraCompileFlags,
  2477. bool shared)
  2478. {
  2479. // Header files shouldn't have build rules.
  2480. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  2481. {
  2482. return;
  2483. }
  2484. std::string comment = "object file";
  2485. std::string objectFile = std::string(shortName) +
  2486. this->GetOutputExtension(source.GetSourceExtension().c_str());
  2487. objectFile = cmSystemTools::ConvertToOutputPath(objectFile.c_str());
  2488. cmSystemTools::FileFormat format =
  2489. cmSystemTools::GetFileFormat(source.GetSourceExtension().c_str());
  2490. std::vector<std::string> rules;
  2491. std::string flags;
  2492. if(extraCompileFlags)
  2493. {
  2494. flags += extraCompileFlags;
  2495. }
  2496. std::string sourceFile =
  2497. cmSystemTools::ConvertToOutputPath(source.GetFullPath().c_str());
  2498. std::string buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2499. buildType = cmSystemTools::UpperCase(buildType);
  2500. switch(format)
  2501. {
  2502. case cmSystemTools::C_FILE_FORMAT:
  2503. {
  2504. rules.push_back(m_Makefile->GetDefinition("CMAKE_C_COMPILE_OBJECT"));
  2505. flags += this->GetSafeDefinition("CMAKE_C_FLAGS");
  2506. flags += " ";
  2507. if(buildType.size())
  2508. {
  2509. std::string build = "CMAKE_C_FLAGS_";
  2510. build += buildType;
  2511. flags += this->GetSafeDefinition(build.c_str());
  2512. flags += " ";
  2513. }
  2514. if(shared)
  2515. {
  2516. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_C_FLAGS");
  2517. flags += " ";
  2518. }
  2519. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2520. {
  2521. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_C_FLAGS");
  2522. flags += " ";
  2523. }
  2524. break;
  2525. }
  2526. case cmSystemTools::CXX_FILE_FORMAT:
  2527. {
  2528. rules.push_back(m_Makefile->GetDefinition("CMAKE_CXX_COMPILE_OBJECT"));
  2529. flags += this->GetSafeDefinition("CMAKE_CXX_FLAGS");
  2530. flags += " ";
  2531. if(buildType.size())
  2532. {
  2533. std::string build = "CMAKE_CXX_FLAGS_";
  2534. build += buildType;
  2535. flags += this->GetSafeDefinition(build.c_str());
  2536. flags += " ";
  2537. }
  2538. if(shared)
  2539. {
  2540. flags += this->GetSafeDefinition("CMAKE_SHARED_LIBRARY_CXX_FLAGS");
  2541. flags += " ";
  2542. }
  2543. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  2544. {
  2545. flags += this->GetSafeDefinition("CMAKE_SHARED_BUILD_CXX_FLAGS");
  2546. flags += " ";
  2547. }
  2548. break;
  2549. }
  2550. case cmSystemTools::HEADER_FILE_FORMAT:
  2551. return;
  2552. case cmSystemTools::DEFINITION_FILE_FORMAT:
  2553. return;
  2554. case cmSystemTools::RESOURCE_FILE_FORMAT:
  2555. {
  2556. flags = " $(INCLUDE_FLAGS) ";
  2557. // use rc rule here if it is defined
  2558. const char* rule = m_Makefile->GetDefinition("CMAKE_COMPILE_RESOURCE");
  2559. if(rule)
  2560. {
  2561. rules.push_back(rule);
  2562. }
  2563. }
  2564. break;
  2565. case cmSystemTools::NO_FILE_FORMAT:
  2566. case cmSystemTools::JAVA_FILE_FORMAT:
  2567. case cmSystemTools::STATIC_LIBRARY_FILE_FORMAT:
  2568. case cmSystemTools::SHARED_LIBRARY_FILE_FORMAT:
  2569. case cmSystemTools::MODULE_FILE_FORMAT:
  2570. case cmSystemTools::OBJECT_FILE_FORMAT:
  2571. case cmSystemTools::UNKNOWN_FILE_FORMAT:
  2572. cmSystemTools::Error("Unexpected file type ",
  2573. sourceFile.c_str());
  2574. break;
  2575. }
  2576. flags += "$(INCLUDE_FLAGS) ";
  2577. // expand multi-command semi-colon separated lists
  2578. // of commands into separate commands
  2579. std::vector<std::string> commands;
  2580. cmSystemTools::ExpandList(rules, commands);
  2581. for(std::vector<std::string>::iterator i = commands.begin();
  2582. i != commands.end(); ++i)
  2583. {
  2584. this->ExpandRuleVariables(*i,
  2585. 0, // no objects
  2586. 0, // no target
  2587. 0, // no link libs
  2588. sourceFile.c_str(),
  2589. objectFile.c_str(),
  2590. flags.c_str());
  2591. }
  2592. std::vector<std::string> sourceAndDeps;
  2593. sourceAndDeps.push_back(sourceFile);
  2594. // Check for extra object-file dependencies.
  2595. std::vector<std::string> depends;
  2596. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  2597. if(additionalDeps)
  2598. {
  2599. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  2600. for(std::vector<std::string>::iterator i = depends.begin();
  2601. i != depends.end(); ++i)
  2602. {
  2603. sourceAndDeps.push_back(cmSystemTools::ConvertToOutputPath(i->c_str()));
  2604. }
  2605. }
  2606. this->OutputMakeRule(fout,
  2607. comment.c_str(),
  2608. objectFile.c_str(),
  2609. sourceAndDeps,
  2610. commands);
  2611. }
  2612. void cmLocalUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  2613. {
  2614. fout << "# Rules to build " << this->GetOutputExtension("")
  2615. << " files from their sources:\n";
  2616. std::set<std::string> rules;
  2617. // Iterate over every target.
  2618. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  2619. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  2620. target != targets.end(); ++target)
  2621. {
  2622. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  2623. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  2624. std::string exportsDef = "";
  2625. if(shared)
  2626. {
  2627. std::string export_symbol;
  2628. if (const char* custom_export_name = target->second.GetProperty("DEFINE_SYMBOL"))
  2629. {
  2630. export_symbol = custom_export_name;
  2631. }
  2632. else
  2633. {
  2634. std::string in = target->first + "_EXPORTS";
  2635. export_symbol = cmSystemTools::MakeCindentifier(in.c_str());
  2636. }
  2637. exportsDef = "-D"+ export_symbol;
  2638. }
  2639. // Iterate over every source for this target.
  2640. const std::vector<cmSourceFile*>& sources =
  2641. target->second.GetSourceFiles();
  2642. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  2643. source != sources.end(); ++source)
  2644. {
  2645. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  2646. !(*source)->GetCustomCommand())
  2647. {
  2648. std::string shortName;
  2649. std::string sourceName;
  2650. // If the full path to the source file includes this
  2651. // directory, we want to use the relative path for the
  2652. // filename of the object file. Otherwise, we will use just
  2653. // the filename portion.
  2654. if((cmSystemTools::GetFilenamePath(
  2655. (*source)->GetFullPath()).find(
  2656. m_Makefile->GetCurrentDirectory()) == 0)
  2657. || (cmSystemTools::GetFilenamePath(
  2658. (*source)->GetFullPath()).find(
  2659. m_Makefile->GetCurrentOutputDirectory()) == 0))
  2660. {
  2661. sourceName = (*source)->GetSourceName()+"."+
  2662. (*source)->GetSourceExtension();
  2663. shortName = (*source)->GetSourceName();
  2664. // The path may be relative. See if a directory needs to be
  2665. // created for the output file. This is a ugly, and perhaps
  2666. // should be moved elsewhere.
  2667. std::string relPath =
  2668. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  2669. if(relPath != "")
  2670. {
  2671. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  2672. outPath += "/"+relPath;
  2673. cmSystemTools::MakeDirectory(outPath.c_str());
  2674. }
  2675. }
  2676. else
  2677. {
  2678. sourceName = (*source)->GetFullPath();
  2679. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  2680. }
  2681. std::string shortNameWithExt = shortName +
  2682. (*source)->GetSourceExtension();
  2683. // Only output a rule for each .o once.
  2684. std::string compileFlags = exportsDef;
  2685. compileFlags += " ";
  2686. if(rules.find(shortNameWithExt) == rules.end())
  2687. {
  2688. if((*source)->GetProperty("COMPILE_FLAGS"))
  2689. {
  2690. compileFlags += (*source)->GetProperty("COMPILE_FLAGS");
  2691. compileFlags += " ";
  2692. }
  2693. this->OutputBuildObjectFromSource(fout,
  2694. shortName.c_str(),
  2695. *(*source),
  2696. compileFlags.c_str(),
  2697. shared);
  2698. rules.insert(shortNameWithExt);
  2699. }
  2700. }
  2701. }
  2702. }
  2703. }
  2704. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2705. const char* comment,
  2706. const char* target,
  2707. const char* depends,
  2708. const char* command,
  2709. const char* command2,
  2710. const char* command3,
  2711. const char* command4)
  2712. {
  2713. std::vector<std::string> commands;
  2714. if(command)
  2715. {
  2716. commands.push_back(command);
  2717. }
  2718. if(command2)
  2719. {
  2720. commands.push_back(command2);
  2721. }
  2722. if(command3)
  2723. {
  2724. commands.push_back(command3);
  2725. }
  2726. if(command4)
  2727. {
  2728. commands.push_back(command4);
  2729. }
  2730. this->OutputMakeRule(fout, comment, target, depends, commands);
  2731. }
  2732. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2733. const char* comment,
  2734. const char* target,
  2735. const char* depends,
  2736. const std::vector<std::string>& commands)
  2737. {
  2738. std::vector<std::string> depend;
  2739. if(depends)
  2740. {
  2741. depend.push_back(depends);
  2742. }
  2743. this->OutputMakeRule(fout, comment, target, depend, commands);
  2744. }
  2745. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2746. const char* comment,
  2747. const char* target,
  2748. const std::vector<std::string>& depends,
  2749. const char* command)
  2750. {
  2751. std::vector<std::string> commands;
  2752. if(command)
  2753. {
  2754. commands.push_back(command);
  2755. }
  2756. this->OutputMakeRule(fout, comment, target, depends, commands);
  2757. }
  2758. void cmLocalUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  2759. const char* comment,
  2760. const char* target,
  2761. const std::vector<std::string>& depends,
  2762. const std::vector<std::string>& commands)
  2763. {
  2764. if(!target)
  2765. {
  2766. cmSystemTools::Error("no target for OutputMakeRule");
  2767. return;
  2768. }
  2769. std::string replace;
  2770. if(comment)
  2771. {
  2772. replace = comment;
  2773. m_Makefile->ExpandVariablesInString(replace);
  2774. fout << "#---------------------------------------------------------\n";
  2775. fout << "# " << replace;
  2776. fout << "\n#\n";
  2777. }
  2778. fout << "\n";
  2779. replace = target;
  2780. m_Makefile->ExpandVariablesInString(replace);
  2781. std::string tgt = cmSystemTools::ConvertToOutputPath(replace.c_str());
  2782. if(depends.empty())
  2783. {
  2784. fout << tgt.c_str() << ":\n";
  2785. }
  2786. else
  2787. {
  2788. // Split dependencies into multiple rule lines. This allows for
  2789. // very long dependency lists.
  2790. for(std::vector<std::string>::const_iterator dep = depends.begin();
  2791. dep != depends.end(); ++dep)
  2792. {
  2793. replace = *dep;
  2794. m_Makefile->ExpandVariablesInString(replace);
  2795. fout << tgt.c_str() << ": " << replace.c_str() << "\n";
  2796. }
  2797. }
  2798. int count = 0;
  2799. for (std::vector<std::string>::const_iterator i = commands.begin();
  2800. i != commands.end(); ++i)
  2801. {
  2802. replace = *i;
  2803. m_Makefile->ExpandVariablesInString(replace);
  2804. if(count == 0 && replace.find_first_not_of(" \t\n\r") != std::string::npos &&
  2805. replace[0] != '-' && replace.find("echo") != 0
  2806. && replace.find("$(MAKE)") != 0)
  2807. {
  2808. std::string echostring = "Building ";
  2809. echostring += comment;
  2810. echostring += " ";
  2811. echostring += target;
  2812. echostring += "...";
  2813. this->OutputEcho(fout,echostring.c_str());
  2814. }
  2815. fout << "\t" << replace.c_str() << "\n";
  2816. count++;
  2817. }
  2818. fout << "\n";
  2819. }
  2820. const char* cmLocalUnixMakefileGenerator::GetSafeDefinition(const char* def)
  2821. {
  2822. const char* ret = m_Makefile->GetDefinition(def);
  2823. if(!ret)
  2824. {
  2825. return "";
  2826. }
  2827. return ret;
  2828. }
  2829. std::string cmLocalUnixMakefileGenerator::LowerCasePath(const char* path)
  2830. {
  2831. #ifdef _WIN32
  2832. return cmSystemTools::LowerCase(path);
  2833. #else
  2834. return std::string(path);
  2835. #endif
  2836. }
  2837. std::string
  2838. cmLocalUnixMakefileGenerator::CreateMakeVariable(const char* sin, const char* s2in)
  2839. {
  2840. std::string s = sin;
  2841. std::string s2 = s2in;
  2842. std::string unmodified = s;
  2843. unmodified += s2;
  2844. // if there is no restriction on the length of make variables
  2845. // and there are no "." charactors in the string, then return the
  2846. // unmodified combination.
  2847. if(!m_MakefileVariableSize && unmodified.find('.') == s.npos)
  2848. {
  2849. return unmodified;
  2850. }
  2851. // see if the variable has been defined before and return
  2852. // the modified version of the variable
  2853. std::map<cmStdString, cmStdString>::iterator i = m_MakeVariableMap.find(unmodified);
  2854. if(i != m_MakeVariableMap.end())
  2855. {
  2856. return i->second;
  2857. }
  2858. // start with the unmodified variable
  2859. std::string ret = unmodified;
  2860. // if this there is no value for m_MakefileVariableSize then
  2861. // the string must have bad characters in it
  2862. if(!m_MakefileVariableSize)
  2863. {
  2864. cmSystemTools::ReplaceString(ret, ".", "_");
  2865. int ni = 0;
  2866. char buffer[5];
  2867. // make sure the _ version is not already used, if
  2868. // it is used then add number to the end of the variable
  2869. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2870. {
  2871. ++ni;
  2872. sprintf(buffer, "%04d", ni);
  2873. ret = unmodified + buffer;
  2874. }
  2875. m_ShortMakeVariableMap[ret] = "1";
  2876. m_MakeVariableMap[unmodified] = ret;
  2877. return ret;
  2878. }
  2879. // if the string is greater the 32 chars it is an invalid vairable name
  2880. // for borland make
  2881. if(static_cast<int>(ret.size()) > m_MakefileVariableSize)
  2882. {
  2883. int keep = m_MakefileVariableSize - 8;
  2884. int size = keep + 3;
  2885. std::string str1 = s;
  2886. std::string str2 = s2;
  2887. // we must shorten the combined string by 4 charactors
  2888. // keep no more than 24 charactors from the second string
  2889. if(static_cast<int>(str2.size()) > keep)
  2890. {
  2891. str2 = str2.substr(0, keep);
  2892. }
  2893. if(static_cast<int>(str1.size()) + static_cast<int>(str2.size()) > size)
  2894. {
  2895. str1 = str1.substr(0, size - str2.size());
  2896. }
  2897. char buffer[5];
  2898. int ni = 0;
  2899. sprintf(buffer, "%04d", ni);
  2900. ret = str1 + str2 + buffer;
  2901. while(m_ShortMakeVariableMap.count(ret) && ni < 1000)
  2902. {
  2903. ++ni;
  2904. sprintf(buffer, "%04d", ni);
  2905. ret = str1 + str2 + buffer;
  2906. }
  2907. if(ni == 1000)
  2908. {
  2909. cmSystemTools::Error("Borland makefile variable length too long");
  2910. return unmodified;
  2911. }
  2912. // once an unused variable is found
  2913. m_ShortMakeVariableMap[ret] = "1";
  2914. }
  2915. // always make an entry into the unmodified to variable map
  2916. m_MakeVariableMap[unmodified] = ret;
  2917. return ret;
  2918. }
  2919. void cmLocalUnixMakefileGenerator::GetLibraryNames(const char* n,
  2920. const cmTarget& t,
  2921. std::string& name,
  2922. std::string& soName,
  2923. std::string& realName,
  2924. std::string& baseName)
  2925. {
  2926. // Check for library version properties.
  2927. const char* version = t.GetProperty("VERSION");
  2928. const char* soversion = t.GetProperty("SOVERSION");
  2929. if((t.GetType() != cmTarget::SHARED_LIBRARY &&
  2930. t.GetType() != cmTarget::MODULE_LIBRARY) ||
  2931. !m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  2932. {
  2933. // Versioning is supported only for shared libraries and modules,
  2934. // and then only when the platform supports an soname flag.
  2935. version = 0;
  2936. soversion = 0;
  2937. }
  2938. if(version && !soversion)
  2939. {
  2940. // The soversion must be set if the library version is set. Use
  2941. // the library version as the soversion.
  2942. soversion = version;
  2943. }
  2944. // The library name.
  2945. name = this->GetFullTargetName(n, t);
  2946. // The library's soname.
  2947. soName = name;
  2948. if(soversion)
  2949. {
  2950. soName += ".";
  2951. soName += soversion;
  2952. }
  2953. // The library's real name on disk.
  2954. realName = name;
  2955. if(version)
  2956. {
  2957. realName += ".";
  2958. realName += version;
  2959. }
  2960. else if(soversion)
  2961. {
  2962. realName += ".";
  2963. realName += soversion;
  2964. }
  2965. // The library name without extension.
  2966. baseName = this->GetBaseTargetName(n, t);
  2967. }