msvc6+psdk fix
[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_dsw = ProjectNode.name + ".dsw";
57 string filename_sln = ProjectNode.name + ".sln";
58
59 printf ( "Creating MSVC workspace: %s\n", filename_dsw.c_str() );
60 printf ( "Creating MSVC workspace: %s\n", filename_sln.c_str() );
61
62 ProcessModules();
63
64 m_dswFile = fopen ( filename_dsw.c_str(), "wb" );
65 m_slnFile = fopen ( filename_sln.c_str(), "wb" );
66
67 if ( !m_dswFile )
68 {
69 printf ( "Could not create file '%s'.\n", filename_dsw.c_str() );
70 return;
71 }
72 _generate_wine_dsw ( m_dswFile );
73
74
75 if ( !m_slnFile )
76 {
77 printf ( "Could not create file '%s'.\n", filename_sln.c_str() );
78 return;
79 }
80 _generate_sln ( m_slnFile );
81
82 fclose ( m_dswFile );
83 fclose ( m_slnFile );
84
85 printf ( "Done.\n" );
86 }
87
88 void MSVCBackend::ProcessModules()
89 {
90 for(size_t i = 0; i < ProjectNode.modules.size(); i++)
91 {
92 Module &module = *ProjectNode.modules[i];
93
94 module.guid = _gen_guid();
95 this->_generate_dsp ( module );
96 this->_generate_vcproj ( module );
97
98
99 /*for(size_t k = 0; k < module.non_if_data.files.size(); k++)
100 {
101 File &file = *module.non_if_data.files[k];
102
103 ProcessFile(file.name);
104 }*/
105 }
106 }
107
108 static bool FileExists(string &filename)
109 {
110 ifstream file(filename.c_str());
111
112 if(!file.is_open())
113 return false;
114
115 file.close();
116 return true;
117 }
118
119 void MSVCBackend::ProcessFile(string &filepath)
120 {
121 // Remove the .\ at the start of the filenames
122 if ( filepath[0] == '.' && strchr ( "/\\", filepath[1] ) )
123 filepath.erase(0, 2);
124
125 if(!FileExists(filepath))
126 return;
127
128 // Change the \ to /
129 for(size_t i = 0; i < filepath.length(); i++)
130 {
131 if(filepath[i] == '\\')
132 filepath[i] = '/';
133 }
134
135 // Remove the filename from the path
136 string folder = "";
137
138 size_t pos = filepath.rfind(string("/"), filepath.length() - 1);
139
140 if(pos != string::npos)
141 {
142 folder = filepath;
143 folder.erase(pos, folder.length() - pos);
144 }
145
146 FileUnit fileUnit;
147 fileUnit.filename = filepath;
148 fileUnit.folder = folder;
149
150 m_fileUnits.push_back(fileUnit);
151
152 if(folder != "")
153 AddFolders(folder);
154
155 m_unitCount++;
156 }
157
158 bool MSVCBackend::CheckFolderAdded(string &folder)
159 {
160 for(size_t i = 0; i < m_folders.size(); i++)
161 {
162 if(m_folders[i] == folder)
163 return true;
164 }
165
166 return false;
167 }
168
169 void MSVCBackend::AddFolders(string &folder)
170 {
171 // Check if this folder was already added. true if it was, false otherwise.
172 if(CheckFolderAdded(folder))
173 return;
174
175 m_folders.push_back(folder);
176
177 size_t pos = folder.rfind(string("/"), folder.length() - 1);
178
179 if(pos == string::npos)
180 return;
181
182 folder.erase(pos, folder.length() - pos);
183 AddFolders(folder);
184 }
185
186 void MSVCBackend::OutputFolders()
187 {
188 #if 0
189 m_devFile << "Folders=";
190
191 for(size_t i = 0; i < m_folders.size(); i++)
192 {
193 if(i > 0)
194 m_devFile << ",";
195
196 m_devFile << m_folders[i];
197 }
198 #endif
199 }
200
201 std::string
202 MSVCBackend::DspFileName ( const Module& module ) const
203 {
204 return DosSeparator(
205 ReplaceExtension ( module.GetPath(), ".dsp" )
206 );
207 }
208
209 std::string
210 MSVCBackend::VcprojFileName ( const Module& module ) const
211 {
212 return DosSeparator(
213 ReplaceExtension ( module.GetPath(), ".vcproj" )
214 );
215 }