cmDSPWriter.cxx 14 KB

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