cmLocalUnixMakefileGenerator.cxx 105 KB

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