cmDSPWriter.cxx 13 KB

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