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