cmLocalUnixMakefileGenerator.cxx 71 KB

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