cmLocalUnixMakefileGenerator.cxx 107 KB

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