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