cmLocalUnixMakefileGenerator.cxx 92 KB

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