fixing OutputFile in *.vcproj files
[reactos.git] / reactos / tools / rbuild / backend / msvc / msvc.cpp
1 /*
2 * Copyright (C) 2005 Trevor McCort
3 * Copyright (C) 2005 Casper S. Hornstrup
4 * Copyright (C) 2005 Steven Edwards
5 * Copyright (C) 2005 Royce Mitchell
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 #ifdef _MSC_VER
22 #pragma warning ( disable : 4786 )
23 #endif//_MSC_VER
24
25 #include <iostream>
26 #include <fstream>
27 #include <string>
28
29 #include "msvc.h"
30 #include "../mingw/mingw.h"
31
32 using namespace std;
33
34 static class MSVCFactory : public Backend::Factory
35 {
36 public:
37
38 MSVCFactory() : Factory("MSVC") {}
39 Backend *operator() (Project &project,
40 Configuration& configuration)
41 {
42 return new MSVCBackend(project, configuration);
43 }
44
45 } factory;
46
47
48 MSVCBackend::MSVCBackend(Project &project,
49 Configuration& configuration) : Backend(project, configuration)
50 {
51 m_unitCount = 0;
52 }
53
54 void MSVCBackend::Process()
55 {
56 string filename_sln ( ProjectNode.name );
57 if ( configuration.VSProjectVersion == "6.00" )
58 filename_sln += ".dsw";
59 else
60 filename_sln += ".sln";
61 printf ( "Creating MSVC workspace: %s\n", filename_sln.c_str() );
62
63 ProcessModules();
64 m_slnFile = fopen ( filename_sln.c_str(), "wb" );
65
66 if ( !m_slnFile )
67 {
68 printf ( "Could not create file '%s'.\n", filename_sln.c_str() );
69 return;
70 }
71
72 if ( configuration.VSProjectVersion == "6.00" )
73 _generate_wine_dsw ( m_slnFile );
74 else
75 _generate_sln ( m_slnFile );
76
77 fclose ( m_slnFile );
78 printf ( "Done.\n" );
79 }
80
81 void MSVCBackend::ProcessModules()
82 {
83 for(size_t i = 0; i < ProjectNode.modules.size(); i++)
84 {
85 Module &module = *ProjectNode.modules[i];
86
87 module.guid = _gen_guid();
88
89 if (configuration.VSProjectVersion == "6.00")
90 this->_generate_dsp ( module );
91 else
92 this->_generate_vcproj ( module );
93
94
95 /*for(size_t k = 0; k < module.non_if_data.files.size(); k++)
96 {
97 File &file = *module.non_if_data.files[k];
98
99 ProcessFile(file.name);
100 }*/
101 }
102 }
103
104 static bool FileExists(string &filename)
105 {
106 ifstream file(filename.c_str());
107
108 if(!file.is_open())
109 return false;
110
111 file.close();
112 return true;
113 }
114
115 void MSVCBackend::ProcessFile(string &filepath)
116 {
117 // Remove the .\ at the start of the filenames
118 if ( filepath[0] == '.' && strchr ( "/\\", filepath[1] ) )
119 filepath.erase(0, 2);
120
121 if(!FileExists(filepath))
122 return;
123
124 // Change the \ to /
125 for(size_t i = 0; i < filepath.length(); i++)
126 {
127 if(filepath[i] == '\\')
128 filepath[i] = '/';
129 }
130
131 // Remove the filename from the path
132 string folder = "";
133
134 size_t pos = filepath.rfind(string("/"), filepath.length() - 1);
135
136 if(pos != string::npos)
137 {
138 folder = filepath;
139 folder.erase(pos, folder.length() - pos);
140 }
141
142 FileUnit fileUnit;
143 fileUnit.filename = filepath;
144 fileUnit.folder = folder;
145
146 m_fileUnits.push_back(fileUnit);
147
148 if(folder != "")
149 AddFolders(folder);
150
151 m_unitCount++;
152 }
153
154 bool MSVCBackend::CheckFolderAdded(string &folder)
155 {
156 for(size_t i = 0; i < m_folders.size(); i++)
157 {
158 if(m_folders[i] == folder)
159 return true;
160 }
161
162 return false;
163 }
164
165 void MSVCBackend::AddFolders(string &folder)
166 {
167 // Check if this folder was already added. true if it was, false otherwise.
168 if(CheckFolderAdded(folder))
169 return;
170
171 m_folders.push_back(folder);
172
173 size_t pos = folder.rfind(string("/"), folder.length() - 1);
174
175 if(pos == string::npos)
176 return;
177
178 folder.erase(pos, folder.length() - pos);
179 AddFolders(folder);
180 }
181
182 void MSVCBackend::OutputFolders()
183 {
184 #if 0
185 m_devFile << "Folders=";
186
187 for(size_t i = 0; i < m_folders.size(); i++)
188 {
189 if(i > 0)
190 m_devFile << ",";
191
192 m_devFile << m_folders[i];
193 }
194 #endif
195 }
196
197 std::string
198 MSVCBackend::DspFileName ( const Module& module ) const
199 {
200 return DosSeparator(
201 ReplaceExtension ( module.GetPath(), ".dsp" )
202 );
203 }
204
205 std::string
206 MSVCBackend::VcprojFileName ( const Module& module ) const
207 {
208 return DosSeparator(
209 ReplaceExtension ( module.GetPath(), ".vcproj" )
210 );
211 }