cmLocalUnixMakefileGenerator.cxx 85 KB

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