cmUnixMakefileGenerator.cxx 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  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(cmSystemTools::GetExecutableExtension()),
  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)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  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)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  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)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  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. cmStringStream linklibs;
  615. this->OutputLinkLibraries(linklibs, name, t);
  616. command2 += linklibs.str();
  617. std::string customCommands = this->CreateTargetRules(t, name);
  618. const char* cc = 0;
  619. if(customCommands.size() > 0)
  620. {
  621. cc = customCommands.c_str();
  622. }
  623. this->OutputMakeRule(fout, "rules for a shared library",
  624. target.c_str(),
  625. depend.c_str(),
  626. command.c_str(),
  627. command2.c_str(),
  628. cc);
  629. }
  630. void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  631. const char* name,
  632. const cmTarget &t)
  633. {
  634. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  635. std::string depend = "$(";
  636. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  637. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  638. std::string command = "$(RM) lib" + std::string(name) + "$(MODULE_SUFFIX)";
  639. std::string command2;
  640. if(t.HasCxx())
  641. {
  642. command2 = "$(CMAKE_CXX_LINK_SHARED) $(CMAKE_CXX_MODULE_LINK_FLAGS) "
  643. "$(CMAKE_CXX_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  644. }
  645. else
  646. {
  647. command2 = "$(CMAKE_C_LINK_SHARED) $(CMAKE_SHLIB_LINK_FLAGS) "
  648. "$(CMAKE_SHLIB_BUILD_FLAGS) -o \\\n";
  649. }
  650. command2 += "\t ";
  651. std::string libName = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  652. libName = this->ConvertToOutputPath(libName.c_str());
  653. command2 += libName + " \\\n";
  654. command2 += "\t $(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  655. cmStringStream linklibs;
  656. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  657. command2 += linklibs.str();
  658. std::string customCommands = this->CreateTargetRules(t, name);
  659. const char* cc = 0;
  660. if(customCommands.size() > 0)
  661. {
  662. cc = customCommands.c_str();
  663. }
  664. this->OutputMakeRule(fout, "rules for a shared module library",
  665. target.c_str(),
  666. depend.c_str(),
  667. command.c_str(),
  668. command2.c_str(),
  669. cc);
  670. }
  671. void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  672. const char* name,
  673. const cmTarget &t)
  674. {
  675. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  676. target = this->ConvertToOutputPath(target.c_str());
  677. std::string depend = "$(";
  678. depend += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  679. std::string command;
  680. if(t.HasCxx())
  681. {
  682. command = "$(CMAKE_CXX_AR) $(CMAKE_CXX_AR_ARGS) ";
  683. }
  684. else
  685. {
  686. command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  687. }
  688. command += target;
  689. command += " $(";
  690. command += this->CreateMakeVariable(name, "_SRC_OBJS") + ")";
  691. std::string command2 = "$(CMAKE_RANLIB) ";
  692. command2 += target;
  693. std::string comment = "rule to build static library: ";
  694. comment += name;
  695. std::string customCommands = this->CreateTargetRules(t, name);
  696. const char* cc = 0;
  697. if(customCommands.size() > 0)
  698. {
  699. cc = customCommands.c_str();
  700. }
  701. this->OutputMakeRule(fout,
  702. comment.c_str(),
  703. target.c_str(),
  704. depend.c_str(),
  705. command.c_str(),
  706. command2.c_str(),
  707. cc);
  708. }
  709. void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  710. const char* name,
  711. const cmTarget &t)
  712. {
  713. std::string target = m_ExecutableOutputPath + name + m_ExecutableExtension;
  714. std::string depend = "$(";
  715. depend += this->CreateMakeVariable(name, "_SRC_OBJS")
  716. + ") $(" + this->CreateMakeVariable(name, "_DEPEND_LIBS") + ")";
  717. std::string command;
  718. if(t.HasCxx())
  719. {
  720. command =
  721. "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  722. }
  723. else
  724. {
  725. command =
  726. "$(CMAKE_C_COMPILER) $(CMAKE_C_SHLIB_LINK_FLAGS) $(CMAKE_C_FLAGS) ";
  727. }
  728. command += "$(" + this->CreateMakeVariable(name, "_SRC_OBJS") + ") ";
  729. cmStringStream linklibs;
  730. this->OutputLinkLibraries(linklibs, 0, t);
  731. command += linklibs.str();
  732. std::string outputFile = m_ExecutableOutputPath + name;
  733. command += " -o " + this->ConvertToOutputPath(outputFile.c_str());
  734. std::string comment = "rule to build executable: ";
  735. comment += name;
  736. std::string customCommands = this->CreateTargetRules(t, name);
  737. const char* cc = 0;
  738. if(customCommands.size() > 0)
  739. {
  740. cc = customCommands.c_str();
  741. }
  742. this->OutputMakeRule(fout,
  743. comment.c_str(),
  744. target.c_str(),
  745. depend.c_str(),
  746. command.c_str(),
  747. cc);
  748. }
  749. void cmUnixMakefileGenerator::OutputUtilityRule(std::ostream& fout,
  750. const char* name,
  751. const cmTarget &t)
  752. {
  753. std::string customCommands = this->CreateTargetRules(t, name);
  754. const char* cc = 0;
  755. if(customCommands.size() > 0)
  756. {
  757. cc = customCommands.c_str();
  758. }
  759. std::string comment = "Rule to build Utility ";
  760. comment += name;
  761. std::string depends;
  762. const std::vector<cmCustomCommand> &ccs = t.GetCustomCommands();
  763. for(std::vector<cmCustomCommand>::const_iterator i = ccs.begin();
  764. i != ccs.end(); ++i)
  765. {
  766. const std::vector<std::string> & dep = i->GetDepends();
  767. for(std::vector<std::string>::const_iterator d = dep.begin();
  768. d != dep.end(); ++d)
  769. {
  770. depends += " \\\n";
  771. depends += *d;
  772. }
  773. }
  774. this->OutputMakeRule(fout,
  775. comment.c_str(),
  776. name,
  777. depends.c_str(),
  778. cc);
  779. }
  780. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  781. {
  782. // for each target
  783. const cmTargets &tgts = m_Makefile->GetTargets();
  784. for(cmTargets::const_iterator l = tgts.begin();
  785. l != tgts.end(); l++)
  786. {
  787. switch(l->second.GetType())
  788. {
  789. case cmTarget::STATIC_LIBRARY:
  790. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  791. break;
  792. case cmTarget::SHARED_LIBRARY:
  793. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  794. break;
  795. case cmTarget::MODULE_LIBRARY:
  796. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  797. break;
  798. case cmTarget::EXECUTABLE:
  799. case cmTarget::WIN32_EXECUTABLE:
  800. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  801. break;
  802. case cmTarget::UTILITY:
  803. this->OutputUtilityRule(fout, l->first.c_str(), l->second);
  804. break;
  805. // This is handled by the OutputCustomRules method
  806. case cmTarget::INSTALL_FILES:
  807. // This is handled by the OutputInstallRules method
  808. case cmTarget::INSTALL_PROGRAMS:
  809. // This is handled by the OutputInstallRules method
  810. break;
  811. }
  812. }
  813. }
  814. // For each target that is an executable or shared library, generate
  815. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  816. void cmUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  817. {
  818. // Build a set of libraries that will be linked into any target in
  819. // this directory.
  820. std::set<std::string> used;
  821. // for each target
  822. const cmTargets &tgts = m_Makefile->GetTargets();
  823. for(cmTargets::const_iterator l = tgts.begin();
  824. l != tgts.end(); l++)
  825. {
  826. // Each dependency should only be emitted once per target.
  827. std::set<std::string> emitted;
  828. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  829. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  830. || (l->second.GetType() == cmTarget::EXECUTABLE)
  831. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  832. {
  833. fout << this->CreateMakeVariable(l->first.c_str(), "_DEPEND_LIBS") << " = ";
  834. // A library should not depend on itself!
  835. emitted.insert(l->first);
  836. // Now, look at all link libraries specific to this target.
  837. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  838. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  839. lib != tlibs.end(); ++lib)
  840. {
  841. // Record that this library was used.
  842. used.insert(lib->first);
  843. // Don't emit the same library twice for this target.
  844. if(emitted.insert(lib->first).second)
  845. {
  846. // Output this dependency.
  847. this->OutputLibDepend(fout, lib->first.c_str());
  848. }
  849. }
  850. // Now, look at all utilities specific to this target.
  851. const std::set<cmStdString>& tutils = l->second.GetUtilities();
  852. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  853. util != tutils.end(); ++util)
  854. {
  855. // Record that this utility was used.
  856. used.insert(*util);
  857. // Don't emit the same utility twice for this target.
  858. if(emitted.insert(*util).second)
  859. {
  860. // Output this dependency.
  861. this->OutputExeDepend(fout, util->c_str());
  862. }
  863. }
  864. fout << "\n";
  865. }
  866. }
  867. fout << "\n";
  868. // Loop over the libraries used and make sure there is a rule to
  869. // build them in this makefile. If the library is in another
  870. // directory, add a rule to jump to that directory and make sure it
  871. // exists.
  872. for(std::set<std::string>::const_iterator lib = used.begin();
  873. lib != used.end(); ++lib)
  874. {
  875. // loop over the list of directories that the libraries might
  876. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  877. // be stored in the cache
  878. std::string libPath = *lib + "_CMAKE_PATH";
  879. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  880. // if cache and not the current directory add a rule, to
  881. // jump into the directory and build for the first time
  882. if(cacheValue &&
  883. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  884. {
  885. // add the correct extension
  886. std::string ltname = *lib+"_LIBRARY_TYPE";
  887. const char* libType
  888. = m_Makefile->GetDefinition(ltname.c_str());
  889. // if it was a library..
  890. if (libType)
  891. {
  892. std::string library = m_LibraryPrefix;
  893. library += *lib;
  894. std::string libpath = cacheValue;
  895. if(libType && std::string(libType) == "SHARED")
  896. {
  897. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  898. }
  899. else if(libType && std::string(libType) == "MODULE")
  900. {
  901. library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  902. }
  903. else if(libType && std::string(libType) == "STATIC")
  904. {
  905. library += m_StaticLibraryExtension;
  906. }
  907. else
  908. {
  909. cmSystemTools::Error("Unknown library type!");
  910. return;
  911. }
  912. if(m_LibraryOutputPath.size())
  913. {
  914. libpath = m_LibraryOutputPath;
  915. }
  916. else
  917. {
  918. libpath += "/";
  919. }
  920. libpath += library;
  921. // put out a rule to build the library if it does not exist
  922. this->OutputBuildTargetInDir(fout,
  923. cacheValue,
  924. library.c_str(),
  925. libpath.c_str(),
  926. m_Makefile->
  927. GetDefinition("LIBRARY_OUTPUT_PATH")
  928. );
  929. }
  930. // something other than a library...
  931. else
  932. {
  933. std::string exepath = cacheValue;
  934. if(m_ExecutableOutputPath.size())
  935. {
  936. exepath = m_ExecutableOutputPath;
  937. }
  938. else
  939. {
  940. exepath += "/";
  941. }
  942. exepath += *lib;
  943. this->OutputBuildTargetInDir(fout,
  944. cacheValue,
  945. lib->c_str(),
  946. exepath.c_str(),
  947. m_Makefile->
  948. GetDefinition("EXECUTABLE_OUTPUT_PATH")
  949. );
  950. }
  951. }
  952. }
  953. }
  954. void cmUnixMakefileGenerator::OutputBuildTargetInDir(std::ostream& fout,
  955. const char* path,
  956. const char* library,
  957. const char* fullpath,
  958. const char* outputPath)
  959. {
  960. const char* makeTarget = library;
  961. if(outputPath && strcmp( outputPath, "" ) != 0)
  962. {
  963. makeTarget = fullpath;
  964. }
  965. fout << this->ConvertToOutputPath(fullpath)
  966. << ":\n\tcd " << this->ConvertToOutputPath(path)
  967. << "; $(MAKE) $(MAKESILENT) cmake.depends"
  968. << "; $(MAKE) $(MAKESILENT) cmake.check_depends"
  969. << "; $(MAKE) $(MAKESILENT) -f cmake.check_depends"
  970. << "; $(MAKE) $(MAKESILENT) "
  971. << this->ConvertToOutputPath(makeTarget) << "\n\n";
  972. }
  973. bool cmUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  974. {
  975. return strcmp(path1, path2) == 0;
  976. }
  977. void cmUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  978. const char* name)
  979. {
  980. std::string libPath = name;
  981. libPath += "_CMAKE_PATH";
  982. const char* cacheValue = m_Makefile->GetDefinition(libPath.c_str());
  983. if(cacheValue )
  984. {
  985. // if there is a cache value, then this is a library that cmake
  986. // knows how to build, so we can depend on it
  987. std::string libpath;
  988. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  989. {
  990. // if the library is not in the current directory, then get the full
  991. // path to it
  992. if(m_LibraryOutputPath.size())
  993. {
  994. libpath = m_LibraryOutputPath;
  995. libpath += m_LibraryPrefix;
  996. }
  997. else
  998. {
  999. libpath = cacheValue;
  1000. libpath += "/";
  1001. libpath += m_LibraryPrefix;
  1002. }
  1003. }
  1004. else
  1005. {
  1006. // library is in current Makefile so use lib as a prefix
  1007. libpath = m_LibraryOutputPath;
  1008. libpath += m_LibraryPrefix;
  1009. }
  1010. // add the library name
  1011. libpath += name;
  1012. // add the correct extension
  1013. std::string ltname = name;
  1014. ltname += "_LIBRARY_TYPE";
  1015. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  1016. if(libType && std::string(libType) == "SHARED")
  1017. {
  1018. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1019. }
  1020. else if (libType && std::string(libType) == "MODULE")
  1021. {
  1022. libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1023. }
  1024. else if (libType && std::string(libType) == "STATIC")
  1025. {
  1026. libpath += m_StaticLibraryExtension;
  1027. }
  1028. fout << this->ConvertToOutputPath(libpath.c_str()) << " ";
  1029. }
  1030. }
  1031. void cmUnixMakefileGenerator::OutputExeDepend(std::ostream& fout,
  1032. const char* name)
  1033. {
  1034. std::string exePath = name;
  1035. exePath += "_CMAKE_PATH";
  1036. const char* cacheValue = m_Makefile->GetDefinition(exePath.c_str());
  1037. if(cacheValue )
  1038. {
  1039. // if there is a cache value, then this is a executable/utility that cmake
  1040. // knows how to build, so we can depend on it
  1041. std::string exepath;
  1042. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  1043. {
  1044. // if the exe/utility is not in the current directory, then get the full
  1045. // path to it
  1046. if(m_ExecutableOutputPath.size())
  1047. {
  1048. exepath = m_ExecutableOutputPath;
  1049. }
  1050. else
  1051. {
  1052. exepath = cacheValue;
  1053. exepath += "/";
  1054. }
  1055. }
  1056. else
  1057. {
  1058. // library is in current Makefile
  1059. exepath = m_ExecutableOutputPath;
  1060. }
  1061. // add the library name
  1062. exepath += name;
  1063. // add the correct extension
  1064. exepath += m_ExecutableExtension;
  1065. fout << this->ConvertToOutputPath(exepath.c_str()) << " ";
  1066. }
  1067. }
  1068. // fix up names of directories so they can be used
  1069. // as targets in makefiles.
  1070. inline std::string FixDirectoryName(const char* dir)
  1071. {
  1072. std::string s = dir;
  1073. // replace ../ with 3 under bars
  1074. size_t pos = s.find("../");
  1075. if(pos != std::string::npos)
  1076. {
  1077. s.replace(pos, 3, "___");
  1078. }
  1079. // replace / directory separators with a single under bar
  1080. pos = s.find("/");
  1081. while(pos != std::string::npos)
  1082. {
  1083. s.replace(pos, 1, "_");
  1084. pos = s.find("/");
  1085. }
  1086. return s;
  1087. }
  1088. void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  1089. const char* dir,
  1090. const char* target1,
  1091. const char* target2,
  1092. bool silent)
  1093. {
  1094. std::string directory = this->ConvertToOutputPath(dir);
  1095. if(target1)
  1096. {
  1097. fout << "\t@if test ! -d " << directory
  1098. << "; then $(MAKE) rebuild_cache; fi\n";
  1099. if (!silent)
  1100. {
  1101. fout << "\techo " << directory << ": building " << target1 << "\n";
  1102. }
  1103. fout << "\t@cd " << directory
  1104. << "; $(MAKE) " << target1 << "\n";
  1105. }
  1106. if(target2)
  1107. {
  1108. if (!silent)
  1109. {
  1110. fout << "\techo " << directory << ": building " << target2 << "\n";
  1111. }
  1112. fout << "\t@cd " << directory
  1113. << "; $(MAKE) " << target2 << "\n";
  1114. }
  1115. fout << "\n";
  1116. }
  1117. void
  1118. cmUnixMakefileGenerator::
  1119. OutputSubDirectoryVars(std::ostream& fout,
  1120. const char* var,
  1121. const char* target,
  1122. const char* target1,
  1123. const char* target2,
  1124. const char* depend,
  1125. const std::vector<std::string>& SubDirectories,
  1126. bool silent)
  1127. {
  1128. if(!depend)
  1129. {
  1130. depend = "";
  1131. }
  1132. if( SubDirectories.size() == 0)
  1133. {
  1134. return;
  1135. }
  1136. fout << "# Variable for making " << target << " in subdirectories.\n";
  1137. fout << var << " = \\\n";
  1138. unsigned int i;
  1139. for(i =0; i < SubDirectories.size(); i++)
  1140. {
  1141. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1142. fout << target << "_" << subdir.c_str();
  1143. if(i == SubDirectories.size()-1)
  1144. {
  1145. fout << " \n\n";
  1146. }
  1147. else
  1148. {
  1149. fout << " \\\n";
  1150. }
  1151. }
  1152. fout << "# Targets for making " << target << " in subdirectories.\n";
  1153. std::string last = "";
  1154. for(unsigned int i =0; i < SubDirectories.size(); i++)
  1155. {
  1156. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  1157. fout << target << "_" << subdir.c_str() << ": " << depend;
  1158. // Make each subdirectory depend on previous one. This forces
  1159. // parallel builds (make -j 2) to build in same order as a single
  1160. // threaded build to avoid dependency problems.
  1161. if(i > 0)
  1162. {
  1163. fout << " " << target << "_" << last.c_str();
  1164. }
  1165. fout << "\n";
  1166. last = subdir;
  1167. std::string dir = m_Makefile->GetCurrentOutputDirectory();
  1168. dir += "/";
  1169. dir += SubDirectories[i];
  1170. this->BuildInSubDirectory(fout, dir.c_str(),
  1171. target1, target2, silent);
  1172. }
  1173. fout << "\n\n";
  1174. }
  1175. // output rules for decending into sub directories
  1176. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  1177. {
  1178. // Output Sub directory build rules
  1179. const std::vector<std::string>& SubDirectories
  1180. = m_Makefile->GetSubDirectories();
  1181. if( SubDirectories.size() == 0)
  1182. {
  1183. return;
  1184. }
  1185. this->OutputSubDirectoryVars(fout,
  1186. "SUBDIR_BUILD",
  1187. "default_target",
  1188. "default_target",
  1189. 0, "$(TARGETS)",
  1190. SubDirectories,
  1191. false);
  1192. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  1193. "clean",
  1194. 0, 0,
  1195. SubDirectories);
  1196. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  1197. "depend",
  1198. 0, 0,
  1199. SubDirectories);
  1200. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  1201. "install",
  1202. 0, 0,
  1203. SubDirectories);
  1204. }
  1205. // Output the depend information for all the classes
  1206. // in the makefile. These would have been generated
  1207. // by the class cmMakeDepend GenerateMakefile
  1208. bool cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  1209. {
  1210. bool ret = false;
  1211. // Iterate over every target.
  1212. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1213. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1214. target != targets.end(); ++target)
  1215. {
  1216. // Iterate over every source for this target.
  1217. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1218. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1219. source != sources.end(); ++source)
  1220. {
  1221. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1222. {
  1223. if(!(*source)->GetDepends().empty())
  1224. {
  1225. fout << (*source)->GetSourceName() << m_ObjectFileExtension << " :";
  1226. // Iterate through all the dependencies for this source.
  1227. for(std::vector<std::string>::const_iterator dep =
  1228. (*source)->GetDepends().begin();
  1229. dep != (*source)->GetDepends().end(); ++dep)
  1230. {
  1231. fout << " \\\n"
  1232. << this->ConvertToOutputPath(dep->c_str());
  1233. ret = true;
  1234. }
  1235. fout << "\n\n";
  1236. }
  1237. }
  1238. }
  1239. }
  1240. return ret;
  1241. }
  1242. // Output the depend information for all the classes
  1243. // in the makefile. These would have been generated
  1244. // by the class cmMakeDepend GenerateMakefile
  1245. void cmUnixMakefileGenerator::OutputCheckDepends(std::ostream& fout)
  1246. {
  1247. std::set<std::string> emittedLowerPath;
  1248. std::set<std::string> emitted;
  1249. // Iterate over every target.
  1250. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1251. fout << "# Suppresses display of executed commands\n";
  1252. fout << ".SILENT:\n";
  1253. fout << "# disable some common implicit rules to speed things up\n";
  1254. fout << ".SUFFIXES:\n";
  1255. fout << ".SUFFIXES:.hpuxmakemusthaverule\n";
  1256. this->OutputMakeVariables(fout);
  1257. fout << "default:\n";
  1258. fout << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends all\n"
  1259. << "\t$(MAKE) $(MAKESILENT) -f cmake.check_depends cmake.depends\n\n";
  1260. fout << "all: ";
  1261. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1262. target != targets.end(); ++target)
  1263. {
  1264. // Iterate over every source for this target.
  1265. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1266. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1267. source != sources.end(); ++source)
  1268. {
  1269. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1270. {
  1271. if(!(*source)->GetDepends().empty())
  1272. {
  1273. for(std::vector<std::string>::const_iterator dep =
  1274. (*source)->GetDepends().begin();
  1275. dep != (*source)->GetDepends().end(); ++dep)
  1276. {
  1277. std::string dependfile =
  1278. this->ConvertToOutputPath(cmSystemTools::CollapseFullPath(dep->c_str()).c_str());
  1279. // use the lower path function to create uniqe names
  1280. std::string lowerpath = this->LowerCasePath(dependfile.c_str());
  1281. if(emittedLowerPath.insert(lowerpath).second)
  1282. {
  1283. emitted.insert(dependfile);
  1284. fout << " \\\n" << dependfile ;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }
  1290. }
  1291. fout << "\n\n# if any of these files changes run make dependlocal\n";
  1292. fout << "cmake.depends: ";
  1293. std::set<std::string>::iterator i;
  1294. for(i = emitted.begin(); i != emitted.end(); ++i)
  1295. {
  1296. fout << " \\\n" << *i;
  1297. }
  1298. fout << "\n\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1299. fout << "\n\n";
  1300. fout << "# if a .h file is removed then run make dependlocal\n\n";
  1301. for(std::set<std::string>::iterator i = emitted.begin();
  1302. i != emitted.end(); ++i)
  1303. {
  1304. fout << *i << ":\n"
  1305. << "\t$(MAKE) $(MAKESILENT) dependlocal\n\n";
  1306. }
  1307. }
  1308. // Output each custom rule in the following format:
  1309. // output: source depends...
  1310. // (tab) command...
  1311. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  1312. {
  1313. // We may be modifying the source groups temporarily, so make a copy.
  1314. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  1315. const cmTargets &tgts = m_Makefile->GetTargets();
  1316. for(cmTargets::const_iterator tgt = tgts.begin();
  1317. tgt != tgts.end(); ++tgt)
  1318. {
  1319. // add any custom rules to the source groups
  1320. for (std::vector<cmCustomCommand>::const_iterator cr =
  1321. tgt->second.GetCustomCommands().begin();
  1322. cr != tgt->second.GetCustomCommands().end(); ++cr)
  1323. {
  1324. // if the source for the custom command is the same name
  1325. // as the target, then to not create a rule in the makefile for
  1326. // the custom command, as the command will be fired when the other target
  1327. // is built.
  1328. if ( cr->GetSourceName().compare(tgt->first) !=0)
  1329. {
  1330. cmSourceGroup& sourceGroup =
  1331. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  1332. sourceGroups);
  1333. cmCustomCommand cc(*cr);
  1334. cc.ExpandVariables(*m_Makefile);
  1335. sourceGroup.AddCustomCommand(cc);
  1336. }
  1337. }
  1338. }
  1339. // Loop through every source group.
  1340. for(std::vector<cmSourceGroup>::const_iterator sg =
  1341. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  1342. {
  1343. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  1344. if(buildRules.empty())
  1345. { continue; }
  1346. std::string name = sg->GetName();
  1347. if(name != "")
  1348. {
  1349. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  1350. }
  1351. // Loop through each source in the source group.
  1352. for(cmSourceGroup::BuildRules::const_iterator cc =
  1353. buildRules.begin(); cc != buildRules.end(); ++ cc)
  1354. {
  1355. std::string source = cc->first;
  1356. const cmSourceGroup::Commands& commands = cc->second.m_Commands;
  1357. // Loop through every command generating code from the current source.
  1358. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1359. c != commands.end(); ++c)
  1360. {
  1361. // escape spaces and convert to native slashes path for
  1362. // the command
  1363. std::string command =
  1364. this->ConvertToOutputPath(c->second.m_Command.c_str());
  1365. command += " ";
  1366. // now add the arguments
  1367. command += c->second.m_Arguments;
  1368. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1369. // if the command has no outputs, then it is a utility command
  1370. // with no outputs
  1371. if(commandFiles.m_Outputs.size() == 0)
  1372. {
  1373. std::string depends;
  1374. // collect out all the dependencies for this rule.
  1375. for(std::set<std::string>::const_iterator d =
  1376. commandFiles.m_Depends.begin();
  1377. d != commandFiles.m_Depends.end(); ++d)
  1378. {
  1379. std::string dep = this->ConvertToOutputPath(d->c_str());
  1380. depends += " ";
  1381. depends += dep;
  1382. }
  1383. // output rule
  1384. this->OutputMakeRule(fout,
  1385. "Custom command",
  1386. source.c_str(),
  1387. depends.c_str(),
  1388. command.c_str());
  1389. }
  1390. // Write a rule for every output generated by this command.
  1391. for(std::set<std::string>::const_iterator output =
  1392. commandFiles.m_Outputs.begin();
  1393. output != commandFiles.m_Outputs.end(); ++output)
  1394. {
  1395. std::string src = this->ConvertToOutputPath(source.c_str());
  1396. std::string depends;
  1397. depends += src;
  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. output->c_str(),
  1411. depends.c_str(),
  1412. command.c_str());
  1413. }
  1414. }
  1415. }
  1416. if(name != "")
  1417. {
  1418. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1419. }
  1420. }
  1421. }
  1422. void cmUnixMakefileGenerator::GenerateCacheOnly()
  1423. {
  1424. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  1425. std::string dest = m_Makefile->GetStartOutputDirectory();
  1426. dest += "/Makefile";
  1427. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  1428. this->OutputMakefile(dest.c_str());
  1429. return;
  1430. }
  1431. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  1432. {
  1433. std::vector<cmMakefile*> makefiles;
  1434. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  1435. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  1436. i != makefiles.end(); ++i)
  1437. {
  1438. cmMakefile* mf = *i;
  1439. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  1440. {
  1441. mf->AddDefinition("RUN_CONFIGURE", true);
  1442. }
  1443. cmUnixMakefileGenerator* gen =
  1444. static_cast<cmUnixMakefileGenerator*>(mf->GetMakefileGenerator());
  1445. gen->SetCacheOnlyOn();
  1446. gen->SetRecurseOff();
  1447. mf->GenerateMakefile();
  1448. }
  1449. // CLEAN up the makefiles created
  1450. for(unsigned int i =0; i < makefiles.size(); ++i)
  1451. {
  1452. delete makefiles[i];
  1453. }
  1454. }
  1455. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1456. {
  1457. const char* variables =
  1458. "# the standard shell for make\n"
  1459. "SHELL = /bin/sh\n"
  1460. "\n"
  1461. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1462. "CMAKE_AR = @CMAKE_AR@\n"
  1463. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1464. "CMAKE_CXX_AR = @CMAKE_CXX_AR@\n"
  1465. "CMAKE_CXX_AR_ARGS = @CMAKE_CXX_AR_ARGS@\n"
  1466. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1467. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1468. "CMAKE_C_LINK_SHARED = @CMAKE_C_LINK_SHARED@\n"
  1469. "CMAKE_CXX_LINK_SHARED = @CMAKE_CXX_LINK_SHARED@\n"
  1470. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1471. "CMAKE_CXX_SHLIB_CFLAGS = @CMAKE_CXX_SHLIB_CFLAGS@\n"
  1472. "CMAKE_CXX_SHLIB_BUILD_FLAGS = @CMAKE_CXX_SHLIB_BUILD_FLAGS@\n"
  1473. "CMAKE_CXX_SHLIB_LINK_FLAGS = @CMAKE_CXX_SHLIB_LINK_FLAGS@\n"
  1474. "CMAKE_CXX_MODULE_BUILD_FLAGS = @CMAKE_CXX_MODULE_BUILD_FLAGS@\n"
  1475. "CMAKE_CXX_MODULE_LINK_FLAGS = @CMAKE_CXX_MODULE_LINK_FLAGS@\n"
  1476. "CMAKE_CXX_SHLIB_RUNTIME_FLAG = @CMAKE_CXX_SHLIB_RUNTIME_FLAG@\n"
  1477. "CMAKE_CXX_SHLIB_RUNTIME_SEP = @CMAKE_CXX_SHLIB_RUNTIME_SEP@\n"
  1478. "\n"
  1479. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1480. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1481. "\n"
  1482. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1483. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1484. "CMAKE_C_SHLIB_LINK_FLAGS = @CMAKE_C_SHLIB_LINK_FLAGS@\n"
  1485. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1486. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1487. "CMAKE_C_SHLIB_RUNTIME_FLAG = @CMAKE_C_SHLIB_RUNTIME_FLAG@\n"
  1488. "CMAKE_SHLIB_RUNTIME_FLAG = @CMAKE_SHLIB_RUNTIME_FLAG@\n"
  1489. "CMAKE_SHLIB_RUNTIME_SEP = @CMAKE_SHLIB_RUNTIME_SEP@\n"
  1490. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1491. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1492. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1493. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1494. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1495. "RM = rm -f\n"
  1496. "\n";
  1497. std::string replaceVars = variables;
  1498. m_Makefile->ExpandVariablesInString(replaceVars);
  1499. fout << replaceVars.c_str();
  1500. fout << "CMAKE_COMMAND = "
  1501. << this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_COMMAND"))
  1502. << "\n";
  1503. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1504. {
  1505. fout << "CMAKE_EDIT_COMMAND = "
  1506. << this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1507. << "\n";
  1508. }
  1509. fout << "CMAKE_CURRENT_SOURCE = " <<
  1510. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << "\n";
  1511. fout << "CMAKE_CURRENT_BINARY = " <<
  1512. this->ConvertToOutputPath(m_Makefile->GetStartOutputDirectory()) << "\n";
  1513. fout << "CMAKE_SOURCE_DIR = " <<
  1514. this->ConvertToOutputPath(m_Makefile->GetHomeDirectory()) << "\n";
  1515. fout << "CMAKE_BINARY_DIR = " <<
  1516. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1517. // Output Include paths
  1518. fout << "INCLUDE_FLAGS = ";
  1519. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1520. std::vector<std::string>::iterator i;
  1521. fout << "-I" <<
  1522. this->ConvertToOutputPath(m_Makefile->GetStartDirectory()) << " ";
  1523. for(i = includes.begin(); i != includes.end(); ++i)
  1524. {
  1525. std::string include = *i;
  1526. // Don't output a -I for the standard include path "/usr/include".
  1527. // This can cause problems with certain standard library
  1528. // implementations because the wrong headers may be found first.
  1529. if(include != "/usr/include")
  1530. {
  1531. fout << "-I" << this->ConvertToOutputPath(i->c_str()) << " ";
  1532. }
  1533. }
  1534. fout << m_Makefile->GetDefineFlags();
  1535. fout << "\n\n";
  1536. }
  1537. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1538. {
  1539. const char* root
  1540. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1541. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1542. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1543. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1544. const cmTargets &tgts = m_Makefile->GetTargets();
  1545. fout << "install: $(SUBDIR_INSTALL)\n";
  1546. fout << "\t@echo \"Installing ...\"\n";
  1547. const char* prefix
  1548. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1549. if (!prefix)
  1550. {
  1551. prefix = "/usr/local";
  1552. }
  1553. for(cmTargets::const_iterator l = tgts.begin();
  1554. l != tgts.end(); l++)
  1555. {
  1556. if (l->second.GetInstallPath() != "")
  1557. {
  1558. // first make the directories for each target
  1559. fout << "\t@if [ ! -d $(DESTDIR)" << prefix << l->second.GetInstallPath() <<
  1560. " ] ; then \\\n";
  1561. fout << "\t echo \"Making directory $(DESTDIR)" << prefix
  1562. << l->second.GetInstallPath() << " \"; \\\n";
  1563. fout << "\t mkdir -p $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1564. << "; \\\n";
  1565. fout << "\t chmod 755 $(DESTDIR)" << prefix << l->second.GetInstallPath()
  1566. << "; \\\n";
  1567. fout << "\t else true; \\\n";
  1568. fout << "\t fi\n";
  1569. // now install the target
  1570. switch (l->second.GetType())
  1571. {
  1572. case cmTarget::STATIC_LIBRARY:
  1573. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1574. << l->first;
  1575. fout << ".a";
  1576. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1577. break;
  1578. case cmTarget::SHARED_LIBRARY:
  1579. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1580. << l->first;
  1581. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1582. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1583. break;
  1584. case cmTarget::MODULE_LIBRARY:
  1585. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1586. << l->first;
  1587. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1588. fout << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1589. break;
  1590. case cmTarget::WIN32_EXECUTABLE:
  1591. case cmTarget::EXECUTABLE:
  1592. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1593. << l->first
  1594. << cmSystemTools::GetExecutableExtension()
  1595. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "\n";
  1596. break;
  1597. case cmTarget::INSTALL_FILES:
  1598. {
  1599. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1600. std::vector<std::string>::const_iterator i;
  1601. for (i = sf.begin(); i != sf.end(); ++i)
  1602. {
  1603. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1604. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1605. // avoid using install-sh to install install-sh
  1606. // does not work on windows....
  1607. if(*i == "install-sh")
  1608. {
  1609. fout << "\t cp ";
  1610. }
  1611. else
  1612. {
  1613. fout << "\t $(INSTALL_DATA) ";
  1614. }
  1615. fout << *i
  1616. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1617. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1618. // avoid using install-sh to install install-sh
  1619. // does not work on windows....
  1620. if(*i == "install-sh")
  1621. {
  1622. fout << "\t cp ";
  1623. }
  1624. else
  1625. {
  1626. fout << "\t $(INSTALL_DATA) ";
  1627. }
  1628. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1629. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1630. fout << "\telse \\\n";
  1631. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1632. << " \"; \\\n";
  1633. fout << "\t fi\n";
  1634. }
  1635. }
  1636. break;
  1637. case cmTarget::INSTALL_PROGRAMS:
  1638. {
  1639. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1640. std::vector<std::string>::const_iterator i;
  1641. for (i = sf.begin(); i != sf.end(); ++i)
  1642. {
  1643. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1644. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1645. // avoid using install-sh to install install-sh
  1646. // does not work on windows....
  1647. if(*i == "install-sh")
  1648. {
  1649. fout << "\t cp ";
  1650. }
  1651. else
  1652. {
  1653. fout << "\t $(INSTALL_PROGRAM) ";
  1654. }
  1655. fout << *i
  1656. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1657. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1658. // avoid using install-sh to install install-sh
  1659. // does not work on windows....
  1660. if(*i == "install-sh")
  1661. {
  1662. fout << "\t cp ";
  1663. }
  1664. else
  1665. {
  1666. fout << "\t $(INSTALL_PROGRAM) ";
  1667. }
  1668. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1669. << " $(DESTDIR)" << prefix << l->second.GetInstallPath() << "; \\\n";
  1670. fout << "\telse \\\n";
  1671. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1672. << " \"; \\\n";
  1673. fout << "\t fi\n";
  1674. }
  1675. }
  1676. break;
  1677. case cmTarget::UTILITY:
  1678. default:
  1679. break;
  1680. }
  1681. }
  1682. }
  1683. }
  1684. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1685. {
  1686. this->OutputMakeRule(fout,
  1687. "Default build rule",
  1688. "all",
  1689. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1690. 0);
  1691. this->OutputMakeRule(fout,
  1692. "remove generated files",
  1693. "clean",
  1694. "$(SUBDIR_CLEAN)",
  1695. "-@ $(RM) $(CLEAN_OBJECT_FILES) "
  1696. " $(TARGETS) $(GENERATED_QT_FILES) $(GENERATED_FLTK_FILES)");
  1697. // collect up all the sources
  1698. std::string allsources;
  1699. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1700. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1701. target != targets.end(); ++target)
  1702. {
  1703. // Iterate over every source for this target.
  1704. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1705. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1706. source != sources.end(); ++source)
  1707. {
  1708. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1709. {
  1710. allsources += " \\\n";
  1711. allsources += this->ConvertToOutputPath((*source)->GetFullPath().c_str());
  1712. }
  1713. }
  1714. }
  1715. this->OutputMakeRule(fout,
  1716. "Rule to build the cmake.depends and Makefile as side effect, if a source cmakelist file is out of date.",
  1717. "cmake.depends",
  1718. "$(CMAKE_MAKEFILE_SOURCES)",
  1719. "$(CMAKE_COMMAND) "
  1720. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1721. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1722. );
  1723. this->OutputMakeRule(fout,
  1724. "Rule to build the cmake.check_depends and Makefile as side effect, if any source file has changed.",
  1725. "cmake.check_depends",
  1726. allsources.c_str(),
  1727. "$(CMAKE_COMMAND) "
  1728. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1729. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)"
  1730. );
  1731. this->OutputMakeRule(fout,
  1732. "Rule to force the build of cmake.depends",
  1733. "depend",
  1734. "$(SUBDIR_DEPEND)",
  1735. "$(CMAKE_COMMAND) "
  1736. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1737. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1738. this->OutputMakeRule(fout,
  1739. "Rule to force the build of cmake.depends "
  1740. "in the current directory only.",
  1741. "dependlocal",
  1742. 0,
  1743. "$(CMAKE_COMMAND) "
  1744. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1745. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1746. this->OutputMakeRule(fout,
  1747. "Rebuild CMakeCache.txt file",
  1748. "rebuild_cache",
  1749. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1750. "$(CMAKE_COMMAND) "
  1751. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1752. // if CMAKE_EDIT_COMMAND is defined then add a rule to run it
  1753. // called edit_cache
  1754. if(m_Makefile->GetDefinition("CMAKE_EDIT_COMMAND"))
  1755. {
  1756. this->OutputMakeRule(fout,
  1757. "Edit the CMakeCache.txt file with ccmake or CMakeSetup",
  1758. "edit_cache",
  1759. 0,
  1760. "$(CMAKE_EDIT_COMMAND) "
  1761. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1762. }
  1763. this->OutputMakeRule(fout,
  1764. "Create CMakeCache.txt file",
  1765. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1766. 0,
  1767. "$(CMAKE_COMMAND) "
  1768. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1769. this->OutputMakeRule(fout,
  1770. "Rule to keep make from removing Makefiles "
  1771. "if control-C is hit during a run of cmake.",
  1772. ".PRECIOUS",
  1773. "Makefile cmake.depends",
  1774. 0);
  1775. this->OutputSourceObjectBuildRules(fout);
  1776. // find ctest
  1777. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1778. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1779. ctest += "/";
  1780. ctest += "ctest";
  1781. ctest += cmSystemTools::GetExecutableExtension();
  1782. if(!cmSystemTools::FileExists(ctest.c_str()))
  1783. {
  1784. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1785. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1786. ctest += "/Debug/";
  1787. ctest += "ctest";
  1788. ctest += cmSystemTools::GetExecutableExtension();
  1789. }
  1790. if(!cmSystemTools::FileExists(ctest.c_str()))
  1791. {
  1792. ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1793. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1794. ctest += "/Release/";
  1795. ctest += "ctest";
  1796. ctest += cmSystemTools::GetExecutableExtension();
  1797. }
  1798. if (cmSystemTools::FileExists(ctest.c_str()))
  1799. {
  1800. this->OutputMakeRule(fout,
  1801. "run any tests",
  1802. "test",
  1803. "",
  1804. this->ConvertToOutputPath(ctest.c_str()).c_str());
  1805. }
  1806. }
  1807. void
  1808. cmUnixMakefileGenerator::
  1809. OutputBuildObjectFromSource(std::ostream& fout,
  1810. const char* shortName,
  1811. const cmSourceFile& source,
  1812. const char* extraCompileFlags,
  1813. bool shared)
  1814. {
  1815. // Header files shouldn't have build rules.
  1816. if(source.GetPropertyAsBool("HEADER_FILE_ONLY"))
  1817. {
  1818. return;
  1819. }
  1820. std::string comment = "Build ";
  1821. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1822. objectFile = this->ConvertToOutputPath(objectFile.c_str());
  1823. comment += objectFile + " From ";
  1824. comment += source.GetFullPath();
  1825. std::string compileCommand;
  1826. std::string ext = source.GetSourceExtension();
  1827. if(ext == "c" )
  1828. {
  1829. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1830. compileCommand += extraCompileFlags;
  1831. if(shared)
  1832. {
  1833. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1834. }
  1835. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1836. compileCommand +=
  1837. this->ConvertToOutputPath(source.GetFullPath().c_str());
  1838. compileCommand += " -o ";
  1839. compileCommand += objectFile;
  1840. }
  1841. else
  1842. {
  1843. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1844. compileCommand += extraCompileFlags;
  1845. if(shared)
  1846. {
  1847. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1848. }
  1849. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1850. compileCommand +=
  1851. this->ConvertToOutputPath(source.GetFullPath().c_str());
  1852. compileCommand += " -o ";
  1853. compileCommand += objectFile;
  1854. }
  1855. this->OutputMakeRule(fout,
  1856. comment.c_str(),
  1857. objectFile.c_str(),
  1858. this->ConvertToOutputPath(source.GetFullPath().
  1859. c_str()).c_str(),
  1860. compileCommand.c_str());
  1861. }
  1862. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1863. {
  1864. fout << "# Rules to build " << m_ObjectFileExtension
  1865. << " files from their sources:\n";
  1866. std::set<std::string> rules;
  1867. // Iterate over every target.
  1868. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1869. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1870. target != targets.end(); ++target)
  1871. {
  1872. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1873. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1874. std::string exportsDef = "";
  1875. if(shared)
  1876. {
  1877. exportsDef = "-D"+target->first+"_EXPORTS ";
  1878. }
  1879. // Iterate over every source for this target.
  1880. const std::vector<cmSourceFile*>& sources = target->second.GetSourceFiles();
  1881. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1882. source != sources.end(); ++source)
  1883. {
  1884. if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  1885. {
  1886. std::string shortName;
  1887. std::string sourceName;
  1888. // If the full path to the source file includes this
  1889. // directory, we want to use the relative path for the
  1890. // filename of the object file. Otherwise, we will use just
  1891. // the filename portion.
  1892. if((cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1893. || (cmSystemTools::GetFilenamePath((*source)->GetFullPath()).find(m_Makefile->
  1894. GetCurrentOutputDirectory()) == 0))
  1895. {
  1896. sourceName = (*source)->GetSourceName()+"."+(*source)->GetSourceExtension();
  1897. shortName = (*source)->GetSourceName();
  1898. // The path may be relative. See if a directory needs to be
  1899. // created for the output file. This is a ugly, and perhaps
  1900. // should be moved elsewhere.
  1901. std::string relPath =
  1902. cmSystemTools::GetFilenamePath((*source)->GetSourceName());
  1903. if(relPath != "")
  1904. {
  1905. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1906. outPath += "/"+relPath;
  1907. cmSystemTools::MakeDirectory(outPath.c_str());
  1908. }
  1909. }
  1910. else
  1911. {
  1912. sourceName = (*source)->GetFullPath();
  1913. shortName = cmSystemTools::GetFilenameName((*source)->GetSourceName());
  1914. }
  1915. std::string shortNameWithExt = shortName +
  1916. (*source)->GetSourceExtension();
  1917. // Only output a rule for each .o once.
  1918. if(rules.find(shortNameWithExt) == rules.end())
  1919. {
  1920. if((*source)->GetProperty("COMPILE_FLAGS"))
  1921. {
  1922. exportsDef += (*source)->GetProperty("COMPILE_FLAGS");
  1923. exportsDef += " ";
  1924. }
  1925. this->OutputBuildObjectFromSource(fout,
  1926. shortName.c_str(),
  1927. *(*source),
  1928. exportsDef.c_str(),
  1929. shared);
  1930. rules.insert(shortNameWithExt);
  1931. }
  1932. }
  1933. }
  1934. }
  1935. }
  1936. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1937. const char* comment,
  1938. const char* target,
  1939. const char* depends,
  1940. const char* command,
  1941. const char* command2,
  1942. const char* command3,
  1943. const char* command4)
  1944. {
  1945. if(!target)
  1946. {
  1947. cmSystemTools::Error("no target for OutputMakeRule");
  1948. return;
  1949. }
  1950. std::string replace;
  1951. if(comment)
  1952. {
  1953. replace = comment;
  1954. m_Makefile->ExpandVariablesInString(replace);
  1955. fout << "#---------------------------------------------------------\n";
  1956. fout << "# " << replace;
  1957. fout << "\n#\n";
  1958. }
  1959. fout << "\n";
  1960. replace = target;
  1961. m_Makefile->ExpandVariablesInString(replace);
  1962. fout << this->ConvertToOutputPath(replace.c_str()) << ": ";
  1963. if(depends)
  1964. {
  1965. replace = depends;
  1966. m_Makefile->ExpandVariablesInString(replace);
  1967. fout << replace.c_str();
  1968. }
  1969. fout << "\n";
  1970. const char* commands[] = { command, command2, command3, command4 };
  1971. for (unsigned int i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i)
  1972. {
  1973. if(commands[i])
  1974. {
  1975. replace = commands[i];
  1976. m_Makefile->ExpandVariablesInString(replace);
  1977. if(replace[0] != '-' && replace.find("echo") != 0
  1978. && replace.find("$(MAKE)") != 0)
  1979. {
  1980. std::string echostring = replace;
  1981. // for unix we want to quote the output of echo
  1982. // for nmake and borland, the echo should not be quoted
  1983. if(strcmp(this->GetName(), "Unix Makefiles") == 0)
  1984. {
  1985. cmSystemTools::ReplaceString(echostring, "\\\n", " ");
  1986. cmSystemTools::ReplaceString(echostring, " \t", " ");
  1987. cmSystemTools::ReplaceString(echostring, "\n\t", "\"\n\techo \"");
  1988. fout << "\techo \"" << echostring.c_str() << "\"\n";
  1989. }
  1990. else
  1991. {
  1992. cmSystemTools::ReplaceString(echostring, "\n\t", "\n\techo ");
  1993. fout << "\techo " << echostring.c_str() << "\n";
  1994. }
  1995. }
  1996. fout << "\t" << replace.c_str() << "\n";
  1997. }
  1998. }
  1999. fout << "\n";
  2000. }
  2001. void cmUnixMakefileGenerator::SetLocal (bool local)
  2002. {
  2003. if (local)
  2004. {
  2005. m_CacheOnly = false;
  2006. m_Recurse = false;
  2007. }
  2008. else
  2009. {
  2010. m_CacheOnly = true;
  2011. m_Recurse = true;
  2012. }
  2013. }
  2014. void cmUnixMakefileGenerator::EnableLanguage(const char* lang)
  2015. {
  2016. if (m_CacheOnly)
  2017. {
  2018. // see man putenv for explaination of this stupid code....
  2019. static char envCXX[5000];
  2020. static char envCC[5000];
  2021. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  2022. {
  2023. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  2024. m_Makefile->ExpandVariablesInString(env);
  2025. strncpy(envCXX, env.c_str(), 4999);
  2026. envCXX[4999] = 0;
  2027. putenv(envCXX);
  2028. }
  2029. if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
  2030. {
  2031. std::string env = "CC=${CMAKE_C_COMPILER}";
  2032. m_Makefile->ExpandVariablesInString(env);
  2033. strncpy(envCC, env.c_str(), 4999);
  2034. envCC[4999] = 0;
  2035. putenv(envCC);
  2036. }
  2037. std::string output;
  2038. std::string root
  2039. = this->ConvertToOutputPath(m_Makefile->GetDefinition("CMAKE_ROOT"));
  2040. // if no lang specified use CXX
  2041. if(!lang )
  2042. {
  2043. lang = "CXX";
  2044. }
  2045. // if CXX or C, then enable C
  2046. if((!this->GetLanguageEnabled(lang) && lang[0] == 'C'))
  2047. {
  2048. std::string cmd = root;
  2049. cmd += "/Templates/cconfigure";
  2050. cmSystemTools::RunCommand(cmd.c_str(), output,
  2051. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()).c_str());
  2052. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  2053. fpath += "/CCMakeSystemConfig.cmake";
  2054. m_Makefile->ReadListFile(NULL,fpath.c_str());
  2055. this->SetLanguageEnabled("C");
  2056. }
  2057. // if CXX
  2058. if(!this->GetLanguageEnabled(lang) || strcmp(lang, "CXX") == 0)
  2059. {
  2060. std::string cmd = root;
  2061. cmd += "/Templates/cxxconfigure";
  2062. cmSystemTools::RunCommand(cmd.c_str(), output,
  2063. this->ConvertToOutputPath(m_Makefile->GetHomeOutputDirectory()).c_str());
  2064. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  2065. fpath += "/CXXCMakeSystemConfig.cmake";
  2066. m_Makefile->ReadListFile(NULL,fpath.c_str());
  2067. this->SetLanguageEnabled("CXX");
  2068. }
  2069. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  2070. }
  2071. }