cmUnixMakefile.cxx 7.5 KB

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