cmUnixMakefileGenerator.cxx 74 KB

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