cmLocalUnixMakefileGenerator.cxx 107 KB

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