cmDSPWriter.cxx 12 KB

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