cmDSWWriter.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifdef _MSC_VER
  2. #pragma warning ( disable : 4786 )
  3. #endif
  4. #include "cmDSWMakefile.h"
  5. #include "cmSystemTools.h"
  6. #include "cmDSPMakefile.h"
  7. #include <iostream>
  8. #include <fstream>
  9. #include <windows.h>
  10. // microsoft nonsense
  11. #undef GetCurrentDirectory
  12. #undef SetCurrentDirectory
  13. // output the DSW file
  14. void cmDSWMakefile::OutputDSWFile()
  15. {
  16. if(m_OutputDirectory == "")
  17. {
  18. // default to build in place
  19. m_OutputDirectory = m_cmHomeDirectory;
  20. }
  21. // If the output directory is not the m_cmHomeDirectory
  22. // then create it.
  23. if(m_OutputDirectory != m_cmHomeDirectory)
  24. {
  25. if(!cmSystemTools::MakeDirectory(m_OutputDirectory.c_str()))
  26. {
  27. MessageBox(0, "Error creating directory ", 0, MB_OK);
  28. MessageBox(0, m_OutputDirectory.c_str(), 0, MB_OK);
  29. }
  30. }
  31. std::string fname;
  32. fname = m_OutputDirectory;
  33. fname += "/";
  34. fname += this->m_LibraryName;
  35. fname += ".dsw";
  36. std::cerr << "writting dsw file " << fname.c_str() << std::endl;
  37. std::ofstream fout(fname.c_str());
  38. if(!fout)
  39. {
  40. std::cerr << "Error can not open "
  41. << fname.c_str() << " for write" << std::endl;
  42. return;
  43. }
  44. this->WriteDSWFile(fout);
  45. }
  46. // ------------------------------------------------
  47. // Recursive function to find all the CMakeLists.txt files
  48. // in a project. As each file is read in, any directories in
  49. // the SUBDIR variable are also passed back to this function.
  50. // The result is a vector of cmDSPMakefile objects, one for
  51. // each directory with a CMakeLists.txt file
  52. //
  53. void cmDSWMakefile::FindAllCMakeListsFiles(const char* subdir,
  54. std::vector<cmDSPMakefile*>& makefiles)
  55. {
  56. std::string currentDir = this->GetCurrentDirectory();
  57. currentDir += "/";
  58. currentDir += subdir;
  59. currentDir += "/";
  60. currentDir += "CMakeLists.txt";
  61. // CMakeLists.txt exits in the subdirectory
  62. // then create a cmDSPMakefile for it
  63. if(cmSystemTools::FileExists(currentDir.c_str()))
  64. {
  65. // Create a new cmDSPMakefile to read the currentDir CMakeLists.txt file
  66. cmDSPMakefile* dsp = new cmDSPMakefile;
  67. // add it to the vector
  68. makefiles.push_back(dsp);
  69. // Set up the file with the current context
  70. dsp->SetOutputHomeDirectory(this->GetOutputDirectory());
  71. dsp->SetHomeDirectory(this->GetHomeDirectory());
  72. // set the current directory in the Source as a full
  73. // path
  74. std::string currentDir = this->GetCurrentDirectory();
  75. currentDir += "/";
  76. currentDir += subdir;
  77. dsp->SetCurrentDirectory(currentDir.c_str());
  78. // Parse the CMakeLists.txt file
  79. currentDir += "/CMakeLists.txt";
  80. dsp->ReadMakefile(currentDir.c_str());
  81. // Set the output directory which may be different than the source
  82. std::string outdir = m_OutputDirectory;
  83. outdir += "/";
  84. outdir += subdir;
  85. dsp->SetOutputDirectory(outdir.c_str());
  86. // Create the DSP file
  87. dsp->OutputDSPFile();
  88. // Look at any sub directories parsed (SUBDIRS) and
  89. // recurse into them
  90. const std::vector<std::string>& subdirs = dsp->GetSubDirectories();
  91. for(std::vector<std::string>::const_iterator i = subdirs.begin();
  92. i != subdirs.end(); ++i)
  93. {
  94. // append the subdirectory to the current directoy subdir
  95. std::string nextDir = subdir;
  96. nextDir += "/";
  97. nextDir += i->c_str();
  98. // recurse into nextDir
  99. this->FindAllCMakeListsFiles(nextDir.c_str(),
  100. makefiles);
  101. }
  102. }
  103. else
  104. {
  105. std::cerr << "Can not find CMakeLists.txt in " << currentDir.c_str()
  106. << std::endl;
  107. }
  108. }
  109. // Write a DSW file to the stream
  110. void cmDSWMakefile::WriteDSWFile(std::ostream& fout)
  111. {
  112. // Write out the header for a DSW file
  113. this->WriteDSWHeader(fout);
  114. // Create an array of dsp files for the project
  115. std::vector<cmDSPMakefile*> dspfiles;
  116. // loop over all the subdirectories for the DSW file,
  117. // and find all sub directory projects
  118. for(std::vector<std::string>::iterator j = m_SubDirectories.begin();
  119. j != m_SubDirectories.end(); ++j)
  120. {
  121. this->FindAllCMakeListsFiles(j->c_str(), dspfiles);
  122. }
  123. // For each DSP file created insert them into the DSW file
  124. for(std::vector<cmDSPMakefile*>::iterator k = dspfiles.begin();
  125. k != dspfiles.end(); ++k)
  126. {
  127. // Get the directory for the dsp file, it comes
  128. // from the source, so it has the source path which needs
  129. // to be removed as this may be built in a different directory
  130. // than the source
  131. std::string dir = (*k)->GetCurrentDirectory();
  132. // Get the home directory with the trailing slash
  133. std::string homedir = this->GetHomeDirectory();
  134. homedir += "/";
  135. // make the directory relative by removing the home directory part
  136. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  137. // Get the list of create dsp files from the cmDSPMakefile, more
  138. // than one dsp could have been created per input CMakeLists.txt file
  139. std::vector<std::string> dspnames = (*k)->GetCreatedProjectNames();
  140. std::cerr << "Create dsp for "
  141. << dspnames.size()
  142. << " number of dsp files in " << dir << std::endl;
  143. for(std::vector<std::string>::iterator si = dspnames.begin();
  144. si != dspnames.end(); ++si)
  145. {
  146. // Write the project into the DSW file
  147. this->WriteProject(fout, si->c_str(), dir.c_str());
  148. }
  149. // delete the cmDSPMakefile object once done with it to avoid
  150. // leaks
  151. delete *k;
  152. }
  153. // Write the footer for the DSW file
  154. this->WriteDSWFooter(fout);
  155. }
  156. void cmDSWMakefile::WriteProject(std::ostream& fout,
  157. const char* dspname,
  158. const char* dir)
  159. {
  160. fout << "###############################################################################\n\n";
  161. fout << "Project: \"" << dspname << "\"="
  162. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  163. fout << "Package=<5>\n{{{\n}}}\n\n";
  164. fout << "Package=<4>\n";
  165. fout << "{{{\n";
  166. // insert Begin Project Dependency Project_Dep_Name project stuff here
  167. fout << "}}}\n\n";
  168. }
  169. void cmDSWMakefile::WriteDSWFooter(std::ostream& fout)
  170. {
  171. fout << "###############################################################################\n\n";
  172. fout << "Global:\n\n";
  173. fout << "Package=<5>\n{{{\n}}}\n\n";
  174. fout << "Package=<3>\n{{{\n}}}\n\n";
  175. fout << "###############################################################################\n\n";
  176. }
  177. void cmDSWMakefile::WriteDSWHeader(std::ostream& fout)
  178. {
  179. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  180. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  181. }