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