cmDSPWriter.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include "cmRegularExpression.h"
  15. #include "cmCacheManager.h"
  16. cmDSPMakefile::~cmDSPMakefile()
  17. {
  18. }
  19. cmDSPMakefile::cmDSPMakefile(cmMakefile*mf)
  20. {
  21. m_Makefile = mf;
  22. }
  23. void cmDSPMakefile::OutputDSPFile()
  24. {
  25. // If not an in source build, then create the output directory
  26. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  27. m_Makefile->GetHomeDirectory()) != 0)
  28. {
  29. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  30. {
  31. cmSystemTools::Error("Error creating directory ",
  32. m_Makefile->GetStartOutputDirectory());
  33. }
  34. }
  35. // Setup /I and /LIBPATH options for the resulting DSP file
  36. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  37. std::vector<std::string>::iterator i;
  38. for(i = includes.begin(); i != includes.end(); ++i)
  39. {
  40. m_IncludeOptions += "/I \"";
  41. m_IncludeOptions += *i;
  42. m_IncludeOptions += "\" ";
  43. }
  44. std::vector<std::string>& libs = m_Makefile->GetLinkLibraries();
  45. for(i = libs.begin(); i != libs.end(); ++i)
  46. {
  47. m_DebugLibraryOptions += " ";
  48. m_DebugLibraryOptions += *i;
  49. m_DebugLibraryOptions += ".lib ";
  50. }
  51. std::vector<std::string>& libswin32 = m_Makefile->GetLinkLibrariesWin32();
  52. for(i = libswin32.begin(); i != libswin32.end(); ++i)
  53. {
  54. m_DebugLibraryOptions += " ";
  55. m_DebugLibraryOptions += *i;
  56. m_DebugLibraryOptions += ".lib ";
  57. }
  58. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  59. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  60. {
  61. m_DebugLibraryOptions += " /LIBPATH:\"";
  62. m_DebugLibraryOptions += *i;
  63. if(i->find("Debug") == std::string::npos)
  64. {
  65. if(i->find("Release") == std::string::npos)
  66. {
  67. m_DebugLibraryOptions += "/$(OUTDIR)\" ";
  68. }
  69. }
  70. }
  71. m_DebugLibraryOptions += "/STACK:10000000 ";
  72. // add any extra define flags
  73. m_ReleaseLibraryOptions = m_DebugLibraryOptions;
  74. m_DebugDLLLibraryOptions = m_DebugLibraryOptions;
  75. m_ReleaseDLLLibraryOptions = m_DebugDLLLibraryOptions;
  76. m_ReleaseMinSizeLibraryOptions = m_ReleaseLibraryOptions;
  77. // Create the DSP or set of DSP's for libraries and executables
  78. if(strlen(m_Makefile->GetLibraryName()) != 0)
  79. {
  80. const char* cacheValue
  81. = cmCacheManager::GetInstance()->GetCacheValue("BUILD_SHARED_LIBS");
  82. if(cacheValue && strcmp(cacheValue,"0"))
  83. {
  84. this->SetBuildType(DLL);
  85. }
  86. else
  87. {
  88. this->SetBuildType(STATIC_LIBRARY);
  89. }
  90. this->CreateSingleDSP();
  91. }
  92. // if there are executables build them
  93. if (m_Makefile->HasExecutables())
  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. if (classfile.m_IsExecutable)
  105. {
  106. std::string fname = m_Makefile->GetStartOutputDirectory();
  107. fname += "/";
  108. fname += classfile.m_ClassName;
  109. fname += ".dsp";
  110. std::ofstream fout(fname.c_str());
  111. if(!fout)
  112. {
  113. cmSystemTools::Error("Error Writing ",
  114. fname.c_str());
  115. }
  116. else
  117. {
  118. m_Makefile->SetLibraryName(classfile.m_ClassName.c_str());
  119. this->SetBuildType(EXECUTABLE);
  120. std::string pname = m_Makefile->GetLibraryName();
  121. m_CreatedProjectNames.push_back(pname);
  122. this->WriteDSPHeader(fout);
  123. this->WriteDSPBeginGroup(fout, "Source Files", "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  124. this->WriteDSPBuildRule(fout, classfile.m_FullPath.c_str());
  125. this->WriteDSPEndGroup(fout);
  126. this->WriteDSPBuildRule(fout);
  127. this->WriteDSPFooter(fout);
  128. }
  129. }
  130. }
  131. }
  132. void cmDSPMakefile::CreateSingleDSP()
  133. {
  134. std::string fname;
  135. fname = m_Makefile->GetStartOutputDirectory();
  136. fname += "/";
  137. fname += m_Makefile->GetLibraryName();
  138. fname += ".dsp";
  139. m_CreatedProjectNames.clear();
  140. std::string pname = m_Makefile->GetLibraryName();
  141. m_CreatedProjectNames.push_back(pname);
  142. std::ofstream fout(fname.c_str());
  143. if(!fout)
  144. {
  145. cmSystemTools::Error("Error Writing ",
  146. fname.c_str());
  147. }
  148. this->WriteDSPFile(fout);
  149. }
  150. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout)
  151. {
  152. std::string dspname = *(m_CreatedProjectNames.end()-1);
  153. dspname += ".dsp";
  154. std::string makefileIn = m_Makefile->GetStartDirectory();
  155. makefileIn += "/";
  156. makefileIn += "CMakeLists.txt";
  157. std::string dsprule = m_Makefile->GetHomeDirectory();
  158. dsprule += "/CMake/Source/CMakeSetupCMD ";
  159. dsprule += makefileIn;
  160. dsprule += " -DSP -H";
  161. dsprule += m_Makefile->GetHomeDirectory();
  162. dsprule += " -S";
  163. dsprule += m_Makefile->GetStartDirectory();
  164. dsprule += " -O";
  165. dsprule += m_Makefile->GetStartOutputDirectory();
  166. dsprule += " -B";
  167. dsprule += m_Makefile->GetHomeOutputDirectory();
  168. std::vector<std::string> depends;
  169. this->WriteCustomRule(fout, makefileIn.c_str(),
  170. dspname.c_str(),
  171. dsprule.c_str(),
  172. depends);
  173. }
  174. void cmDSPMakefile::WriteDSPFile(std::ostream& fout)
  175. {
  176. this->WriteDSPHeader(fout);
  177. this->WriteDSPBeginGroup(fout, "Source Files", "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  178. this->WriteDSPBuildRules(fout,"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  179. this->WriteDSPEndGroup(fout);
  180. this->WriteDSPBeginGroup(fout, "Header Files", "h;hpp;hxx;hm;inl");
  181. this->WriteDSPBuildRules(fout,"h;hpp;hxx;hm;inl");
  182. this->WriteDSPEndGroup(fout);
  183. this->WriteDSPBeginGroup(fout, "XML Files", "xml");
  184. this->WriteDSPBuildRules(fout,"xml");
  185. this->WriteDSPEndGroup(fout);
  186. this->WriteDSPBuildRule(fout);
  187. this->WriteDSPFooter(fout);
  188. }
  189. void cmDSPMakefile::WriteCustomRule(std::ostream& fout,
  190. const char* source,
  191. const char* result,
  192. const char* command,
  193. std::vector<std::string>& depends)
  194. {
  195. fout << "# Begin Source File\n\n";
  196. fout << "SOURCE=" << source << "\n\n";
  197. std::vector<std::string>::iterator i;
  198. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  199. {
  200. if (i == m_Configurations.begin())
  201. {
  202. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  203. }
  204. else
  205. {
  206. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  207. }
  208. fout << "# Begin Custom Build\n\n";
  209. fout << '\"' << result << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\" ";
  210. for (int i = 0; i < depends.size(); i++)
  211. {
  212. fout << "\"" << depends[i].c_str() << "\" ";
  213. }
  214. fout << "\n " << command << "\n\n";
  215. fout << "# End Custom Build\n\n";
  216. }
  217. fout << "!ENDIF\n\n";
  218. fout << "# End Source File\n";
  219. }
  220. void cmDSPMakefile::WriteDSPBeginGroup(std::ostream& fout,
  221. const char* group,
  222. const char* filter)
  223. {
  224. fout << "# Begin Group \"" << group << "\"\n"
  225. "# PROP Default_Filter \"" << filter << "\"\n";
  226. }
  227. void cmDSPMakefile::WriteDSPEndGroup(std::ostream& fout)
  228. {
  229. fout << "# End Group\n";
  230. }
  231. void cmDSPMakefile::SetBuildType(BuildType b)
  232. {
  233. m_BuildType = b;
  234. switch(b)
  235. {
  236. case STATIC_LIBRARY:
  237. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  238. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  239. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  240. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  241. break;
  242. case DLL:
  243. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  244. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  245. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  246. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  247. break;
  248. case EXECUTABLE:
  249. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  250. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  251. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  252. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  253. break;
  254. }
  255. // once the build type is set, determine what configurations are
  256. // possible
  257. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  258. cmRegularExpression reg("# Name ");
  259. if(!fin)
  260. {
  261. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  262. }
  263. char buffer[2048];
  264. while(fin)
  265. {
  266. fin.getline(buffer, 2048);
  267. std::string line = buffer;
  268. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  269. m_Makefile->GetLibraryName());
  270. if (reg.find(line))
  271. {
  272. m_Configurations.push_back(line.substr(reg.end()));
  273. }
  274. }
  275. }
  276. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout)
  277. {
  278. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  279. if(!fin)
  280. {
  281. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  282. }
  283. char buffer[2048];
  284. while(fin)
  285. {
  286. fin.getline(buffer, 2048);
  287. std::string line = buffer;
  288. cmSystemTools::ReplaceString(line, "CM_RELEASE_LIBRARIES",
  289. m_ReleaseLibraryOptions.c_str());
  290. cmSystemTools::ReplaceString(line, "CM_RELEASEMINSIZE_LIBRARIES",
  291. m_ReleaseMinSizeLibraryOptions.c_str());
  292. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  293. m_DebugLibraryOptions.c_str());
  294. cmSystemTools::ReplaceString(line, "CM_RELEASEDLL_LIBRARIES",
  295. m_ReleaseDLLLibraryOptions.c_str());
  296. cmSystemTools::ReplaceString(line, "CM_DEBUGDLL_LIBRARIES",
  297. m_DebugDLLLibraryOptions.c_str());
  298. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  299. m_IncludeOptions.c_str());
  300. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  301. m_Makefile->GetLibraryName());
  302. cmSystemTools::ReplaceString(line,
  303. "EXTRA_DEFINES",
  304. m_Makefile->GetDefineFlags());
  305. fout << line.c_str() << std::endl;
  306. }
  307. }
  308. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  309. {
  310. std::ifstream fin(m_DSPFooterTemplate.c_str());
  311. if(!fin)
  312. {
  313. cmSystemTools::Error("Error Reading ",
  314. m_DSPFooterTemplate.c_str());
  315. }
  316. char buffer[2048];
  317. while(fin)
  318. {
  319. fin.getline(buffer, 2048);
  320. fout << buffer << std::endl;
  321. }
  322. }
  323. void cmDSPMakefile::WriteDSPBuildRules(std::ostream& fout, const char *ext)
  324. {
  325. // make a list if matching extentions
  326. std::vector<std::string> exts;
  327. std::string inExts = ext;
  328. std::string::size_type pos = inExts.find(';');
  329. std::string::size_type lastPos = 0;
  330. while (pos != std::string::npos)
  331. {
  332. std::string anExt = inExts.substr(lastPos, pos - lastPos);
  333. exts.push_back(anExt);
  334. lastPos = pos + 1;
  335. pos = inExts.find(';',lastPos);
  336. }
  337. exts.push_back(inExts.substr(lastPos,inExts.size() - lastPos));
  338. // loop over any classes
  339. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  340. for(int i = 0; i < Classes.size(); ++i)
  341. {
  342. if(!Classes[i].m_IsExecutable && !Classes[i].m_HeaderFileOnly)
  343. {
  344. // is this class of the appropriate type ?
  345. if (std::find(exts.begin(),exts.end(),Classes[i].m_ClassExtension)
  346. != exts.end())
  347. {
  348. this->WriteDSPBuildRule(fout, Classes[i].m_FullPath.c_str());
  349. }
  350. }
  351. }
  352. // loop over any custom commands
  353. std::vector<cmMakefile::customCommand>& ccoms =
  354. this->GetMakefile()->GetCustomCommands();
  355. int numCust = ccoms.size();
  356. for (int j = 0; j < numCust; j++)
  357. {
  358. cmMakefile::customCommand &cc = ccoms[j];
  359. // is the source of the command the correct type
  360. pos = cc.m_Source.rfind('.');
  361. if(pos != std::string::npos)
  362. {
  363. if (std::find(exts.begin(),exts.end(),
  364. cc.m_Source.substr(pos+1,cc.m_Source.size() - pos - 1))
  365. != exts.end())
  366. {
  367. this->WriteCustomRule(fout, cc.m_Source.c_str(),
  368. cc.m_Result.c_str(),
  369. cc.m_Command.c_str(),
  370. cc.m_Depends);
  371. }
  372. }
  373. }
  374. }
  375. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout, const char* path)
  376. {
  377. fout << "# Begin Source File\n\n";
  378. fout << "SOURCE="
  379. << path << "\n";
  380. fout << "# End Source File\n";
  381. }