cmUnixMakefileGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 << " ${LOCAL_INCLUDE_FLAGS} ";
  137. fout << "\n";
  138. fout << "default_target: all\n\n";
  139. // see if there are files to compile in this makefile
  140. // These are used for both libraries and executables
  141. }
  142. // output verbatim section
  143. void cmUnixMakefileGenerator::OutputVerbatim(std::ostream& fout)
  144. {
  145. std::vector<std::string>& MakeVerbatim = m_Makefile->GetMakeVerbatim();
  146. // Ouput user make text embeded in the input file
  147. for(unsigned int i =0; i < MakeVerbatim.size(); i++)
  148. {
  149. fout << MakeVerbatim[i] << "\n";
  150. }
  151. fout << "\n\n";
  152. }
  153. // output executables
  154. void cmUnixMakefileGenerator::OutputExecutableRules(std::ostream& fout)
  155. {
  156. if(!m_Makefile->HasExecutables())
  157. {
  158. return ;
  159. }
  160. // collect all the flags needed for linking libraries
  161. std::string linkLibs;
  162. std::vector<std::string>::iterator j;
  163. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  164. for(j = libdirs.begin(); j != libdirs.end(); ++j)
  165. {
  166. std::string::size_type pos = (*j).find("-L");
  167. if((pos == std::string::npos || pos > 0)
  168. && (*j).find("${") == std::string::npos)
  169. {
  170. linkLibs += "-L";
  171. }
  172. linkLibs += *j;
  173. linkLibs += " ";
  174. }
  175. std::string librariesLinked;
  176. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  177. for(j = libs.begin(); j != libs.end(); ++j)
  178. {
  179. std::string::size_type pos = (*j).find("-l");
  180. if((pos == std::string::npos || pos > 0)
  181. && (*j).find("${") == std::string::npos)
  182. {
  183. librariesLinked += "-l";
  184. }
  185. librariesLinked += *j;
  186. librariesLinked += " ";
  187. }
  188. // Add these in twice so order does not matter
  189. linkLibs += librariesLinked;
  190. linkLibs += librariesLinked;
  191. std::vector<std::string>& libsUnix = m_Makefile->GetLinkLibrariesUnix();
  192. for(j = libsUnix.begin(); j != libsUnix.end(); ++j)
  193. {
  194. linkLibs += *j;
  195. linkLibs += " ";
  196. }
  197. linkLibs += " ${LOCAL_LINK_FLAGS} ";
  198. // create and output a varible in the makefile that
  199. // each executable will depend on. This will have all the
  200. // libraries that the executable uses
  201. fout << "CMAKE_DEPEND_LIBS = ";
  202. this->OutputDependLibraries(fout);
  203. // Now create rules for all of the executables to be built
  204. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  205. for(unsigned int i = 0; i < Classes.size(); i++)
  206. {
  207. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  208. && Classes[i].m_IsExecutable)
  209. {
  210. std::string DotO = Classes[i].m_ClassName;
  211. DotO += ".o";
  212. fout << Classes[i].m_ClassName << ": " << DotO << " ";
  213. fout << "${CMAKE_DEPEND_LIBS}\n";
  214. fout << "\t${CXX} ${CXX_FLAGS} " << m_Makefile->GetDefineFlags()
  215. << DotO.c_str() << " "
  216. << linkLibs.c_str()
  217. << " -o $@ ""\n\n";
  218. }
  219. }
  220. // ouput the list of executables
  221. fout << "EXECUTABLES = \\\n";
  222. for(unsigned int i = 0; i < Classes.size(); i++)
  223. {
  224. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly
  225. && Classes[i].m_IsExecutable)
  226. {
  227. fout << Classes[i].m_ClassName;
  228. if(i < Classes.size()-1)
  229. {
  230. fout << " \\";
  231. }
  232. fout << "\n";
  233. }
  234. }
  235. fout << "\n";
  236. }
  237. // fix up names of directories so they can be used
  238. // as targets in makefiles.
  239. inline std::string FixDirectoryName(const char* dir)
  240. {
  241. std::string s = dir;
  242. // replace ../ with 3 under bars
  243. size_t pos = s.find("../");
  244. if(pos != std::string::npos)
  245. {
  246. s.replace(pos, 3, "___");
  247. }
  248. // replace / directory separators with a single under bar
  249. pos = s.find("/");
  250. while(pos != std::string::npos)
  251. {
  252. s.replace(pos, 1, "_");
  253. pos = s.find("/");
  254. }
  255. return s;
  256. }
  257. // output rules for decending into sub directories
  258. void cmUnixMakefileGenerator::OutputSubDirectoryRules(std::ostream& fout)
  259. {
  260. // Output Sub directory build rules
  261. const std::vector<std::string>& SubDirectories
  262. = m_Makefile->GetSubDirectories();
  263. if( SubDirectories.size() == 0)
  264. {
  265. return;
  266. }
  267. fout << "SUBDIR_BUILD = \\\n";
  268. unsigned int i;
  269. for(i =0; i < SubDirectories.size(); i++)
  270. {
  271. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  272. fout << "build_" << subdir.c_str();
  273. if(i == SubDirectories.size()-1)
  274. {
  275. fout << " \n\n";
  276. }
  277. else
  278. {
  279. fout << " \\\n";
  280. }
  281. }
  282. fout << std::endl;
  283. fout << "SUBDIR_CLEAN = \\\n";
  284. for(i =0; i < SubDirectories.size(); i++)
  285. {
  286. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  287. fout << "clean_" << subdir.c_str();
  288. if(i == SubDirectories.size()-1)
  289. {
  290. fout << " \n\n";
  291. }
  292. else
  293. {
  294. fout << " \\\n";
  295. }
  296. }
  297. fout << std::endl;
  298. fout << "alldirs : ${SUBDIR_BUILD}\n\n";
  299. for(i =0; i < SubDirectories.size(); i++)
  300. {
  301. std::string subdir = FixDirectoryName(SubDirectories[i].c_str());
  302. fout << "build_" << subdir.c_str() << ":\n";
  303. fout << "\tcd " << SubDirectories[i].c_str()
  304. << "; ${MAKE} -${MAKEFLAGS} CMakeTargets.make\n";
  305. fout << "\tcd " << SubDirectories[i].c_str()
  306. << "; ${MAKE} -${MAKEFLAGS} all\n\n";
  307. fout << "clean_" << subdir.c_str() << ": \n";
  308. fout << "\tcd " << SubDirectories[i].c_str()
  309. << "; ${MAKE} -${MAKEFLAGS} clean\n\n";
  310. }
  311. }
  312. // Output the depend information for all the classes
  313. // in the makefile. These would have been generated
  314. // by the class cmMakeDepend GenerateMakefile
  315. void cmUnixMakefileGenerator::OutputDepends(std::ostream& fout)
  316. {
  317. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  318. for(unsigned int i = 0; i < Classes.size(); i++)
  319. {
  320. if(!Classes[i].m_AbstractClass && !Classes[i].m_HeaderFileOnly)
  321. {
  322. if( Classes[i].m_Depends.size())
  323. {
  324. fout << Classes[i].m_ClassName << ".o : \\\n";
  325. for(std::vector<std::string>::iterator j =
  326. Classes[i].m_Depends.begin();
  327. j != Classes[i].m_Depends.end(); ++j)
  328. {
  329. if(j+1 == Classes[i].m_Depends.end())
  330. {
  331. fout << *j << " \n";
  332. }
  333. else
  334. {
  335. fout << *j << " \\\n";
  336. }
  337. }
  338. fout << "\n\n";
  339. }
  340. }
  341. }
  342. }
  343. // Output each custom rule in the following format:
  344. // m_Result: m_Source m_Depends[0] m_Depends[1] ...
  345. // (tab) m_Command
  346. void cmUnixMakefileGenerator::OutputCustomRules(std::ostream& fout)
  347. {
  348. for(std::vector<cmMakefile::customCommand>::const_iterator c =
  349. m_Makefile->GetCustomCommands().begin();
  350. c != m_Makefile->GetCustomCommands().end(); ++c)
  351. {
  352. fout << c->m_Result.c_str() << ": " << c->m_Source.c_str();
  353. for(std::vector<std::string>::const_iterator d = c->m_Depends.begin();
  354. d != c->m_Depends.end(); ++ d)
  355. {
  356. fout << " " << d->c_str();
  357. }
  358. fout << "\n\t" << c->m_Command.c_str() << "\n\n";
  359. }
  360. }