1
0

cmLocalUnixMakefileGenerator.cxx 96 KB

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