cmUnixMakefileGenerator.cxx 70 KB

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