cmLocalUnixMakefileGenerator.cxx 107 KB

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