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