cmDSPWriter.cxx 14 KB

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