cmUnixMakefileGenerator.cxx 71 KB

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