cmDSPWriter.cxx 9.0 KB

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