FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / cmDSWWriter.cxx
blob8f01153f7791a6c0ef83c18b55f6647a12f61dcb
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmDSWWriter.cxx,v $
5 Language: C++
6 Date: $Date: 2002-06-27 19:57:09 $
7 Version: $Revision: 1.50 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDSWWriter.h"
18 #include "cmStandardIncludes.h"
19 #include "cmSystemTools.h"
20 #include "cmDSPWriter.h"
21 #include "cmMSProjectGenerator.h"
22 #include "cmCacheManager.h"
25 cmDSWWriter::cmDSWWriter(cmMakefile* m)
27 m_Makefile = m;
30 // output the DSW file
31 void cmDSWWriter::OutputDSWFile()
33 // if this is an out of source build, create the output directory
34 if(strcmp(m_Makefile->GetStartOutputDirectory(),
35 m_Makefile->GetHomeDirectory()) != 0)
37 if(!cmSystemTools::MakeDirectory(m_Makefile->GetStartOutputDirectory()))
39 cmSystemTools::Error("Error creating output directory for DSW file",
40 m_Makefile->GetStartOutputDirectory());
43 // create the dsw file name
44 std::string fname;
45 fname = m_Makefile->GetStartOutputDirectory();
46 fname += "/";
47 if(strlen(m_Makefile->GetProjectName()))
49 fname += m_Makefile->GetProjectName();
51 else
53 fname += "Project";
55 fname += ".dsw";
56 std::ofstream fout(fname.c_str());
57 if(!fout)
59 cmSystemTools::Error("Error can not open DSW file for write: "
60 ,fname.c_str());
61 return;
63 this->WriteDSWFile(fout);
67 inline std::string removeQuotes(const std::string& s)
69 if(s[0] == '\"' && s[s.size()-1] == '\"')
71 return s.substr(1, s.size()-2);
73 return s;
77 // Write a DSW file to the stream
78 void cmDSWWriter::WriteDSWFile(std::ostream& fout)
80 // Write out the header for a DSW file
81 this->WriteDSWHeader(fout);
83 // Create a list of cmMakefile created from all the
84 // CMakeLists.txt files that are in sub directories of
85 // this one.
86 std::vector<cmMakefile*> allListFiles;
87 // add this makefile to the list
88 allListFiles.push_back(m_Makefile);
89 // add a special target that depends on ALL projects for easy build
90 // of Debug only
91 m_Makefile->AddUtilityCommand("ALL_BUILD", "echo", "\"Build all projects\"",
92 false);
93 std::string ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
94 ctest = removeQuotes(ctest);
95 ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
96 ctest += "/";
97 ctest += "ctest";
98 ctest += cmSystemTools::GetExecutableExtension();
99 if(!cmSystemTools::FileExists(ctest.c_str()))
101 ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
102 ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
103 ctest += "/Debug/";
104 ctest += "ctest";
105 ctest += cmSystemTools::GetExecutableExtension();
107 if(!cmSystemTools::FileExists(ctest.c_str()))
109 ctest = m_Makefile->GetDefinition("CMAKE_COMMAND");
110 ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
111 ctest += "/Release/";
112 ctest += "ctest";
113 ctest += cmSystemTools::GetExecutableExtension();
115 // if we found ctest
116 if (cmSystemTools::FileExists(ctest.c_str()))
118 // Create a full path filename for output Testfile
119 std::string fname;
120 fname = m_Makefile->GetStartOutputDirectory();
121 fname += "/";
122 fname += "DartTestfile.txt";
124 // If the file doesn't exist, then ENABLE_TESTING hasn't been run
125 if (cmSystemTools::FileExists(fname.c_str()))
127 m_Makefile->AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",
128 false);
132 m_Makefile->FindSubDirectoryCMakeListsFiles(allListFiles);
133 // For each cmMakefile, create a DSP for it, and
134 // add it to this DSW file
135 for(std::vector<cmMakefile*>::iterator k = allListFiles.begin();
136 k != allListFiles.end(); ++k)
138 cmMakefile* mf = *k;
139 cmMSProjectGenerator* pg = (cmMSProjectGenerator*)mf->GetMakefileGenerator();
140 // make sure the generator is building dsp files
141 pg->BuildDSWOff();
142 mf->GenerateMakefile();
143 // Get the source directory from the makefile
144 std::string dir = mf->GetStartDirectory();
145 // Get the home directory with the trailing slash
146 std::string homedir = m_Makefile->GetHomeDirectory();
147 homedir += "/";
148 // remove the home directory and / from the source directory
149 // this gives a relative path
150 cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
152 // Get the list of create dsp files names from the cmDSPWriter, more
153 // than one dsp could have been created per input CMakeLists.txt file
154 // for each target
155 std::vector<std::string> dspnames =
156 pg->GetDSPWriter()->GetCreatedProjectNames();
157 cmTargets &tgts = pg->GetDSPWriter()->GetMakefile()->GetTargets();
158 cmTargets::iterator l = tgts.begin();
159 for(std::vector<std::string>::iterator si = dspnames.begin();
160 l != tgts.end(); ++l)
162 // special handling for the current makefile
163 if(mf == m_Makefile)
165 dir = "."; // no subdirectory for project generated
166 // if this is the special ALL_BUILD utility, then
167 // make it depend on every other non UTILITY project.
168 // This is done by adding the names to the GetUtilities
169 // vector on the makefile
170 if(l->first == "ALL_BUILD")
172 for(std::vector<cmMakefile*>::iterator a = allListFiles.begin();
173 a != allListFiles.end(); ++a)
175 const cmTargets &atgts = (*a)->GetTargets();
176 for(cmTargets::const_iterator al = atgts.begin();
177 al != atgts.end(); ++al)
179 if (al->second.IsInAll())
181 if (al->second.GetType() == cmTarget::UTILITY)
183 l->second.AddUtility(al->first.c_str());
185 else
187 l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
194 // Write the project into the DSW file
195 if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
197 cmCustomCommand cc = l->second.GetCustomCommands()[0];
199 // dodgy use of the cmCustomCommand's members to store the
200 // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
201 std::vector<std::string> stuff = cc.GetDepends();
202 std::vector<std::string> depends = cc.GetOutputs();
203 this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
204 ++si;
206 else if ((l->second.GetType() != cmTarget::INSTALL_FILES)
207 && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
209 this->WriteProject(fout, si->c_str(), dir.c_str(),
210 pg->GetDSPWriter(),l->second);
211 ++si;
214 // delete the cmMakefile which also deletes the cmMSProjectGenerator
215 if(mf != m_Makefile)
217 delete mf;
221 // Write the footer for the DSW file
222 this->WriteDSWFooter(fout);
226 // Write a dsp file into the DSW file,
227 // Note, that dependencies from executables to
228 // the libraries it uses are also done here
229 void cmDSWWriter::WriteProject(std::ostream& fout,
230 const char* dspname,
231 const char* dir,
232 cmDSPWriter*,
233 const cmTarget& target
236 fout << "#########################################################"
237 "######################\n\n";
238 fout << "Project: \"" << dspname << "\"="
239 << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
240 fout << "Package=<5>\n{{{\n}}}\n\n";
241 fout << "Package=<4>\n";
242 fout << "{{{\n";
244 // insert Begin Project Dependency Project_Dep_Name project stuff here
245 if (target.GetType() != cmTarget::STATIC_LIBRARY)
247 cmTarget::LinkLibraries::const_iterator j, jend;
248 j = target.GetLinkLibraries().begin();
249 jend = target.GetLinkLibraries().end();
250 for(;j!= jend; ++j)
252 if(j->first != dspname)
254 // is the library part of this DSW ? If so add dependency
255 std::string libPath = j->first + "_CMAKE_PATH";
256 const char* cacheValue
257 = m_Makefile->GetDefinition(libPath.c_str());
258 if(cacheValue)
260 fout << "Begin Project Dependency\n";
261 fout << "Project_Dep_Name " << j->first << "\n";
262 fout << "End Project Dependency\n";
268 std::set<cmStdString>::const_iterator i, end;
269 // write utility dependencies.
270 i = target.GetUtilities().begin();
271 end = target.GetUtilities().end();
272 for(;i!= end; ++i)
274 if(*i != dspname)
276 fout << "Begin Project Dependency\n";
277 fout << "Project_Dep_Name " << *i << "\n";
278 fout << "End Project Dependency\n";
281 fout << "}}}\n\n";
285 // Write a dsp file into the DSW file,
286 // Note, that dependencies from executables to
287 // the libraries it uses are also done here
288 void cmDSWWriter::WriteExternalProject(std::ostream& fout,
289 const char* name,
290 const char* location,
291 const std::vector<std::string>& dependencies)
293 fout << "#########################################################"
294 "######################\n\n";
295 fout << "Project: \"" << name << "\"="
296 << location << " - Package Owner=<4>\n\n";
297 fout << "Package=<5>\n{{{\n}}}\n\n";
298 fout << "Package=<4>\n";
299 fout << "{{{\n";
302 std::vector<std::string>::const_iterator i, end;
303 // write dependencies.
304 i = dependencies.begin();
305 end = dependencies.end();
306 for(;i!= end; ++i)
308 fout << "Begin Project Dependency\n";
309 fout << "Project_Dep_Name " << *i << "\n";
310 fout << "End Project Dependency\n";
312 fout << "}}}\n\n";
317 // Standard end of dsw file
318 void cmDSWWriter::WriteDSWFooter(std::ostream& fout)
320 fout << "######################################################"
321 "#########################\n\n";
322 fout << "Global:\n\n";
323 fout << "Package=<5>\n{{{\n}}}\n\n";
324 fout << "Package=<3>\n{{{\n}}}\n\n";
325 fout << "#####################################################"
326 "##########################\n\n";
330 // ouput standard header for dsw file
331 void cmDSWWriter::WriteDSWHeader(std::ostream& fout)
333 fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
334 fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";