cmLocalUnixMakefileGenerator.cxx 92 KB

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