cmUnixMakefileGenerator.cxx 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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. // create a make variable with all of the sources for this Makefile
  212. // for depend purposes.
  213. fout << "CMAKE_MAKEFILE_SOURCES = ";
  214. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  215. i != lfiles.end(); ++i)
  216. {
  217. fout << " " << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(i->c_str()).c_str());
  218. }
  219. // Add the cache to the list
  220. std::string cacheFile = m_Makefile->GetHomeOutputDirectory();
  221. cacheFile += "/CMakeCache.txt";
  222. fout << " " <<
  223. this->ConvertToNativePath(cmSystemTools::EscapeSpaces(cacheFile.c_str()).c_str());
  224. fout << "\n\n\n";
  225. this->OutputMakeVariables(fout);
  226. // Set up the default target as the VERY first target, so that make with no arguments will run it
  227. this->OutputMakeRule(fout,
  228. "Default target executed when no arguments are given to make",
  229. "default_target",
  230. 0,
  231. "$(MAKE) -$(MAKEFLAGS) cmake.depends",
  232. "$(MAKE) -$(MAKEFLAGS) all");
  233. this->OutputTargetRules(fout);
  234. this->OutputDependLibs(fout);
  235. this->OutputTargets(fout);
  236. this->OutputSubDirectoryRules(fout);
  237. std::string dependName = m_Makefile->GetStartOutputDirectory();
  238. dependName += "/cmake.depends";
  239. if(!this->m_CacheOnly)
  240. {
  241. std::ofstream dependout(dependName.c_str());
  242. if(!dependout)
  243. {
  244. cmSystemTools::Error("Error can not open for write: ", dependName.c_str());
  245. return;
  246. }
  247. this->OutputObjectDepends(dependout);
  248. }
  249. this->OutputCustomRules(fout);
  250. this->OutputMakeRules(fout);
  251. this->OutputInstallRules(fout);
  252. // only add the depend include if the depend file exists
  253. if(cmSystemTools::FileExists(dependName.c_str()))
  254. {
  255. this->OutputIncludeMakefile(fout, "cmake.depends");
  256. }
  257. }
  258. void cmUnixMakefileGenerator::OutputIncludeMakefile(std::ostream& fout,
  259. const char* file)
  260. {
  261. fout << "include " << file << "\n";
  262. }
  263. std::string
  264. cmUnixMakefileGenerator::GetOutputExtension(const char*)
  265. {
  266. return m_ObjectFileExtension;
  267. }
  268. // Output the rules for any targets
  269. void cmUnixMakefileGenerator::OutputTargetRules(std::ostream& fout)
  270. {
  271. // for each target add to the list of targets
  272. fout << "TARGETS = ";
  273. const cmTargets &tgts = m_Makefile->GetTargets();
  274. // list libraries first
  275. for(cmTargets::const_iterator l = tgts.begin();
  276. l != tgts.end(); l++)
  277. {
  278. if (l->second.IsInAll())
  279. {
  280. std::string path = m_LibraryOutputPath + m_LibraryPrefix;
  281. if(l->second.GetType() == cmTarget::STATIC_LIBRARY)
  282. {
  283. path = path + l->first + m_StaticLibraryExtension;
  284. fout << " \\\n"
  285. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  286. }
  287. else if(l->second.GetType() == cmTarget::SHARED_LIBRARY)
  288. {
  289. path = path + l->first +
  290. m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  291. fout << " \\\n"
  292. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  293. }
  294. else if(l->second.GetType() == cmTarget::MODULE_LIBRARY)
  295. {
  296. path = path + l->first +
  297. m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  298. fout << " \\\n"
  299. << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  300. }
  301. }
  302. }
  303. // executables
  304. for(cmTargets::const_iterator l = tgts.begin();
  305. l != tgts.end(); l++)
  306. {
  307. if ((l->second.GetType() == cmTarget::EXECUTABLE ||
  308. l->second.GetType() == cmTarget::WIN32_EXECUTABLE) &&
  309. l->second.IsInAll())
  310. {
  311. std::string path = m_ExecutableOutputPath + l->first +
  312. m_ExecutableExtension;
  313. fout << " \\\n" << this->ConvertToNativePath(cmSystemTools::EscapeSpaces(path.c_str()).c_str());
  314. }
  315. }
  316. // list utilities last
  317. for(cmTargets::const_iterator l = tgts.begin();
  318. l != tgts.end(); l++)
  319. {
  320. if (l->second.GetType() == cmTarget::UTILITY &&
  321. l->second.IsInAll())
  322. {
  323. fout << " \\\n" << l->first.c_str();
  324. }
  325. }
  326. fout << "\n\n";
  327. // get the classes from the source lists then add them to the groups
  328. for(cmTargets::const_iterator l = tgts.begin();
  329. l != tgts.end(); l++)
  330. {
  331. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  332. if (classes.begin() != classes.end())
  333. {
  334. fout << l->first << "_SRC_OBJS = ";
  335. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  336. i != classes.end(); i++)
  337. {
  338. if(!i->IsAHeaderFileOnly())
  339. {
  340. std::string outExt(this->GetOutputExtension(i->GetSourceExtension().c_str()));
  341. if(outExt.size())
  342. {
  343. fout << "\\\n" << this->ConvertToNativePath(i->GetSourceName().c_str())
  344. << outExt.c_str() << " ";
  345. }
  346. }
  347. }
  348. fout << "\n\n";
  349. }
  350. }
  351. fout << "CLEAN_OBJECT_FILES = ";
  352. for(cmTargets::const_iterator l = tgts.begin();
  353. l != tgts.end(); l++)
  354. {
  355. std::vector<cmSourceFile> classes = l->second.GetSourceFiles();
  356. if (classes.begin() != classes.end())
  357. {
  358. fout << "$(" << l->first << "_SRC_OBJS) ";
  359. }
  360. }
  361. fout << "\n\n";
  362. }
  363. /**
  364. * Output the linking rules on a command line. For executables,
  365. * targetLibrary should be a NULL pointer. For libraries, it should point
  366. * to the name of the library. This will not link a library against itself.
  367. */
  368. void cmUnixMakefileGenerator::OutputLinkLibraries(std::ostream& fout,
  369. const char* targetLibrary,
  370. const cmTarget &tgt)
  371. {
  372. // Try to emit each search path once
  373. std::set<std::string> emitted;
  374. // Embed runtime search paths if possible and if required.
  375. bool outputRuntime = true;
  376. std::string runtimeFlag;
  377. std::string runtimeSep;
  378. std::vector<std::string> runtimeDirs;
  379. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG"))
  380. {
  381. runtimeFlag = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_FLAG");
  382. }
  383. if(m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP"))
  384. {
  385. runtimeSep = m_Makefile->GetDefinition("CMAKE_SHLIB_RUNTIME_SEP");
  386. }
  387. // concatenate all paths or no?
  388. bool runtimeConcatenate = ( runtimeSep!="" );
  389. if(runtimeFlag == "")
  390. {
  391. outputRuntime = false;
  392. }
  393. // Some search paths should never be emitted
  394. emitted.insert("");
  395. emitted.insert("/usr/lib");
  396. // collect all the flags needed for linking libraries
  397. std::string linkLibs;
  398. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  399. for(std::vector<std::string>::iterator libDir = libdirs.begin();
  400. libDir != libdirs.end(); ++libDir)
  401. {
  402. std::string libpath = cmSystemTools::EscapeSpaces(libDir->c_str());
  403. if(emitted.insert(libpath).second)
  404. {
  405. std::string::size_type pos = libDir->find("-L");
  406. if((pos == std::string::npos || pos > 0)
  407. && libDir->find("${") == std::string::npos)
  408. {
  409. linkLibs += "-L";
  410. if(outputRuntime)
  411. {
  412. runtimeDirs.push_back( libpath );
  413. }
  414. }
  415. linkLibs += libpath;
  416. linkLibs += " ";
  417. }
  418. }
  419. std::string librariesLinked;
  420. const cmTarget::LinkLibraries& libs = tgt.GetLinkLibraries();
  421. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  422. lib != libs.end(); ++lib)
  423. {
  424. // Don't link the library against itself!
  425. if(targetLibrary && (lib->first == targetLibrary)) continue;
  426. // don't look at debug libraries
  427. if (lib->second == cmTarget::DEBUG) continue;
  428. // skip zero size library entries, this may happen
  429. // if a variable expands to nothing.
  430. if (lib->first.size() == 0) continue;
  431. // if it is a full path break it into -L and -l
  432. cmRegularExpression reg("(^[ \t]*\\-l)|(^[ \t]*\\-framework)|(\\${)");
  433. if(lib->first.find('/') != std::string::npos
  434. && !reg.find(lib->first))
  435. {
  436. std::string dir, file;
  437. cmSystemTools::SplitProgramPath(lib->first.c_str(),
  438. dir, file);
  439. std::string libpath = cmSystemTools::EscapeSpaces(dir.c_str());
  440. if(emitted.insert(libpath).second)
  441. {
  442. linkLibs += "-L";
  443. linkLibs += libpath;
  444. linkLibs += " ";
  445. if(outputRuntime)
  446. {
  447. runtimeDirs.push_back( libpath );
  448. }
  449. }
  450. cmRegularExpression libname("lib(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  451. cmRegularExpression libname_noprefix("(.*)(\\.so|\\.sl|\\.a|\\.dylib).*");
  452. if(libname.find(file))
  453. {
  454. librariesLinked += "-l";
  455. file = libname.match(1);
  456. librariesLinked += file;
  457. librariesLinked += " ";
  458. }
  459. else if(libname_noprefix.find(file))
  460. {
  461. librariesLinked += "-l";
  462. file = libname_noprefix.match(1);
  463. librariesLinked += file;
  464. librariesLinked += " ";
  465. }
  466. }
  467. // not a full path, so add -l name
  468. else
  469. {
  470. if(!reg.find(lib->first))
  471. {
  472. librariesLinked += "-l";
  473. }
  474. librariesLinked += lib->first;
  475. librariesLinked += " ";
  476. }
  477. }
  478. linkLibs += librariesLinked;
  479. if(!targetLibrary)
  480. {
  481. // For executables, add these a second time so order does not matter
  482. linkLibs += librariesLinked;
  483. }
  484. fout << linkLibs;
  485. if(outputRuntime && runtimeDirs.size()>0)
  486. {
  487. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  488. // a "-R a -R b -R c" type link line
  489. fout << runtimeFlag;
  490. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  491. fout << *itr;
  492. ++itr;
  493. for( ; itr != runtimeDirs.end(); ++itr )
  494. {
  495. if(runtimeConcatenate)
  496. {
  497. fout << runtimeSep << *itr;
  498. }
  499. else
  500. {
  501. fout << " " << runtimeFlag << *itr;
  502. }
  503. }
  504. fout << " ";
  505. }
  506. }
  507. void cmUnixMakefileGenerator::OutputSharedLibraryRule(std::ostream& fout,
  508. const char* name,
  509. const cmTarget &t)
  510. {
  511. std::string target = m_LibraryOutputPath + "lib" + name + "$(SHLIB_SUFFIX)";
  512. std::string depend = "$(";
  513. depend += name;
  514. depend += "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  515. std::string command = "rm -f lib";
  516. command += name;
  517. command += "$(SHLIB_SUFFIX)";
  518. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) "
  519. "$(CMAKE_SHLIB_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  520. command2 += "\t ";
  521. command2 += m_LibraryOutputPath + "lib" + std::string(name) + "$(SHLIB_SUFFIX) \\\n";
  522. command2 += "\t $(" + std::string(name) + "_SRC_OBJS) ";
  523. std::strstream linklibs;
  524. this->OutputLinkLibraries(linklibs, name, t);
  525. linklibs << std::ends;
  526. command2 += linklibs.str();
  527. delete [] linklibs.str();
  528. this->OutputMakeRule(fout, "rules for a shared library",
  529. target.c_str(),
  530. depend.c_str(),
  531. command.c_str(),
  532. command2.c_str());
  533. }
  534. void cmUnixMakefileGenerator::OutputModuleLibraryRule(std::ostream& fout,
  535. const char* name,
  536. const cmTarget &t)
  537. {
  538. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX)";
  539. std::string depend = "$(";
  540. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  541. std::string command = "rm -f lib" + std::string(name) + "$(MODULE_SUFFIX)";
  542. std::string command2 = "$(CMAKE_CXX_COMPILER) $(CMAKE_MODULE_LINK_FLAGS) "
  543. "$(CMAKE_MODULE_BUILD_FLAGS) $(CMAKE_CXX_FLAGS) -o \\\n";
  544. command2 += "\t ";
  545. command2 += m_LibraryOutputPath + "lib" + std::string(name) + "$(MODULE_SUFFIX) \\\n";
  546. command2 += "\t $(" + std::string(name) + "_SRC_OBJS) ";
  547. std::strstream linklibs;
  548. this->OutputLinkLibraries(linklibs, std::string(name).c_str(), t);
  549. linklibs << std::ends;
  550. command2 += linklibs.str();
  551. delete [] linklibs.str();
  552. this->OutputMakeRule(fout, "rules for a shared module library",
  553. target.c_str(),
  554. depend.c_str(),
  555. command.c_str(),
  556. command2.c_str());
  557. }
  558. void cmUnixMakefileGenerator::OutputStaticLibraryRule(std::ostream& fout,
  559. const char* name,
  560. const cmTarget &)
  561. {
  562. std::string target = m_LibraryOutputPath + "lib" + std::string(name) + ".a";
  563. std::string depend = "$(";
  564. depend += std::string(name) + "_SRC_OBJS)";
  565. std::string command = "$(CMAKE_AR) $(CMAKE_AR_ARGS) ";
  566. command += m_LibraryOutputPath;
  567. command += "lib";
  568. command += name;
  569. command += ".a $(";
  570. command += std::string(name) + "_SRC_OBJS)";
  571. std::string command2 = "$(CMAKE_RANLIB) ";
  572. command2 += m_LibraryOutputPath;
  573. command2 += "lib";
  574. command2 += std::string(name) + ".a";
  575. std::string comment = "rule to build static library: ";
  576. comment += name;
  577. this->OutputMakeRule(fout,
  578. comment.c_str(),
  579. target.c_str(),
  580. depend.c_str(),
  581. command.c_str(),
  582. command2.c_str());
  583. }
  584. void cmUnixMakefileGenerator::OutputExecutableRule(std::ostream& fout,
  585. const char* name,
  586. const cmTarget &t)
  587. {
  588. std::string target = m_ExecutableOutputPath + name;
  589. std::string depend = "$(";
  590. depend += std::string(name) + "_SRC_OBJS) $(" + std::string(name) + "_DEPEND_LIBS)";
  591. std::string command =
  592. "$(CMAKE_CXX_COMPILER) $(CMAKE_SHLIB_LINK_FLAGS) $(CMAKE_CXX_FLAGS) ";
  593. command += "$(" + std::string(name) + "_SRC_OBJS) ";
  594. std::strstream linklibs;
  595. this->OutputLinkLibraries(linklibs, 0, t);
  596. linklibs << std::ends;
  597. command += linklibs.str();
  598. command += " -o " + m_ExecutableOutputPath + name;
  599. std::string comment = "rule to build executable: ";
  600. comment += name;
  601. this->OutputMakeRule(fout,
  602. comment.c_str(),
  603. target.c_str(),
  604. depend.c_str(),
  605. command.c_str());
  606. }
  607. void cmUnixMakefileGenerator::OutputTargets(std::ostream& fout)
  608. {
  609. // for each target
  610. const cmTargets &tgts = m_Makefile->GetTargets();
  611. for(cmTargets::const_iterator l = tgts.begin();
  612. l != tgts.end(); l++)
  613. {
  614. switch(l->second.GetType())
  615. {
  616. case cmTarget::STATIC_LIBRARY:
  617. this->OutputStaticLibraryRule(fout, l->first.c_str(), l->second);
  618. break;
  619. case cmTarget::SHARED_LIBRARY:
  620. this->OutputSharedLibraryRule(fout, l->first.c_str(), l->second);
  621. break;
  622. case cmTarget::MODULE_LIBRARY:
  623. this->OutputModuleLibraryRule(fout, l->first.c_str(), l->second);
  624. break;
  625. case cmTarget::EXECUTABLE:
  626. case cmTarget::WIN32_EXECUTABLE:
  627. this->OutputExecutableRule(fout, l->first.c_str(), l->second);
  628. break;
  629. case cmTarget::UTILITY:
  630. // This is handled by the OutputCustomRules method
  631. case cmTarget::INSTALL_FILES:
  632. // This is handled by the OutputInstallRules method
  633. case cmTarget::INSTALL_PROGRAMS:
  634. // This is handled by the OutputInstallRules method
  635. case cmTarget::GENERATED_CODE:
  636. // This is handled by the CustomCommands
  637. break;
  638. }
  639. }
  640. }
  641. // For each target that is an executable or shared library, generate
  642. // the "<name>_DEPEND_LIBS" variable listing its library dependencies.
  643. void cmUnixMakefileGenerator::OutputDependLibs(std::ostream& fout)
  644. {
  645. // Build a set of libraries that will be linked into any target in
  646. // this directory.
  647. std::set<std::string> used;
  648. // for each target
  649. const cmTargets &tgts = m_Makefile->GetTargets();
  650. for(cmTargets::const_iterator l = tgts.begin();
  651. l != tgts.end(); l++)
  652. {
  653. // Each dependency should only be emitted once per target.
  654. std::set<std::string> emitted;
  655. if ((l->second.GetType() == cmTarget::SHARED_LIBRARY)
  656. || (l->second.GetType() == cmTarget::MODULE_LIBRARY)
  657. || (l->second.GetType() == cmTarget::EXECUTABLE)
  658. || (l->second.GetType() == cmTarget::WIN32_EXECUTABLE))
  659. {
  660. fout << l->first << "_DEPEND_LIBS = ";
  661. // A library should not depend on itself!
  662. emitted.insert(l->first);
  663. // First look at all makefile level link libraries.
  664. const cmTarget::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  665. for(cmTarget::LinkLibraries::const_iterator lib = libs.begin();
  666. lib != libs.end(); ++lib)
  667. {
  668. // Record that this library was used.
  669. used.insert(lib->first);
  670. // Don't emit the same library twice for this target.
  671. if(emitted.insert(lib->first).second)
  672. {
  673. // Output this dependency.
  674. this->OutputLibDepend(fout, lib->first.c_str());
  675. }
  676. }
  677. // Now, look at all link libraries specific to this target.
  678. const cmTarget::LinkLibraries& tlibs = l->second.GetLinkLibraries();
  679. for(cmTarget::LinkLibraries::const_iterator lib = tlibs.begin();
  680. lib != tlibs.end(); ++lib)
  681. {
  682. // Record that this library was used.
  683. used.insert(lib->first);
  684. // Don't emit the same library twice for this target.
  685. if(emitted.insert(lib->first).second)
  686. {
  687. // Output this dependency.
  688. this->OutputLibDepend(fout, lib->first.c_str());
  689. }
  690. }
  691. fout << "\n";
  692. }
  693. }
  694. fout << "\n";
  695. // Loop over the libraries used and make sure there is a rule to
  696. // build them in this makefile. If the library is in another
  697. // directory, add a rule to jump to that directory and make sure it
  698. // exists.
  699. for(std::set<std::string>::const_iterator lib = used.begin();
  700. lib != used.end(); ++lib)
  701. {
  702. // loop over the list of directories that the libraries might
  703. // be in, looking for an ADD_LIBRARY(lib...) line. This would
  704. // be stored in the cache
  705. const char* cacheValue = m_Makefile->GetDefinition(lib->c_str());
  706. // if cache and not the current directory add a rule, to
  707. // jump into the directory and build for the first time
  708. if(cacheValue &&
  709. (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue)))
  710. {
  711. std::string library = m_LibraryPrefix;
  712. library += *lib;
  713. std::string libpath = cacheValue;
  714. // add the correct extension
  715. std::string ltname = *lib+"_LIBRARY_TYPE";
  716. const char* libType
  717. = m_Makefile->GetDefinition(ltname.c_str());
  718. if(libType && std::string(libType) == "SHARED")
  719. {
  720. library += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  721. }
  722. else if(libType && std::string(libType) == "MODULE")
  723. {
  724. library += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  725. }
  726. else
  727. {
  728. library += m_StaticLibraryExtension;
  729. }
  730. if(m_LibraryOutputPath.size())
  731. {
  732. libpath = m_LibraryOutputPath;
  733. }
  734. else
  735. {
  736. libpath += "/";
  737. }
  738. libpath += library;
  739. // put out a rule to build the library if it does not exist
  740. this->OutputBuildLibraryInDir(fout,
  741. cacheValue,
  742. library.c_str(),
  743. libpath.c_str());
  744. }
  745. }
  746. }
  747. void cmUnixMakefileGenerator::OutputBuildLibraryInDir(std::ostream& fout,
  748. const char* path,
  749. const char* ,
  750. const char* fullpath)
  751. {
  752. fout << cmSystemTools::EscapeSpaces(fullpath)
  753. << ":\n\tcd " << cmSystemTools::EscapeSpaces(path)
  754. << "; $(MAKE) " << fullpath << "\n\n";
  755. }
  756. bool cmUnixMakefileGenerator::SamePath(const char* path1, const char* path2)
  757. {
  758. return strcmp(path1, path2) == 0;
  759. }
  760. void cmUnixMakefileGenerator::OutputLibDepend(std::ostream& fout,
  761. const char* name)
  762. {
  763. const char* cacheValue = m_Makefile->GetDefinition(name);
  764. if(cacheValue )
  765. {
  766. // if there is a cache value, then this is a library that cmake
  767. // knows how to build, so we can depend on it
  768. std::string libpath;
  769. if (!this->SamePath(m_Makefile->GetCurrentOutputDirectory(), cacheValue))
  770. {
  771. // if the library is not in the current directory, then get the full
  772. // path to it
  773. libpath = cacheValue;
  774. if(m_LibraryOutputPath.size())
  775. {
  776. libpath = m_LibraryOutputPath;
  777. libpath += m_LibraryPrefix;
  778. }
  779. else
  780. {
  781. libpath += "/";
  782. libpath += m_LibraryPrefix;
  783. }
  784. }
  785. else
  786. {
  787. // library is in current Makefile so use lib as a prefix
  788. libpath = m_LibraryOutputPath;
  789. libpath += m_LibraryPrefix;
  790. }
  791. // add the library name
  792. libpath += name;
  793. // add the correct extension
  794. std::string ltname = name;
  795. ltname += "_LIBRARY_TYPE";
  796. const char* libType = m_Makefile->GetDefinition(ltname.c_str());
  797. if(libType && std::string(libType) == "SHARED")
  798. {
  799. libpath += m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  800. }
  801. else if (libType && std::string(libType) == "MODULE")
  802. {
  803. libpath += m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  804. }
  805. else
  806. {
  807. libpath += m_StaticLibraryExtension;
  808. }
  809. fout << cmSystemTools::EscapeSpaces(libpath.c_str()) << " ";
  810. }
  811. }
  812. // fix up names of directories so they can be used
  813. // as targets in makefiles.
  814. inline std::string FixDirectoryName(const char* dir)
  815. {
  816. std::string s = dir;
  817. // replace ../ with 3 under bars
  818. size_t pos = s.find("../");
  819. if(pos != std::string::npos)
  820. {
  821. s.replace(pos, 3, "___");
  822. }
  823. // replace / directory separators with a single under bar
  824. pos = s.find("/");
  825. while(pos != std::string::npos)
  826. {
  827. s.replace(pos, 1, "_");
  828. pos = s.find("/");
  829. }
  830. return s;
  831. }
  832. void cmUnixMakefileGenerator::BuildInSubDirectory(std::ostream& fout,
  833. const char* dir,
  834. const char* target1,
  835. const char* target2)
  836. {
  837. std::string directory = cmSystemTools::EscapeSpaces(dir);
  838. if(target1)
  839. {
  840. fout << "\t@if test ! -d " << directory
  841. << "; then $(MAKE) rebuild_cache; fi\n"
  842. "\tcd " << directory
  843. << "; $(MAKE) -$(MAKEFLAGS) " << target1 << "\n";
  844. }
  845. if(target2)
  846. {
  847. fout << "\t@cd " << directory
  848. << "; $(MAKE) -$(MAKEFLAGS) " << target2 << "\n";
  849. }
  850. }
  851. void
  852. cmUnixMakefileGenerator::
  853. OutputSubDirectoryVars(std::ostream& fout,
  854. const char* var,
  855. const char* target,
  856. const char* target1,
  857. const char* target2,
  858. const std::vector<std::string>& SubDirectories)
  859. {
  860. if( SubDirectories.size() == 0)
  861. {
  862. return;
  863. }
  864. fout << "# Variable for making " << target << " in subdirectories.\n";
  865. fout << var << " = \\\n";
  866. unsigned int i;
  867. for(i =0; i < SubDirectories.size(); i++)
  868. {
  869. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  870. fout << target << "_" << subdir.c_str();
  871. if(i == SubDirectories.size()-1)
  872. {
  873. fout << " \n\n";
  874. }
  875. else
  876. {
  877. fout << " \\\n";
  878. }
  879. }
  880. fout << "# Targets for making " << target << " in subdirectories.\n";
  881. for(unsigned int i =0; i < SubDirectories.size(); i++)
  882. {
  883. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  884. fout << target << "_" << subdir.c_str() << ":";
  885. const std::set<cmStdString>& subdirDepends = m_Makefile->GetSubdirDepends(SubDirectories[i].c_str());
  886. for(std::set<cmStdString>::const_iterator d = subdirDepends.begin();
  887. d != subdirDepends.end(); ++d)
  888. {
  889. std::string fixed_d = FixDirectoryName(d->c_str());
  890. fout << " " << target << "_" << fixed_d.c_str();
  891. }
  892. fout << "\n";
  893. this->BuildInSubDirectory(fout, SubDirectories[i].c_str(),
  894. target1, target2);
  895. }
  896. fout << "\n\n";
  897. }
  898. // output rules for decending into sub directories
  899. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  900. {
  901. // Output Sub directory build rules
  902. const std::vector<std::string>& SubDirectories
  903. = m_Makefile->GetSubDirectories();
  904. if( SubDirectories.size() == 0)
  905. {
  906. return;
  907. }
  908. this->OutputSubDirectoryVars(fout, "SUBDIR_BUILD", "build",
  909. "cmake.depends",
  910. "all",
  911. SubDirectories);
  912. this->OutputSubDirectoryVars(fout, "SUBDIR_CLEAN", "clean",
  913. "clean",
  914. 0,
  915. SubDirectories);
  916. this->OutputSubDirectoryVars(fout, "SUBDIR_DEPEND", "depend",
  917. "depend",
  918. 0,
  919. SubDirectories);
  920. this->OutputSubDirectoryVars(fout, "SUBDIR_INSTALL", "install",
  921. "install",
  922. 0,
  923. SubDirectories);
  924. }
  925. // Output the depend information for all the classes
  926. // in the makefile. These would have been generated
  927. // by the class cmMakeDepend GenerateMakefile
  928. void cmUnixMakefileGenerator::OutputObjectDepends(std::ostream& fout)
  929. {
  930. // Iterate over every target.
  931. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  932. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  933. target != targets.end(); ++target)
  934. {
  935. // Iterate over every source for this target.
  936. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  937. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  938. source != sources.end(); ++source)
  939. {
  940. if(!source->IsAHeaderFileOnly())
  941. {
  942. if(!source->GetDepends().empty())
  943. {
  944. fout << source->GetSourceName() << m_ObjectFileExtension << " :";
  945. // Iterate through all the dependencies for this source.
  946. for(std::vector<std::string>::const_iterator dep =
  947. source->GetDepends().begin();
  948. dep != source->GetDepends().end(); ++dep)
  949. {
  950. fout << " \\\n" << cmSystemTools::EscapeSpaces(dep->c_str());
  951. }
  952. fout << "\n\n";
  953. }
  954. }
  955. }
  956. }
  957. }
  958. // Output each custom rule in the following format:
  959. // output: source depends...
  960. // (tab) command...
  961. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  962. {
  963. // We may be modifying the source groups temporarily, so make a copy.
  964. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  965. const cmTargets &tgts = m_Makefile->GetTargets();
  966. for(cmTargets::const_iterator tgt = tgts.begin();
  967. tgt != tgts.end(); ++tgt)
  968. {
  969. // add any custom rules to the source groups
  970. for (std::vector<cmCustomCommand>::const_iterator cr =
  971. tgt->second.GetCustomCommands().begin();
  972. cr != tgt->second.GetCustomCommands().end(); ++cr)
  973. {
  974. cmSourceGroup& sourceGroup =
  975. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  976. sourceGroups);
  977. cmCustomCommand cc(*cr);
  978. cc.ExpandVariables(*m_Makefile);
  979. sourceGroup.AddCustomCommand(cc);
  980. }
  981. }
  982. // Loop through every source group.
  983. for(std::vector<cmSourceGroup>::const_iterator sg =
  984. sourceGroups.begin(); sg != sourceGroups.end(); ++sg)
  985. {
  986. const cmSourceGroup::BuildRules& buildRules = sg->GetBuildRules();
  987. if(buildRules.empty())
  988. { continue; }
  989. std::string name = sg->GetName();
  990. if(name != "")
  991. {
  992. fout << "# Start of source group \"" << name.c_str() << "\"\n";
  993. }
  994. // Loop through each source in the source group.
  995. for(cmSourceGroup::BuildRules::const_iterator cc =
  996. buildRules.begin(); cc != buildRules.end(); ++ cc)
  997. {
  998. std::string source = cc->first;
  999. const cmSourceGroup::Commands& commands = cc->second;
  1000. // Loop through every command generating code from the current source.
  1001. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  1002. c != commands.end(); ++c)
  1003. {
  1004. std::string command = c->first;
  1005. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  1006. // if the command has no outputs, then it is a utility command
  1007. // with no outputs
  1008. if(commandFiles.m_Outputs.size() == 0)
  1009. {
  1010. std::string depends;
  1011. // collect out all the dependencies for this rule.
  1012. for(std::set<std::string>::const_iterator d =
  1013. commandFiles.m_Depends.begin();
  1014. d != commandFiles.m_Depends.end(); ++d)
  1015. {
  1016. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  1017. depends += " ";
  1018. depends += dep;
  1019. }
  1020. // output rule
  1021. this->OutputMakeRule(fout,
  1022. "Custom command",
  1023. source.c_str(),
  1024. depends.c_str(),
  1025. command.c_str());
  1026. }
  1027. // Write a rule for every output generated by this command.
  1028. for(std::set<std::string>::const_iterator output =
  1029. commandFiles.m_Outputs.begin();
  1030. output != commandFiles.m_Outputs.end(); ++output)
  1031. {
  1032. std::string src = cmSystemTools::EscapeSpaces(source.c_str());
  1033. std::string depends;
  1034. depends += src;
  1035. // Collect out all the dependencies for this rule.
  1036. for(std::set<std::string>::const_iterator d =
  1037. commandFiles.m_Depends.begin();
  1038. d != commandFiles.m_Depends.end(); ++d)
  1039. {
  1040. std::string dep = cmSystemTools::EscapeSpaces(d->c_str());
  1041. depends += " ";
  1042. depends += dep;
  1043. }
  1044. // output rule
  1045. this->OutputMakeRule(fout,
  1046. "Custom command",
  1047. output->c_str(),
  1048. depends.c_str(),
  1049. command.c_str());
  1050. }
  1051. }
  1052. }
  1053. if(name != "")
  1054. {
  1055. fout << "# End of source group \"" << name.c_str() << "\"\n\n";
  1056. }
  1057. }
  1058. }
  1059. void cmUnixMakefileGenerator::GenerateCacheOnly()
  1060. {
  1061. cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory());
  1062. std::string dest = m_Makefile->GetStartOutputDirectory();
  1063. dest += "/Makefile";
  1064. std::cout << "cmake: creating : " << dest.c_str() << std::endl;
  1065. this->OutputMakefile(dest.c_str());
  1066. return;
  1067. }
  1068. void cmUnixMakefileGenerator::RecursiveGenerateCacheOnly()
  1069. {
  1070. std::vector<cmMakefile*> makefiles;
  1071. m_Makefile->FindSubDirectoryCMakeListsFiles(makefiles);
  1072. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  1073. i != makefiles.end(); ++i)
  1074. {
  1075. cmMakefile* mf = *i;
  1076. if(m_Makefile->GetDefinition("RUN_CONFIGURE"))
  1077. {
  1078. mf->AddDefinition("RUN_CONFIGURE", true);
  1079. }
  1080. cmUnixMakefileGenerator* gen =
  1081. static_cast<cmUnixMakefileGenerator*>(this->CreateObject());
  1082. gen->SetCacheOnlyOn();
  1083. gen->SetRecurseOff();
  1084. mf->SetMakefileGenerator(gen);
  1085. mf->GenerateMakefile();
  1086. }
  1087. // CLEAN up the makefiles created
  1088. for(unsigned int i =0; i < makefiles.size(); ++i)
  1089. {
  1090. delete makefiles[i];
  1091. }
  1092. }
  1093. void cmUnixMakefileGenerator::OutputMakeVariables(std::ostream& fout)
  1094. {
  1095. const char* variables =
  1096. "# the standard shell for make\n"
  1097. "SHELL = /bin/sh\n"
  1098. "\n"
  1099. "CMAKE_LIB_EXT = @CMAKE_LIB_EXT@\n"
  1100. "CMAKE_RANLIB = @CMAKE_RANLIB@\n"
  1101. "CMAKE_AR = @CMAKE_AR@\n"
  1102. "CMAKE_AR_ARGS = @CMAKE_AR_ARGS@\n"
  1103. "CMAKE_C_COMPILER = @CMAKE_C_COMPILER@\n"
  1104. "CMAKE_C_FLAGS = @CMAKE_C_FLAGS@\n"
  1105. "CMAKE_SHLIB_CFLAGS = @CMAKE_SHLIB_CFLAGS@\n"
  1106. "\n"
  1107. "CMAKE_CXX_COMPILER = @CMAKE_CXX_COMPILER@\n"
  1108. "CMAKE_CXX_FLAGS = @CMAKE_CXX_FLAGS@\n"
  1109. "\n"
  1110. "CMAKE_SHLIB_BUILD_FLAGS = @CMAKE_SHLIB_BUILD_FLAGS@\n"
  1111. "CMAKE_SHLIB_LINK_FLAGS = @CMAKE_SHLIB_LINK_FLAGS@\n"
  1112. "CMAKE_MODULE_BUILD_FLAGS = @CMAKE_MODULE_BUILD_FLAGS@\n"
  1113. "CMAKE_MODULE_LINK_FLAGS = @CMAKE_MODULE_LINK_FLAGS@\n"
  1114. "DL_LIBS = @CMAKE_DL_LIBS@\n"
  1115. "SHLIB_LD_LIBS = @CMAKE_SHLIB_LD_LIBS@\n"
  1116. "SHLIB_SUFFIX = @CMAKE_SHLIB_SUFFIX@\n"
  1117. "MODULE_SUFFIX = @CMAKE_MODULE_SUFFIX@\n"
  1118. "THREAD_LIBS = @CMAKE_THREAD_LIBS@\n"
  1119. "\n"
  1120. "# set up the path to the rulesgen program\n"
  1121. "CMAKE_COMMAND = ${CMAKE_COMMAND}"
  1122. "\n"
  1123. "\n"
  1124. "\n";
  1125. std::string replaceVars = variables;
  1126. m_Makefile->ExpandVariablesInString(replaceVars);
  1127. fout << replaceVars.c_str();
  1128. fout << "CMAKE_CURRENT_SOURCE = " <<
  1129. cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << "\n";
  1130. fout << "CMAKE_CURRENT_BINARY = " <<
  1131. cmSystemTools::EscapeSpaces(m_Makefile->GetStartOutputDirectory()) << "\n";
  1132. fout << "CMAKE_SOURCE_DIR = " <<
  1133. cmSystemTools::EscapeSpaces(m_Makefile->GetHomeDirectory()) << "\n";
  1134. fout << "CMAKE_BINARY_DIR = " <<
  1135. cmSystemTools::EscapeSpaces(m_Makefile->GetHomeOutputDirectory()) << "\n";
  1136. // Output Include paths
  1137. fout << "INCLUDE_FLAGS = ";
  1138. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1139. std::vector<std::string>::iterator i;
  1140. fout << "-I" <<
  1141. cmSystemTools::EscapeSpaces(m_Makefile->GetStartDirectory()) << " ";
  1142. for(i = includes.begin(); i != includes.end(); ++i)
  1143. {
  1144. std::string include = *i;
  1145. // Don't output a -I for the standard include path "/usr/include".
  1146. // This can cause problems with certain standard library
  1147. // implementations because the wrong headers may be found first.
  1148. if(include != "/usr/include")
  1149. {
  1150. fout << "-I" << cmSystemTools::EscapeSpaces(i->c_str()).c_str() << " ";
  1151. }
  1152. }
  1153. fout << m_Makefile->GetDefineFlags();
  1154. fout << "\n\n";
  1155. }
  1156. void cmUnixMakefileGenerator::OutputInstallRules(std::ostream& fout)
  1157. {
  1158. const char* root
  1159. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1160. fout << "INSTALL = " << root << "/Templates/install-sh -c\n";
  1161. fout << "INSTALL_PROGRAM = $(INSTALL)\n";
  1162. fout << "INSTALL_DATA = $(INSTALL) -m 644\n";
  1163. const cmTargets &tgts = m_Makefile->GetTargets();
  1164. fout << "install: $(SUBDIR_INSTALL)\n";
  1165. fout << "\t@echo \"Installing ...\"\n";
  1166. const char* prefix
  1167. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  1168. if (!prefix)
  1169. {
  1170. prefix = "/usr/local";
  1171. }
  1172. for(cmTargets::const_iterator l = tgts.begin();
  1173. l != tgts.end(); l++)
  1174. {
  1175. if (l->second.GetInstallPath() != "")
  1176. {
  1177. // first make the directories for each target
  1178. fout << "\t@if [ ! -d " << prefix << l->second.GetInstallPath() <<
  1179. " ] ; then \\\n";
  1180. fout << "\t echo \"Making directory " << prefix
  1181. << l->second.GetInstallPath() << " \"; \\\n";
  1182. fout << "\t mkdir -p " << prefix << l->second.GetInstallPath()
  1183. << "; \\\n";
  1184. fout << "\t chmod 755 " << prefix << l->second.GetInstallPath()
  1185. << "; \\\n";
  1186. fout << "\t else true; \\\n";
  1187. fout << "\t fi\n";
  1188. // now install the target
  1189. switch (l->second.GetType())
  1190. {
  1191. case cmTarget::STATIC_LIBRARY:
  1192. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1193. << l->first;
  1194. fout << ".a";
  1195. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  1196. break;
  1197. case cmTarget::SHARED_LIBRARY:
  1198. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1199. << l->first;
  1200. fout << m_Makefile->GetDefinition("CMAKE_SHLIB_SUFFIX");
  1201. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  1202. break;
  1203. case cmTarget::MODULE_LIBRARY:
  1204. fout << "\t$(INSTALL_DATA) " << m_LibraryOutputPath << "lib"
  1205. << l->first;
  1206. fout << m_Makefile->GetDefinition("CMAKE_MODULE_SUFFIX");
  1207. fout << " " << prefix << l->second.GetInstallPath() << "\n";
  1208. break;
  1209. case cmTarget::WIN32_EXECUTABLE:
  1210. case cmTarget::EXECUTABLE:
  1211. fout << "\t$(INSTALL_PROGRAM) " << m_ExecutableOutputPath
  1212. << l->first
  1213. << cmSystemTools::GetExecutableExtension()
  1214. << " " << prefix << l->second.GetInstallPath() << "\n";
  1215. break;
  1216. case cmTarget::INSTALL_FILES:
  1217. {
  1218. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1219. std::vector<std::string>::const_iterator i;
  1220. for (i = sf.begin(); i != sf.end(); ++i)
  1221. {
  1222. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1223. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1224. // avoid using install-sh to install install-sh
  1225. // does not work on windows....
  1226. if(*i == "install-sh")
  1227. {
  1228. fout << "\t cp ";
  1229. }
  1230. else
  1231. {
  1232. fout << "\t $(INSTALL_DATA) ";
  1233. }
  1234. fout << *i
  1235. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1236. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1237. // avoid using install-sh to install install-sh
  1238. // does not work on windows....
  1239. if(*i == "install-sh")
  1240. {
  1241. fout << "\t cp ";
  1242. }
  1243. else
  1244. {
  1245. fout << "\t $(INSTALL_DATA) ";
  1246. }
  1247. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1248. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1249. fout << "\telse \\\n";
  1250. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1251. << " \"; \\\n";
  1252. fout << "\t fi\n";
  1253. }
  1254. }
  1255. break;
  1256. case cmTarget::INSTALL_PROGRAMS:
  1257. {
  1258. const std::vector<std::string> &sf = l->second.GetSourceLists();
  1259. std::vector<std::string>::const_iterator i;
  1260. for (i = sf.begin(); i != sf.end(); ++i)
  1261. {
  1262. fout << "\t@ echo \"Installing " << *i << " \"\n";
  1263. fout << "\t@if [ -f " << *i << " ] ; then \\\n";
  1264. // avoid using install-sh to install install-sh
  1265. // does not work on windows....
  1266. if(*i == "install-sh")
  1267. {
  1268. fout << "\t cp ";
  1269. }
  1270. else
  1271. {
  1272. fout << "\t $(INSTALL_PROGRAM) ";
  1273. }
  1274. fout << *i
  1275. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1276. fout << "\t elif [ -f $(CMAKE_CURRENT_SOURCE)/" << *i << " ] ; then \\\n";
  1277. // avoid using install-sh to install install-sh
  1278. // does not work on windows....
  1279. if(*i == "install-sh")
  1280. {
  1281. fout << "\t cp ";
  1282. }
  1283. else
  1284. {
  1285. fout << "\t $(INSTALL_PROGRAM) ";
  1286. }
  1287. fout << "$(CMAKE_CURRENT_SOURCE)/" << *i
  1288. << " " << prefix << l->second.GetInstallPath() << "; \\\n";
  1289. fout << "\telse \\\n";
  1290. fout << "\t echo \" ERROR!!! Unable to find: " << *i
  1291. << " \"; \\\n";
  1292. fout << "\t fi\n";
  1293. }
  1294. }
  1295. break;
  1296. case cmTarget::UTILITY:
  1297. default:
  1298. break;
  1299. }
  1300. }
  1301. }
  1302. }
  1303. void cmUnixMakefileGenerator::OutputMakeRules(std::ostream& fout)
  1304. {
  1305. this->OutputMakeRule(fout,
  1306. "Default build rule",
  1307. "all",
  1308. "cmake.depends $(TARGETS) $(SUBDIR_BUILD)",
  1309. 0);
  1310. if (m_Makefile->IsOn("QT_WRAP_CPP") ||
  1311. m_Makefile->IsOn("QT_WRAP_UI") ||
  1312. m_Makefile->IsOn("FLTK_WRAP_UI") )
  1313. {
  1314. this->OutputMakeRule(fout,
  1315. "remove generated files and dependency file",
  1316. "clean",
  1317. "$(SUBDIR_CLEAN)",
  1318. "rm -f $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
  1319. " $(TARGETS) ${GENERATED_QT_FILES}"
  1320. " ${GENERATED_FLTK_FILES}",
  1321. "make depend");
  1322. }
  1323. else
  1324. {
  1325. this->OutputMakeRule(fout,
  1326. "remove generated files",
  1327. "clean",
  1328. "$(SUBDIR_CLEAN)",
  1329. "rm -f $(CLEAN_OBJECT_FILES) $(EXECUTABLES)"
  1330. " $(TARGETS)");
  1331. }
  1332. this->OutputMakeRule(fout,
  1333. "Rule to build the cmake.depends and Makefile as side effect",
  1334. "cmake.depends",
  1335. "$(CMAKE_MAKEFILE_SOURCES) ",
  1336. "$(CMAKE_COMMAND) "
  1337. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1338. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1339. this->OutputMakeRule(fout,
  1340. "Rule to force the build of cmake.depends",
  1341. "depend",
  1342. "$(SUBDIR_DEPEND)",
  1343. "$(CMAKE_COMMAND) "
  1344. "-S$(CMAKE_CURRENT_SOURCE) -O$(CMAKE_CURRENT_BINARY) "
  1345. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1346. this->OutputMakeRule(fout,
  1347. "Rebuild CMakeCache.txt file",
  1348. "rebuild_cache",
  1349. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1350. "$(CMAKE_COMMAND) "
  1351. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1352. this->OutputMakeRule(fout,
  1353. "Create CMakeCache.txt file",
  1354. "$(CMAKE_BINARY_DIR)/CMakeCache.txt",
  1355. 0,
  1356. "$(CMAKE_COMMAND) "
  1357. "-H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)");
  1358. this->OutputMakeRule(fout,
  1359. "Rule to keep make from removing Makefiles "
  1360. "if control-C is hit during a run of cmake.",
  1361. ".PRECIOUS",
  1362. "Makefile cmake.depends",
  1363. 0);
  1364. this->OutputSourceObjectBuildRules(fout);
  1365. // find ctest
  1366. std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
  1367. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  1368. ctest += "/";
  1369. ctest += "ctest";
  1370. ctest += m_ExecutableExtension;
  1371. if (cmSystemTools::FileExists(ctest.c_str()))
  1372. {
  1373. this->OutputMakeRule(fout,
  1374. "run any tests",
  1375. "test",
  1376. "",
  1377. ctest.c_str());
  1378. }
  1379. }
  1380. void
  1381. cmUnixMakefileGenerator::
  1382. OutputBuildObjectFromSource(std::ostream& fout,
  1383. const char* shortName,
  1384. const cmSourceFile& source,
  1385. const char* extraCompileFlags,
  1386. bool shared)
  1387. {
  1388. std::string comment = "Build ";
  1389. std::string objectFile = std::string(shortName) + m_ObjectFileExtension;
  1390. comment += objectFile + " From ";
  1391. comment += source.GetFullPath();
  1392. std::string compileCommand;
  1393. std::string ext = source.GetSourceExtension();
  1394. if(ext == "c" )
  1395. {
  1396. compileCommand = "$(CMAKE_C_COMPILER) $(CMAKE_C_FLAGS) ";
  1397. compileCommand += extraCompileFlags;
  1398. if(shared)
  1399. {
  1400. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1401. }
  1402. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1403. compileCommand +=
  1404. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  1405. compileCommand += " -o ";
  1406. compileCommand += objectFile;
  1407. }
  1408. else
  1409. {
  1410. compileCommand = "$(CMAKE_CXX_COMPILER) $(CMAKE_CXX_FLAGS) ";
  1411. compileCommand += extraCompileFlags;
  1412. if(shared)
  1413. {
  1414. compileCommand += "$(CMAKE_SHLIB_CFLAGS) ";
  1415. }
  1416. compileCommand += "$(INCLUDE_FLAGS) -c ";
  1417. compileCommand +=
  1418. cmSystemTools::EscapeSpaces(source.GetFullPath().c_str());
  1419. compileCommand += " -o ";
  1420. compileCommand += objectFile;
  1421. }
  1422. this->OutputMakeRule(fout,
  1423. comment.c_str(),
  1424. objectFile.c_str(),
  1425. cmSystemTools::EscapeSpaces(source.GetFullPath().
  1426. c_str()).c_str(),
  1427. compileCommand.c_str());
  1428. }
  1429. void cmUnixMakefileGenerator::OutputSourceObjectBuildRules(std::ostream& fout)
  1430. {
  1431. fout << "# Rules to build " << m_ObjectFileExtension
  1432. << " files from their sources:\n";
  1433. std::set<std::string> rules;
  1434. // Iterate over every target.
  1435. std::map<cmStdString, cmTarget>& targets = m_Makefile->GetTargets();
  1436. for(std::map<cmStdString, cmTarget>::const_iterator target = targets.begin();
  1437. target != targets.end(); ++target)
  1438. {
  1439. bool shared = ((target->second.GetType() == cmTarget::SHARED_LIBRARY) ||
  1440. (target->second.GetType() == cmTarget::MODULE_LIBRARY));
  1441. std::string exportsDef = "";
  1442. if(shared)
  1443. {
  1444. exportsDef = "-D"+target->first+"_EXPORTS ";
  1445. }
  1446. // Iterate over every source for this target.
  1447. const std::vector<cmSourceFile>& sources = target->second.GetSourceFiles();
  1448. for(std::vector<cmSourceFile>::const_iterator source = sources.begin();
  1449. source != sources.end(); ++source)
  1450. {
  1451. if(!source->IsAHeaderFileOnly())
  1452. {
  1453. std::string shortName;
  1454. std::string sourceName;
  1455. // If the full path to the source file includes this
  1456. // directory, we want to use the relative path for the
  1457. // filename of the object file. Otherwise, we will use just
  1458. // the filename portion.
  1459. if((cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->GetCurrentDirectory()) == 0)
  1460. || (cmSystemTools::GetFilenamePath(source->GetFullPath()).find(m_Makefile->
  1461. GetCurrentOutputDirectory()) == 0))
  1462. {
  1463. sourceName = source->GetSourceName()+"."+source->GetSourceExtension();
  1464. shortName = source->GetSourceName();
  1465. // The path may be relative. See if a directory needs to be
  1466. // created for the output file. This is a ugly, and perhaps
  1467. // should be moved elsewhere.
  1468. std::string relPath =
  1469. cmSystemTools::GetFilenamePath(source->GetSourceName());
  1470. if(relPath != "")
  1471. {
  1472. std::string outPath = m_Makefile->GetCurrentOutputDirectory();
  1473. outPath += "/"+relPath;
  1474. cmSystemTools::MakeDirectory(outPath.c_str());
  1475. }
  1476. }
  1477. else
  1478. {
  1479. sourceName = source->GetFullPath();
  1480. shortName = cmSystemTools::GetFilenameName(source->GetSourceName());
  1481. }
  1482. std::string shortNameWithExt = shortName +
  1483. source->GetSourceExtension();
  1484. // Only output a rule for each .o once.
  1485. if(rules.find(shortNameWithExt) == rules.end())
  1486. {
  1487. this->OutputBuildObjectFromSource(fout,
  1488. shortName.c_str(),
  1489. *source,
  1490. exportsDef.c_str(),
  1491. shared);
  1492. rules.insert(shortNameWithExt);
  1493. }
  1494. }
  1495. }
  1496. }
  1497. }
  1498. void cmUnixMakefileGenerator::OutputMakeRule(std::ostream& fout,
  1499. const char* comment,
  1500. const char* target,
  1501. const char* depends,
  1502. const char* command,
  1503. const char* command2,
  1504. const char* command3,
  1505. const char* command4)
  1506. {
  1507. if(!target)
  1508. {
  1509. cmSystemTools::Error("no target for OutputMakeRule");
  1510. return;
  1511. }
  1512. std::string replace;
  1513. if(comment)
  1514. {
  1515. replace = comment;
  1516. m_Makefile->ExpandVariablesInString(replace);
  1517. fout << "#---------------------------------------------------------\n";
  1518. fout << "# " << comment;
  1519. fout << "\n#\n";
  1520. }
  1521. fout << "\n";
  1522. replace = target;
  1523. m_Makefile->ExpandVariablesInString(replace);
  1524. fout << replace.c_str() << ": ";
  1525. if(depends)
  1526. {
  1527. replace = depends;
  1528. m_Makefile->ExpandVariablesInString(replace);
  1529. fout << replace.c_str();
  1530. }
  1531. fout << "\n";
  1532. if(command)
  1533. {
  1534. replace = command;
  1535. m_Makefile->ExpandVariablesInString(replace);
  1536. fout << "\t" << replace.c_str() << "\n";
  1537. }
  1538. if(command2)
  1539. {
  1540. replace = command2;
  1541. m_Makefile->ExpandVariablesInString(replace);
  1542. fout << "\t" << replace.c_str() << "\n";
  1543. }
  1544. if(command3)
  1545. {
  1546. replace = command3;
  1547. m_Makefile->ExpandVariablesInString(replace);
  1548. fout << "\t" << replace.c_str() << "\n";
  1549. }
  1550. if(command4)
  1551. {
  1552. replace = command4;
  1553. m_Makefile->ExpandVariablesInString(replace);
  1554. fout << "\t" << replace.c_str() << "\n";
  1555. }
  1556. fout << "\n";
  1557. }
  1558. void cmUnixMakefileGenerator::SetLocal (bool local)
  1559. {
  1560. if (local)
  1561. {
  1562. m_CacheOnly = false;
  1563. m_Recurse = false;
  1564. }
  1565. else
  1566. {
  1567. m_CacheOnly = true;
  1568. m_Recurse = true;
  1569. }
  1570. }
  1571. void cmUnixMakefileGenerator::ComputeSystemInfo()
  1572. {
  1573. if (m_CacheOnly)
  1574. {
  1575. if(m_Makefile->GetDefinition("CMAKE_CXX_COMPILER"))
  1576. {
  1577. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  1578. m_Makefile->ExpandVariablesInString(env);
  1579. putenv(const_cast<char*>(env.c_str()));
  1580. }
  1581. if(m_Makefile->GetDefinition("CMAKE_C_COMPILER"))
  1582. {
  1583. std::string env = "CC=${CMAKE_C_COMPILER}";
  1584. m_Makefile->ExpandVariablesInString(env);
  1585. putenv(const_cast<char*>(env.c_str()));
  1586. }
  1587. // currently we run configure shell script here to determine the info
  1588. std::string output;
  1589. std::string cmd = "cd ";
  1590. cmd += cmSystemTools::EscapeSpaces(m_Makefile->GetHomeOutputDirectory());
  1591. cmd += "; ";
  1592. const char* root
  1593. = m_Makefile->GetDefinition("CMAKE_ROOT");
  1594. cmd += root;
  1595. cmd += "/Templates/configure";
  1596. cmSystemTools::RunCommand(cmd.c_str(), output);
  1597. m_Makefile->AddDefinition("RUN_CONFIGURE", true);
  1598. }
  1599. // now load the settings
  1600. std::string fpath = m_Makefile->GetHomeOutputDirectory();
  1601. fpath += "/CMakeSystemConfig.cmake";
  1602. m_Makefile->ReadListFile(NULL,fpath.c_str());
  1603. }