cmDSPWriter.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. m_OutputLibName = m_Makefile->GetLibraryName();
  67. // Create the DSP or set of DSP's for libraries and executables
  68. if(strlen(m_Makefile->GetLibraryName()) != 0)
  69. {
  70. const char* cacheValue
  71. = cmCacheManager::GetInstance()->GetCacheValue("BUILD_SHARED_LIBS");
  72. if(cacheValue && strcmp(cacheValue,"0"))
  73. {
  74. this->SetBuildType(DLL);
  75. }
  76. else
  77. {
  78. this->SetBuildType(STATIC_LIBRARY);
  79. }
  80. this->CreateSingleDSP();
  81. }
  82. // if there are executables build them
  83. if (m_Makefile->HasExecutables())
  84. {
  85. this->CreateExecutableDSPFiles();
  86. }
  87. }
  88. void cmDSPMakefile::CreateExecutableDSPFiles()
  89. {
  90. std::vector<cmClassFile>& Classes = m_Makefile->GetClasses();
  91. for(int i = 0; i < Classes.size(); ++i)
  92. {
  93. cmClassFile& classfile = Classes[i];
  94. if (classfile.m_IsExecutable)
  95. {
  96. std::string fname = m_Makefile->GetStartOutputDirectory();
  97. fname += "/";
  98. fname += classfile.m_ClassName;
  99. fname += ".dsp";
  100. std::ofstream fout(fname.c_str());
  101. if(!fout)
  102. {
  103. cmSystemTools::Error("Error Writing ",
  104. fname.c_str());
  105. }
  106. else
  107. {
  108. // m_Makefile->SetLibraryName(classfile.m_ClassName.c_str());
  109. this->SetBuildType(EXECUTABLE);
  110. m_OutputLibName = classfile.m_ClassName;
  111. std::string pname = classfile.m_ClassName.c_str(); //m_Makefile->GetLibraryName();
  112. m_CreatedProjectNames.push_back(pname);
  113. this->WriteDSPHeader(fout);
  114. this->WriteDSPBeginGroup(fout, "Source Files", "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat");
  115. this->WriteDSPBuildRule(fout, classfile.m_FullPath.c_str());
  116. this->WriteDSPEndGroup(fout);
  117. this->WriteDSPBuildRule(fout);
  118. this->WriteDSPFooter(fout);
  119. }
  120. }
  121. }
  122. }
  123. void cmDSPMakefile::CreateSingleDSP()
  124. {
  125. std::string fname;
  126. fname = m_Makefile->GetStartOutputDirectory();
  127. fname += "/";
  128. fname += m_Makefile->GetLibraryName();
  129. fname += ".dsp";
  130. m_CreatedProjectNames.clear();
  131. std::string pname = m_Makefile->GetLibraryName();
  132. m_CreatedProjectNames.push_back(pname);
  133. std::ofstream fout(fname.c_str());
  134. if(!fout)
  135. {
  136. cmSystemTools::Error("Error Writing ",
  137. fname.c_str());
  138. }
  139. this->WriteDSPFile(fout);
  140. }
  141. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout)
  142. {
  143. std::string dspname = *(m_CreatedProjectNames.end()-1);
  144. dspname += ".dsp";
  145. std::string makefileIn = m_Makefile->GetStartDirectory();
  146. makefileIn += "/";
  147. makefileIn += "CMakeLists.txt";
  148. std::string dsprule = m_Makefile->GetHomeDirectory();
  149. dsprule += "/CMake/Source/CMakeSetupCMD ";
  150. dsprule += makefileIn;
  151. dsprule += " -DSP -H";
  152. dsprule += m_Makefile->GetHomeDirectory();
  153. dsprule += " -S";
  154. dsprule += m_Makefile->GetStartDirectory();
  155. dsprule += " -O";
  156. dsprule += m_Makefile->GetStartOutputDirectory();
  157. dsprule += " -B";
  158. dsprule += m_Makefile->GetHomeOutputDirectory();
  159. std::set<std::string> depends;
  160. std::set<std::string> outputs;
  161. outputs.insert(outputs.begin(), dspname);
  162. fout << "# Begin Source File\n\n";
  163. fout << "SOURCE=" << makefileIn.c_str() << "\n\n";
  164. this->WriteCustomRule(fout, dsprule.c_str(), depends, outputs);
  165. fout << "# End Source File\n";
  166. }
  167. void cmDSPMakefile::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  168. {
  169. std::string dspname = *(m_CreatedProjectNames.end()-1);
  170. dspname += ".dsp";
  171. std::string makefileIn = m_Makefile->GetStartDirectory();
  172. makefileIn += "/";
  173. makefileIn += "CMakeLists.txt";
  174. std::string dsprule = m_Makefile->GetHomeDirectory();
  175. dsprule += "/CMake/Source/CMakeSetupCMD ";
  176. dsprule += makefileIn;
  177. dsprule += " -DSP -H";
  178. dsprule += m_Makefile->GetHomeDirectory();
  179. dsprule += " -S";
  180. dsprule += m_Makefile->GetStartDirectory();
  181. dsprule += " -O";
  182. dsprule += m_Makefile->GetStartOutputDirectory();
  183. dsprule += " -B";
  184. dsprule += m_Makefile->GetHomeOutputDirectory();
  185. std::vector<std::string> depends;
  186. std::vector<std::string> outputs;
  187. outputs.push_back(dspname);
  188. sourceGroup.AddCustomCommand(makefileIn.c_str(), dsprule.c_str(),
  189. depends, outputs);
  190. }
  191. void cmDSPMakefile::WriteDSPFile(std::ostream& fout)
  192. {
  193. // Write the DSP file's header.
  194. this->WriteDSPHeader(fout);
  195. // We may be modifying the source groups temporarily, so make a copy.
  196. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  197. // Find the group in which the CMakeLists.txt source belongs, and add
  198. // the rule to generate this DSP file.
  199. for(std::vector<cmSourceGroup>::reverse_iterator sg = sourceGroups.rbegin();
  200. sg != sourceGroups.rend(); ++sg)
  201. {
  202. if(sg->Matches("CMakeLists.txt"))
  203. {
  204. this->AddDSPBuildRule(*sg);
  205. break;
  206. }
  207. }
  208. // Loop through every source group.
  209. for(std::vector<cmSourceGroup>::const_iterator sg = sourceGroups.begin();
  210. sg != sourceGroups.end(); ++sg)
  211. {
  212. const std::vector<std::string>& sources = sg->GetSources();
  213. const cmSourceGroup::CustomCommands& customCommands = sg->GetCustomCommands();
  214. // If the group is empty, don't write it at all.
  215. if(sources.empty() && customCommands.empty())
  216. { continue; }
  217. // If the group has a name, write the header.
  218. std::string name = sg->GetName();
  219. if(name != "")
  220. {
  221. this->WriteDSPBeginGroup(fout, name.c_str(), "");
  222. }
  223. // Loop through each source in the source group.
  224. for(std::vector<std::string>::const_iterator s = sources.begin();
  225. s != sources.end(); ++s)
  226. {
  227. this->WriteDSPBuildRule(fout, s->c_str());
  228. }
  229. // Loop through each custom command in the source group.
  230. for(cmSourceGroup::CustomCommands::const_iterator cc =
  231. customCommands.begin(); cc != customCommands.end(); ++ cc)
  232. {
  233. std::string source = cc->first;
  234. const cmSourceGroup::Commands& commands = cc->second;
  235. fout << "# Begin Source File\n\n";
  236. fout << "SOURCE=" << source << "\n\n";
  237. // Loop through every command generating code from the current source.
  238. for(cmSourceGroup::Commands::const_iterator c = commands.begin();
  239. c != commands.end(); ++c)
  240. {
  241. std::string command = c->first;
  242. const cmSourceGroup::CommandFiles& commandFiles = c->second;
  243. this->WriteCustomRule(fout, command.c_str(), commandFiles.m_Depends,
  244. commandFiles.m_Outputs);
  245. }
  246. fout << "# End Source File\n";
  247. }
  248. // If the group has a name, write the footer.
  249. if(name != "")
  250. {
  251. this->WriteDSPEndGroup(fout);
  252. }
  253. }
  254. // Write the DSP file's footer.
  255. this->WriteDSPFooter(fout);
  256. }
  257. void cmDSPMakefile::WriteCustomRule(std::ostream& fout,
  258. const char* command,
  259. const std::set<std::string>& depends,
  260. const std::set<std::string>& outputs)
  261. {
  262. std::vector<std::string>::iterator i;
  263. for(i = m_Configurations.begin(); i != m_Configurations.end(); ++i)
  264. {
  265. if (i == m_Configurations.begin())
  266. {
  267. fout << "!IF \"$(CFG)\" == " << i->c_str() << std::endl;
  268. }
  269. else
  270. {
  271. fout << "!ELSEIF \"$(CFG)\" == " << i->c_str() << std::endl;
  272. }
  273. fout << "# Begin Custom Build\n\n";
  274. // Write a rule for every output generated by this command.
  275. for(std::set<std::string>::const_iterator output = outputs.begin();
  276. output != outputs.end(); ++output)
  277. {
  278. fout << "\"" << output->c_str()
  279. << "\" : \"$(SOURCE)\" \"$(INTDIR)\" \"$(OUTDIR)\"";
  280. // Write out all the dependencies for this rule.
  281. for(std::set<std::string>::const_iterator d = depends.begin();
  282. d != depends.end(); ++d)
  283. {
  284. fout << " \"" << d->c_str() << "\"";
  285. }
  286. fout << "\n " << command << "\n\n";
  287. }
  288. fout << "# End Custom Build\n\n";
  289. }
  290. fout << "!ENDIF\n\n";
  291. }
  292. void cmDSPMakefile::WriteDSPBeginGroup(std::ostream& fout,
  293. const char* group,
  294. const char* filter)
  295. {
  296. fout << "# Begin Group \"" << group << "\"\n"
  297. "# PROP Default_Filter \"" << filter << "\"\n";
  298. }
  299. void cmDSPMakefile::WriteDSPEndGroup(std::ostream& fout)
  300. {
  301. fout << "# End Group\n";
  302. }
  303. void cmDSPMakefile::SetBuildType(BuildType b)
  304. {
  305. m_BuildType = b;
  306. switch(b)
  307. {
  308. case STATIC_LIBRARY:
  309. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  310. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  311. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  312. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  313. break;
  314. case DLL:
  315. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  316. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  317. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  318. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  319. break;
  320. case EXECUTABLE:
  321. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  322. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  323. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  324. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  325. break;
  326. }
  327. // once the build type is set, determine what configurations are
  328. // possible
  329. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  330. cmRegularExpression reg("# Name ");
  331. if(!fin)
  332. {
  333. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  334. }
  335. // reset m_Configurations
  336. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  337. // now add all the configurations possible
  338. char buffer[2048];
  339. while(fin)
  340. {
  341. fin.getline(buffer, 2048);
  342. std::string line = buffer;
  343. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  344. m_Makefile->GetLibraryName());
  345. if (reg.find(line))
  346. {
  347. m_Configurations.push_back(line.substr(reg.end()));
  348. }
  349. }
  350. }
  351. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout)
  352. {
  353. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  354. if(!fin)
  355. {
  356. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  357. }
  358. char buffer[2048];
  359. while(fin)
  360. {
  361. fin.getline(buffer, 2048);
  362. std::string line = buffer;
  363. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  364. m_LibraryOptions.c_str());
  365. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  366. m_IncludeOptions.c_str());
  367. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",
  368. m_OutputLibName.c_str());
  369. cmSystemTools::ReplaceString(line,
  370. "EXTRA_DEFINES",
  371. m_Makefile->GetDefineFlags());
  372. fout << line.c_str() << std::endl;
  373. }
  374. }
  375. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  376. {
  377. std::ifstream fin(m_DSPFooterTemplate.c_str());
  378. if(!fin)
  379. {
  380. cmSystemTools::Error("Error Reading ",
  381. m_DSPFooterTemplate.c_str());
  382. }
  383. char buffer[2048];
  384. while(fin)
  385. {
  386. fin.getline(buffer, 2048);
  387. fout << buffer << std::endl;
  388. }
  389. }
  390. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout, const char* path)
  391. {
  392. fout << "# Begin Source File\n\n";
  393. fout << "SOURCE="
  394. << path << "\n";
  395. fout << "# End Source File\n";
  396. }
  397. bool cmDSPMakefile::NeedsDependencies(const char* dspname)
  398. {
  399. if(strcmp(m_Makefile->GetLibraryName(), dspname) == 0)
  400. {
  401. // only shared libs need depend info
  402. const char* cacheValue
  403. = cmCacheManager::GetInstance()->GetCacheValue("BUILD_SHARED_LIBS");
  404. if(cacheValue && strcmp(cacheValue,"0"))
  405. {
  406. return true;
  407. }
  408. else
  409. {
  410. return false;
  411. }
  412. }
  413. // must be an executable so it needs depends
  414. return true;
  415. }