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