Verbose mode
[reactos.git] / reactos / tools / rbuild / backend / devcpp / devcpp.cpp
1 /*
2 * Dev-C++ Backend
3 * Copyright (C) 2005 Trevor McCort
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef _MSC_VER
21 #pragma warning ( disable : 4786 )
22 #endif//_MSC_VER
23
24 #include <iostream>
25 #include <fstream>
26 #include <string>
27
28 #include "devcpp.h"
29
30 using namespace std;
31
32 static class DevCppFactory : public Backend::Factory
33 {
34 public:
35
36 DevCppFactory() : Factory("devcpp") {}
37 Backend *operator() (Project &project, bool verbose)
38 {
39 return new DevCppBackend(project, verbose);
40 }
41
42 } factory;
43
44
45 DevCppBackend::DevCppBackend(Project &project, bool verbose) : Backend(project, verbose)
46 {
47 m_unitCount = 0;
48 }
49
50 void DevCppBackend::Process()
51 {
52 string filename = ProjectNode.name + ".dev";
53
54 cout << "Creating Dev-C++ project: " << filename << endl;
55
56 ProcessModules();
57
58 m_devFile.open(filename.c_str());
59
60 if(!m_devFile.is_open())
61 {
62 cout << "Could not open file." << endl;
63 return;
64 }
65
66 m_devFile << "[Project]" << endl;
67
68 m_devFile << "FileName=" << filename << endl
69 << "Name=" << ProjectNode.name << endl
70 << "UnitCount=" << m_unitCount << endl
71 << "Type=1" << endl
72 << "Ver=1" << endl
73 << "ObjFiles=" << endl
74 << "Includes=" << endl
75 << "Libs=" << endl
76 << "PrivateResource=" << endl
77 << "ResourceIncludes=" << endl
78 << "MakeIncludes=" << endl
79 << "Compiler=" << endl
80 << "CppCompiler=" << endl
81 << "Linker=" << endl
82 << "IsCpp=1" << endl
83 << "Icon=" << endl
84 << "ExeOutput=" << endl
85 << "ObjectOutput=" << endl
86 << "OverrideOutput=0" << endl
87 << "OverrideOutputName=" << endl
88 << "HostApplication=" << endl
89 << "CommandLine=" << endl
90 << "UseCustomMakefile=1" << endl
91 << "CustomMakefile=" << ProjectNode.makefile << endl
92 << "IncludeVersionInto=0" << endl
93 << "SupportXPThemes=0" << endl
94 << "CompilerSet=0" << endl
95
96 << "CompilerSettings=0000000000000000000000" << endl;
97
98 OutputFolders();
99
100 m_devFile << endl << endl;
101
102 OutputFileUnits();
103
104 m_devFile.close();
105
106 // Dev-C++ needs a makefile, so use the MinGW backend to create one.
107
108 cout << "Creating Makefile: " << ProjectNode.makefile << endl;
109
110 Backend *backend = Backend::Factory::Create("mingw", ProjectNode, verbose );
111 backend->Process();
112 delete backend;
113
114 cout << "Done." << endl << endl;
115
116 cout << "You may want to disable Class browsing (see below) before you open this project in Dev-C++, as the "
117 << "parsing required for large projects can take quite awhile."
118 << endl << endl
119 << "(Tools->Editor Options->Class browsing->Enable class browsing check box)"
120 << endl << endl;
121 }
122
123 void DevCppBackend::ProcessModules()
124 {
125 for(size_t i = 0; i < ProjectNode.modules.size(); i++)
126 {
127 Module &module = *ProjectNode.modules[i];
128
129 for(size_t k = 0; k < module.non_if_data.files.size(); k++)
130 {
131 File &file = *module.non_if_data.files[k];
132
133 ProcessFile(file.name);
134 }
135 }
136 }
137
138 bool FileExists(string &filename)
139 {
140 ifstream file(filename.c_str());
141
142 if(!file.is_open())
143 return false;
144
145 file.close();
146 return true;
147 }
148
149 void DevCppBackend::ProcessFile(string &filepath)
150 {
151 // Remove the .\ at the start of the filenames
152 filepath.erase(0, 2);
153
154 if(!FileExists(filepath))
155 return;
156
157 // Change the \ to /
158 for(size_t i = 0; i < filepath.length(); i++)
159 {
160 if(filepath[i] == '\\')
161 filepath[i] = '/';
162 }
163
164 // Remove the filename from the path
165 string folder = "";
166
167 size_t pos = filepath.rfind(string("/"), filepath.length() - 1);
168
169 if(pos != string::npos)
170 {
171 folder = filepath;
172 folder.erase(pos, folder.length() - pos);
173 }
174
175 FileUnit fileUnit;
176 fileUnit.filename = filepath;
177 fileUnit.folder = folder;
178
179 m_fileUnits.push_back(fileUnit);
180
181 if(folder != "")
182 AddFolders(folder);
183
184 m_unitCount++;
185 }
186
187 bool DevCppBackend::CheckFolderAdded(string &folder)
188 {
189 for(size_t i = 0; i < m_folders.size(); i++)
190 {
191 if(m_folders[i] == folder)
192 return true;
193 }
194
195 return false;
196 }
197
198 void DevCppBackend::AddFolders(string &folder)
199 {
200 // Check if this folder was already added. true if it was, false otherwise.
201 if(CheckFolderAdded(folder))
202 return;
203
204 m_folders.push_back(folder);
205
206 size_t pos = folder.rfind(string("/"), folder.length() - 1);
207
208 if(pos == string::npos)
209 return;
210
211 folder.erase(pos, folder.length() - pos);
212 AddFolders(folder);
213 }
214
215 void DevCppBackend::OutputFolders()
216 {
217 m_devFile << "Folders=";
218
219 for(size_t i = 0; i < m_folders.size(); i++)
220 {
221 if(i > 0)
222 m_devFile << ",";
223
224 m_devFile << m_folders[i];
225 }
226 }
227
228 void DevCppBackend::OutputFileUnits()
229 {
230 for(size_t i = 0; i < m_fileUnits.size(); i++)
231 {
232 m_devFile << "[Unit" << i + 1 << "]" << endl;
233
234
235 m_devFile << "FileName=" << m_fileUnits[i].filename << endl;
236 m_devFile << "CompileCpp=1" << endl;
237 m_devFile << "Folder=" << m_fileUnits[i].folder << endl;
238 m_devFile << "Compile=1" << endl;
239 m_devFile << "Link=1" << endl;
240 m_devFile << "Priority=1000" << endl;
241 m_devFile << "OverrideBuildCmd=0" << endl;
242 m_devFile << "BuildCmd=" << endl << endl;;
243 }
244 }