cmLocalUnixMakefileGenerator.cxx 100 KB

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