Nathan Woods <npwoods _NOSPAM_at_NOSPAM cybercom.net> - Implement a working VS.NET...
[reactos.git] / reactos / tools / rbuild / backend / msvc / vcprojmaker.cpp
1 /*
2 * Copyright (C) 2002 Patrik Stridvall
3 * Copyright (C) 2005 Royce Mitchell III
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifdef _MSC_VER
21 #pragma warning ( disable : 4786 )
22 #endif//_MSC_VER
23
24 #include <string>
25 #include <vector>
26
27 #include <stdio.h>
28
29 #include "msvc.h"
30
31 using std::string;
32 using std::vector;
33
34 void
35 MSVCBackend::_generate_vcproj ( const Module& module )
36 {
37 size_t i;
38 // TODO FIXME wine hack?
39 //const bool wine = false;
40
41 string vcproj_file = VcprojFileName(module);
42 printf ( "Creating MSVC.NET project: '%s'\n", vcproj_file.c_str() );
43 FILE* OUT = fopen ( vcproj_file.c_str(), "wb" );
44
45 vector<string> imports;
46 for ( i = 0; i < module.non_if_data.libraries.size(); i++ )
47 {
48 imports.push_back ( module.non_if_data.libraries[i]->name );
49 }
50
51 string module_type = GetExtension(module.GetTargetName());
52 bool lib = (module_type == ".lib") || (module_type == ".a");
53 bool dll = (module_type == ".dll");
54 bool exe = (module_type == ".exe");
55 // TODO FIXME - need more checks here for 'sys' and possibly 'drv'?
56
57 bool console = exe && (module.type == Win32CUI);
58
59 // TODO FIXME - not sure if the count here is right...
60 int parts = 0;
61 const char* p = strpbrk ( vcproj_file.c_str(), "/\\" );
62 while ( p )
63 {
64 ++parts;
65 p = strpbrk ( p+1, "/\\" );
66 }
67 string msvc_wine_dir = "..";
68 while ( --parts )
69 msvc_wine_dir += "\\..";
70
71 string wine_include_dir = msvc_wine_dir + "\\include";
72
73 //$progress_current++;
74 //$output->progress("$dsp_file (file $progress_current of $progress_max)");
75
76 // TODO FIXME - what's diff. betw. 'c_srcs' and 'source_files'?
77 string vcproj_path = module.GetBasePath();
78 vector<string> c_srcs, source_files, resource_files, includes, libraries, defines;
79 vector<const IfableData*> ifs_list;
80 ifs_list.push_back ( &module.non_if_data );
81
82 defines.push_back ( "WIN32" );
83 defines.push_back ( "_WINDOWS" );
84 defines.push_back ( "WIN32" );
85 defines.push_back ( "_MBCS" );
86 defines.push_back ( "STDCALL=__stdcall" );
87
88 while ( ifs_list.size() )
89 {
90 const IfableData& data = *ifs_list.back();
91 ifs_list.pop_back();
92 // TODO FIXME - refactor needed - we're discarding if conditions
93 for ( i = 0; i < data.ifs.size(); i++ )
94 ifs_list.push_back ( &data.ifs[i]->data );
95 const vector<File*>& files = data.files;
96 for ( i = 0; i < files.size(); i++ )
97 {
98 // TODO FIXME - do we want the full path of the file here?
99 string file = string(".") + &files[i]->name[vcproj_path.size()];
100
101 source_files.push_back ( file );
102 if ( !stricmp ( Right(file,2).c_str(), ".c" ) )
103 c_srcs.push_back ( file );
104 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
105 resource_files.push_back ( file );
106 }
107 const vector<Include*>& incs = data.includes;
108 for ( i = 0; i < incs.size(); i++ )
109 {
110 string path = Path::RelativeFromDirectory (
111 incs[i]->directory,
112 module.GetBasePath() );
113 includes.push_back ( path );
114 }
115 const vector<Library*>& libs = data.libraries;
116 for ( i = 0; i < libs.size(); i++ )
117 {
118 libraries.push_back ( libs[i]->name + ".lib" );
119 }
120 const vector<Define*>& defs = data.defines;
121 for ( i = 0; i < defs.size(); i++ )
122 {
123 if ( defs[i]->value[0] )
124 defines.push_back ( defs[i]->name + "=" + defs[i]->value );
125 else
126 defines.push_back ( defs[i]->name );
127 }
128 }
129
130 vector<string> header_files;
131
132 bool no_cpp = true;
133 bool no_msvc_headers = true;
134
135 std::vector<std::string> cfgs;
136
137 cfgs.push_back ( module.name + " - Win32" );
138
139 if (!no_cpp)
140 {
141 std::vector<std::string> _cfgs;
142 for ( i = 0; i < cfgs.size(); i++ )
143 {
144 _cfgs.push_back ( cfgs[i] + " C" );
145 _cfgs.push_back ( cfgs[i] + " C++" );
146 }
147 cfgs.resize(0);
148 cfgs = _cfgs;
149 }
150
151 if (!no_msvc_headers)
152 {
153 std::vector<std::string> _cfgs;
154 for ( i = 0; i < cfgs.size(); i++ )
155 {
156 _cfgs.push_back ( cfgs[i] + " MSVC Headers" );
157 _cfgs.push_back ( cfgs[i] + " Wine Headers" );
158 }
159 cfgs.resize(0);
160 cfgs = _cfgs;
161 }
162
163 string default_cfg = cfgs.back();
164
165 fprintf ( OUT, "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\r\n" );
166 fprintf ( OUT, "<VisualStudioProject\r\n" );
167 fprintf ( OUT, "\tProjectType=\"Visual C++\"\r\n" );
168 fprintf ( OUT, "\tVersion=\"7.00\"\r\n" );
169 fprintf ( OUT, "\tName=\"%s\"\r\n", module.name.c_str() );
170 fprintf ( OUT, "\tKeyword=\"Win32Proj\">\r\n" );
171
172 fprintf ( OUT, "\t<Platforms>\r\n" );
173 fprintf ( OUT, "\t\t<Platform\r\n" );
174 fprintf ( OUT, "\t\t\tName=\"Win32\"/>\r\n" );
175 fprintf ( OUT, "\t</Platforms>\r\n" );
176
177 int n = 0;
178
179 std::string output_dir;
180
181 fprintf ( OUT, "\t<Configurations>\r\n" );
182 for ( size_t icfg = 0; icfg < cfgs.size(); icfg++ )
183 {
184 std::string& cfg = cfgs[icfg];
185
186 bool debug = !strstr ( cfg.c_str(), "Release" );
187 //bool msvc_headers = ( 0 != strstr ( cfg.c_str(), "MSVC Headers" ) );
188
189 fprintf ( OUT, "\t\t<Configuration\r\n" );
190 fprintf ( OUT, "\t\t\tName=\"%s|Win32\"\r\n", cfg.c_str() );
191 fprintf ( OUT, "\t\t\tOutputDirectory=\"%s\"\r\n", cfg.c_str() );
192 fprintf ( OUT, "\t\t\tIntermediateDirectory=\"%s\"\r\n", cfg.c_str() );
193 fprintf ( OUT, "\t\t\tConfigurationType=\"%d\"\r\n", exe ? 1 : dll ? 2 : lib ? 4 : -1 );
194 fprintf ( OUT, "\t\t\tCharacterSet=\"2\">\r\n" );
195
196 fprintf ( OUT, "\t\t\t<Tool\r\n" );
197 fprintf ( OUT, "\t\t\t\tName=\"VCCLCompilerTool\"\r\n" );
198 fprintf ( OUT, "\t\t\t\tOptimization=\"%d\"\r\n", debug ? 0 : 2 );
199
200 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
201 bool multiple_includes = false;
202 for ( i = 0; i < includes.size(); i++ )
203 {
204 const string& include = includes[i];
205 if ( strcmp ( include.c_str(), "." ) )
206 {
207 if ( multiple_includes )
208 fprintf ( OUT, ";" );
209 fprintf ( OUT, "%s", include.c_str() );
210 multiple_includes = true;
211 }
212 }
213 fprintf ( OUT, "\"\r\n " );
214
215 if ( debug )
216 {
217 defines.push_back ( "_DEBUG" );
218 if ( lib || exe )
219 {
220 defines.push_back ( "_LIB" );
221 }
222 else
223 {
224 defines.push_back ( "_WINDOWS" );
225 defines.push_back ( "_USRDLL" );
226 }
227 }
228 else
229 {
230 defines.push_back ( "NDEBUG" );
231 if ( lib || exe )
232 {
233 defines.push_back ( "_LIB" );
234 }
235 else
236 {
237 defines.push_back ( "_WINDOWS" );
238 defines.push_back ( "_USRDLL" );
239 }
240 }
241
242 fprintf ( OUT, "\t\t\t\tPreprocessorDefinitions=\"" );
243 for ( i = 0; i < defines.size(); i++ )
244 {
245 if ( i > 0 )
246 fprintf ( OUT, ";" );
247 fprintf ( OUT, "%s", defines[i].c_str() );
248 }
249 fprintf ( OUT, "\"\r\n" );
250
251 fprintf ( OUT, "\t\t\t\tMinimalRebuild=\"TRUE\"\r\n" );
252 fprintf ( OUT, "\t\t\t\tBasicRuntimeChecks=\"3\"\r\n" );
253 fprintf ( OUT, "\t\t\t\tRuntimeLibrary=\"5\"\r\n" );
254 fprintf ( OUT, "\t\t\t\tBufferSecurityCheck=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
255 fprintf ( OUT, "\t\t\t\tEnableFunctionLevelLinking=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
256 fprintf ( OUT, "\t\t\t\tUsePrecompiledHeader=\"0\"\r\n" );
257 fprintf ( OUT, "\t\t\t\tWarningLevel=\"1\"\r\n" );
258 fprintf ( OUT, "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\r\n" );
259 fprintf ( OUT, "\t\t\t\tDebugInformationFormat=\"4\"/>\r\n" );
260
261 fprintf ( OUT, "\t\t\t<Tool\r\n" );
262 fprintf ( OUT, "\t\t\t\tName=\"VCCustomBuildTool\"/>\r\n" );
263
264 if ( lib )
265 {
266 fprintf ( OUT, "\t\t\t<Tool\r\n" );
267 fprintf ( OUT, "\t\t\t\tName=\"VCLibrarianTool\"\r\n" );
268 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.%s\"/>\r\n", module.name.c_str(), module_type.c_str() );
269 }
270 else
271 {
272 fprintf ( OUT, "\t\t\t<Tool\r\n" );
273 fprintf ( OUT, "\t\t\t\tName=\"VCLinkerTool\"\r\n" );
274
275 fprintf ( OUT, "\t\t\t\tAdditionalDependencies=\"" );
276 for ( i = 0; i < libraries.size(); i++ )
277 {
278 if ( i > 0 )
279 fprintf ( OUT, " " );
280 fprintf ( OUT, "%s", libraries[i].c_str() );
281 }
282 fprintf ( OUT, "\"\r\n" );
283
284 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.%s\"\r\n", module.name.c_str(), module_type.c_str() );
285 fprintf ( OUT, "\t\t\t\tLinkIncremental=\"%d\"\r\n", debug ? 2 : 1 );
286 fprintf ( OUT, "\t\t\t\tGenerateDebugInformation=\"TRUE\"\r\n" );
287
288 if ( debug )
289 fprintf ( OUT, "\t\t\t\tProgramDatabaseFile=\"$(OutDir)/%s.pdb\"\r\n", module.name.c_str() );
290
291 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", console ? 1 : 2 );
292 fprintf ( OUT, "\t\t\t\tTargetMachine=\"%d\"/>\r\n", 1 );
293 }
294
295 fprintf ( OUT, "\t\t\t<Tool\r\n" );
296 fprintf ( OUT, "\t\t\t\tName=\"VCMIDLTool\"/>\r\n" );
297 fprintf ( OUT, "\t\t\t<Tool\r\n" );
298 fprintf ( OUT, "\t\t\t\tName=\"VCPostBuildEventTool\"/>\r\n" );
299 fprintf ( OUT, "\t\t\t<Tool\r\n" );
300 fprintf ( OUT, "\t\t\t\tName=\"VCPreBuildEventTool\"/>\r\n" );
301 fprintf ( OUT, "\t\t\t<Tool\r\n" );
302 fprintf ( OUT, "\t\t\t\tName=\"VCPreLinkEventTool\"/>\r\n" );
303 fprintf ( OUT, "\t\t\t<Tool\r\n" );
304 fprintf ( OUT, "\t\t\t\tName=\"VCResourceCompilerTool\"/>\r\n" );
305 fprintf ( OUT, "\t\t\t<Tool\r\n" );
306 fprintf ( OUT, "\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"/>\r\n" );
307 fprintf ( OUT, "\t\t\t<Tool\r\n" );
308 fprintf ( OUT, "\t\t\t\tName=\"VCWebDeploymentTool\"/>\r\n" );
309 fprintf ( OUT, "\t\t</Configuration>\r\n" );
310
311 n++;
312 }
313 fprintf ( OUT, "\t</Configurations>\r\n" );
314
315 fprintf ( OUT, "\t<Files>\r\n" );
316
317 // Source files
318 fprintf ( OUT, "\t\t<Filter\r\n" );
319 fprintf ( OUT, "\t\t\tName=\"Source Files\"\r\n" );
320 fprintf ( OUT, "\t\t\tFilter=\"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\">\r\n" );
321 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
322 {
323 const string& source_file = DosSeparator(source_files[isrcfile]);
324 fprintf ( OUT, "\t\t\t<File\r\n" );
325 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", source_file.c_str() );
326 fprintf ( OUT, "\t\t\t</File>\r\n" );
327 }
328 fprintf ( OUT, "\t\t</Filter>\r\n" );
329
330 // Header files
331 fprintf ( OUT, "\t\t<Filter\r\n" );
332 fprintf ( OUT, "\t\t\tName=\"Header Files\"\r\n" );
333 fprintf ( OUT, "\t\t\tFilter=\"h;hpp;hxx;hm;inl\">\r\n" );
334 for ( i = 0; i < header_files.size(); i++ )
335 {
336 const string& header_file = header_files[i];
337 fprintf ( OUT, "\t\t\t<File\r\n" );
338 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", header_file.c_str() );
339 fprintf ( OUT, "\t\t\t</File>\r\n" );
340 }
341 fprintf ( OUT, "\t\t</Filter>\r\n" );
342
343 // Resource files
344 fprintf ( OUT, "\t\t<Filter\r\n" );
345 fprintf ( OUT, "\t\t\tName=\"Resource Files\"\r\n" );
346 fprintf ( OUT, "\t\t\tFilter=\"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\">\r\n" );
347 for ( i = 0; i < header_files.size(); i++ )
348 {
349 const string& resource_file = resource_files[i];
350 fprintf ( OUT, "\t\t\t<File\r\n" );
351 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", resource_file.c_str() );
352 fprintf ( OUT, "\t\t\t</File>\r\n" );
353 }
354 fprintf ( OUT, "\t\t</Filter>\r\n" );
355
356 fprintf ( OUT, "\t</Files>\r\n" );
357 fprintf ( OUT, "\t<Globals>\r\n" );
358 fprintf ( OUT, "\t</Globals>\r\n" );
359 fprintf ( OUT, "</VisualStudioProject>\r\n" );
360 fclose(OUT);
361 }
362
363 void
364 MSVCBackend::_generate_sln_header ( FILE* OUT )
365 {
366 fprintf ( OUT, "Microsoft Visual Studio Solution File, Format Version 9.00\r\n" );
367 fprintf ( OUT, "# Visual C++ Express 2005\r\n" );
368 fprintf ( OUT, "\r\n" );
369 }
370
371 void
372 MSVCBackend::_generate_sln ( FILE* OUT )
373 {
374 _generate_sln_header(OUT);
375 // TODO FIXME - is it necessary to sort them?
376 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
377 {
378 Module& module = *ProjectNode.modules[i];
379
380 std::string vcproj_file = VcprojFileName ( module );
381 _generate_dsw_project ( OUT, module, vcproj_file, module.dependencies );
382 }
383 // _generate_dsw_footer ( OUT );
384 }
385
386
387
388 /*
389 m_devFile << "Microsoft Visual Studio Solution File, Format Version 9.00" << endl;
390 m_devFile << "# Visual C++ Express 2005" << endl;
391
392 m_devFile << "# FIXME Project listings here" << endl;
393 m_devFile << "EndProject" << endl;
394 m_devFile << "Global" << endl;
395 m_devFile << " GlobalSection(SolutionConfigurationPlatforms) = preSolution" << endl;
396 m_devFile << " Debug|Win32 = Debug|Win32" << endl;
397 m_devFile << " Release|Win32 = Release|Win32" << endl;
398 m_devFile << " EndGlobalSection" << endl;
399 m_devFile << " GlobalSection(ProjectConfigurationPlatforms) = postSolution" << endl;
400 m_devFile << " #FIXME Project Listings Here" << endl;
401 m_devFile << " EndGlobalSection" << endl;
402 m_devFile << " GlobalSection(SolutionProperties) = preSolution" << endl;
403 m_devFile << " HideSolutionNode = FALSE" << endl;
404 m_devFile << " EndGlobalSection" << endl;
405 m_devFile << "EndGlobal" << endl;
406
407 m_devFile << endl << endl;
408 */