cmDSPWriter.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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->WriteDSPBuildRule(fout);
  189. this->WriteDSPFooter(fout);
  190. }
  191. void cmDSPMakefile::WriteCustomRule(std::ostream& fout,
  192. const char* source,
  193. const char* result,
  194. const char* command,
  195. std::vector<std::string>& depends)
  196. {
  197. fout << "# Begin Source File\n\n";
  198. fout << "SOURCE=" << source << "\n\n";
  199. std::vector<std::string>::iterator i;
  200. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  201. {
  202. if (i == m_Configurations.begin())
  203. {
  204. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  205. }
  206. else
  207. {
  208. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  209. }
  210. fout << "# Begin Custom Build\n\n";
  211. fout << '\"' << result << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\" ";
  212. for (int i = 0; i < depends.size(); i++)
  213. {
  214. fout << "\"" << depends[i].c_str() << "\" ";
  215. }
  216. fout << "\n " << command << "\n\n";
  217. fout << "# End Custom Build\n\n";
  218. }
  219. fout << "!ENDIF\n\n";
  220. fout << "# End Source File\n";
  221. }
  222. void cmDSPMakefile::WriteDSPBeginGroup(std::ostream& fout,
  223. const char* group,
  224. const char* filter)
  225. {
  226. fout << "# Begin Group \"" << group << "\"\n"
  227. "# PROP Default_Filter \"" << filter << "\"\n";
  228. }
  229. void cmDSPMakefile::WriteDSPEndGroup(std::ostream& fout)
  230. {
  231. fout << "# End Group\n";
  232. }
  233. void cmDSPMakefile::SetBuildType(BuildType b)
  234. {
  235. switch(b)
  236. {
  237. case STATIC_LIBRARY:
  238. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  239. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  240. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  241. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  242. break;
  243. case DLL:
  244. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  245. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  246. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  247. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  248. break;
  249. case EXECUTABLE:
  250. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  251. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  252. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  253. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  254. break;
  255. }
  256. // once the build type is set, determine what configurations are
  257. // possible
  258. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  259. cmRegularExpression reg("# Name ");
  260. if(!fin)
  261. {
  262. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  263. }
  264. char buffer[2048];
  265. while(fin)
  266. {
  267. fin.getline(buffer, 2048);
  268. std::string line = buffer;
  269. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  270. m_Makefile->GetLibraryName());
  271. if (reg.find(line))
  272. {
  273. m_Configurations.push_back(line.substr(reg.end()));
  274. }
  275. }
  276. }
  277. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout)
  278. {
  279. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  280. if(!fin)
  281. {
  282. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  283. }
  284. char buffer[2048];
  285. while(fin)
  286. {
  287. fin.getline(buffer, 2048);
  288. std::string line = buffer;
  289. cmSystemTools::ReplaceString(line, "CM_RELEASE_LIBRARIES",
  290. m_ReleaseLibraryOptions.c_str());
  291. cmSystemTools::ReplaceString(line, "CM_RELEASEMINSIZE_LIBRARIES",
  292. m_ReleaseMinSizeLibraryOptions.c_str());
  293. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  294. m_DebugLibraryOptions.c_str());
  295. cmSystemTools::ReplaceString(line, "CM_RELEASEDLL_LIBRARIES",
  296. m_ReleaseDLLLibraryOptions.c_str());
  297. cmSystemTools::ReplaceString(line, "CM_DEBUGDLL_LIBRARIES",
  298. m_DebugDLLLibraryOptions.c_str());
  299. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  300. m_IncludeOptions.c_str());
  301. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  302. m_Makefile->GetLibraryName());
  303. cmSystemTools::ReplaceString(line,
  304. "EXTRA_DEFINES",
  305. m_Makefile->GetDefineFlags());
  306. fout << line.c_str() << std::endl;
  307. }
  308. }
  309. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  310. {
  311. std::ifstream fin(m_DSPFooterTemplate.c_str());
  312. if(!fin)
  313. {
  314. cmSystemTools::Error("Error Reading ",
  315. m_DSPFooterTemplate.c_str());
  316. }
  317. char buffer[2048];
  318. while(fin)
  319. {
  320. fin.getline(buffer, 2048);
  321. fout << buffer << std::endl;
  322. }
  323. }
  324. void cmDSPMakefile::WriteDSPBuildRules(std::ostream& fout, const char *ext)
  325. {
  326. // make a list if matching extentions
  327. std::vector<std::string> exts;
  328. std::string inExts = ext;
  329. std::string::size_type pos = inExts.find(';');
  330. std::string::size_type lastPos = 0;
  331. while (pos != std::string::npos)
  332. {
  333. std::string anExt = inExts.substr(lastPos, pos - lastPos);
  334. exts.push_back(anExt);
  335. lastPos = pos + 1;
  336. pos = inExts.find(';',lastPos);
  337. }
  338. exts.push_back(inExts.substr(lastPos,inExts.size() - lastPos));
  339. // loop over any classes
  340. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  341. for(int i = 0; i < Classes.size(); ++i)
  342. {
  343. if(!Classes[i].m_IsExecutable && !Classes[i].m_HeaderFileOnly)
  344. {
  345. // is this class of the appropriate type ?
  346. if (std::find(exts.begin(),exts.end(),Classes[i].m_ClassExtension)
  347. != exts.end())
  348. {
  349. this->WriteDSPBuildRule(fout, Classes[i].m_FullPath.c_str());
  350. }
  351. }
  352. }
  353. // loop over any custom commands
  354. std::vector<cmMakefile::customCommand>& ccoms =
  355. this->GetMakefile()->GetCustomCommands();
  356. int numCust = ccoms.size();
  357. for (int j = 0; j < numCust; j++)
  358. {
  359. cmMakefile::customCommand &cc = ccoms[j];
  360. // is the source of the command the correct type
  361. pos = cc.m_Source.rfind('.');
  362. if(pos != std::string::npos)
  363. {
  364. if (std::find(exts.begin(),exts.end(),
  365. cc.m_Source.substr(pos+1,cc.m_Source.size() - pos - 1))
  366. != exts.end())
  367. {
  368. this->WriteCustomRule(fout, cc.m_Source.c_str(),
  369. cc.m_Result.c_str(),
  370. cc.m_Command.c_str(),
  371. cc.m_Depends);
  372. }
  373. }
  374. }
  375. }
  376. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout, const char* path)
  377. {
  378. fout << "# Begin Source File\n\n";
  379. fout << "SOURCE="
  380. << path << "\n";
  381. fout << "# End Source File\n";
  382. }