move from branch
[reactos.git] / reactos / tools / rbuild / backend / msvc / vcprojmaker.cpp
1 /*
2 * Copyright (C) 2002 Patrik Stridvall
3 * Copyright (C) 2005 Royce Mitchell III
4 * Copyright (C) 2006 Hervé Poussineau
5 * Copyright (C) 2006 Christoph von Wittich
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifdef _MSC_VER
23 #pragma warning ( disable : 4786 )
24 #endif//_MSC_VER
25
26 #include <string>
27 #include <vector>
28 #include <set>
29
30 #include <stdio.h>
31
32 #include "msvc.h"
33
34 using std::string;
35 using std::vector;
36 using std::set;
37
38 typedef set<string> StringSet;
39
40 #ifdef OUT
41 #undef OUT
42 #endif//OUT
43
44 MSVCConfiguration::MSVCConfiguration ( const OptimizationType optimization, const HeadersType headers, const std::string &name )
45 {
46 this->optimization = optimization;
47 this->headers = headers;
48 if ( name != "" )
49 this->name = name;
50 else
51 {
52 std::string headers_name;
53 if ( headers == MSVCHeaders )
54 headers_name = "";
55 else
56 headers_name = " - Wine headers";
57 if ( optimization == Debug )
58 this->name = "Debug" + headers_name;
59 else if ( optimization == Release )
60 this->name = "Release" + headers_name;
61 else if ( optimization == Speed )
62 this->name = "Speed" + headers_name;
63 else
64 this->name = "Unknown" + headers_name;
65 }
66 }
67
68 void
69 MSVCBackend::_generate_vcproj ( const Module& module )
70 {
71 size_t i;
72
73 string vcproj_file = VcprojFileName(module);
74
75 string username = getenv ( "USERNAME" );
76 string computername = getenv ( "COMPUTERNAME" );
77 string vcproj_file_user = "";
78
79 if ((computername != "") && (username != ""))
80 vcproj_file_user = vcproj_file + "." + computername + "." + username + ".user";
81
82 printf ( "Creating MSVC.NET project: '%s'\n", vcproj_file.c_str() );
83 FILE* OUT = fopen ( vcproj_file.c_str(), "wb" );
84
85 vector<string> imports;
86 string module_type = GetExtension(module.GetTargetName());
87 bool lib = (module.type == ObjectLibrary) || (module.type == RpcClient) ||(module.type == RpcServer) || (module_type == ".lib") || (module_type == ".a");
88 bool dll = (module_type == ".dll") || (module_type == ".cpl");
89 bool exe = (module_type == ".exe") || (module_type == ".scr");
90 bool sys = (module_type == ".sys");
91
92 string path_basedir = module.GetPathToBaseDir ();
93 string intenv = Environment::GetIntermediatePath ();
94 string outenv = Environment::GetOutputPath ();
95 string outdir;
96 string intdir;
97 string vcdir;
98
99
100 if ( intenv == "obj-i386" )
101 intdir = path_basedir + "obj-i386"; /* append relative dir from project dir */
102 else
103 intdir = intenv;
104
105 if ( outenv == "output-i386" )
106 outdir = path_basedir + "output-i386";
107 else
108 outdir = outenv;
109
110 if ( configuration.UseVSVersionInPath )
111 {
112 vcdir = "\\" + _get_vc_dir();
113 }
114 // TODO FIXME - need more checks here for 'sys' and possibly 'drv'?
115
116 bool console = exe && (module.type == Win32CUI);
117 bool include_idl = false;
118
119 string vcproj_path = module.GetBasePath();
120 vector<string> source_files, resource_files, includes, includes_wine, libraries;
121 StringSet common_defines;
122 vector<const IfableData*> ifs_list;
123 ifs_list.push_back ( &module.project.non_if_data );
124 ifs_list.push_back ( &module.non_if_data );
125
126 string baseaddr;
127
128 while ( ifs_list.size() )
129 {
130 const IfableData& data = *ifs_list.back();
131 ifs_list.pop_back();
132 for ( i = 0; i < data.ifs.size(); i++ )
133 {
134 const Property* property = _lookup_property( module, data.ifs[i]->property );
135 if ( property != NULL )
136 {
137 if ( data.ifs[i]->value == property->value && data.ifs[i]->negated == false ||
138 data.ifs[i]->value != property->value && data.ifs[i]->negated)
139 ifs_list.push_back ( &data.ifs[i]->data );
140 }
141 }
142 const vector<File*>& files = data.files;
143 for ( i = 0; i < files.size(); i++ )
144 {
145 // TODO FIXME - do we want the full path of the file here?
146 string file = string(".") + &files[i]->name[vcproj_path.size()];
147
148 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
149 resource_files.push_back ( file );
150 else
151 source_files.push_back ( file );
152 }
153 const vector<Include*>& incs = data.includes;
154 for ( i = 0; i < incs.size(); i++ )
155 {
156 string path = Path::RelativeFromDirectory (
157 incs[i]->directory,
158 module.GetBasePath() );
159 if ( module.type != RpcServer && module.type != RpcClient )
160 {
161 if ( path.find ("/include/reactos/idl") != string::npos)
162 {
163 include_idl = true;
164 continue;
165 }
166 }
167 // add to another list win32api and include/wine directories
168 if ( !strncmp(incs[i]->directory.c_str(), "include\\ddk", 11 ) ||
169 !strncmp(incs[i]->directory.c_str(), "include\\crt", 11 ) ||
170 !strncmp(incs[i]->directory.c_str(), "include\\GL", 10 ) ||
171 !strncmp(incs[i]->directory.c_str(), "include\\ddk", 11 ) ||
172 !strncmp(incs[i]->directory.c_str(), "include\\psdk", 12 ) ||
173 !strncmp(incs[i]->directory.c_str(), "include\\reactos\\wine", 20 ) )
174 {
175 includes_wine.push_back ( path );
176 }
177 else
178 {
179 includes.push_back ( path );
180 }
181 }
182 const vector<Library*>& libs = data.libraries;
183 for ( i = 0; i < libs.size(); i++ )
184 {
185 string libpath = outdir + "\\" + libs[i]->importedModule->GetBasePath() + "\\" + _get_vc_dir() + "\\---\\" + libs[i]->name + ".lib";
186 libraries.push_back ( libpath );
187 }
188 const vector<Define*>& defs = data.defines;
189 for ( i = 0; i < defs.size(); i++ )
190 {
191 if ( defs[i]->value[0] )
192 common_defines.insert( defs[i]->name + "=" + defs[i]->value );
193 else
194 common_defines.insert( defs[i]->name );
195 }
196 for ( i = 0; i < data.properties.size(); i++ )
197 {
198 Property& prop = *data.properties[i];
199 if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
200 baseaddr = prop.value;
201 }
202 }
203
204 vector<string> header_files;
205
206 string include_string;
207
208 fprintf ( OUT, "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\r\n" );
209 fprintf ( OUT, "<VisualStudioProject\r\n" );
210 fprintf ( OUT, "\tProjectType=\"Visual C++\"\r\n" );
211
212 if (configuration.VSProjectVersion.empty())
213 configuration.VSProjectVersion = MS_VS_DEF_VERSION;
214
215 fprintf ( OUT, "\tVersion=\"%s\"\r\n", configuration.VSProjectVersion.c_str() );
216 fprintf ( OUT, "\tName=\"%s\"\r\n", module.name.c_str() );
217 fprintf ( OUT, "\tProjectGUID=\"%s\"\r\n", module.guid.c_str() );
218 fprintf ( OUT, "\tKeyword=\"Win32Proj\">\r\n" );
219
220 fprintf ( OUT, "\t<Platforms>\r\n" );
221 fprintf ( OUT, "\t\t<Platform\r\n" );
222 fprintf ( OUT, "\t\t\tName=\"Win32\"/>\r\n" );
223 fprintf ( OUT, "\t</Platforms>\r\n" );
224
225 //fprintf ( OUT, "\t<ToolFiles>\r\n" );
226 //fprintf ( OUT, "\t\t<ToolFile\r\n" );
227
228 //string path = Path::RelativeFromDirectory ( ProjectNode.name, module.GetBasePath() );
229 //path.erase(path.find(ProjectNode.name, 0), ProjectNode.name.size() + 1);
230
231 //fprintf ( OUT, "\t\t\tRelativePath=\"%sgccasm.rules\"/>\r\n", path.c_str() );
232 //fprintf ( OUT, "\t</ToolFiles>\r\n" );
233
234 int n = 0;
235
236 std::string output_dir;
237
238 fprintf ( OUT, "\t<Configurations>\r\n" );
239 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
240 {
241 const MSVCConfiguration& cfg = *m_configurations[icfg];
242
243 bool debug = ( cfg.optimization == Debug );
244 bool release = ( cfg.optimization == Release );
245 bool speed = ( cfg.optimization == Speed );
246
247 fprintf ( OUT, "\t\t<Configuration\r\n" );
248 fprintf ( OUT, "\t\t\tName=\"%s|Win32\"\r\n", cfg.name.c_str() );
249
250 if ( configuration.UseConfigurationInPath )
251 {
252 fprintf ( OUT, "\t\t\tOutputDirectory=\"%s\\%s%s\\%s\"\r\n", outdir.c_str (), module.GetBasePath ().c_str (), vcdir.c_str (), cfg.name.c_str() );
253 fprintf ( OUT, "\t\t\tIntermediateDirectory=\"%s\\%s%s\\%s\"\r\n", intdir.c_str (), module.GetBasePath ().c_str (), vcdir.c_str (), cfg.name.c_str() );
254 }
255 else
256 {
257 fprintf ( OUT, "\t\t\tOutputDirectory=\"%s\\%s%s\"\r\n", outdir.c_str (), module.GetBasePath ().c_str (), vcdir.c_str () );
258 fprintf ( OUT, "\t\t\tIntermediateDirectory=\"%s\\%s%s\"\r\n", intdir.c_str (), module.GetBasePath ().c_str (), vcdir.c_str () );
259 }
260
261 fprintf ( OUT, "\t\t\tConfigurationType=\"%d\"\r\n", exe ? 1 : dll ? 2 : lib ? 4 : -1 );
262 fprintf ( OUT, "\t\t\tCharacterSet=\"2\">\r\n" );
263
264 fprintf ( OUT, "\t\t\t<Tool\r\n" );
265 fprintf ( OUT, "\t\t\t\tName=\"VCCLCompilerTool\"\r\n" );
266 fprintf ( OUT, "\t\t\t\tOptimization=\"%d\"\r\n", release ? 2 : 0 );
267
268 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
269 bool multiple_includes = false;
270 fprintf ( OUT, "./;" );
271 for ( i = 0; i < includes.size(); i++ )
272 {
273 const std::string& include = includes[i];
274 if ( strcmp ( include.c_str(), "." ) )
275 {
276 if ( multiple_includes )
277 fprintf ( OUT, ";" );
278 fprintf ( OUT, "%s", include.c_str() );
279 include_string += " /I " + include;
280 multiple_includes = true;
281 }
282 }
283 if ( include_idl )
284 {
285 if ( multiple_includes )
286 fprintf ( OUT, ";" );
287
288 if ( configuration.UseConfigurationInPath )
289 {
290 fprintf ( OUT, "%s\\include\\reactos\\idl%s\\%s\r\n", intdir.c_str (), vcdir.c_str (), cfg.name.c_str() );
291 }
292 else
293 {
294 fprintf ( OUT, "%s\\include\\reactos\\idl\r\n", intdir.c_str () );
295 }
296 }
297 if ( cfg.headers == WineHeaders )
298 {
299 for ( i = 0; i < includes_wine.size(); i++ )
300 {
301 const std::string& include = includes_wine[i];
302 if ( multiple_includes )
303 fprintf ( OUT, ";" );
304 fprintf ( OUT, "%s", include.c_str() );
305 //include_string += " /I " + include;
306 multiple_includes = true;
307 }
308 }
309 fprintf ( OUT, "\"\r\n" );
310
311 StringSet defines = common_defines;
312
313 if ( debug )
314 {
315 defines.insert ( "_DEBUG" );
316 }
317
318 if ( cfg.headers == MSVCHeaders )
319 {
320 // this is a define in MinGW w32api, but not Microsoft's headers
321 defines.insert ( "STDCALL=__stdcall" );
322 }
323
324 if ( lib || exe )
325 {
326 defines.insert ( "_LIB" );
327 }
328 else
329 {
330 defines.insert ( "_WINDOWS" );
331 defines.insert ( "_USRDLL" );
332 }
333
334 fprintf ( OUT, "\t\t\t\tPreprocessorDefinitions=\"" );
335 for ( StringSet::iterator it1=defines.begin(); it1!=defines.end(); it1++ )
336 {
337 if ( i > 0 )
338 fprintf ( OUT, ";" );
339
340 string unescaped = *it1;
341 defines.erase(unescaped);
342 const string& escaped = _replace_str(unescaped, "\"","&quot;");
343
344 defines.insert(escaped);
345 fprintf ( OUT, "%s", escaped.c_str() );
346 }
347 fprintf ( OUT, "\"\r\n" );
348 fprintf ( OUT, "\t\t\t\tForcedIncludeFiles=\"%s\"\r\n", "warning.h");
349 fprintf ( OUT, "\t\t\t\tMinimalRebuild=\"%s\"\r\n", speed ? "FALSE" : "TRUE" );
350 fprintf ( OUT, "\t\t\t\tBasicRuntimeChecks=\"%s\"\r\n", sys ? 0 : (debug ? "3" : "0") );
351 fprintf ( OUT, "\t\t\t\tRuntimeLibrary=\"%d\"\r\n", debug ? 1 : 5 ); // 1=/MTd 5=/MT
352 fprintf ( OUT, "\t\t\t\tBufferSecurityCheck=\"%s\"\r\n", sys ? "FALSE" : (debug ? "TRUE" : "FALSE" ));
353 fprintf ( OUT, "\t\t\t\tEnableFunctionLevelLinking=\"%s\"\r\n", debug ? "TRUE" : "FALSE" );
354
355 if ( module.pch != NULL )
356 {
357 fprintf ( OUT, "\t\t\t\tUsePrecompiledHeader=\"2\"\r\n" );
358 string pch_path = Path::RelativeFromDirectory (
359 module.pch->file.name,
360 module.GetBasePath() );
361 string::size_type pos = pch_path.find_last_of ("/");
362 if ( pos != string::npos )
363 pch_path.erase(0, pos+1);
364 fprintf ( OUT, "\t\t\t\tPrecompiledHeaderThrough=\"%s\"\r\n", pch_path.c_str() );
365 }
366 else
367 {
368 fprintf ( OUT, "\t\t\t\tUsePrecompiledHeader=\"0\"\r\n" );
369 }
370
371 fprintf ( OUT, "\t\t\t\tWholeProgramOptimization=\"%s\"\r\n", release ? "FALSE" : "FALSE");
372 if ( release )
373 {
374 fprintf ( OUT, "\t\t\t\tFavorSizeOrSpeed=\"1\"\r\n" );
375 fprintf ( OUT, "\t\t\t\tStringPooling=\"true\"\r\n" );
376 }
377
378 fprintf ( OUT, "\t\t\t\tWarningLevel=\"%s\"\r\n", speed ? "0" : "3" );
379 fprintf ( OUT, "\t\t\t\tDetect64BitPortabilityProblems=\"%s\"\r\n", "FALSE");
380 if ( !module.cplusplus )
381 fprintf ( OUT, "\t\t\t\tCompileAs=\"1\"\r\n" );
382
383 if ( module.type == Win32CUI || module.type == Win32GUI )
384 {
385 fprintf ( OUT, "\t\t\t\tCallingConvention=\"%d\"\r\n", 0 ); // 0=__cdecl
386 }
387 else
388 {
389 fprintf ( OUT, "\t\t\t\tCallingConvention=\"%d\"\r\n", 2 ); // 2=__stdcall
390 }
391
392 fprintf ( OUT, "\t\t\t\tDebugInformationFormat=\"%s\"/>\r\n", speed ? "0" : release ? "3": "4"); // 3=/Zi 4=ZI
393
394 fprintf ( OUT, "\t\t\t<Tool\r\n" );
395 fprintf ( OUT, "\t\t\t\tName=\"VCCustomBuildTool\"/>\r\n" );
396
397 if ( lib )
398 {
399 fprintf ( OUT, "\t\t\t<Tool\r\n" );
400 fprintf ( OUT, "\t\t\t\tName=\"VCLibrarianTool\"\r\n" );
401 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.lib\"/>\r\n", module.name.c_str() );
402 }
403 else
404 {
405 fprintf ( OUT, "\t\t\t<Tool\r\n" );
406 fprintf ( OUT, "\t\t\t\tName=\"VCLinkerTool\"\r\n" );
407 if (module.GetEntryPoint(false) == "0")
408 fprintf ( OUT, "AdditionalOptions=\"/noentry\"" );
409
410 if (module.importLibrary != NULL)
411 fprintf ( OUT, "\t\t\t\tModuleDefinitionFile=\"%s\"\r\n", module.importLibrary->definition.c_str());
412 fprintf ( OUT, "\t\t\t\tAdditionalDependencies=\"" );
413 bool use_msvcrt_lib = false;
414 for ( i = 0; i < libraries.size(); i++ )
415 {
416 if ( i > 0 )
417 fprintf ( OUT, " " );
418 string libpath = libraries[i].c_str();
419 libpath = libpath.erase (0, libpath.find_last_of ("\\") + 1 );
420 if ( libpath == "msvcrt.lib" )
421 {
422 use_msvcrt_lib = true;
423 }
424 fprintf ( OUT, "%s", libpath.c_str() );
425 }
426 fprintf ( OUT, "\"\r\n" );
427
428 fprintf ( OUT, "\t\t\t\tAdditionalLibraryDirectories=\"" );
429 for (i = 0; i < libraries.size (); i++)
430 {
431 if ( i > 0 )
432 fprintf ( OUT, ";" );
433
434 string libpath = libraries[i].c_str();
435 libpath.replace (libpath.find("---"), 3, cfg.name);
436 libpath = libpath.substr (0, libpath.find_last_of ("\\") );
437 fprintf ( OUT, "%s", libpath.c_str() );
438 }
439 fprintf ( OUT, "\"\r\n" );
440
441 fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s%s\"\r\n", module.name.c_str(), module_type.c_str() );
442 fprintf ( OUT, "\t\t\t\tLinkIncremental=\"%d\"\r\n", debug ? 2 : 1 );
443 fprintf ( OUT, "\t\t\t\tGenerateDebugInformation=\"%s\"\r\n", speed ? "FALSE" : "TRUE" );
444 fprintf ( OUT, "\t\t\t\tLinkTimeCodeGeneration=\"%d\"\r\n", release? 0 : 0); // whole program optimization
445
446 if ( debug )
447 fprintf ( OUT, "\t\t\t\tProgramDatabaseFile=\"$(OutDir)/%s.pdb\"\r\n", module.name.c_str() );
448
449 if ( sys )
450 {
451 fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /ALIGN:0x20 /SECTION:INIT,D /IGNORE:4001,4037,4039,4065,4070,4078,4087,4089,4096\"\r\n" );
452 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
453 fprintf ( OUT, "\t\t\t\tGenerateManifest=\"FALSE\"\r\n" );
454 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", 3 );
455 fprintf ( OUT, "\t\t\t\tDriver=\"%d\"\r\n", 1 );
456 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.GetEntryPoint(false) == "" ? "DriverEntry" : module.GetEntryPoint(false).c_str ());
457 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str ());
458 }
459 else if ( exe )
460 {
461 if ( module.type == Kernel )
462 {
463 fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /SECTION:INIT,D /ALIGN:0x80\"\r\n" );
464 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
465 fprintf ( OUT, "\t\t\t\tGenerateManifest=\"FALSE\"\r\n" );
466 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", 3 );
467 fprintf ( OUT, "\t\t\t\tDriver=\"%d\"\r\n", 1 );
468 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"KiSystemStartup\"\r\n" );
469 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr.c_str ());
470 }
471 else if ( module.type == NativeCUI )
472 {
473 fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /ALIGN:0x20\"\r\n" );
474 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", 1 );
475 fprintf ( OUT, "\t\t\t\tGenerateManifest=\"FALSE\"\r\n" );
476 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
477 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"NtProcessStartup\"\r\n" );
478 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr.c_str ());
479 }
480 else if ( module.type == Win32CUI || module.type == Win32GUI || module.type == Win32SCR)
481 {
482 if ( use_msvcrt_lib )
483 {
484 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
485 }
486 fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", console ? 1 : 2 );
487 }
488 }
489 else if ( dll )
490 {
491 if (module.GetEntryPoint(false) == "0")
492 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"\"\r\n" );
493 else
494 {
495 // get rid of DllMain@12 because MSVC needs to link to _DllMainCRTStartup@12
496 // when using CRT
497 if (module.GetEntryPoint(false) == "DllMain@12")
498 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"\"\r\n" );
499 else
500 fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.GetEntryPoint(false).c_str ());
501 }
502 fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str ());
503 if ( use_msvcrt_lib )
504 {
505 fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );
506 }
507 }
508 fprintf ( OUT, "\t\t\t\tTargetMachine=\"%d\"/>\r\n", 1 );
509 }
510
511 fprintf ( OUT, "\t\t\t<Tool\r\n" );
512 fprintf ( OUT, "\t\t\t\tName=\"VCResourceCompilerTool\"\r\n" );
513 fprintf ( OUT, "\t\t\t\tAdditionalIncludeDirectories=\"" );
514 multiple_includes = false;
515 fprintf ( OUT, "./;" );
516 for ( i = 0; i < includes.size(); i++ )
517 {
518 const std::string& include = includes[i];
519 if ( strcmp ( include.c_str(), "." ) )
520 {
521 if ( multiple_includes )
522 fprintf ( OUT, ";" );
523 fprintf ( OUT, "%s", include.c_str() );
524 multiple_includes = true;
525 }
526 }
527 if ( cfg.headers == WineHeaders )
528 {
529 for ( i = 0; i < includes_wine.size(); i++ )
530 {
531 const std::string& include = includes_wine[i];
532 if ( multiple_includes )
533 fprintf ( OUT, ";" );
534 fprintf ( OUT, "%s", include.c_str() );
535 multiple_includes = true;
536 }
537 }
538 fprintf ( OUT, "\"/>\r\n " );
539
540 fprintf ( OUT, "\t\t\t<Tool\r\n" );
541 fprintf ( OUT, "\t\t\t\tName=\"VCMIDLTool\"/>\r\n" );
542 fprintf ( OUT, "\t\t\t<Tool\r\n" );
543 if (configuration.VSProjectVersion == "8.00")
544 {
545 fprintf ( OUT, "\t\t\t\tName=\"VCManifestTool\"\r\n" );
546 fprintf ( OUT, "\t\t\t\tEmbedManifest=\"false\"/>\r\n" );
547 }
548 fprintf ( OUT, "\t\t\t<Tool\r\n" );
549 fprintf ( OUT, "\t\t\t\tName=\"VCPostBuildEventTool\"/>\r\n" );
550 fprintf ( OUT, "\t\t\t<Tool\r\n" );
551 fprintf ( OUT, "\t\t\t\tName=\"VCPreBuildEventTool\"/>\r\n" );
552 fprintf ( OUT, "\t\t\t<Tool\r\n" );
553 fprintf ( OUT, "\t\t\t\tName=\"VCPreLinkEventTool\"/>\r\n" );
554 fprintf ( OUT, "\t\t\t<Tool\r\n" );
555 fprintf ( OUT, "\t\t\t\tName=\"VCWebServiceProxyGeneratorTool\"/>\r\n" );
556 fprintf ( OUT, "\t\t\t<Tool\r\n" );
557 fprintf ( OUT, "\t\t\t\tName=\"VCWebDeploymentTool\"/>\r\n" );
558 fprintf ( OUT, "\t\t</Configuration>\r\n" );
559
560 n++;
561 }
562 fprintf ( OUT, "\t</Configurations>\r\n" );
563
564 fprintf ( OUT, "\t<Files>\r\n" );
565
566 // Source files
567 fprintf ( OUT, "\t\t<Filter\r\n" );
568 fprintf ( OUT, "\t\t\tName=\"Source Files\"\r\n" );
569 fprintf ( OUT, "\t\t\tFilter=\"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat;S\">\r\n" );
570 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
571 {
572 string source_file = DosSeparator(source_files[isrcfile]);
573 fprintf ( OUT, "\t\t\t<File\r\n" );
574 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", source_file.c_str() );
575
576 for ( size_t iconfig = 0; iconfig < m_configurations.size(); iconfig++ )
577 {
578 const MSVCConfiguration& config = *m_configurations[iconfig];
579
580 if (( isrcfile == 0 ) && ( module.pch != NULL ))
581 {
582 /* little hack to speed up PCH */
583 fprintf ( OUT, "\t\t\t\t<FileConfiguration\r\n" );
584 fprintf ( OUT, "\t\t\t\t\tName=\"" );
585 fprintf ( OUT, config.name.c_str() );
586 fprintf ( OUT, "|Win32\">\r\n" );
587 fprintf ( OUT, "\t\t\t\t\t<Tool\r\n" );
588 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCLCompilerTool\"\r\n" );
589 fprintf ( OUT, "\t\t\t\t\t\tUsePrecompiledHeader=\"1\"/>\r\n" );
590 fprintf ( OUT, "\t\t\t\t</FileConfiguration>\r\n" );
591 }
592
593 //if (configuration.VSProjectVersion < "8.00") {
594 if ((source_file.find(".idl") != string::npos) || ((source_file.find(".asm") != string::npos || tolower(source_file.at(source_file.size() - 1)) == 's')))
595 {
596 fprintf ( OUT, "\t\t\t\t<FileConfiguration\r\n" );
597 fprintf ( OUT, "\t\t\t\t\tName=\"" );
598 fprintf ( OUT, config.name.c_str() );
599 fprintf ( OUT, "|Win32\">\r\n" );
600 fprintf ( OUT, "\t\t\t\t\t<Tool\r\n" );
601 if (source_file.find(".idl") != string::npos)
602 {
603 string src = source_file.substr (0, source_file.find(".idl"));
604
605 if ( src.find (".\\") != string::npos )
606 src.erase (0, 2);
607
608 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );
609
610 if ( module.type == RpcClient )
611 {
612 fprintf ( OUT, "\t\t\t\t\t\tCommandLine=\"midl.exe /cstub %s_c.c /header %s_c.h /server none &quot;$(InputPath)&quot; /out &quot;$(IntDir)&quot;", src.c_str (), src.c_str () );
613 fprintf ( OUT, "&#x0D;&#x0A;");
614 fprintf ( OUT, "cl.exe /Od /D &quot;WIN32&quot; /D &quot;_DEBUG&quot; /D &quot;_WINDOWS&quot; /D &quot;_WIN32_WINNT=0x502&quot; /D &quot;_UNICODE&quot; /D &quot;UNICODE&quot; /Gm /EHsc /RTC1 /MDd /Fo&quot;$(IntDir)\\%s.obj&quot; /W3 /c /Wp64 /ZI /TC &quot;$(IntDir)\\%s_c.c&quot; /nologo /errorReport:prompt", src.c_str (), src.c_str () );
615 }
616 else
617 {
618 fprintf ( OUT, "\t\t\t\t\t\tCommandLine=\"midl.exe /sstub %s_s.c /header %s_s.h /client none &quot;$(InputPath)&quot; /out &quot;$(IntDir)&quot;", src.c_str (), src.c_str () );
619 fprintf ( OUT, "&#x0D;&#x0A;");
620 fprintf ( OUT, "cl.exe /Od /D &quot;WIN32&quot; /D &quot;_DEBUG&quot; /D &quot;_WINDOWS&quot; /D &quot;_WIN32_WINNT=0x502&quot; /D &quot;_UNICODE&quot; /D &quot;UNICODE&quot; /Gm /EHsc /RTC1 /MDd /Fo&quot;$(IntDir)\\%s.obj&quot; /W3 /c /Wp64 /ZI /TC &quot;$(IntDir)\\%s_s.c&quot; /nologo /errorReport:prompt", src.c_str (), src.c_str () );
621
622 }
623 fprintf ( OUT, "&#x0D;&#x0A;");
624 fprintf ( OUT, "lib.exe /OUT:&quot;$(OutDir)\\%s.lib&quot; &quot;$(IntDir)\\%s.obj&quot;&#x0D;&#x0A;\"\r\n", module.name.c_str (), src.c_str () );
625 fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(IntDir)\\$(InputName).obj\"/>\r\n" );
626 }
627 else if ((source_file.find(".asm") != string::npos))
628 {
629 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );
630 fprintf ( OUT, "\t\t\t\t\t\tCommandLine=\"nasmw $(InputPath) -f coff -o &quot;$(OutDir)\\$(InputName).obj&quot;\"\r\n");
631 fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(OutDir)\\$(InputName).obj\"/>\r\n" );
632 }
633 else if ((tolower(source_file.at(source_file.size() - 1)) == 's'))
634 {
635 fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );
636 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() );
637 fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(OutDir)\\$(InputName).obj\"/>\r\n" );
638 }
639 fprintf ( OUT, "\t\t\t\t</FileConfiguration>\r\n" );
640 }
641 //}
642 }
643 fprintf ( OUT, "\t\t\t</File>\r\n" );
644 }
645 fprintf ( OUT, "\t\t</Filter>\r\n" );
646
647 // Header files
648 fprintf ( OUT, "\t\t<Filter\r\n" );
649 fprintf ( OUT, "\t\t\tName=\"Header Files\"\r\n" );
650 fprintf ( OUT, "\t\t\tFilter=\"h;hpp;hxx;hm;inl\">\r\n" );
651 for ( i = 0; i < header_files.size(); i++ )
652 {
653 const string& header_file = header_files[i];
654 fprintf ( OUT, "\t\t\t<File\r\n" );
655 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", header_file.c_str() );
656 fprintf ( OUT, "\t\t\t</File>\r\n" );
657 }
658 fprintf ( OUT, "\t\t</Filter>\r\n" );
659
660 // Resource files
661 fprintf ( OUT, "\t\t<Filter\r\n" );
662 fprintf ( OUT, "\t\t\tName=\"Resource Files\"\r\n" );
663 fprintf ( OUT, "\t\t\tFilter=\"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\">\r\n" );
664 for ( i = 0; i < resource_files.size(); i++ )
665 {
666 const string& resource_file = resource_files[i];
667 fprintf ( OUT, "\t\t\t<File\r\n" );
668 fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", resource_file.c_str() );
669 fprintf ( OUT, "\t\t\t</File>\r\n" );
670 }
671 fprintf ( OUT, "\t\t</Filter>\r\n" );
672
673 fprintf ( OUT, "\t</Files>\r\n" );
674 fprintf ( OUT, "\t<Globals>\r\n" );
675 fprintf ( OUT, "\t</Globals>\r\n" );
676 fprintf ( OUT, "</VisualStudioProject>\r\n" );
677 fclose ( OUT );
678
679 /* User configuration file */
680 if (vcproj_file_user != "")
681 {
682 OUT = fopen ( vcproj_file_user.c_str(), "wb" );
683 fprintf ( OUT, "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\r\n" );
684 fprintf ( OUT, "<VisualStudioUserFile\r\n" );
685 fprintf ( OUT, "\tProjectType=\"Visual C++\"\r\n" );
686 fprintf ( OUT, "\tVersion=\"%s\"\r\n", configuration.VSProjectVersion.c_str() );
687 fprintf ( OUT, "\tShowAllFiles=\"false\"\r\n" );
688 fprintf ( OUT, "\t>\r\n" );
689
690 fprintf ( OUT, "\t<Configurations>\r\n" );
691 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
692 {
693 const MSVCConfiguration& cfg = *m_configurations[icfg];
694 fprintf ( OUT, "\t\t<Configuration\r\n" );
695 fprintf ( OUT, "\t\t\tName=\"%s|Win32\"\r\n", cfg.name.c_str() );
696 fprintf ( OUT, "\t\t\t>\r\n" );
697 fprintf ( OUT, "\t\t\t<DebugSettings\r\n" );
698 if ( module_type == ".cpl" )
699 {
700 fprintf ( OUT, "\t\t\t\tCommand=\"rundll32.exe\"\r\n" );
701 fprintf ( OUT, "\t\t\t\tCommandArguments=\" shell32,Control_RunDLL &quot;$(TargetPath)&quot;,@\"\r\n" );
702 }
703 else
704 {
705 fprintf ( OUT, "\t\t\t\tCommand=\"$(TargetPath)\"\r\n" );
706 fprintf ( OUT, "\t\t\t\tCommandArguments=\"\"\r\n" );
707 }
708 fprintf ( OUT, "\t\t\t\tAttach=\"false\"\r\n" );
709 fprintf ( OUT, "\t\t\t\tDebuggerType=\"3\"\r\n" );
710 fprintf ( OUT, "\t\t\t\tRemote=\"1\"\r\n" );
711 string remote_machine = "\t\t\t\tRemoteMachine=\"" + computername + "\"\r\n";
712 fprintf ( OUT, remote_machine.c_str() );
713 fprintf ( OUT, "\t\t\t\tRemoteCommand=\"\"\r\n" );
714 fprintf ( OUT, "\t\t\t\tHttpUrl=\"\"\r\n" );
715 fprintf ( OUT, "\t\t\t\tPDBPath=\"\"\r\n" );
716 fprintf ( OUT, "\t\t\t\tSQLDebugging=\"\"\r\n" );
717 fprintf ( OUT, "\t\t\t\tEnvironment=\"\"\r\n" );
718 fprintf ( OUT, "\t\t\t\tEnvironmentMerge=\"true\"\r\n" );
719 fprintf ( OUT, "\t\t\t\tDebuggerFlavor=\"\"\r\n" );
720 fprintf ( OUT, "\t\t\t\tMPIRunCommand=\"\"\r\n" );
721 fprintf ( OUT, "\t\t\t\tMPIRunArguments=\"\"\r\n" );
722 fprintf ( OUT, "\t\t\t\tMPIRunWorkingDirectory=\"\"\r\n" );
723 fprintf ( OUT, "\t\t\t\tApplicationCommand=\"\"\r\n" );
724 fprintf ( OUT, "\t\t\t\tApplicationArguments=\"\"\r\n" );
725 fprintf ( OUT, "\t\t\t\tShimCommand=\"\"\r\n" );
726 fprintf ( OUT, "\t\t\t\tMPIAcceptMode=\"\"\r\n" );
727 fprintf ( OUT, "\t\t\t\tMPIAcceptFilter=\"\"\r\n" );
728 fprintf ( OUT, "\t\t\t/>\r\n" );
729 fprintf ( OUT, "\t\t</Configuration>\r\n" );
730 }
731 fprintf ( OUT, "\t</Configurations>\r\n" );
732 fprintf ( OUT, "</VisualStudioUserFile>\r\n" );
733 fclose ( OUT );
734 }
735
736 }
737
738 std::string
739 MSVCBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
740 {
741 std::string::size_type pos = string1.find(find_str, 0);
742 int intLen = find_str.length();
743
744 while(std::string::npos != pos)
745 {
746 string1.replace(pos, intLen, replace_str);
747 pos = string1.find(find_str, intLen + pos);
748 }
749
750 return string1;
751 }
752
753 std::string
754 MSVCBackend::_get_solution_verion ( void )
755 {
756 string version;
757
758 if (configuration.VSProjectVersion.empty())
759 configuration.VSProjectVersion = MS_VS_DEF_VERSION;
760
761 if (configuration.VSProjectVersion == "7.00")
762 version = "7.00";
763
764 if (configuration.VSProjectVersion == "7.10")
765 version = "8.00";
766
767 if (configuration.VSProjectVersion == "8.00")
768 version = "9.00";
769
770 return version;
771 }
772
773 void
774 MSVCBackend::_generate_sln_header ( FILE* OUT )
775 {
776 fprintf ( OUT, "Microsoft Visual Studio Solution File, Format Version %s\r\n", _get_solution_verion().c_str() );
777 fprintf ( OUT, "# Visual Studio 2005\r\n" );
778 fprintf ( OUT, "\r\n" );
779 }
780
781
782 void
783 MSVCBackend::_generate_sln_project (
784 FILE* OUT,
785 const Module& module,
786 std::string vcproj_file,
787 std::string sln_guid,
788 std::string vcproj_guid,
789 const std::vector<Library*>& libraries )
790 {
791 vcproj_file = DosSeparator ( std::string(".\\") + vcproj_file );
792
793 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() );
794
795 //FIXME: only omit ProjectDependencies in VS 2005 when there are no dependencies
796 //NOTE: VS 2002 do not use ProjectSection; it uses GlobalSection instead
797 if ((configuration.VSProjectVersion == "7.10") || (libraries.size() > 0)) {
798 fprintf ( OUT, "\tProjectSection(ProjectDependencies) = postProject\r\n" );
799 for ( size_t i = 0; i < libraries.size(); i++ )
800 {
801 const Module& module = *libraries[i]->importedModule;
802 fprintf ( OUT, "\t\t%s = %s\r\n", module.guid.c_str(), module.guid.c_str() );
803 }
804 fprintf ( OUT, "\tEndProjectSection\r\n" );
805 }
806
807 fprintf ( OUT, "EndProject\r\n" );
808 }
809
810
811 void
812 MSVCBackend::_generate_sln_footer ( FILE* OUT )
813 {
814 fprintf ( OUT, "Global\r\n" );
815 fprintf ( OUT, "\tGlobalSection(SolutionConfiguration) = preSolution\r\n" );
816 for ( size_t i = 0; i < m_configurations.size(); i++ )
817 fprintf ( OUT, "\t\t%s = %s\r\n", m_configurations[i]->name.c_str(), m_configurations[i]->name.c_str() );
818 fprintf ( OUT, "\tEndGlobalSection\r\n" );
819 fprintf ( OUT, "\tGlobalSection(ProjectConfiguration) = postSolution\r\n" );
820 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
821 {
822 Module& module = *ProjectNode.modules[i];
823 std::string guid = module.guid;
824 _generate_sln_configurations ( OUT, guid.c_str() );
825 }
826 fprintf ( OUT, "\tEndGlobalSection\r\n" );
827 fprintf ( OUT, "\tGlobalSection(ExtensibilityGlobals) = postSolution\r\n" );
828 fprintf ( OUT, "\tEndGlobalSection\r\n" );
829 fprintf ( OUT, "\tGlobalSection(ExtensibilityAddIns) = postSolution\r\n" );
830 fprintf ( OUT, "\tEndGlobalSection\r\n" );
831
832 if (configuration.VSProjectVersion == "7.00") {
833 fprintf ( OUT, "\tGlobalSection(ProjectDependencies) = postSolution\r\n" );
834 //FIXME: Add dependencies for VS 2002
835 fprintf ( OUT, "\tEndGlobalSection\r\n" );
836 }
837
838 if (configuration.VSProjectVersion == "8.00") {
839 fprintf ( OUT, "\tGlobalSection(SolutionProperties) = preSolution\r\n" );
840 fprintf ( OUT, "\t\tHideSolutionNode = FALSE\r\n" );
841 fprintf ( OUT, "\tEndGlobalSection\r\n" );
842 }
843
844 fprintf ( OUT, "EndGlobal\r\n" );
845 fprintf ( OUT, "\r\n" );
846 }
847
848
849 void
850 MSVCBackend::_generate_sln_configurations ( FILE* OUT, std::string vcproj_guid )
851 {
852 for ( size_t i = 0; i < m_configurations.size (); i++)
853 {
854 const MSVCConfiguration& cfg = *m_configurations[i];
855 fprintf ( OUT, "\t\t%s.%s|Win32.ActiveCfg = %s|Win32\r\n", vcproj_guid.c_str(), cfg.name.c_str(), cfg.name.c_str() );
856 fprintf ( OUT, "\t\t%s.%s|Win32.Build.0 = %s|Win32\r\n", vcproj_guid.c_str(), cfg.name.c_str(), cfg.name.c_str() );
857 }
858 }
859
860 void
861 MSVCBackend::_generate_sln ( FILE* OUT )
862 {
863 string sln_guid = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
864 vector<string> guids;
865
866 _generate_sln_header(OUT);
867 // TODO FIXME - is it necessary to sort them?
868 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
869 {
870 Module& module = *ProjectNode.modules[i];
871
872 std::string vcproj_file = VcprojFileName ( module );
873 _generate_sln_project ( OUT, module, vcproj_file, sln_guid, module.guid, module.non_if_data.libraries );
874 }
875 _generate_sln_footer ( OUT );
876 }
877
878 const Property*
879 MSVCBackend::_lookup_property ( const Module& module, const std::string& name ) const
880 {
881 /* Check local values */
882 for ( size_t i = 0; i < module.non_if_data.properties.size(); i++ )
883 {
884 const Property& property = *module.non_if_data.properties[i];
885 if ( property.name == name )
886 return &property;
887 }
888 // TODO FIXME - should we check local if-ed properties?
889 for ( size_t i = 0; i < module.project.non_if_data.properties.size(); i++ )
890 {
891 const Property& property = *module.project.non_if_data.properties[i];
892 if ( property.name == name )
893 return &property;
894 }
895 // TODO FIXME - should we check global if-ed properties?
896 return NULL;
897 }