cmLocalUnixMakefileGenerator.cxx 86 KB

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