fix vcprojmaker to add the correct GUIDs
[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") || (module_type == ".cpl");
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.project.non_if_data );
81 ifs_list.push_back ( &module.non_if_data );
82
83 // this is a define in MinGW w32api, but not Microsoft's headers
84 defines.push_back ( "STDCALL=__stdcall" );
85
86 while ( ifs_list.size() )
87 {
88 const IfableData& data = *ifs_list.back();
89 ifs_list.pop_back();
90 // TODO FIXME - refactor needed - we're discarding if conditions
91 for ( i = 0; i < data.ifs.size(); i++ )
92 ifs_list.push_back ( &data.ifs[i]->data );
93 const vector<File*>& files = data.files;
94 for ( i = 0; i < files.size(); i++ )
95 {
96 // TODO FIXME - do we want the full path of the file here?
97 string file = string(".") + &files[i]->name[vcproj_path.size()];
98
99 source_files.push_back ( file );
100 if ( !stricmp ( Right(file,2).c_str(), ".c" ) )
101 c_srcs.push_back ( file );
102 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
103 resource_files.push_back ( file );
104 }
105 const vector<Include*>& incs = data.includes;
106 for ( i = 0; i < incs.size(); i++ )
107 {
108 // explicitly omit win32api directories
109 if ( !strncmp(incs[i]->directory.c_str(), "w32api", 6 ) )
110 continue;
111
112 // explicitly omit include/wine directories
113 if ( !strncmp(incs[i]->directory.c_str(), "include\\wine", 12 ) )
114 continue;
115
116 string path = Path::RelativeFromDirectory (
117 incs[i]->directory,
118 module.GetBasePath() );
119 includes.push_back ( path );
120 }
121 const vector<Library*>& libs = data.libraries;
122 for ( i = 0; i < libs.size(); i++ )
123 {
124 libraries.push_back ( libs[i]->name + ".lib" );
125 }
126 const vector<Define*>& defs = data.defines;
127 for ( i = 0; i < defs.size(); i++ )
128 {
129 if ( defs[i]->value[0] )
130 defines.push_back ( defs[i]->name + "=" + defs[i]->value );
131 else
132 defines.push_back ( defs[i]->name );
133 }
134 }
135
136 vector<string> header_files;
137
138 bool no_cpp = true;
139 bool no_msvc_headers = true;
140
141 std::vector<std::string> cfgs;
142
143 cfgs.push_back ( "Debug" );
144 cfgs.push_back ( "Release" );
145
146 if (!no_cpp)
147 {
148 std::vector<std::string> _cfgs;
149 for ( i = 0; i < cfgs.size(); i++ )
150 {
151 _cfgs.push_back ( cfgs[i] + " C" );
152 _cfgs.push_back ( cfgs[i] + " C++" );
153 }
154 cfgs.resize(0);
155 cfgs = _cfgs;
156 }
157
158 if (!no_msvc_headers)
159 {
160 std::vector<std::string> _cfgs;
161 for ( i = 0; i < cfgs.size(); i++ )
162 {
163 _cfgs.push_back ( cfgs[i] + " MSVC Headers" );
164 _cfgs.push_back ( cfgs[i] + " Wine Headers" );
165 }
166 cfgs.resize(0);
167 cfgs = _cfgs;
168 }
169
170 string default_cfg = cfgs.back();
171
172 fprintf ( OUT, "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\r\n" );
173 fprintf ( OUT, "<VisualStudioProject\r\n" );
174 fprintf ( OUT, "\tProjectType=\"Visual C++\"\r\n" );
175
176 if (configuration.VSProjectVersion.empty())
177 configuration.VSProjectVersion = MS_VS_DEF_VERSION;
178
179 fprintf ( OUT, "\tVersion=\"%s\"\r\n", configuration.VSProjectVersion.c_str() );
180 fprintf ( OUT, "\tName=\"%s\"\r\n", module.name.c_str() );
181 fprintf ( OUT, "\tProjectGUID=\"%s\"\r\n", module.guid.c_str() );
182 fprintf ( OUT, "\tKeyword=\"Win32Proj\">\r\n" );
183
184 fprintf ( OUT, "\t<Platforms>\r\n" );
185 fprintf ( OUT, "\t\t<Platform\r\n" );
186 fprintf ( OUT, "\t\t\tName=\"Win32\"/>\r\n" );
187 fprintf ( OUT, "\t</Platforms>\r\n" );
188
189 int n = 0;
190
191 std::string output_dir;
192
193 fprintf ( OUT, "\t<Configurations>\r\n" );
194 for ( size_t icfg = 0; icfg < cfgs.size(); icfg++ )
195 {
196 std::string& cfg = cfgs[icfg];
197
198 bool debug = !strstr ( cfg.c_str(), "Release" );
199 //bool msvc_headers = ( 0 != strstr ( cfg.c_str(), "MSVC Headers" ) );
200
201 fprintf ( OUT, "\t\t<Configuration\r\n" );
202 fprintf ( OUT, "\t\t\tName=\"%s|Win32\"\r\n", cfg.c_str() );
203 fprintf ( OUT, "\t\t\tOutputDirectory=\"%s\"\r\n", cfg.c_str() );
204 fprintf ( OUT, "\t\t\tIntermediateDirectory=\"%s\"\r\n", cfg.c_str() );
205 fprintf ( OUT, "\t\t\tConfigurationType=\"%d\"\r\n", exe ? 1 : dll ? 2 : lib ? 4 : -1 );
206 fprintf ( OUT, "\t\t\tCharacterSet=\"2\">\r\n" );
207
208 fprintf ( OUT, "\t\t\t<Tool\r\n" );
209 fprintf ( OUT, "\t\t\t\tName=\"VCCLCompilerTool\"\r\n" );
210 fprintf ( OUT, "\t\t\t\tOptimization=\"%d\"\r\n", debug ? 0 : 2 );
211
212 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
213 bool multiple_includes = false;
214 fprintf ( OUT, "./;" );
215 for ( i = 0; i < includes.size(); i++ )
216 {
217 const string& include = includes[i];
218 if ( strcmp ( include.c_str(), "." ) )
219 {
220 if ( multiple_includes )
221 fprintf ( OUT, ";" );
222 fprintf ( OUT, "%s", include.c_str() );
223 multiple_includes = true;
224 }
225 }
226 fprintf ( OUT, "\"\r\n " );
227
228 if ( debug )
229 {
230 defines.push_back ( "_DEBUG" );
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 else
242 {
243 defines.push_back ( "NDEBUG" );
244 if ( lib || exe )
245 {
246 defines.push_back ( "_LIB" );
247 }
248 else
249 {
250 defines.push_back ( "_WINDOWS" );
251 defines.push_back ( "_USRDLL" );
252 }
253 }
254
255 fprintf ( OUT, "\t\t\t\tPreprocessorDefinitions=\"" );
256 for ( i = 0; i < defines.size(); i++ )
257 {
258 if ( i > 0 )
259 fprintf ( OUT, ";" );
260
261 defines[i] = _replace_str(defines[i], "\"","&quot;");
262 fprintf ( OUT, "%s", defines[i].c_str() );
263 }
264 fprintf ( OUT, "\"\r\n" );
265
266 fprintf ( OUT, "\t\t\t\tMinimalRebuild=\"TRUE\"\r\n" );
267 fprintf ( OUT, "\t\t\t\tBasicRuntimeChecks=\"3\"\r\n" );
268 fprintf ( OUT, "\t\t\t\tRuntimeLibrary=\"5\"\r\n" );
269 fprintf ( OUT, "\t\t\t\tBufferSecurityCheck=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
270 fprintf ( OUT, "\t\t\t\tEnableFunctionLevelLinking=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
271 fprintf ( OUT, "\t\t\t\tUsePrecompiledHeader=\"0\"\r\n" );
272 fprintf ( OUT, "\t\t\t\tWarningLevel=\"1\"\r\n" );
273 fprintf ( OUT, "\t\t\t\tDetect64BitPortabilityProblems=\"TRUE\"\r\n" );
274 fprintf ( OUT, "\t\t\t\tDebugInformationFormat=\"4\"/>\r\n" );
275
276 fprintf ( OUT, "\t\t\t<Tool\r\n" );
277 fprintf ( OUT, "\t\t\t\tName=\"VCCustomBuildTool\"/>\r\n" );
278
279 if ( lib )
280 {
281 fprintf ( OUT, "\t\t\t<Tool\r\n" );
282 fprintf ( OUT, "\t\t\t\tName=\"VCLibrarianTool\"\r\n" );
283 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.%s\"/>\r\n", module.name.c_str(), module_type.c_str() );
284 }
285 else
286 {
287 fprintf ( OUT, "\t\t\t<Tool\r\n" );
288 fprintf ( OUT, "\t\t\t\tName=\"VCLinkerTool\"\r\n" );
289
290 fprintf ( OUT, "\t\t\t\tAdditionalDependencies=\"" );
291 for ( i = 0; i < libraries.size(); i++ )
292 {
293 if ( i > 0 )
294 fprintf ( OUT, " " );
295 fprintf ( OUT, "%s", libraries[i].c_str() );
296 }
297 fprintf ( OUT, "\"\r\n" );
298
299 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.%s\"\r\n", module.name.c_str(), module_type.c_str() );
300 fprintf ( OUT, "\t\t\t\tLinkIncremental=\"%d\"\r\n", debug ? 2 : 1 );
301 fprintf ( OUT, "\t\t\t\tGenerateDebugInformation=\"TRUE\"\r\n" );
302
303 if ( debug )
304 fprintf ( OUT, "\t\t\t\tProgramDatabaseFile=\"$(OutDir)/%s.pdb\"\r\n", module.name.c_str() );
305
306 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", console ? 1 : 2 );
307 fprintf ( OUT, "\t\t\t\tTargetMachine=\"%d\"/>\r\n", 1 );
308 }
309
310 fprintf ( OUT, "\t\t\t<Tool\r\n" );
311 fprintf ( OUT, "\t\t\t\tName=\"VCResourceCompilerTool\"\r\n" );
312 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
313 multiple_includes = false;
314 fprintf ( OUT, "./;" );
315 for ( i = 0; i < includes.size(); i++ )
316 {
317 const string& include = includes[i];
318 if ( strcmp ( include.c_str(), "." ) )
319 {
320 if ( multiple_includes )
321 fprintf ( OUT, ";" );
322 fprintf ( OUT, "%s", include.c_str() );
323 multiple_includes = true;
324 }
325 }
326 fprintf ( OUT, "\"/>\r\n " );
327
328 fprintf ( OUT, "\t\t\t<Tool\r\n" );
329 fprintf ( OUT, "\t\t\t\tName=\"VCMIDLTool\"/>\r\n" );
330 fprintf ( OUT, "\t\t\t<Tool\r\n" );
331 fprintf ( OUT, "\t\t\t\tName=\"VCPostBuildEventTool\"/>\r\n" );
332 fprintf ( OUT, "\t\t\t<Tool\r\n" );
333 fprintf ( OUT, "\t\t\t\tName=\"VCPreBuildEventTool\"/>\r\n" );
334 fprintf ( OUT, "\t\t\t<Tool\r\n" );
335 fprintf ( OUT, "\t\t\t\tName=\"VCPreLinkEventTool\"/>\r\n" );
336 fprintf ( OUT, "\t\t\t<Tool\r\n" );
337 fprintf ( OUT, "\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"/>\r\n" );
338 fprintf ( OUT, "\t\t\t<Tool\r\n" );
339 fprintf ( OUT, "\t\t\t\tName=\"VCWebDeploymentTool\"/>\r\n" );
340 fprintf ( OUT, "\t\t</Configuration>\r\n" );
341
342 n++;
343 }
344 fprintf ( OUT, "\t</Configurations>\r\n" );
345
346 fprintf ( OUT, "\t<Files>\r\n" );
347
348 // Source files
349 fprintf ( OUT, "\t\t<Filter\r\n" );
350 fprintf ( OUT, "\t\t\tName=\"Source Files\"\r\n" );
351 fprintf ( OUT, "\t\t\tFilter=\"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\">\r\n" );
352 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
353 {
354 const string& source_file = DosSeparator(source_files[isrcfile]);
355 fprintf ( OUT, "\t\t\t<File\r\n" );
356 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", source_file.c_str() );
357 fprintf ( OUT, "\t\t\t</File>\r\n" );
358 }
359 fprintf ( OUT, "\t\t</Filter>\r\n" );
360
361 // Header files
362 fprintf ( OUT, "\t\t<Filter\r\n" );
363 fprintf ( OUT, "\t\t\tName=\"Header Files\"\r\n" );
364 fprintf ( OUT, "\t\t\tFilter=\"h;hpp;hxx;hm;inl\">\r\n" );
365 for ( i = 0; i < header_files.size(); i++ )
366 {
367 const string& header_file = header_files[i];
368 fprintf ( OUT, "\t\t\t<File\r\n" );
369 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", header_file.c_str() );
370 fprintf ( OUT, "\t\t\t</File>\r\n" );
371 }
372 fprintf ( OUT, "\t\t</Filter>\r\n" );
373
374 // Resource files
375 fprintf ( OUT, "\t\t<Filter\r\n" );
376 fprintf ( OUT, "\t\t\tName=\"Resource Files\"\r\n" );
377 fprintf ( OUT, "\t\t\tFilter=\"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\">\r\n" );
378 for ( i = 0; i < header_files.size(); i++ )
379 {
380 const string& resource_file = resource_files[i];
381 fprintf ( OUT, "\t\t\t<File\r\n" );
382 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", resource_file.c_str() );
383 fprintf ( OUT, "\t\t\t</File>\r\n" );
384 }
385 fprintf ( OUT, "\t\t</Filter>\r\n" );
386
387 fprintf ( OUT, "\t</Files>\r\n" );
388 fprintf ( OUT, "\t<Globals>\r\n" );
389 fprintf ( OUT, "\t</Globals>\r\n" );
390 fprintf ( OUT, "</VisualStudioProject>\r\n" );
391 fclose(OUT);
392 }
393
394 std::string
395 MSVCBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
396 {
397 std::string::size_type pos = string1.find(find_str, 0);
398 int intLen = find_str.length();
399
400 while(std::string::npos != pos)
401 {
402 string1.replace(pos, intLen, replace_str);
403 pos = string1.find(find_str, intLen + pos);
404 }
405
406 return string1;
407 }
408
409 void
410 MSVCBackend::_generate_sln_header ( FILE* OUT )
411 {
412 if (configuration.VSProjectVersion.empty())
413 configuration.VSProjectVersion = MS_VS_DEF_VERSION;
414
415 string version;
416
417 if (configuration.VSProjectVersion == "7.00")
418 version = "7.00";
419
420 if (configuration.VSProjectVersion == "7.10")
421 version = "8.00";
422
423 if (configuration.VSProjectVersion == "8.00")
424 version = "9.00";
425
426 fprintf ( OUT, "Microsoft Visual Studio Solution File, Format Version %s\r\n", version.c_str() );
427 fprintf ( OUT, "# Visual Studio 2005\r\n" );
428 fprintf ( OUT, "\r\n" );
429 }
430
431
432 void
433 MSVCBackend::_generate_sln_project (
434 FILE* OUT,
435 const Module& module,
436 std::string vcproj_file,
437 std::string sln_guid,
438 std::string vcproj_guid,
439 const std::vector<Dependency*>& dependencies )
440 {
441 vcproj_file = DosSeparator ( std::string(".\\") + vcproj_file );
442
443 fprintf ( OUT, "Project(\"%s\") = \"%s\", \"%s\", \"%s\"\r\n", sln_guid.c_str() , module.name.c_str(), vcproj_file.c_str(), vcproj_guid.c_str() );
444 fprintf ( OUT, "\tProjectSection(ProjectDependencies) = postProject\r\n" );
445 fprintf ( OUT, "\tEndProjectSection\r\n" );
446 fprintf ( OUT, "EndProject\r\n" );
447 }
448
449
450 void
451 MSVCBackend::_generate_sln_footer ( FILE* OUT )
452 {
453 fprintf ( OUT, "Global\r\n" );
454 fprintf ( OUT, "\tGlobalSection(SolutionConfiguration) = preSolution\r\n" );
455 fprintf ( OUT, "\t\tDebug = Debug\r\n" );
456 fprintf ( OUT, "\t\tRelease = Release\r\n" );
457 fprintf ( OUT, "\tEndGlobalSection\r\n" );
458 fprintf ( OUT, "\tGlobalSection(ProjectConfiguration) = postSolution\r\n" );
459 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
460 {
461 Module& module = *ProjectNode.modules[i];
462 std::string guid = module.guid;
463 _generate_sln_configurations ( OUT, guid.c_str() );
464 }
465 fprintf ( OUT, "\tEndGlobalSection\r\n" );
466 fprintf ( OUT, "\tGlobalSection(ExtensibilityGlobals) = postSolution\r\n" );
467 fprintf ( OUT, "\tEndGlobalSection\r\n" );
468 fprintf ( OUT, "\tGlobalSection(ExtensibilityAddIns) = postSolution\r\n" );
469 fprintf ( OUT, "\tEndGlobalSection\r\n" );
470 fprintf ( OUT, "EndGlobal\r\n" );
471
472
473 fprintf ( OUT, "\r\n" );
474 }
475
476
477 void
478 MSVCBackend::_generate_sln_configurations ( FILE* OUT, std::string vcproj_guid )
479 {
480 fprintf ( OUT, "\t\t%s.Debug.ActiveCfg = Debug|Win32\r\n", vcproj_guid.c_str() );
481 fprintf ( OUT, "\t\t%s.Debug.Build.0 = Debug|Win32\r\n", vcproj_guid.c_str() );
482 fprintf ( OUT, "\t\t%s.Debug.Release.ActiveCfg = Release|Win32\r\n", vcproj_guid.c_str() );
483 fprintf ( OUT, "\t\t%s.Debug.Release.Build.0 = Release|Win32\r\n", vcproj_guid.c_str() );
484 }
485
486 void
487 MSVCBackend::_generate_sln ( FILE* OUT )
488 {
489 string sln_guid = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
490 vector<string> guids;
491
492 _generate_sln_header(OUT);
493 // TODO FIXME - is it necessary to sort them?
494 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
495 {
496 Module& module = *ProjectNode.modules[i];
497
498 std::string vcproj_file = VcprojFileName ( module );
499 _generate_sln_project ( OUT, module, vcproj_file, sln_guid, module.guid, module.dependencies );
500 }
501 _generate_sln_footer ( OUT );
502 }
503