cmDSPWriter.cxx 13 KB

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