cmLocalUnixMakefileGenerator.cxx 71 KB

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