cmUnixMakefileGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. this->OutputCustomRules(fout);
  55. }
  56. // Output the LIBRARY and SRC_OBJS list based on
  57. // the library name and cmClassFile objects in the
  58. // makefile
  59. void cmUnixMakefileGenerator::OutputSourceToObjectList(std::ostream& fout)
  60. {
  61. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  62. if(Classes.size() == 0)
  63. {
  64. return;
  65. }
  66. // Ouput Library name if there are SRC_OBJS
  67. if(strlen(m_Makefile->GetLibraryName()) > 0)
  68. {
  69. fout << "LIBRARY = " << m_Makefile->GetLibraryName() << "\n\n";
  70. fout << "BUILD_LIB_FILE = lib${LIBRARY}${CMAKE_LIB_EXT}\n\n";
  71. }
  72. // Output SRC_OBJ list for all the classes to be compiled
  73. fout << "SRC_OBJ = \\\n";
  74. for(unsigned int i = 0; i < Classes.size(); i++)
  75. {
  76. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  77. && !Classes[i].m_IsExecutable)
  78. {
  79. fout << Classes[i].m_ClassName << ".o ";
  80. if(i == Classes.size() -1)
  81. {
  82. fout << "\n\n";
  83. }
  84. else
  85. {
  86. fout << "\\\n";
  87. }
  88. }
  89. }
  90. fout << "\n";
  91. }
  92. // output the list of libraries that the executables
  93. // in this makefile will depend on.
  94. void cmUnixMakefileGenerator::OutputDependLibraries(std::ostream& fout)
  95. {
  96. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  97. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  98. std::vector<std::string>::iterator dir, lib;
  99. // Search the list of libraries that will be linked into
  100. // the executable
  101. for(lib = libs.begin(); lib != libs.end(); ++lib)
  102. {
  103. bool found = false;
  104. // loop over the list of directories that the libraries might
  105. // be in, looking for a LIBRARY=(lib) line.
  106. for(dir = libdirs.begin(); dir != libdirs.end() && !found; ++dir)
  107. {
  108. std::string expression = "LIBRARY.*=.*";
  109. expression += lib->c_str();
  110. if(cmSystemTools::Grep(dir->c_str(), "CMakeTargets.make",
  111. expression.c_str()))
  112. {
  113. std::string libpath = *dir;
  114. libpath += "/lib";
  115. libpath += *lib;
  116. libpath += "${CMAKE_LIB_EXT}";
  117. fout << libpath << " ";
  118. found = true;
  119. }
  120. }
  121. }
  122. fout << "\n";
  123. }
  124. // output make include flags
  125. void cmUnixMakefileGenerator::OutputMakeFlags(std::ostream& fout)
  126. {
  127. // Output Include paths
  128. fout << "INCLUDE_FLAGS = ";
  129. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  130. std::vector<std::string>::iterator i;
  131. for(i = includes.begin(); i != includes.end(); ++i)
  132. {
  133. std::string include = *i;
  134. fout << "-I" << i->c_str() << " ";
  135. }
  136. fout << m_Makefile->GetDefineFlags();
  137. fout << " ${LOCAL_INCLUDE_FLAGS} ";
  138. fout << "\n";
  139. fout << "default_target: all\n\n";
  140. // see if there are files to compile in this makefile
  141. // These are used for both libraries and executables
  142. }
  143. // output verbatim section
  144. void cmUnixMakefileGenerator::OutputVerbatim(std::ostream& fout)
  145. {
  146. std::vector<std::string>& MakeVerbatim = m_Makefile->GetMakeVerbatim();
  147. // Ouput user make text embeded in the input file
  148. for(unsigned int i =0; i < MakeVerbatim.size(); i++)
  149. {
  150. fout << MakeVerbatim[i] << "\n";
  151. }
  152. fout << "\n\n";
  153. }
  154. // output executables
  155. void cmUnixMakefileGenerator::OutputExecutableRules(std::ostream& fout)
  156. {
  157. if(!m_Makefile->HasExecutables())
  158. {
  159. return ;
  160. }
  161. // collect all the flags needed for linking libraries
  162. std::string linkLibs;
  163. std::vector<std::string>::iterator j;
  164. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  165. for(j = libdirs.begin(); j != libdirs.end(); ++j)
  166. {
  167. std::string::size_type pos = (*j).find("-L");
  168. if((pos == std::string::npos || pos > 0)
  169. && (*j).find("${") == std::string::npos)
  170. {
  171. linkLibs += "-L";
  172. }
  173. linkLibs += *j;
  174. linkLibs += " ";
  175. }
  176. std::string librariesLinked;
  177. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  178. for(j = libs.begin(); j != libs.end(); ++j)
  179. {
  180. std::string::size_type pos = (*j).find("-l");
  181. if((pos == std::string::npos || pos > 0)
  182. && (*j).find("${") == std::string::npos)
  183. {
  184. librariesLinked += "-l";
  185. }
  186. librariesLinked += *j;
  187. librariesLinked += " ";
  188. }
  189. // Add these in twice so order does not matter
  190. linkLibs += librariesLinked;
  191. linkLibs += librariesLinked;
  192. std::vector<std::string>& libsUnix = m_Makefile->GetLinkLibrariesUnix();
  193. for(j = libsUnix.begin(); j != libsUnix.end(); ++j)
  194. {
  195. linkLibs += *j;
  196. linkLibs += " ";
  197. }
  198. linkLibs += " ${LOCAL_LINK_FLAGS} ";
  199. // create and output a varible in the makefile that
  200. // each executable will depend on. This will have all the
  201. // libraries that the executable uses
  202. fout << "CMAKE_DEPEND_LIBS = ";
  203. this->OutputDependLibraries(fout);
  204. // Now create rules for all of the executables to be built
  205. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  206. for(unsigned int i = 0; i < Classes.size(); i++)
  207. {
  208. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  209. && Classes[i].m_IsExecutable)
  210. {
  211. std::string DotO = Classes[i].m_ClassName;
  212. DotO += ".o";
  213. fout << Classes[i].m_ClassName << ": " << DotO << " ";
  214. fout << "${CMAKE_DEPEND_LIBS}\n";
  215. fout << "\t${CXX} ${CXX_FLAGS} "
  216. << DotO.c_str() << " "
  217. << linkLibs.c_str()
  218. << " -o $@ ""\n\n";
  219. }
  220. }
  221. // ouput the list of executables
  222. fout << "EXECUTABLES = \\\n";
  223. for(unsigned int i = 0; i < Classes.size(); i++)
  224. {
  225. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  226. && Classes[i].m_IsExecutable)
  227. {
  228. fout << Classes[i].m_ClassName;
  229. if(i < Classes.size()-1)
  230. {
  231. fout << " \\";
  232. }
  233. fout << "\n";
  234. }
  235. }
  236. fout << "\n";
  237. }
  238. // fix up names of directories so they can be used
  239. // as targets in makefiles.
  240. inline std::string FixDirectoryName(const char* dir)
  241. {
  242. std::string s = dir;
  243. // replace ../ with 3 under bars
  244. size_t pos = s.find("../");
  245. if(pos != std::string::npos)
  246. {
  247. s.replace(pos, 3, "___");
  248. }
  249. // replace / directory separators with a single under bar
  250. pos = s.find("/");
  251. while(pos != std::string::npos)
  252. {
  253. s.replace(pos, 1, "_");
  254. pos = s.find("/");
  255. }
  256. return s;
  257. }
  258. // output rules for decending into sub directories
  259. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  260. {
  261. // Output Sub directory build rules
  262. const std::vector<std::string>& SubDirectories
  263. = m_Makefile->GetSubDirectories();
  264. if( SubDirectories.size() == 0)
  265. {
  266. return;
  267. }
  268. fout << "SUBDIR_BUILD = \\\n";
  269. unsigned int i;
  270. for(i =0; i < SubDirectories.size(); i++)
  271. {
  272. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  273. fout << "build_" << subdir.c_str();
  274. if(i == SubDirectories.size()-1)
  275. {
  276. fout << " \n\n";
  277. }
  278. else
  279. {
  280. fout << " \\\n";
  281. }
  282. }
  283. fout << std::endl;
  284. fout << "SUBDIR_CLEAN = \\\n";
  285. for(i =0; i < SubDirectories.size(); i++)
  286. {
  287. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  288. fout << "clean_" << subdir.c_str();
  289. if(i == SubDirectories.size()-1)
  290. {
  291. fout << " \n\n";
  292. }
  293. else
  294. {
  295. fout << " \\\n";
  296. }
  297. }
  298. fout << std::endl;
  299. fout << "alldirs : ${SUBDIR_BUILD}\n\n";
  300. for(i =0; i < SubDirectories.size(); i++)
  301. {
  302. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  303. fout << "build_" << subdir.c_str() << ":\n";
  304. fout << "\tcd " << SubDirectories[i].c_str()
  305. << "; ${MAKE} -${MAKEFLAGS} CMakeTargets.make\n";
  306. fout << "\tcd " << SubDirectories[i].c_str()
  307. << "; ${MAKE} -${MAKEFLAGS} all\n\n";
  308. fout << "clean_" << subdir.c_str() << ": \n";
  309. fout << "\tcd " << SubDirectories[i].c_str()
  310. << "; ${MAKE} -${MAKEFLAGS} clean\n\n";
  311. }
  312. }
  313. // Output the depend information for all the classes
  314. // in the makefile. These would have been generated
  315. // by the class cmMakeDepend GenerateMakefile
  316. void cmUnixMakefileGenerator::OutputDepends(std::ostream& fout)
  317. {
  318. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  319. for(unsigned int i = 0; i < Classes.size(); i++)
  320. {
  321. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
  322. {
  323. if( Classes[i].m_Depends.size())
  324. {
  325. fout << Classes[i].m_ClassName << ".o : \\\n";
  326. for(std::vector<std::string>::iterator j =
  327. Classes[i].m_Depends.begin();
  328. j != Classes[i].m_Depends.end(); ++j)
  329. {
  330. if(j+1 == Classes[i].m_Depends.end())
  331. {
  332. fout << *j << " \n";
  333. }
  334. else
  335. {
  336. fout << *j << " \\\n";
  337. }
  338. }
  339. fout << "\n\n";
  340. }
  341. }
  342. }
  343. }
  344. // Output each custom rule in the following format:
  345. // m_Result: m_Source m_Depends[0] m_Depends[1] ...
  346. // (tab) m_Command
  347. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  348. {
  349. for(std::vector<cmMakefile::customCommand>::const_iterator c =
  350. m_Makefile->GetCustomCommands().begin();
  351. c != m_Makefile->GetCustomCommands().end(); ++c)
  352. {
  353. fout << c->m_Result.c_str() << ": " << c->m_Source.c_str();
  354. for(std::vector<std::string>::const_iterator d = c->m_Depends.begin();
  355. d != c->m_Depends.end(); ++ d)
  356. {
  357. fout << " " << d->c_str();
  358. }
  359. fout << "\n\t" << c->m_Command.c_str() << "\n\n";
  360. }
  361. }