cmDSPWriter.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmDSPMakefile.h"
  33. #include "cmStandardIncludes.h"
  34. #include "cmSystemTools.h"
  35. #include "cmRegularExpression.h"
  36. #include "cmCacheManager.h"
  37. cmDSPMakefile::~cmDSPMakefile()
  38. {
  39. }
  40. cmDSPMakefile::cmDSPMakefile(cmMakefile*mf)
  41. {
  42. m_Makefile = mf;
  43. }
  44. void cmDSPMakefile::OutputDSPFile()
  45. {
  46. // If not an in source build, then create the output directory
  47. if(strcmp(m_Makefile->GetStartOutputDirectory(),
  48. m_Makefile->GetHomeDirectory()) != 0)
  49. {
  50. if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
  51. {
  52. cmSystemTools::Error("Error creating directory ",
  53. m_Makefile->GetStartOutputDirectory());
  54. }
  55. }
  56. // Setup /I and /LIBPATH options for the resulting DSP file
  57. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  58. std::vector<std::string>::iterator i;
  59. for(i = includes.begin(); i != includes.end(); ++i)
  60. {
  61. m_IncludeOptions += "/I \"";
  62. m_IncludeOptions += *i;
  63. m_IncludeOptions += "\" ";
  64. }
  65. // Create the DSP or set of DSP's for libraries and executables
  66. m_LibraryBuildType = STATIC_LIBRARY;
  67. if(cmCacheManager::GetInstance()->IsOn("BUILD_SHARED_LIBS"))
  68. {
  69. m_LibraryBuildType = DLL;
  70. }
  71. // clear project names
  72. m_CreatedProjectNames.clear();
  73. // build any targets
  74. cmTargets &tgts = m_Makefile->GetTargets();
  75. for(cmTargets::iterator l = tgts.begin();
  76. l != tgts.end(); l++)
  77. {
  78. if (l->second.IsALibrary())
  79. {
  80. this->SetBuildType(m_LibraryBuildType, l->first.c_str());
  81. }
  82. else
  83. {
  84. this->SetBuildType(EXECUTABLE,l->first.c_str());
  85. }
  86. this->CreateSingleDSP(l->first.c_str(),l->second);
  87. }
  88. }
  89. void cmDSPMakefile::CreateSingleDSP(const char *lname, cmTarget &target)
  90. {
  91. std::string fname;
  92. fname = m_Makefile->GetStartOutputDirectory();
  93. fname += "/";
  94. fname += lname;
  95. fname += ".dsp";
  96. std::string pname = lname;
  97. m_CreatedProjectNames.push_back(pname);
  98. std::ofstream fout(fname.c_str());
  99. if(!fout)
  100. {
  101. cmSystemTools::Error("Error Writing ", fname.c_str());
  102. }
  103. this->WriteDSPFile(fout,lname,target);
  104. }
  105. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout)
  106. {
  107. std::string dspname = *(m_CreatedProjectNames.end()-1);
  108. dspname += ".dsp";
  109. std::string makefileIn = "\"";
  110. makefileIn += m_Makefile->GetStartDirectory();
  111. makefileIn += "/";
  112. makefileIn += "CMakeLists.txt\"";
  113. std::string dsprule = "\"";
  114. dsprule += m_Makefile->GetHomeDirectory();
  115. dsprule += "/CMake/Source/CMakeSetupCMD\" ";
  116. dsprule += makefileIn;
  117. dsprule += " -DSP -H\"";
  118. dsprule += m_Makefile->GetHomeDirectory();
  119. dsprule += "\" -S\"";
  120. dsprule += m_Makefile->GetStartDirectory();
  121. dsprule += "\" -O\"";
  122. dsprule += m_Makefile->GetStartOutputDirectory();
  123. dsprule += "\" -B\"";
  124. dsprule += m_Makefile->GetHomeOutputDirectory();
  125. dsprule += "\"";
  126. std::set<std::string> depends;
  127. std::set<std::string> outputs;
  128. outputs.insert(outputs.begin(), dspname);
  129. fout << "# Begin Source File\n\n";
  130. fout << "SOURCE=" << makefileIn.c_str() << "\n\n";
  131. this->WriteCustomRule(fout, dsprule.c_str(), depends, outputs);
  132. fout << "# End Source File\n";
  133. }
  134. void cmDSPMakefile::AddDSPBuildRule(cmSourceGroup& sourceGroup)
  135. {
  136. std::string dspname = *(m_CreatedProjectNames.end()-1);
  137. dspname += ".dsp";
  138. std::string makefileIn = "\"";
  139. makefileIn += m_Makefile->GetStartDirectory();
  140. makefileIn += "/";
  141. makefileIn += "CMakeLists.txt\"";
  142. std::string dsprule = "\"";
  143. dsprule += m_Makefile->GetHomeDirectory();
  144. dsprule += "/CMake/Source/CMakeSetupCMD\" ";
  145. dsprule += makefileIn;
  146. dsprule += " -DSP -H\"";
  147. dsprule += m_Makefile->GetHomeDirectory();
  148. dsprule += "\" -S\"";
  149. dsprule += m_Makefile->GetStartDirectory();
  150. dsprule += "\" -O\"";
  151. dsprule += m_Makefile->GetStartOutputDirectory();
  152. dsprule += "\" -B\"";
  153. dsprule += m_Makefile->GetHomeOutputDirectory();
  154. dsprule += "\"";
  155. std::vector<std::string> depends;
  156. std::vector<std::string> outputs;
  157. outputs.push_back(dspname);
  158. cmCustomCommand cc(makefileIn.c_str(), dsprule.c_str(),
  159. depends, outputs);
  160. sourceGroup.AddCustomCommand(cc);
  161. }
  162. void cmDSPMakefile::WriteDSPFile(std::ostream& fout,
  163. const char *libName,
  164. cmTarget &target)
  165. {
  166. // Write the DSP file's header.
  167. this->WriteDSPHeader(fout, libName, target);
  168. // We may be modifying the source groups temporarily, so make a copy.
  169. std::vector<cmSourceGroup> sourceGroups = m_Makefile->GetSourceGroups();
  170. // get the classes from the source lists then add them to the groups
  171. target.GenerateSourceFilesFromSourceLists(*m_Makefile);
  172. std::vector<cmSourceFile> classes = target.GetSourceFiles();
  173. for(std::vector<cmSourceFile>::iterator i = classes.begin();
  174. i != classes.end(); i++)
  175. {
  176. if(!i->IsAHeaderFileOnly())
  177. {
  178. // Add the file to the list of sources.
  179. std::string source = i->GetFullPath();
  180. cmSourceGroup& sourceGroup = m_Makefile->FindSourceGroup(source.c_str(),
  181. sourceGroups);
  182. sourceGroup.AddSource(source.c_str());
  183. }
  184. }
  185. // add any custom rules to the source groups
  186. for (std::vector<cmCustomCommand>::const_iterator cr =
  187. target.GetCustomCommands().begin();
  188. cr != target.GetCustomCommands().end(); ++cr)
  189. {
  190. cmSourceGroup& sourceGroup =
  191. m_Makefile->FindSourceGroup(cr->GetSourceName().c_str(),
  192. sourceGroups);
  193. cmCustomCommand cc(*cr);
  194. cc.ExpandVariables(*m_Makefile);
  195. sourceGroup.AddCustomCommand(cc);
  196. }
  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, const char *libName)
  304. {
  305. switch(b)
  306. {
  307. case STATIC_LIBRARY:
  308. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  309. m_DSPHeaderTemplate += "/CMake/Source/staticLibHeader.dsptemplate";
  310. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  311. m_DSPFooterTemplate += "/CMake/Source/staticLibFooter.dsptemplate";
  312. break;
  313. case DLL:
  314. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  315. m_DSPHeaderTemplate += "/CMake/Source/DLLHeader.dsptemplate";
  316. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  317. m_DSPFooterTemplate += "/CMake/Source/DLLFooter.dsptemplate";
  318. break;
  319. case EXECUTABLE:
  320. m_DSPHeaderTemplate = m_Makefile->GetHomeDirectory();
  321. m_DSPHeaderTemplate += "/CMake/Source/EXEHeader.dsptemplate";
  322. m_DSPFooterTemplate = m_Makefile->GetHomeDirectory();
  323. m_DSPFooterTemplate += "/CMake/Source/EXEFooter.dsptemplate";
  324. break;
  325. }
  326. // once the build type is set, determine what configurations are
  327. // possible
  328. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  329. cmRegularExpression reg("# Name ");
  330. if(!fin)
  331. {
  332. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  333. }
  334. // reset m_Configurations
  335. m_Configurations.erase(m_Configurations.begin(), m_Configurations.end());
  336. // now add all the configurations possible
  337. char buffer[2048];
  338. while(fin)
  339. {
  340. fin.getline(buffer, 2048);
  341. std::string line = buffer;
  342. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  343. if (reg.find(line))
  344. {
  345. m_Configurations.push_back(line.substr(reg.end()));
  346. }
  347. }
  348. }
  349. void cmDSPMakefile::WriteDSPHeader(std::ostream& fout, const char *libName,
  350. const cmTarget &target)
  351. {
  352. // determine the link directories
  353. std::string libOptions;
  354. std::string libDebugOptions;
  355. std::string libOptimizedOptions;
  356. std::vector<std::string>::iterator i;
  357. std::vector<std::string>& libdirs = m_Makefile->GetLinkDirectories();
  358. for(i = libdirs.begin(); i != libdirs.end(); ++i)
  359. {
  360. libOptions += " /LIBPATH:\"";
  361. libOptions += *i;
  362. libOptions += "/$(OUTDIR)\" ";
  363. libOptions += " /LIBPATH:\"";
  364. libOptions += *i;
  365. libOptions += "\" ";
  366. }
  367. // find link libraries
  368. cmMakefile::LinkLibraries& libs = m_Makefile->GetLinkLibraries();
  369. cmMakefile::LinkLibraries::const_iterator j;
  370. for(j = libs.begin(); j != libs.end(); ++j)
  371. {
  372. // add libraries to executables and dlls (but never include
  373. // a library in a library, bad recursion)
  374. if (!target.IsALibrary() ||
  375. (m_LibraryBuildType == DLL && libName != j->first))
  376. {
  377. if (j->second == cmMakefile::GENERAL)
  378. {
  379. libOptions += " ";
  380. libOptions += j->first;
  381. libOptions += ".lib ";
  382. }
  383. if (j->second == cmMakefile::DEBUG)
  384. {
  385. libDebugOptions += " ";
  386. libDebugOptions += j->first;
  387. libDebugOptions += ".lib ";
  388. }
  389. if (j->second == cmMakefile::OPTIMIZED)
  390. {
  391. libOptimizedOptions += " ";
  392. libOptimizedOptions += j->first;
  393. libOptimizedOptions += ".lib ";
  394. }
  395. }
  396. }
  397. libOptions += "/STACK:10000000 ";
  398. std::ifstream fin(m_DSPHeaderTemplate.c_str());
  399. if(!fin)
  400. {
  401. cmSystemTools::Error("Error Reading ", m_DSPHeaderTemplate.c_str());
  402. }
  403. char buffer[2048];
  404. while(fin)
  405. {
  406. fin.getline(buffer, 2048);
  407. std::string line = buffer;
  408. cmSystemTools::ReplaceString(line, "CM_LIBRARIES",
  409. libOptions.c_str());
  410. cmSystemTools::ReplaceString(line, "CM_DEBUG_LIBRARIES",
  411. libDebugOptions.c_str());
  412. cmSystemTools::ReplaceString(line, "CM_OPTIMIZED_LIBRARIES",
  413. libOptimizedOptions.c_str());
  414. cmSystemTools::ReplaceString(line, "BUILD_INCLUDES",
  415. m_IncludeOptions.c_str());
  416. cmSystemTools::ReplaceString(line, "OUTPUT_LIBNAME",libName);
  417. cmSystemTools::ReplaceString(line,
  418. "EXTRA_DEFINES",
  419. m_Makefile->GetDefineFlags());
  420. fout << line.c_str() << std::endl;
  421. }
  422. }
  423. void cmDSPMakefile::WriteDSPFooter(std::ostream& fout)
  424. {
  425. std::ifstream fin(m_DSPFooterTemplate.c_str());
  426. if(!fin)
  427. {
  428. cmSystemTools::Error("Error Reading ",
  429. m_DSPFooterTemplate.c_str());
  430. }
  431. char buffer[2048];
  432. while(fin)
  433. {
  434. fin.getline(buffer, 2048);
  435. fout << buffer << std::endl;
  436. }
  437. }
  438. void cmDSPMakefile::WriteDSPBuildRule(std::ostream& fout, const char* path)
  439. {
  440. fout << "# Begin Source File\n\n";
  441. fout << "SOURCE="
  442. << path << "\n";
  443. fout << "# End Source File\n";
  444. }