cmUnixMakefileGenerator.cxx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmUnixMakefileGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmStandardIncludes.h"
  14. #include "cmSystemTools.h"
  15. #include "cmClassFile.h"
  16. #include "cmMakeDepend.h"
  17. void cmUnixMakefileGenerator::GenerateMakefile()
  18. {
  19. // Generate depends
  20. cmMakeDepend md;
  21. md.SetMakefile(m_Makefile);
  22. md.DoDepends();
  23. // output the makefile fragment
  24. this->OutputMakefile("CMakeTargets.make");
  25. }
  26. // This is where CMakeTargets.make is generated
  27. void cmUnixMakefileGenerator::OutputMakefile(const char* file)
  28. {
  29. // Create sub directories fro aux source directories
  30. std::vector<std::string>& auxSourceDirs =
  31. m_Makefile->GetAuxSourceDirectories();
  32. if( auxSourceDirs.size() )
  33. {
  34. // For the case when this is running as a remote build
  35. // on unix, make the directory
  36. for(std::vector<std::string>::iterator i = auxSourceDirs.begin();
  37. i != auxSourceDirs.end(); ++i)
  38. {
  39. cmSystemTools::MakeDirectory(i->c_str());
  40. }
  41. }
  42. std::ofstream fout(file);
  43. if(!fout)
  44. {
  45. cmSystemTools::Error("Error can not open for write: ", file);
  46. return;
  47. }
  48. this->OutputMakeFlags(fout);
  49. this->OutputSourceToObjectList(fout);
  50. this->OutputVerbatim(fout);
  51. this->OutputExecutableRules(fout);
  52. this->OutputSubDirectoryRules(fout);
  53. this->OutputDepends(fout);
  54. }
  55. // Output the LIBRARY and SRC_OBJS list based on
  56. // the library name and cmClassFile objects in the
  57. // makefile
  58. void cmUnixMakefileGenerator::OutputSourceToObjectList(std::ostream& fout)
  59. {
  60. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  61. if(Classes.size() == 0)
  62. {
  63. return;
  64. }
  65. // Ouput Library name if there are SRC_OBJS
  66. if(strlen(m_Makefile->GetLibraryName()) > 0)
  67. {
  68. fout << "LIBRARY = " << m_Makefile->GetLibraryName() << "\n\n";
  69. fout << "BUILD_LIB_FILE = lib${LIBRARY}${CMAKE_LIB_EXT}\n\n";
  70. }
  71. // Output SRC_OBJ list for all the classes to be compiled
  72. fout << "SRC_OBJ = \\\n";
  73. for(unsigned int i = 0; i < Classes.size(); i++)
  74. {
  75. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  76. && !Classes[i].m_IsExecutable)
  77. {
  78. fout << Classes[i].m_ClassName << ".o ";
  79. if(i == Classes.size() -1)
  80. {
  81. fout << "\n\n";
  82. }
  83. else
  84. {
  85. fout << "\\\n";
  86. }
  87. }
  88. }
  89. fout << "\n";
  90. }
  91. // output the list of libraries that the executables
  92. // in this makefile will depend on.
  93. void cmUnixMakefileGenerator::OutputDependLibraries(std::ostream& fout)
  94. {
  95. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  96. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  97. std::vector<std::string>::iterator dir, lib;
  98. // Search the list of libraries that will be linked into
  99. // the executable
  100. for(lib = libs.begin(); lib != libs.end(); ++lib)
  101. {
  102. bool found = false;
  103. // loop over the list of directories that the libraries might
  104. // be in, looking for a LIBRARY=(lib) line.
  105. for(dir = libdirs.begin(); dir != libdirs.end() && !found; ++dir)
  106. {
  107. std::string expression = "LIBRARY.*=.*";
  108. expression += lib->c_str();
  109. if(cmSystemTools::Grep(dir->c_str(), "CMakeTargets.make",
  110. expression.c_str()))
  111. {
  112. std::string libpath = *dir;
  113. libpath += "/lib";
  114. libpath += *lib;
  115. libpath += "${CMAKE_LIB_EXT}";
  116. fout << libpath << " ";
  117. found = true;
  118. }
  119. }
  120. }
  121. fout << "\n";
  122. }
  123. // output make include flags
  124. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  125. {
  126. // Output Include paths
  127. fout << "INCLUDE_FLAGS = ";
  128. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  129. std::vector<std::string>::iterator i;
  130. for(i = includes.begin(); i != includes.end(); ++i)
  131. {
  132. std::string include = *i;
  133. fout << "-I" << i->c_str() << " ";
  134. }
  135. fout << " ${LOCAL_INCLUDE_FLAGS} ";
  136. fout << "\n";
  137. fout << "default_target: all\n\n";
  138. // see if there are files to compile in this makefile
  139. // These are used for both libraries and executables
  140. }
  141. // output verbatim section
  142. void cmUnixMakefileGenerator::OutputVerbatim(std::ostream& fout)
  143. {
  144. std::vector<std::string>& MakeVerbatim = m_Makefile->GetMakeVerbatim();
  145. // Ouput user make text embeded in the input file
  146. for(unsigned int i =0; i < MakeVerbatim.size(); i++)
  147. {
  148. fout << MakeVerbatim[i] << "\n";
  149. }
  150. fout << "\n\n";
  151. }
  152. // output executables
  153. void cmUnixMakefileGenerator::OutputExecutableRules(std::ostream& fout)
  154. {
  155. if(!m_Makefile->HasExecutables())
  156. {
  157. return ;
  158. }
  159. // collect all the flags needed for linking libraries
  160. std::string linkLibs;
  161. std::vector<std::string>::iterator j;
  162. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  163. for(j = libdirs.begin(); j != libdirs.end(); ++j)
  164. {
  165. std::string::size_type pos = (*j).find("-L");
  166. if((pos == std::string::npos || pos > 0)
  167. && (*j).find("${") == std::string::npos)
  168. {
  169. linkLibs += "-L";
  170. }
  171. linkLibs += *j;
  172. linkLibs += " ";
  173. }
  174. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  175. for(j = libs.begin(); j != libs.end(); ++j)
  176. {
  177. std::string::size_type pos = (*j).find("-l");
  178. if((pos == std::string::npos || pos > 0)
  179. && (*j).find("${") == std::string::npos)
  180. {
  181. linkLibs += "-l";
  182. }
  183. linkLibs += *j;
  184. linkLibs += " ";
  185. }
  186. std::vector<std::string>& libsUnix = m_Makefile->GetLinkLibrariesUnix();
  187. for(j = libsUnix.begin(); j != libsUnix.end(); ++j)
  188. {
  189. linkLibs += *j;
  190. linkLibs += " ";
  191. }
  192. linkLibs += " ${LOCAL_LINK_FLAGS} ";
  193. // create and output a varible in the makefile that
  194. // each executable will depend on. This will have all the
  195. // libraries that the executable uses
  196. fout << "CMAKE_DEPEND_LIBS = ";
  197. this->OutputDependLibraries(fout);
  198. // Now create rules for all of the executables to be built
  199. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  200. for(unsigned int i = 0; i < Classes.size(); i++)
  201. {
  202. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  203. && Classes[i].m_IsExecutable)
  204. {
  205. std::string DotO = Classes[i].m_ClassName;
  206. DotO += ".o";
  207. fout << Classes[i].m_ClassName << ": " << DotO << " ";
  208. fout << "${CMAKE_DEPEND_LIBS}\n";
  209. fout << "\t${CXX} ${CXX_FLAGS} " << m_Makefile->GetDefineFlags()
  210. << DotO.c_str() << " "
  211. << linkLibs.c_str()
  212. << " -o $@ ""\n\n";
  213. }
  214. }
  215. // ouput the list of executables
  216. fout << "EXECUTABLES = \\\n";
  217. for(unsigned int i = 0; i < Classes.size(); i++)
  218. {
  219. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  220. && Classes[i].m_IsExecutable)
  221. {
  222. fout << Classes[i].m_ClassName;
  223. if(i < Classes.size()-1)
  224. {
  225. fout << " \\";
  226. }
  227. fout << "\n";
  228. }
  229. }
  230. fout << "\n";
  231. }
  232. // fix up names of directories so they can be used
  233. // as targets in makefiles.
  234. inline std::string FixDirectoryName(const char* dir)
  235. {
  236. std::string s = dir;
  237. // replace ../ with 3 under bars
  238. size_t pos = s.find("../");
  239. if(pos != std::string::npos)
  240. {
  241. s.replace(pos, 3, "___");
  242. }
  243. // replace / directory separators with a single under bar
  244. pos = s.find("/");
  245. while(pos != std::string::npos)
  246. {
  247. s.replace(pos, 1, "_");
  248. pos = s.find("/");
  249. }
  250. return s;
  251. }
  252. // output rules for decending into sub directories
  253. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  254. {
  255. // Output Sub directory build rules
  256. const std::vector<std::string>& SubDirectories
  257. = m_Makefile->GetSubDirectories();
  258. if( SubDirectories.size() == 0)
  259. {
  260. return;
  261. }
  262. fout << "SUBDIR_BUILD = \\\n";
  263. unsigned int i;
  264. for(i =0; i < SubDirectories.size(); i++)
  265. {
  266. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  267. fout << "build_" << subdir.c_str();
  268. if(i == SubDirectories.size()-1)
  269. {
  270. fout << " \n\n";
  271. }
  272. else
  273. {
  274. fout << " \\\n";
  275. }
  276. }
  277. fout << std::endl;
  278. fout << "SUBDIR_CLEAN = \\\n";
  279. for(i =0; i < SubDirectories.size(); i++)
  280. {
  281. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  282. fout << "clean_" << subdir.c_str();
  283. if(i == SubDirectories.size()-1)
  284. {
  285. fout << " \n\n";
  286. }
  287. else
  288. {
  289. fout << " \\\n";
  290. }
  291. }
  292. fout << std::endl;
  293. fout << "alldirs : ${SUBDIR_BUILD}\n\n";
  294. for(i =0; i < SubDirectories.size(); i++)
  295. {
  296. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  297. fout << "build_" << subdir.c_str() << ":\n";
  298. fout << "\tcd " << SubDirectories[i].c_str()
  299. << "; ${MAKE} -${MAKEFLAGS} CMakeTargets.make\n";
  300. fout << "\tcd " << SubDirectories[i].c_str()
  301. << "; ${MAKE} -${MAKEFLAGS} all\n\n";
  302. fout << "clean_" << subdir.c_str() << ": \n";
  303. fout << "\tcd " << SubDirectories[i].c_str()
  304. << "; ${MAKE} -${MAKEFLAGS} clean\n\n";
  305. }
  306. }
  307. // Output the depend information for all the classes
  308. // in the makefile. These would have been generated
  309. // by the class cmMakeDepend GenerateMakefile
  310. void cmUnixMakefileGenerator::OutputDepends(std::ostream& fout)
  311. {
  312. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  313. for(unsigned int i = 0; i < Classes.size(); i++)
  314. {
  315. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
  316. {
  317. if( Classes[i].m_Depends.size())
  318. {
  319. fout << Classes[i].m_ClassName << ".o : \\\n";
  320. for(std::vector<std::string>::iterator j =
  321. Classes[i].m_Depends.begin();
  322. j != Classes[i].m_Depends.end(); ++j)
  323. {
  324. if(j+1 == Classes[i].m_Depends.end())
  325. {
  326. fout << *j << " \n";
  327. }
  328. else
  329. {
  330. fout << *j << " \\\n";
  331. }
  332. }
  333. fout << "\n\n";
  334. }
  335. }
  336. }
  337. }