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