fixed BaseAddress
[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 bool sys = (module_type == ".sys");
60 // TODO FIXME - need more checks here for 'sys' and possibly 'drv'?
61
62 bool console = exe && (module.type == Win32CUI);
63
64 // TODO FIXME - not sure if the count here is right...
65 int parts = 0;
66 const char* p = strpbrk ( vcproj_file.c_str(), "/\\" );
67 while ( p )
68 {
69 ++parts;
70 p = strpbrk ( p+1, "/\\" );
71 }
72 string msvc_wine_dir = "..";
73 while ( --parts )
74 msvc_wine_dir += "\\..";
75
76 string wine_include_dir = msvc_wine_dir + "\\include";
77
78 //$progress_current++;
79 //$output->progress("$dsp_file (file $progress_current of $progress_max)");
80
81 // TODO FIXME - what's diff. betw. 'c_srcs' and 'source_files'?
82 string vcproj_path = module.GetBasePath();
83 vector<string> c_srcs, source_files, resource_files, includes, libraries, defines;
84 vector<const IfableData*> ifs_list;
85 ifs_list.push_back ( &module.project.non_if_data );
86 ifs_list.push_back ( &module.non_if_data );
87
88 // this is a define in MinGW w32api, but not Microsoft's headers
89 defines.push_back ( "_CRT_SECURE_NO_DEPRECATE" );
90 defines.push_back ( "_CRT_NON_CONFORMING_SWPRINTFS" );
91 defines.push_back ( "STDCALL=__stdcall" );
92
93 string baseaddr;
94
95 while ( ifs_list.size() )
96 {
97 const IfableData& data = *ifs_list.back();
98 ifs_list.pop_back();
99 // TODO FIXME - refactor needed - we're discarding if conditions
100 for ( i = 0; i < data.ifs.size(); i++ )
101 ifs_list.push_back ( &data.ifs[i]->data );
102 const vector<File*>& files = data.files;
103 for ( i = 0; i < files.size(); i++ )
104 {
105 // TODO FIXME - do we want the full path of the file here?
106 string file = string(".") + &files[i]->name[vcproj_path.size()];
107
108 source_files.push_back ( file );
109 if ( !stricmp ( Right(file,2).c_str(), ".c" ) )
110 c_srcs.push_back ( file );
111 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
112 resource_files.push_back ( file );
113 }
114 const vector<Include*>& incs = data.includes;
115 for ( i = 0; i < incs.size(); i++ )
116 {
117 // explicitly omit win32api directories
118 if ( !strncmp(incs[i]->directory.c_str(), "w32api", 6 ) )
119 continue;
120
121 // explicitly omit include/wine directories
122 if ( !strncmp(incs[i]->directory.c_str(), "include\\wine", 12 ) )
123 continue;
124
125 string path = Path::RelativeFromDirectory (
126 incs[i]->directory,
127 module.GetBasePath() );
128 includes.push_back ( path );
129 }
130 const vector<Library*>& libs = data.libraries;
131 for ( i = 0; i < libs.size(); i++ )
132 {
133 libraries.push_back ( libs[i]->name + ".lib" );
134 }
135 const vector<Define*>& defs = data.defines;
136 for ( i = 0; i < defs.size(); i++ )
137 {
138 if ( defs[i]->value[0] )
139 defines.push_back ( defs[i]->name + "=" + defs[i]->value );
140 else
141 defines.push_back ( defs[i]->name );
142 }
143 for ( i = 0; i < data.properties.size(); i++ )
144 {
145 Property& prop = *data.properties[i];
146 if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
147 baseaddr = prop.value;
148 }
149 }
150
151 vector<string> header_files;
152
153 bool no_cpp = true;
154 bool no_msvc_headers = true;
155
156 std::vector<std::string> cfgs;
157
158 cfgs.push_back ( "Debug" );
159 cfgs.push_back ( "Release" );
160 cfgs.push_back ( "Speed" );
161
162 if (!no_cpp)
163 {
164 std::vector<std::string> _cfgs;
165 for ( i = 0; i < cfgs.size(); i++ )
166 {
167 _cfgs.push_back ( cfgs[i] + " C" );
168 _cfgs.push_back ( cfgs[i] + " C++" );
169 }
170 cfgs.resize(0);
171 cfgs = _cfgs;
172 }
173
174 if (!no_msvc_headers)
175 {
176 std::vector<std::string> _cfgs;
177 for ( i = 0; i < cfgs.size(); i++ )
178 {
179 _cfgs.push_back ( cfgs[i] + " MSVC Headers" );
180 _cfgs.push_back ( cfgs[i] + " Wine Headers" );
181 }
182 cfgs.resize(0);
183 cfgs = _cfgs;
184 }
185
186 string default_cfg = cfgs.back();
187 string include_string;
188
189 fprintf ( OUT, "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\r\n" );
190 fprintf ( OUT, "<VisualStudioProject\r\n" );
191 fprintf ( OUT, "\tProjectType=\"Visual C++\"\r\n" );
192
193 if (configuration.VSProjectVersion.empty())
194 configuration.VSProjectVersion = MS_VS_DEF_VERSION;
195
196 fprintf ( OUT, "\tVersion=\"%s\"\r\n", configuration.VSProjectVersion.c_str() );
197 fprintf ( OUT, "\tName=\"%s\"\r\n", module.name.c_str() );
198 fprintf ( OUT, "\tProjectGUID=\"%s\"\r\n", module.guid.c_str() );
199 fprintf ( OUT, "\tKeyword=\"Win32Proj\">\r\n" );
200
201 fprintf ( OUT, "\t<Platforms>\r\n" );
202 fprintf ( OUT, "\t\t<Platform\r\n" );
203 fprintf ( OUT, "\t\t\tName=\"Win32\"/>\r\n" );
204 fprintf ( OUT, "\t</Platforms>\r\n" );
205
206 fprintf ( OUT, "\t<ToolFiles>\r\n" );
207 fprintf ( OUT, "\t\t<ToolFile\r\n" );
208
209 string path = Path::RelativeFromDirectory ( ProjectNode.name, module.GetBasePath() );
210 path.erase(path.find(ProjectNode.name, 0), ProjectNode.name.size() + 1);
211
212 fprintf ( OUT, "\t\t\tRelativePath=\"%sgccasm.rules\"/>\r\n", path.c_str() );
213 fprintf ( OUT, "\t</ToolFiles>\r\n" );
214
215 int n = 0;
216
217 std::string output_dir;
218
219 fprintf ( OUT, "\t<Configurations>\r\n" );
220 for ( size_t icfg = 0; icfg < cfgs.size(); icfg++ )
221 {
222 std::string& cfg = cfgs[icfg];
223
224 bool debug = strstr ( cfg.c_str(), "Debug" );
225 bool speed = strstr ( cfg.c_str(), "Speed" );
226 bool release = (!debug && !speed );
227
228 //bool msvc_headers = ( 0 != strstr ( cfg.c_str(), "MSVC Headers" ) );
229
230 fprintf ( OUT, "\t\t<Configuration\r\n" );
231 fprintf ( OUT, "\t\t\tName=\"%s|Win32\"\r\n", cfg.c_str() );
232 fprintf ( OUT, "\t\t\tOutputDirectory=\"%s\"\r\n", cfg.c_str() );
233 fprintf ( OUT, "\t\t\tIntermediateDirectory=\"%s\"\r\n", cfg.c_str() );
234 fprintf ( OUT, "\t\t\tConfigurationType=\"%d\"\r\n", exe ? 1 : dll ? 2 : lib ? 4 : -1 );
235 fprintf ( OUT, "\t\t\tCharacterSet=\"2\">\r\n" );
236
237 fprintf ( OUT, "\t\t\t<Tool\r\n" );
238 fprintf ( OUT, "\t\t\t\tName=\"VCCLCompilerTool\"\r\n" );
239 fprintf ( OUT, "\t\t\t\tOptimization=\"%d\"\r\n", release ? 2 : 0 );
240
241 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
242 bool multiple_includes = false;
243 fprintf ( OUT, "./;" );
244 for ( i = 0; i < includes.size(); i++ )
245 {
246 const string& include = includes[i];
247 if ( strcmp ( include.c_str(), "." ) )
248 {
249 if ( multiple_includes )
250 fprintf ( OUT, ";" );
251
252 fprintf ( OUT, "%s", include.c_str() );
253 include_string += " /I " + include;
254 multiple_includes = true;
255 }
256 }
257 fprintf ( OUT, "\"\r\n " );
258
259 if ( debug )
260 {
261 defines.push_back ( "_DEBUG" );
262 }
263 else
264 {
265 defines.push_back ( "NDEBUG" );
266 }
267
268 if ( lib || exe )
269 {
270 defines.push_back ( "_LIB" );
271 }
272 else
273 {
274 defines.push_back ( "_WINDOWS" );
275 defines.push_back ( "_USRDLL" );
276 }
277
278 fprintf ( OUT, "\t\t\t\tPreprocessorDefinitions=\"" );
279 for ( i = 0; i < defines.size(); i++ )
280 {
281 if ( i > 0 )
282 fprintf ( OUT, ";" );
283
284 defines[i] = _replace_str(defines[i], "\"","&quot;");
285 fprintf ( OUT, "%s", defines[i].c_str() );
286 }
287 fprintf ( OUT, "\"\r\n" );
288
289 fprintf ( OUT, "\t\t\t\tMinimalRebuild=\"%s\"\r\n", speed ? "FALSE" : "TRUE" );
290 fprintf ( OUT, "\t\t\t\tBasicRuntimeChecks=\"%s\"\r\n", debug ? "3" : "0" );
291 fprintf ( OUT, "\t\t\t\tRuntimeLibrary=\"5\"\r\n" );
292 fprintf ( OUT, "\t\t\t\tBufferSecurityCheck=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
293 fprintf ( OUT, "\t\t\t\tEnableFunctionLevelLinking=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
294
295 if ( module.pch != NULL )
296 {
297 fprintf ( OUT, "\t\t\t\tUsePrecompiledHeader=\"2\"\r\n" );
298 string pch_path = Path::RelativeFromDirectory (
299 module.pch->file.name,
300 module.GetBasePath() );
301 fprintf ( OUT, "\t\t\t\tPrecompiledHeaderThrough=\"%s\"\r\n", pch_path.c_str() );
302 }
303 else
304 {
305 fprintf ( OUT, "\t\t\t\tUsePrecompiledHeader=\"0\"\r\n" );
306 }
307
308 fprintf ( OUT, "\t\t\t\tWholeProgramOptimization=\"%s\"\r\n", release ? "TRUE" : "FALSE");
309 if ( release )
310 {
311 fprintf ( OUT, "\t\t\t\tFavorSizeOrSpeed=\"1\"\r\n" );
312 fprintf ( OUT, "\t\t\t\tStringPooling=\"true\"\r\n" );
313 }
314
315 fprintf ( OUT, "\t\t\t\tEnablePREfast=\"%s\"\r\n", debug ? "TRUE" : "FALSE");
316 fprintf ( OUT, "\t\t\t\tDisableSpecificWarnings=\"4201;4127\"\r\n" );
317 fprintf ( OUT, "\t\t\t\tWarningLevel=\"%s\"\r\n", release ? "0" : "4" );
318 fprintf ( OUT, "\t\t\t\tDetect64BitPortabilityProblems=\"%s\"\r\n", release ? "FALSE" : "TRUE");
319 fprintf ( OUT, "\t\t\t\tDebugInformationFormat=\"%s\"/>\r\n", speed ? "0" : "4");
320
321 fprintf ( OUT, "\t\t\t<Tool\r\n" );
322 fprintf ( OUT, "\t\t\t\tName=\"VCCustomBuildTool\"/>\r\n" );
323
324 if ( lib )
325 {
326 fprintf ( OUT, "\t\t\t<Tool\r\n" );
327 fprintf ( OUT, "\t\t\t\tName=\"VCLibrarianTool\"\r\n" );
328 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.lib\"/>\r\n", module.name.c_str() );
329 }
330 else
331 {
332 fprintf ( OUT, "\t\t\t<Tool\r\n" );
333 fprintf ( OUT, "\t\t\t\tName=\"VCLinkerTool\"\r\n" );
334
335 fprintf ( OUT, "\t\t\t\tAdditionalDependencies=\"" );
336 for ( i = 0; i < libraries.size(); i++ )
337 {
338 if ( i > 0 )
339 fprintf ( OUT, " " );
340 fprintf ( OUT, "%s", libraries[i].c_str() );
341 }
342 fprintf ( OUT, "\"\r\n" );
343
344 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s%s\"\r\n", module.name.c_str(), module_type.c_str() );
345 fprintf ( OUT, "\t\t\t\tLinkIncremental=\"%d\"\r\n", debug ? 2 : 1 );
346 fprintf ( OUT, "\t\t\t\tGenerateDebugInformation=\"%s\"\r\n", speed ? "FALSE" : "TRUE" );
347
348 if ( debug )
349 fprintf ( OUT, "\t\t\t\tProgramDatabaseFile=\"$(OutDir)/%s.pdb\"\r\n", module.name.c_str() );
350
351 if ( sys )
352 {
353 fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /DRIVER /ALIGN:0x20 /SUBSYSTEM:NATIVE /SECTION:INIT,D /NODEFAULTLIB /IGNORE:4001,4037,4039,4065,4070,4078,4087,4089,4096\"\r\n" );
354 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
355 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.entrypoint == "" ? "DriverEntry" : module.entrypoint.c_str ());
356 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str ());
357 }
358 else if ( exe )
359 {
360 if ( module.type == Kernel )
361 {
362 fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /SUBSYSTEM:NATIVE /NODEFAULTLIB /SECTION:INIT,D /ALIGN:0x80\"\r\n" );
363 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
364 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"KiSystemStartup\"\r\n" );
365 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr.c_str ());
366 }
367 else if ( module.type == NativeCUI )
368 {
369 fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /SUBSYSTEM:NATIVE /NODEFAULTLIB /ALIGN:0x20\"\r\n" );
370 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
371 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"NtProcessStartup\"\r\n" );
372 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr.c_str ());
373 }
374 else if ( module.type == Win32CUI || module.type == Win32GUI )
375 {
376 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", console ? 1 : 2 );
377 }
378 }
379 else if ( dll )
380 {
381 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.entrypoint == "" ? "DllMain" : module.entrypoint.c_str ());
382 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str ());
383 }
384 fprintf ( OUT, "\t\t\t\tTargetMachine=\"%d\"/>\r\n", 1 );
385 }
386
387 fprintf ( OUT, "\t\t\t<Tool\r\n" );
388 fprintf ( OUT, "\t\t\t\tName=\"VCResourceCompilerTool\"\r\n" );
389 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
390 multiple_includes = false;
391 fprintf ( OUT, "./;" );
392 for ( i = 0; i < includes.size(); i++ )
393 {
394 const string& include = includes[i];
395 if ( strcmp ( include.c_str(), "." ) )
396 {
397 if ( multiple_includes )
398 fprintf ( OUT, ";" );
399 fprintf ( OUT, "%s", include.c_str() );
400 multiple_includes = true;
401 }
402 }
403 fprintf ( OUT, "\"/>\r\n " );
404
405 fprintf ( OUT, "\t\t\t<Tool\r\n" );
406 fprintf ( OUT, "\t\t\t\tName=\"VCMIDLTool\"/>\r\n" );
407 fprintf ( OUT, "\t\t\t<Tool\r\n" );
408 fprintf ( OUT, "\t\t\t\tName=\"VCPostBuildEventTool\"/>\r\n" );
409 fprintf ( OUT, "\t\t\t<Tool\r\n" );
410 fprintf ( OUT, "\t\t\t\tName=\"VCPreBuildEventTool\"/>\r\n" );
411 fprintf ( OUT, "\t\t\t<Tool\r\n" );
412 fprintf ( OUT, "\t\t\t\tName=\"VCPreLinkEventTool\"/>\r\n" );
413 fprintf ( OUT, "\t\t\t<Tool\r\n" );
414 fprintf ( OUT, "\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"/>\r\n" );
415 fprintf ( OUT, "\t\t\t<Tool\r\n" );
416 fprintf ( OUT, "\t\t\t\tName=\"VCWebDeploymentTool\"/>\r\n" );
417 fprintf ( OUT, "\t\t</Configuration>\r\n" );
418
419 n++;
420 }
421 fprintf ( OUT, "\t</Configurations>\r\n" );
422
423 fprintf ( OUT, "\t<Files>\r\n" );
424
425 // Source files
426 fprintf ( OUT, "\t\t<Filter\r\n" );
427 fprintf ( OUT, "\t\t\tName=\"Source Files\"\r\n" );
428 fprintf ( OUT, "\t\t\tFilter=\"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat;S\">\r\n" );
429 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
430 {
431 string source_file = DosSeparator(source_files[isrcfile]);
432 fprintf ( OUT, "\t\t\t<File\r\n" );
433 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", source_file.c_str() );
434
435 for ( size_t iconfig = 0; iconfig < cfgs.size(); iconfig++ )
436 {
437 std::string& config = cfgs[iconfig];
438
439 if (( isrcfile == 0 ) && ( module.pch != NULL ))
440 {
441 /* little hack to speed up PCH */
442 fprintf ( OUT, "\t\t\t\t<FileConfiguration\r\n" );
443 fprintf ( OUT, "\t\t\t\t\tName=\"" );
444 fprintf ( OUT, config.c_str() );
445 fprintf ( OUT, "|Win32\">\r\n" );
446 fprintf ( OUT, "\t\t\t\t\t<Tool\r\n" );
447 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCLCompilerTool\"\r\n" );
448 fprintf ( OUT, "\t\t\t\t\t\tUsePrecompiledHeader=\"1\"/>\r\n" );
449 fprintf ( OUT, "\t\t\t\t</FileConfiguration>\r\n" );
450 }
451
452 if (configuration.VSProjectVersion < "8.00") {
453 if ((source_file.find(".idl") != string::npos) || ((source_file.find(".asm") != string::npos || tolower(source_file.at(source_file.size() - 1)) == 's')))
454 {
455 fprintf ( OUT, "\t\t\t\t<FileConfiguration\r\n" );
456 fprintf ( OUT, "\t\t\t\t\tName=\"" );
457 fprintf ( OUT, config.c_str() );
458 fprintf ( OUT, "|Win32\">\r\n" );
459 fprintf ( OUT, "\t\t\t\t\t<Tool\r\n" );
460 if (source_file.find(".idl") != string::npos)
461 {
462 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );
463 fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(OutDir)\\(InputName).obj\"/>\r\n" );
464 }
465 else if ((source_file.find(".asm") != string::npos || tolower(source_file.at(source_file.size() - 1)) == 's'))
466 {
467 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );
468 fprintf ( OUT, "\t\t\t\t\t\tCommandLine=\"cl /E &quot;$(InputPath)&quot; %s /D__ASM__ | as -o &quot;$(OutDir)\\(InputName).obj&quot;\"\r\n",include_string.c_str() );
469 fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(OutDir)\\(InputName).obj\"/>\r\n" );
470 }
471 fprintf ( OUT, "\t\t\t\t</FileConfiguration>\r\n" );
472 }
473 }
474 }
475 fprintf ( OUT, "\t\t\t</File>\r\n" );
476 }
477 fprintf ( OUT, "\t\t</Filter>\r\n" );
478
479 // Header files
480 fprintf ( OUT, "\t\t<Filter\r\n" );
481 fprintf ( OUT, "\t\t\tName=\"Header Files\"\r\n" );
482 fprintf ( OUT, "\t\t\tFilter=\"h;hpp;hxx;hm;inl\">\r\n" );
483 for ( i = 0; i < header_files.size(); i++ )
484 {
485 const string& header_file = header_files[i];
486 fprintf ( OUT, "\t\t\t<File\r\n" );
487 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", header_file.c_str() );
488 fprintf ( OUT, "\t\t\t</File>\r\n" );
489 }
490 fprintf ( OUT, "\t\t</Filter>\r\n" );
491
492 // Resource files
493 fprintf ( OUT, "\t\t<Filter\r\n" );
494 fprintf ( OUT, "\t\t\tName=\"Resource Files\"\r\n" );
495 fprintf ( OUT, "\t\t\tFilter=\"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\">\r\n" );
496 for ( i = 0; i < header_files.size(); i++ )
497 {
498 const string& resource_file = resource_files[i];
499 fprintf ( OUT, "\t\t\t<File\r\n" );
500 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", resource_file.c_str() );
501 fprintf ( OUT, "\t\t\t</File>\r\n" );
502 }
503 fprintf ( OUT, "\t\t</Filter>\r\n" );
504
505 fprintf ( OUT, "\t</Files>\r\n" );
506 fprintf ( OUT, "\t<Globals>\r\n" );
507 fprintf ( OUT, "\t</Globals>\r\n" );
508 fprintf ( OUT, "</VisualStudioProject>\r\n" );
509 fclose(OUT);
510 }
511
512 std::string
513 MSVCBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
514 {
515 std::string::size_type pos = string1.find(find_str, 0);
516 int intLen = find_str.length();
517
518 while(std::string::npos != pos)
519 {
520 string1.replace(pos, intLen, replace_str);
521 pos = string1.find(find_str, intLen + pos);
522 }
523
524 return string1;
525 }
526
527 std::string
528 MSVCBackend::_get_solution_verion ( void ) {
529 string version;
530
531 if (configuration.VSProjectVersion.empty())
532 configuration.VSProjectVersion = MS_VS_DEF_VERSION;
533
534 if (configuration.VSProjectVersion == "7.00")
535 version = "7.00";
536
537 if (configuration.VSProjectVersion == "7.10")
538 version = "8.00";
539
540 if (configuration.VSProjectVersion == "8.00")
541 version = "9.00";
542
543 return version;
544 }
545
546
547 void
548 MSVCBackend::_generate_rules_file ( FILE* OUT )
549 {
550 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" );
551 fprintf ( OUT, "<VisualStudioToolFile\r\n" );
552 fprintf ( OUT, "\tName=\"GCC Assembler\"\r\n" );
553 fprintf ( OUT, "\tVersion=\"%s\"\r\n", _get_solution_verion().c_str() );
554 fprintf ( OUT, "\t>\r\n" );
555 fprintf ( OUT, "\t<Rules>\r\n" );
556 fprintf ( OUT, "\t\t<CustomBuildRule\r\n" );
557 fprintf ( OUT, "\t\t\tName=\"Assembler\"\r\n" );
558 fprintf ( OUT, "\t\t\tDisplayName=\"Assembler Files\"\r\n" );
559 fprintf ( OUT, "\t\t\tCommandLine=\"cl /E &quot;$(InputPath)&quot; | as -o &quot;$(OutDir)\\$(InputName).obj&quot;\"\r\n" );
560 fprintf ( OUT, "\t\t\tOutputs=\"$(OutDir)\\$(InputName).obj\"\r\n" );
561 fprintf ( OUT, "\t\t\tFileExtensions=\"*.S\"\r\n" );
562 fprintf ( OUT, "\t\t\tExecutionDescription=\"asm\"\r\n" );
563 fprintf ( OUT, "\t\t\t>\r\n" );
564 fprintf ( OUT, "\t\t\t<Properties>\r\n" );
565 fprintf ( OUT, "\t\t\t</Properties>\r\n" );
566 fprintf ( OUT, "\t\t</CustomBuildRule>\r\n" );
567 fprintf ( OUT, "\t</Rules>\r\n" );
568 fprintf ( OUT, "</VisualStudioToolFile>\r\n" );
569 }
570
571 void
572 MSVCBackend::_generate_sln_header ( FILE* OUT )
573 {
574 fprintf ( OUT, "Microsoft Visual Studio Solution File, Format Version %s\r\n", _get_solution_verion().c_str() );
575 fprintf ( OUT, "# Visual Studio 2005\r\n" );
576 fprintf ( OUT, "\r\n" );
577 }
578
579
580 void
581 MSVCBackend::_generate_sln_project (
582 FILE* OUT,
583 const Module& module,
584 std::string vcproj_file,
585 std::string sln_guid,
586 std::string vcproj_guid,
587 const std::vector<Dependency*>& dependencies )
588 {
589 vcproj_file = DosSeparator ( std::string(".\\") + vcproj_file );
590
591 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() );
592
593 //FIXME: only omit ProjectDependencies in VS 2005 when there are no dependencies
594 //NOTE: VS 2002 do not use ProjectSection; it uses GlobalSection instead
595 if ((configuration.VSProjectVersion == "7.10") || (dependencies.size() > 0)) {
596 fprintf ( OUT, "\tProjectSection(ProjectDependencies) = postProject\r\n" );
597 for ( size_t i = 0; i < dependencies.size(); i++ )
598 {
599 Dependency& dependency = *dependencies[i];
600 fprintf ( OUT, "\t\t%s = %s\r\n", dependency.module.guid.c_str(), dependency.module.guid.c_str() );
601 }
602 fprintf ( OUT, "\tEndProjectSection\r\n" );
603 }
604
605 fprintf ( OUT, "EndProject\r\n" );
606 }
607
608
609 void
610 MSVCBackend::_generate_sln_footer ( FILE* OUT )
611 {
612 fprintf ( OUT, "Global\r\n" );
613 fprintf ( OUT, "\tGlobalSection(SolutionConfiguration) = preSolution\r\n" );
614 fprintf ( OUT, "\t\tDebug = Debug\r\n" );
615 fprintf ( OUT, "\t\tRelease = Release\r\n" );
616 fprintf ( OUT, "\tEndGlobalSection\r\n" );
617 fprintf ( OUT, "\tGlobalSection(ProjectConfiguration) = postSolution\r\n" );
618 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
619 {
620 Module& module = *ProjectNode.modules[i];
621 std::string guid = module.guid;
622 _generate_sln_configurations ( OUT, guid.c_str() );
623 }
624 fprintf ( OUT, "\tEndGlobalSection\r\n" );
625 fprintf ( OUT, "\tGlobalSection(ExtensibilityGlobals) = postSolution\r\n" );
626 fprintf ( OUT, "\tEndGlobalSection\r\n" );
627 fprintf ( OUT, "\tGlobalSection(ExtensibilityAddIns) = postSolution\r\n" );
628 fprintf ( OUT, "\tEndGlobalSection\r\n" );
629
630 if (configuration.VSProjectVersion == "7.00") {
631 fprintf ( OUT, "\tGlobalSection(ProjectDependencies) = postSolution\r\n" );
632 //FIXME: Add dependencies for VS 2002
633 fprintf ( OUT, "\tEndGlobalSection\r\n" );
634 }
635
636 if (configuration.VSProjectVersion == "8.00") {
637 fprintf ( OUT, "\tGlobalSection(SolutionProperties) = preSolution\r\n" );
638 fprintf ( OUT, "\t\tHideSolutionNode = FALSE\r\n" );
639 fprintf ( OUT, "\tEndGlobalSection\r\n" );
640 }
641
642 fprintf ( OUT, "EndGlobal\r\n" );
643 fprintf ( OUT, "\r\n" );
644 }
645
646
647 void
648 MSVCBackend::_generate_sln_configurations ( FILE* OUT, std::string vcproj_guid )
649 {
650 fprintf ( OUT, "\t\t%s.Debug.ActiveCfg = Debug|Win32\r\n", vcproj_guid.c_str() );
651 fprintf ( OUT, "\t\t%s.Debug.Build.0 = Debug|Win32\r\n", vcproj_guid.c_str() );
652 fprintf ( OUT, "\t\t%s.Debug.Release.ActiveCfg = Release|Win32\r\n", vcproj_guid.c_str() );
653 fprintf ( OUT, "\t\t%s.Debug.Release.Build.0 = Release|Win32\r\n", vcproj_guid.c_str() );
654 }
655
656 void
657 MSVCBackend::_generate_sln ( FILE* OUT )
658 {
659 string sln_guid = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
660 vector<string> guids;
661
662 _generate_sln_header(OUT);
663 // TODO FIXME - is it necessary to sort them?
664 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
665 {
666 Module& module = *ProjectNode.modules[i];
667
668 std::string vcproj_file = VcprojFileName ( module );
669 _generate_sln_project ( OUT, module, vcproj_file, sln_guid, module.guid, module.dependencies );
670 }
671 _generate_sln_footer ( OUT );
672 }
673