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