cmDSPMakefile.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "cmDSPMakefile.h"
  12. #include "cmStandardIncludes.h"
  13. #include "cmSystemTools.h"
  14. cmDSPMakefile::~cmDSPMakefile()
  15. {
  16. }
  17. cmDSPMakefile::cmDSPMakefile(cmMakefile*mf)
  18. {
  19. m_Makefile = mf;
  20. }
  21. void cmDSPMakefile::OutputDSPFile()
  22. {
  23. // If not an in source build, then create the output directory
  24. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  25. m_Makefile->GetHomeDirectory()) != 0)
  26. {
  27. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  28. {
  29. cmSystemTools::Error("Error creating directory ",
  30. m_Makefile->GetStartOutputDirectory());
  31. }
  32. }
  33. // Setup /I and /LIBPATH options for the resulting DSP file
  34. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  35. std::vector<std::string>::iterator i;
  36. for(i = includes.begin(); i != includes.end(); ++i)
  37. {
  38. m_IncludeOptions += "/I \"";
  39. m_IncludeOptions += *i;
  40. m_IncludeOptions += "\" ";
  41. }
  42. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  43. for(i = libs.begin(); i != libs.end(); ++i)
  44. {
  45. m_DebugLibraryOptions += " ";
  46. m_DebugLibraryOptions += *i;
  47. m_DebugLibraryOptions += ".lib ";
  48. }
  49. std::vector<std::string>& libswin32 = m_Makefile->GetLinkLibrariesWin32();
  50. for(i = libswin32.begin(); i != libswin32.end(); ++i)
  51. {
  52. m_DebugLibraryOptions += " ";
  53. m_DebugLibraryOptions += *i;
  54. m_DebugLibraryOptions += ".lib ";
  55. }
  56. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  57. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  58. {
  59. m_DebugLibraryOptions += " /LIBPATH:\"";
  60. m_DebugLibraryOptions += *i;
  61. if(i->find("Debug") == std::string::npos)
  62. {
  63. if(i->find("Release") == std::string::npos)
  64. {
  65. m_DebugLibraryOptions += "/Debug\" ";
  66. }
  67. }
  68. }
  69. m_DebugLibraryOptions += "/STACK:10000000 ";
  70. // add any extra define flags
  71. m_ReleaseLibraryOptions = m_DebugLibraryOptions;
  72. cmSystemTools::ReplaceString(m_ReleaseLibraryOptions, "Debug", "Release");
  73. // Create the DSP or set of DSP's for libraries and executables
  74. if(strlen(m_Makefile->GetLibraryName()) != 0)
  75. {
  76. this->SetBuildType(STATIC_LIBRARY);
  77. this->CreateSingleDSP();
  78. }
  79. // if there are executables build them
  80. if (m_Makefile->HasExecutables())
  81. {
  82. this->CreateExecutableDSPFiles();
  83. }
  84. }
  85. void cmDSPMakefile::CreateExecutableDSPFiles()
  86. {
  87. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  88. for(int i = 0; i < Classes.size(); ++i)
  89. {
  90. cmClassFile& classfile = Classes[i];
  91. if (classfile.m_IsExecutable)
  92. {
  93. std::string fname = m_Makefile->GetStartOutputDirectory();
  94. fname += "/";
  95. fname += classfile.m_ClassName;
  96. fname += ".dsp";
  97. std::ofstream fout(fname.c_str());
  98. if(!fout)
  99. {
  100. cmSystemTools::Error("Error Writing ",
  101. fname.c_str());
  102. }
  103. else
  104. {
  105. m_Makefile->SetLibraryName(classfile.m_ClassName.c_str());
  106. this->SetBuildType(EXECUTABLE);
  107. std::string pname = m_Makefile->GetLibraryName();
  108. m_CreatedProjectNames.push_back(pname);
  109. this->WriteDSPHeader(fout);
  110. this->WriteDSPBeginGroup(fout, "Source Files", "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  111. this->WriteDSPBuildRule(fout, classfile.m_FullPath.c_str());
  112. this->WriteDSPEndGroup(fout);
  113. this->WriteDSPBuildRule(fout);
  114. this->WriteDSPFooter(fout);
  115. }
  116. }
  117. }
  118. }
  119. void cmDSPMakefile::CreateSingleDSP()
  120. {
  121. std::string fname;
  122. fname = m_Makefile->GetStartOutputDirectory();
  123. fname += "/";
  124. fname += m_Makefile->GetLibraryName();
  125. fname += ".dsp";
  126. m_CreatedProjectNames.clear();
  127. std::string pname = m_Makefile->GetLibraryName();
  128. m_CreatedProjectNames.push_back(pname);
  129. std::ofstream fout(fname.c_str());
  130. if(!fout)
  131. {
  132. cmSystemTools::Error("Error Writing ",
  133. fname.c_str());
  134. }
  135. this->WriteDSPFile(fout);
  136. }
  137. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout)
  138. {
  139. std::string dspname = *(m_CreatedProjectNames.end()-1);
  140. dspname += ".dsp";
  141. std::string makefileIn = m_Makefile->GetStartDirectory();
  142. makefileIn += "/";
  143. makefileIn += "CMakeLists.txt";
  144. std::string dsprule = m_Makefile->GetHomeDirectory();
  145. dsprule += "/CMake/Source/CMakeSetupCMD ";
  146. dsprule += makefileIn;
  147. dsprule += " -DSP -H";
  148. dsprule += m_Makefile->GetHomeDirectory();
  149. dsprule += " -S";
  150. dsprule += m_Makefile->GetStartDirectory();
  151. dsprule += " -O";
  152. dsprule += m_Makefile->GetStartOutputDirectory();
  153. dsprule += " -B";
  154. dsprule += m_Makefile->GetHomeOutputDirectory();
  155. this->WriteCustomRule(fout, makefileIn.c_str(),
  156. dspname.c_str(),
  157. dsprule.c_str());
  158. }
  159. void cmDSPMakefile::WriteDSPFile(std::ostream& fout)
  160. {
  161. this->WriteDSPHeader(fout);
  162. this->WriteDSPBeginGroup(fout, "Source Files", "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  163. this->WriteDSPBuildRules(fout);
  164. this->WriteDSPEndGroup(fout);
  165. this->WriteDSPBuildRule(fout);
  166. this->WriteDSPFooter(fout);
  167. }
  168. void cmDSPMakefile::WriteCustomRule(std::ostream& fout,
  169. const char* source,
  170. const char* result,
  171. const char* command)
  172. {
  173. fout << "# Begin Source File\n\n";
  174. fout << "SOURCE=" << source << "\n\n";
  175. fout << "# Begin Custom Build\n\n";
  176. fout << '\"' << result << "\" : $(SOURCE) \"$(INTDIR)\" \"$(OUTDIR)\"\n";
  177. fout << " " << command << "\n\n";
  178. fout << "# End Custom Build\n\n";
  179. fout << "# End Source File\n";
  180. }
  181. void cmDSPMakefile::WriteDSPBeginGroup(std::ostream& fout,
  182. const char* group,
  183. const char* filter)
  184. {
  185. fout << "# Begin Group \"" << group << "\"\n"
  186. "# PROP Default_Filter \"" << filter << "\"\n";
  187. }
  188. void cmDSPMakefile::WriteDSPEndGroup(std::ostream& fout)
  189. {
  190. fout << "# End Group\n";
  191. }
  192. void cmDSPMakefile::SetBuildType(BuildType b)
  193. {
  194. switch(b)
  195. {
  196. case STATIC_LIBRARY:
  197. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  198. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  199. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  200. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  201. break;
  202. case DLL:
  203. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  204. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  205. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  206. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  207. break;
  208. case EXECUTABLE:
  209. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  210. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  211. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  212. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  213. break;
  214. }
  215. }
  216. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout)
  217. {
  218. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  219. if(!fin)
  220. {
  221. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  222. }
  223. char buffer[2048];
  224. while(fin)
  225. {
  226. fin.getline(buffer, 2048);
  227. std::string line = buffer;
  228. cmSystemTools::ReplaceString(line, "CM_RELEASE_LIBRARIES",
  229. m_ReleaseLibraryOptions.c_str());
  230. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  231. m_DebugLibraryOptions.c_str());
  232. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  233. m_IncludeOptions.c_str());
  234. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  235. m_Makefile->GetLibraryName());
  236. cmSystemTools::ReplaceString(line,
  237. "EXTRA_DEFINES",
  238. m_Makefile->GetDefineFlags());
  239. fout << line.c_str() << std::endl;
  240. }
  241. }
  242. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  243. {
  244. std::ifstream fin(m_DSPFooterTemplate.c_str());
  245. if(!fin)
  246. {
  247. cmSystemTools::Error("Error Reading ",
  248. m_DSPFooterTemplate.c_str());
  249. }
  250. char buffer[2048];
  251. while(fin)
  252. {
  253. fin.getline(buffer, 2048);
  254. fout << buffer << std::endl;
  255. }
  256. }
  257. void cmDSPMakefile::WriteDSPBuildRules(std::ostream& fout)
  258. {
  259. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  260. for(int i = 0; i < Classes.size(); ++i)
  261. {
  262. if(!Classes[i].m_IsExecutable && !Classes[i].m_AbstractClass &&
  263. !Classes[i].m_HeaderFileOnly)
  264. {
  265. this->WriteDSPBuildRule(fout, Classes[i].m_FullPath.c_str());
  266. }
  267. }
  268. }
  269. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout, const char* path)
  270. {
  271. fout << "# Begin Source File\n\n";
  272. fout << "SOURCE="
  273. << path << "\n";
  274. fout << "# End Source File\n";
  275. }