4ddc2a117b6327d07052bf8468b9ec587a3e4751
[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
96 if (configuration.VSProjectVersion == "6.00")
97 this->_generate_dsp ( module );
98 else
99 this->_generate_vcproj ( module );
100
101
102 /*for(size_t k = 0; k < module.non_if_data.files.size(); k++)
103 {
104 File &file = *module.non_if_data.files[k];
105
106 ProcessFile(file.name);
107 }*/
108 }
109 }
110
111 static bool FileExists(string &filename)
112 {
113 ifstream file(filename.c_str());
114
115 if(!file.is_open())
116 return false;
117
118 file.close();
119 return true;
120 }
121
122 void MSVCBackend::ProcessFile(string &filepath)
123 {
124 // Remove the .\ at the start of the filenames
125 if ( filepath[0] == '.' && strchr ( "/\\", filepath[1] ) )
126 filepath.erase(0, 2);
127
128 if(!FileExists(filepath))
129 return;
130
131 // Change the \ to /
132 for(size_t i = 0; i < filepath.length(); i++)
133 {
134 if(filepath[i] == '\\')
135 filepath[i] = '/';
136 }
137
138 // Remove the filename from the path
139 string folder = "";
140
141 size_t pos = filepath.rfind(string("/"), filepath.length() - 1);
142
143 if(pos != string::npos)
144 {
145 folder = filepath;
146 folder.erase(pos, folder.length() - pos);
147 }
148
149 FileUnit fileUnit;
150 fileUnit.filename = filepath;
151 fileUnit.folder = folder;
152
153 m_fileUnits.push_back(fileUnit);
154
155 if(folder != "")
156 AddFolders(folder);
157
158 m_unitCount++;
159 }
160
161 bool MSVCBackend::CheckFolderAdded(string &folder)
162 {
163 for(size_t i = 0; i < m_folders.size(); i++)
164 {
165 if(m_folders[i] == folder)
166 return true;
167 }
168
169 return false;
170 }
171
172 void MSVCBackend::AddFolders(string &folder)
173 {
174 // Check if this folder was already added. true if it was, false otherwise.
175 if(CheckFolderAdded(folder))
176 return;
177
178 m_folders.push_back(folder);
179
180 size_t pos = folder.rfind(string("/"), folder.length() - 1);
181
182 if(pos == string::npos)
183 return;
184
185 folder.erase(pos, folder.length() - pos);
186 AddFolders(folder);
187 }
188
189 void MSVCBackend::OutputFolders()
190 {
191 #if 0
192 m_devFile << "Folders=";
193
194 for(size_t i = 0; i < m_folders.size(); i++)
195 {
196 if(i > 0)
197 m_devFile << ",";
198
199 m_devFile << m_folders[i];
200 }
201 #endif
202 }
203
204 std::string
205 MSVCBackend::DspFileName ( const Module& module ) const
206 {
207 return DosSeparator(
208 ReplaceExtension ( module.GetPath(), ".dsp" )
209 );
210 }
211
212 std::string
213 MSVCBackend::VcprojFileName ( const Module& module ) const
214 {
215 return DosSeparator(
216 ReplaceExtension ( module.GetPath(), ".vcproj" )
217 );
218 }