cmUnixMakefile.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "cmUnixMakefile.h"
  2. #include <fstream>
  3. #include <iostream>
  4. // Output the depend information for all the classes
  5. // in the makefile.
  6. void cmUnixMakefile::OutputDepends(std::ostream& fout)
  7. {
  8. for(int i = 0; i < m_Classes.size(); i++)
  9. {
  10. if(!m_Classes[i].m_AbstractClass && !m_Classes[i].m_HeaderFileOnly)
  11. {
  12. if( m_Classes[i].m_Depends.size())
  13. {
  14. fout << m_Classes[i].m_ClassName << ".o : \\\n";
  15. for(std::vector<std::string>::iterator j =
  16. m_Classes[i].m_Depends.begin();
  17. j != m_Classes[i].m_Depends.end(); ++j)
  18. {
  19. if(j+1 == m_Classes[i].m_Depends.end())
  20. {
  21. fout << *j << " \n";
  22. }
  23. else
  24. {
  25. fout << *j << " \\\n";
  26. }
  27. }
  28. fout << "\n\n";
  29. }
  30. }
  31. }
  32. }
  33. // fix up names of directories so they can be used
  34. // as targets in makefiles.
  35. inline std::string FixDirectoryName(const char* dir)
  36. {
  37. std::string s = dir;
  38. // replace ../ with 3 under bars
  39. size_t pos = s.find("../");
  40. if(pos != std::string::npos)
  41. {
  42. s.replace(pos, 3, "___");
  43. }
  44. // replace / directory separators with a single under bar
  45. pos = s.find("/");
  46. while(pos != std::string::npos)
  47. {
  48. s.replace(pos, 1, "_");
  49. pos = s.find("/");
  50. }
  51. return s;
  52. }
  53. // This is where CMakeTargets.make is generated
  54. // This function ouputs the following:
  55. // 1. Include flags for the compiler
  56. // 2. List of .o files that need to be compiled
  57. // 3. Rules to build executables including -l and -L options
  58. // 4. Rules to build in sub directories
  59. // 5. The name of the library being built, if it is a library
  60. void cmUnixMakefile::OutputMakefile(const char* file)
  61. {
  62. std::ofstream fout(file);
  63. if(!fout)
  64. {
  65. std::cerr << "Error can not open " << file << " for write" << std::endl;
  66. return;
  67. }
  68. // Output Include paths
  69. fout << "INCLUDE_FLAGS = ";
  70. std::vector<std::string>& includes = m_BuildFlags.GetIncludeDirectories();
  71. std::vector<std::string>::iterator i;
  72. for(i = includes.begin(); i != includes.end(); ++i)
  73. {
  74. std::string include = *i;
  75. fout << "-I" << i->c_str() << " ";
  76. }
  77. fout << " ${LOCAL_INCLUDE_FLAGS} ";
  78. fout << "\n";
  79. // see if there are files to compile in this makefile
  80. // These are used for both libraries and executables
  81. if(m_Classes.size() )
  82. {
  83. // Ouput Library name if there are SRC_OBJS
  84. if(strlen(this->GetLibraryName()) > 0)
  85. {
  86. fout << "ME = " << this->GetLibraryName() << "\n\n";
  87. fout << "BUILD_LIB_FILE = lib${ME}${ITK_LIB_EXT}\n\n";
  88. }
  89. // Output SRC_OBJ list for all the classes to be compiled
  90. fout << "SRC_OBJ = \\\n";
  91. for(int i = 0; i < m_Classes.size(); i++)
  92. {
  93. if(!m_Classes[i].m_AbstractClass && !m_Classes[i].m_HeaderFileOnly)
  94. {
  95. fout << m_Classes[i].m_ClassName << ".o ";
  96. if(i == m_Classes.size() -1)
  97. {
  98. fout << "\n\n";
  99. }
  100. else
  101. {
  102. fout << "\\\n";
  103. }
  104. }
  105. }
  106. fout << "\n";
  107. }
  108. // Ouput user make text embeded in the input file
  109. for(int i =0; i < m_MakeVerbatim.size(); i++)
  110. {
  111. fout << m_MakeVerbatim[i] << "\n";
  112. }
  113. fout << "\n\n";
  114. // Output rules for building executables
  115. if( m_Executables )
  116. {
  117. // collect all the flags needed for linking libraries
  118. std::string linkLibs;
  119. std::vector<std::string>::iterator j;
  120. std::vector<std::string>& libdirs = m_BuildFlags.GetLinkDirectories();
  121. for(j = libdirs.begin(); j != libdirs.end(); ++j)
  122. {
  123. linkLibs += "-L";
  124. linkLibs += *j;
  125. linkLibs += " ";
  126. }
  127. std::vector<std::string>& libs = m_BuildFlags.GetLinkLibraries();
  128. for(j = libs.begin(); j != libs.end(); ++j)
  129. {
  130. linkLibs += "-l";
  131. linkLibs += *j;
  132. linkLibs += " ";
  133. }
  134. std::vector<std::string>& libsUnix = m_BuildFlags.GetLinkLibrariesUnix();
  135. for(j = libsUnix.begin(); j != libsUnix.end(); ++j)
  136. {
  137. linkLibs += *j;
  138. linkLibs += " ";
  139. }
  140. linkLibs += " ${LOCAL_LINK_FLAGS} ";
  141. // Now create rules for all of the executables to be built
  142. for(int i = 0; i < m_Classes.size(); i++)
  143. {
  144. if(!m_Classes[i].m_AbstractClass && !m_Classes[i].m_HeaderFileOnly)
  145. {
  146. std::string DotO = m_Classes[i].m_ClassName;
  147. DotO += ".o";
  148. fout << m_Classes[i].m_ClassName << ": " << DotO << "\n";
  149. fout << "\t${CXX} ${CXX_FLAGS} "
  150. << DotO.c_str() << " "
  151. << linkLibs.c_str()
  152. << " -o $@ ""\n\n";
  153. }
  154. }
  155. // ouput the list of executables
  156. fout << "EXECUTABLES = \\\n";
  157. for(int i = 0; i < m_Classes.size(); i++)
  158. {
  159. if(!m_Classes[i].m_AbstractClass && !m_Classes[i].m_HeaderFileOnly)
  160. {
  161. fout << m_Classes[i].m_ClassName;
  162. if(i < m_Classes.size()-1)
  163. {
  164. fout << " \\";
  165. }
  166. fout << "\n";
  167. }
  168. }
  169. fout << "\n";
  170. }
  171. // Output Sub directory build rules
  172. if( m_SubDirectories.size() )
  173. {
  174. fout << "SUBDIR_BUILD = \\\n";
  175. int i;
  176. for(i =0; i < m_SubDirectories.size(); i++)
  177. {
  178. std::string subdir = FixDirectoryName(m_SubDirectories[i].c_str());
  179. fout << "build_" << subdir.c_str();
  180. if(i == m_SubDirectories.size()-1)
  181. {
  182. fout << " \n\n";
  183. }
  184. else
  185. {
  186. fout << " \\\n";
  187. }
  188. }
  189. fout << std::endl;
  190. fout << "SUBDIR_CLEAN = \\\n";
  191. for(i =0; i < m_SubDirectories.size(); i++)
  192. {
  193. std::string subdir = FixDirectoryName(m_SubDirectories[i].c_str());
  194. fout << "clean_" << subdir.c_str();
  195. if(i == m_SubDirectories.size()-1)
  196. {
  197. fout << " \n\n";
  198. }
  199. else
  200. {
  201. fout << " \\\n";
  202. }
  203. }
  204. fout << std::endl;
  205. fout << "alldirs : ${SUBDIR_BUILD}\n\n";
  206. for(i =0; i < m_SubDirectories.size(); i++)
  207. {
  208. std::string subdir = FixDirectoryName(m_SubDirectories[i].c_str());
  209. fout << "build_" << subdir.c_str() << ":\n";
  210. fout << "\tcd " << m_SubDirectories[i].c_str()
  211. << "; ${MAKE} -${MAKEFLAGS} CMakeTargets.make\n";
  212. fout << "\tcd " << m_SubDirectories[i].c_str()
  213. << "; ${MAKE} -${MAKEFLAGS} all\n\n";
  214. fout << "clean_" << subdir.c_str() << ": \n";
  215. fout << "\tcd " << m_SubDirectories[i].c_str()
  216. << "; ${MAKE} -${MAKEFLAGS} clean\n\n";
  217. }
  218. }
  219. this->OutputDepends(fout);
  220. }