cmUnixMakefileGenerator.cxx 72 KB

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