cmDSPWriter.cxx 8.0 KB

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