1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "visualstudio.h"
24 #include "config.h"
25 
26 #include <algorithm>
27 #include <fstream>
28 
29 namespace CreateProjectTool {
30 
31 //////////////////////////////////////////////////////////////////////////
32 // Visual Studio Provider (Visual Studio 2008)
33 //////////////////////////////////////////////////////////////////////////
34 
VisualStudioProvider(StringList & global_warnings,std::map<std::string,StringList> & project_warnings,const int version,const MSVCVersion & msvc)35 VisualStudioProvider::VisualStudioProvider(StringList &global_warnings, std::map<std::string, StringList> &project_warnings, const int version, const MSVCVersion &msvc)
36 	: MSVCProvider(global_warnings, project_warnings, version, msvc) {
37 
38 	_archs.push_back(ARCH_X86);
39 	_archs.push_back(ARCH_AMD64);
40 }
41 
getProjectExtension()42 const char *VisualStudioProvider::getProjectExtension() {
43 	return ".vcproj";
44 }
45 
getPropertiesExtension()46 const char *VisualStudioProvider::getPropertiesExtension() {
47 	return ".vsprops";
48 }
49 
createProjectFile(const std::string & name,const std::string & uuid,const BuildSetup & setup,const std::string & moduleDir,const StringList & includeList,const StringList & excludeList)50 void VisualStudioProvider::createProjectFile(const std::string &name, const std::string &uuid, const BuildSetup &setup, const std::string &moduleDir,
51 											 const StringList &includeList, const StringList &excludeList) {
52 	const std::string projectFile = setup.outputDir + '/' + name + getProjectExtension();
53 	std::ofstream project(projectFile.c_str());
54 	if (!project || !project.is_open()) {
55 		error("Could not open \"" + projectFile + "\" for writing");
56 		return;
57 	}
58 
59 	project << "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"
60 	        << "<VisualStudioProject\n"
61 	        << "\tProjectType=\"Visual C++\"\n"
62 	        << "\tVersion=\"" << _version << ".00\"\n"
63 	        << "\tName=\"" << name << "\"\n"
64 	        << "\tProjectGUID=\"{" << uuid << "}\"\n"
65 	        << "\tRootNamespace=\"" << name << "\"\n"
66 	        << "\tKeyword=\"Win32Proj\"\n";
67 
68 	project << "\tTargetFrameworkVersion=\"131072\"\n";
69 
70 	project << "\t>\n"
71 	           "\t<Platforms>\n";
72 	for (std::list<MSVC_Architecture>::const_iterator arch = _archs.begin(); arch != _archs.end(); ++arch) {
73 		project << "\t\t<Platform Name=\"" << getMSVCConfigName(*arch) << "\" />\n";
74 	}
75 	project << "\t</Platforms>\n"
76 	        << "\t<Configurations>\n";
77 
78 	// Check for project-specific warnings:
79 	std::map<std::string, std::list<std::string> >::iterator warningsIterator = _projectWarnings.find(name);
80 
81 	if (setup.devTools || setup.tests || name == setup.projectName) {
82 		for (std::list<MSVC_Architecture>::const_iterator arch = _archs.begin(); arch != _archs.end(); ++arch) {
83 			outputConfiguration(project, setup, false, "Debug", *arch);
84 			outputConfiguration(project, setup, false, "Analysis", *arch);
85 			outputConfiguration(project, setup, false, "LLVM", *arch);
86 			outputConfiguration(project, setup, true, "Release", *arch);
87 		}
88 
89 	} else {
90 		bool enableLanguageExtensions = find(_enableLanguageExtensions.begin(), _enableLanguageExtensions.end(), name) != _enableLanguageExtensions.end();
91 		bool disableEditAndContinue = find(_disableEditAndContinue.begin(), _disableEditAndContinue.end(), name) != _disableEditAndContinue.end();
92 
93 		std::string warnings = "";
94 		if (warningsIterator != _projectWarnings.end())
95 			for (StringList::const_iterator i = warningsIterator->second.begin(); i != warningsIterator->second.end(); ++i)
96 				warnings += *i + ';';
97 
98 		std::string toolConfig;
99 		toolConfig = (!warnings.empty() ? "DisableSpecificWarnings=\"" + warnings + "\"" : "");
100 		toolConfig += (disableEditAndContinue ? "DebugInformationFormat=\"3\" " : "");
101 		toolConfig += (enableLanguageExtensions ? "DisableLanguageExtensions=\"false\" " : "");
102 
103 		for (std::list<MSVC_Architecture>::const_iterator arch = _archs.begin(); arch != _archs.end(); ++arch) {
104 			outputConfiguration(setup, project, toolConfig, "Debug", *arch);
105 			outputConfiguration(setup, project, toolConfig, "Analysis", *arch);
106 			outputConfiguration(setup, project, toolConfig, "LLVM", *arch);
107 			outputConfiguration(setup, project, toolConfig, "Release", *arch);
108 		}
109 	}
110 
111 	project << "\t</Configurations>\n"
112 	        << "\t<Files>\n";
113 
114 	std::string modulePath;
115 	if (!moduleDir.compare(0, setup.srcDir.size(), setup.srcDir)) {
116 		modulePath = moduleDir.substr(setup.srcDir.size());
117 		if (!modulePath.empty() && modulePath.at(0) == '/')
118 			modulePath.erase(0, 1);
119 	}
120 
121 	if (!modulePath.empty())
122 		addFilesToProject(moduleDir, project, includeList, excludeList, setup.filePrefix + '/' + modulePath);
123 	else
124 		addFilesToProject(moduleDir, project, includeList, excludeList, setup.filePrefix);
125 
126 	// Output auto-generated test runner
127 	if (setup.tests) {
128 		project << "\t\t<File RelativePath=\"test_runner.cpp\" />\n";
129 	}
130 
131 	project << "\t</Files>\n"
132 	        << "</VisualStudioProject>\n";
133 }
134 
outputConfiguration(std::ostream & project,const BuildSetup & setup,bool isRelease,const std::string & config,const MSVC_Architecture arch)135 void VisualStudioProvider::outputConfiguration(std::ostream &project, const BuildSetup &setup, bool isRelease, const std::string &config, const MSVC_Architecture arch) {
136 	std::string libraries = outputLibraryDependencies(setup, isRelease);
137 
138 	project << "\t\t<Configuration Name=\"" << config << "|" << getMSVCConfigName(arch) << "\" ConfigurationType=\"1\" InheritedPropertySheets=\".\\" << setup.projectDescription << "_" << config << getMSVCArchName(arch) << ".vsprops\">\n"
139 	        << "\t\t\t<Tool\tName=\"VCCLCompilerTool\" DisableLanguageExtensions=\"false\" DebugInformationFormat=\"3\" />\n"
140 	        << "\t\t\t<Tool\tName=\"VCLinkerTool\" OutputFile=\"$(OutDir)/" << setup.projectName << ".exe\"\n"
141 	        << "\t\t\t\tAdditionalDependencies=\"" << libraries << "\"\n"
142 	        << "\t\t\t/>\n";
143 	outputBuildEvents(project, setup, arch);
144 	project << "\t\t</Configuration>\n";
145 }
146 
outputConfiguration(const BuildSetup & setup,std::ostream & project,const std::string & toolConfig,const std::string & config,const MSVC_Architecture arch)147 void VisualStudioProvider::outputConfiguration(const BuildSetup &setup, std::ostream &project, const std::string &toolConfig, const std::string &config, const MSVC_Architecture arch) {
148 	project << "\t\t<Configuration Name=\"" << config << "|" << getMSVCConfigName(arch) << "\" ConfigurationType=\"4\" InheritedPropertySheets=\".\\" << setup.projectDescription << "_" << config << getMSVCArchName(arch) << ".vsprops\">\n"
149 	        << "\t\t\t<Tool Name=\"VCCLCompilerTool\" " << toolConfig << "/>\n"
150 	        << "\t\t</Configuration>\n";
151 }
152 
outputBuildEvents(std::ostream & project,const BuildSetup & setup,const MSVC_Architecture arch)153 void VisualStudioProvider::outputBuildEvents(std::ostream &project, const BuildSetup &setup, const MSVC_Architecture arch) {
154 	if (!setup.devTools && !setup.tests && setup.runBuildEvents) {
155 		project << "\t\t\t<Tool\tName=\"VCPreBuildEventTool\"\n"
156 		        << "\t\t\t\tCommandLine=\"" << getPreBuildEvent() << "\"\n"
157 		        << "\t\t\t/>\n"
158 		        << "\t\t\t<Tool\tName=\"VCPostBuildEventTool\"\n"
159 		        << "\t\t\t\tCommandLine=\"" << getPostBuildEvent(arch, setup) << "\"\n"
160 		        << "\t\t\t/>\n";
161 	}
162 
163 	// Generate runner file before build for tests
164 	if (setup.tests) {
165 		project << "\t\t\t<Tool\tName=\"VCPreBuildEventTool\"\n"
166 		        << "\t\t\t\tCommandLine=\"" << getTestPreBuildEvent(setup) << "\"\n"
167 		        << "\t\t\t/>\n";
168 
169 		project << "\t\t\t<Tool\tName=\"VCPostBuildEventTool\"\n"
170 		        << "\t\t\t\tCommandLine=\"$(TargetPath)\" IgnoreExitCode=\"true\"\n"
171 		        << "\t\t\t/>\n";
172 	}
173 }
174 
writeReferences(const BuildSetup & setup,std::ofstream & output)175 void VisualStudioProvider::writeReferences(const BuildSetup &setup, std::ofstream &output) {
176 	output << "\tProjectSection(ProjectDependencies) = postProject\n";
177 
178 	for (UUIDMap::const_iterator i = _engineUuidMap.begin(); i != _engineUuidMap.end(); ++i) {
179 		output << "\t\t{" << i->second << "} = {" << i->second << "}\n";
180 	}
181 
182 	output << "\tEndProjectSection\n";
183 }
184 
outputGlobalPropFile(const BuildSetup & setup,std::ofstream & properties,MSVC_Architecture arch,const StringList & defines,const std::string & prefix,bool runBuildEvents)185 void VisualStudioProvider::outputGlobalPropFile(const BuildSetup &setup, std::ofstream &properties, MSVC_Architecture arch, const StringList &defines, const std::string &prefix, bool runBuildEvents) {
186 	std::string warnings;
187 	for (StringList::const_iterator i = _globalWarnings.begin(); i != _globalWarnings.end(); ++i)
188 		warnings += *i + ';';
189 
190 	std::string includeDirsList;
191 	for (StringList::const_iterator i = setup.includeDirs.begin(); i != setup.includeDirs.end(); ++i)
192 		includeDirsList += convertPathToWin(*i) + ';';
193 
194 	std::string definesList;
195 	for (StringList::const_iterator i = defines.begin(); i != defines.end(); ++i) {
196 		if (i != defines.begin())
197 			definesList += ';';
198 		definesList += *i;
199 	}
200 
201 	// Add define to include revision header
202 	if (runBuildEvents)
203 		definesList += REVISION_DEFINE ";";
204 
205 	properties << "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\n"
206 	           << "<VisualStudioPropertySheet\n"
207 	           << "\tProjectType=\"Visual C++\"\n"
208 	           << "\tVersion=\"8.00\"\n"
209 	           << "\tName=\"" << setup.projectDescription << "_Global\"\n"
210 	           << "\tOutputDirectory=\"$(ConfigurationName)" << getMSVCArchName(arch) << "\"\n"
211 	           << "\tIntermediateDirectory=\"$(ConfigurationName)" << getMSVCArchName(arch) << "/$(ProjectName)\"\n"
212 	           << "\t>\n"
213 	           << "\t<Tool\n"
214 	           << "\t\tName=\"VCCLCompilerTool\"\n"
215 	           << "\t\tDisableLanguageExtensions=\"" << (setup.devTools ? "false" : "true") << "\"\n"
216 	           << "\t\tDisableSpecificWarnings=\"" << warnings << "\"\n"
217 	           << "\t\tAdditionalIncludeDirectories=\".\\;" << prefix << ";" << prefix << "\\engines;" << includeDirsList << "$(" << LIBS_DEFINE << ")\\include;$(" << LIBS_DEFINE << ")\\include\\SDL;" << (setup.tests ? prefix + "\\test\\cxxtest;" : "") << "\"\n"
218 	           << "\t\tPreprocessorDefinitions=\"" << definesList << "\"\n"
219 	           << "\t\tExceptionHandling=\"" << ((setup.devTools || setup.tests || _version == 14) ? "1" : "0") << "\"\n";
220 
221 #if NEEDS_RTTI
222 	properties << "\t\tRuntimeTypeInfo=\"true\"\n";
223 #else
224 	properties << "\t\tRuntimeTypeInfo=\"false\"\n";
225 #endif
226 
227 	properties << "\t\tWarningLevel=\"4\"\n"
228 	           << "\t\tWarnAsError=\"false\"\n"
229 	           << "\t\tCompileAs=\"0\"\n"
230 	           << "\t\tObjectFile=\"$(IntDir)dists\\msvc\\%(RelativeDir)\"\n"
231 	           << "\t\t/>\n"
232 	           << "\t<Tool\n"
233 	           << "\t\tName=\"VCLibrarianTool\"\n"
234 	           << "\t\tIgnoreDefaultLibraryNames=\"\"\n"
235 	           << "\t/>\n"
236 	           << "\t<Tool\n"
237 	           << "\t\tName=\"VCLinkerTool\"\n"
238 	           << "\t\tIgnoreDefaultLibraryNames=\"\"\n";
239 	if (setup.featureEnabled("text-console") || setup.devTools || setup.tests) {
240 		properties << "\t\tSubSystem=\"1\"\n";
241 	} else {
242 		properties << "\t\tSubSystem=\"2\"\n";
243 	}
244 
245 	if (!setup.devTools && !setup.tests)
246 		properties << "\t\tEntryPointSymbol=\"WinMainCRTStartup\"\n";
247 
248 	std::string libraryDirsList;
249 	for (StringList::const_iterator i = setup.libraryDirs.begin(); i != setup.libraryDirs.end(); ++i)
250 		libraryDirsList += convertPathToWin(*i) + ';';
251 
252 	properties << "\t\tAdditionalLibraryDirectories=\"" << libraryDirsList << "$(" << LIBS_DEFINE << ")\\lib\\" << getMSVCArchName(arch) << "\"\n"
253 	           << "\t/>\n"
254 	           << "\t<Tool\n"
255 	           << "\t\tName=\"VCResourceCompilerTool\"\n"
256 	           << "\t\tAdditionalIncludeDirectories=\".\\;" << prefix << "\"\n"
257 	           << "\t\tPreprocessorDefinitions=\"" << definesList << "\"\n"
258 	           << "\t/>\n"
259 	           << "</VisualStudioPropertySheet>\n";
260 
261 	properties.flush();
262 }
263 
createBuildProp(const BuildSetup & setup,bool isRelease,MSVC_Architecture arch,const std::string & configuration)264 void VisualStudioProvider::createBuildProp(const BuildSetup &setup, bool isRelease, MSVC_Architecture arch, const std::string &configuration) {
265 
266 	std::ofstream properties((setup.outputDir + '/' + setup.projectDescription + "_" + configuration + getMSVCArchName(arch) + getPropertiesExtension()).c_str());
267 	if (!properties || !properties.is_open()) {
268 		error("Could not open \"" + setup.outputDir + '/' + setup.projectDescription + "_" + configuration + getMSVCArchName(arch) + getPropertiesExtension() + "\" for writing");
269 		return;
270 	}
271 
272 	properties << "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\n"
273 	           << "<VisualStudioPropertySheet\n"
274 	           << "\tProjectType=\"Visual C++\"\n"
275 	           << "\tVersion=\"8.00\"\n"
276 	           << "\tName=\"" << setup.projectDescription << "_" << configuration << getMSVCArchName(arch) << "\"\n"
277 	           << "\tInheritedPropertySheets=\".\\" << setup.projectDescription << "_Global" << getMSVCArchName(arch) << ".vsprops\"\n"
278 	           << "\t>\n"
279 	           << "\t<Tool\n"
280 	           << "\t\tName=\"VCCLCompilerTool\"\n";
281 
282 	if (isRelease) {
283 		properties << "\t\tEnableIntrinsicFunctions=\"true\"\n"
284 		           << "\t\tWholeProgramOptimization=\"true\"\n"
285 		           << "\t\tPreprocessorDefinitions=\"WIN32;RELEASE_BUILD\"\n"
286 		           << "\t\tStringPooling=\"true\"\n"
287 		           << "\t\tBufferSecurityCheck=\"false\"\n"
288 		           << "\t\tDebugInformationFormat=\"0\"\n"
289 		           << "\t\tRuntimeLibrary=\"0\"\n"
290 		           << "\t\tAdditionalOption=\"" << (configuration == "Analysis" ? "/analyze" : "") << "\"\n"
291 		           << "\t/>\n"
292 		           << "\t<Tool\n"
293 		           << "\t\tName=\"VCLinkerTool\"\n"
294 		           << "\t\tLinkIncremental=\"1\"\n"
295 		           << "\t\tGenerateManifest=\"false\"\n"
296 		           << "\t\tIgnoreDefaultLibraryNames=\"\"\n"
297 		           << "\t\tSetChecksum=\"true\"\n";
298 	} else {
299 		properties << "\t\tOptimization=\"0\"\n"
300 		           << "\t\tPreprocessorDefinitions=\"WIN32\"\n"
301 		           << "\t\tMinimalRebuild=\"true\"\n"
302 		           << "\t\tBasicRuntimeChecks=\"3\"\n"
303 		           << "\t\tRuntimeLibrary=\"1\"\n"
304 		           << "\t\tEnableFunctionLevelLinking=\"true\"\n"
305 		           << "\t\tWarnAsError=\"false\"\n"
306 		           << "\t\tDebugInformationFormat=\"" << (arch == ARCH_X86 ? "3" : "4") << "\"\n" // For x64 format "4" (Edit and continue) is not supported, thus we default to "3"
307 		           << "\t\tAdditionalOption=\"" << (configuration == "Analysis" ? "/analyze" : "") << "\"\n"
308 		           << "\t/>\n"
309 		           << "\t<Tool\n"
310 		           << "\t\tName=\"VCLinkerTool\"\n"
311 		           << "\t\tLinkIncremental=\"2\"\n"
312 		           << "\t\tGenerateManifest=\"false\"\n"
313 		           << "\t\tGenerateDebugInformation=\"true\"\n"
314 		           << "\t\tIgnoreDefaultLibraryNames=\"libcmt.lib\"\n";
315 	}
316 
317 	properties << "\t/>\n"
318 	           << "</VisualStudioPropertySheet>\n";
319 
320 	properties.flush();
321 	properties.close();
322 }
323 
writeFileListToProject(const FileNode & dir,std::ofstream & projectFile,const int indentation,const std::string & objPrefix,const std::string & filePrefix)324 void VisualStudioProvider::writeFileListToProject(const FileNode &dir, std::ofstream &projectFile, const int indentation,
325 												  const std::string &objPrefix, const std::string &filePrefix) {
326 	const std::string indentString = getIndent(indentation + 2);
327 
328 	if (indentation)
329 		projectFile << getIndent(indentation + 1) << "<Filter\tName=\"" << dir.name << "\">\n";
330 
331 	for (FileNode::NodeList::const_iterator i = dir.children.begin(); i != dir.children.end(); ++i) {
332 		const FileNode *node = *i;
333 
334 		if (!node->children.empty()) {
335 			writeFileListToProject(*node, projectFile, indentation + 1, objPrefix + node->name + '_', filePrefix + node->name + '/');
336 		} else {
337 			std::string filePath = convertPathToWin(filePrefix + node->name);
338 			if (producesObjectFile(node->name)) {
339 				std::string name, ext;
340 				splitFilename(node->name, name, ext);
341 
342 				if (ext == "asm") {
343 					std::string objFileName = "$(IntDir)\\";
344 					objFileName += objPrefix;
345 					objFileName += "$(InputName).obj";
346 
347 					const std::string toolLine = indentString + "\t\t<Tool Name=\"VCCustomBuildTool\" CommandLine=\"nasm.exe -f win32 -g -o &quot;" + objFileName + "&quot; &quot;$(InputPath)&quot;&#x0D;&#x0A;\" Outputs=\"" + objFileName + "\" />\n";
348 
349 					// NASM is not supported for x64, thus we do not need to add additional entries here :-).
350 					writeFileToProject(projectFile, filePath, ARCH_X86, indentString, toolLine);
351 				} else {
352 					projectFile << indentString << "<File RelativePath=\"" << filePath << "\" />\n";
353 				}
354 			} else {
355 				projectFile << indentString << "<File RelativePath=\"" << filePath << "\" />\n";
356 			}
357 		}
358 	}
359 
360 	if (indentation)
361 		projectFile << getIndent(indentation + 1) << "</Filter>\n";
362 }
363 
writeFileToProject(std::ofstream & projectFile,const std::string & filePath,MSVC_Architecture arch,const std::string & indentString,const std::string & toolLine)364 void VisualStudioProvider::writeFileToProject(std::ofstream &projectFile, const std::string &filePath, MSVC_Architecture arch,
365 											  const std::string &indentString, const std::string &toolLine) {
366 	projectFile << indentString << "<File RelativePath=\"" << filePath << "\">\n"
367 	            << indentString << "\t<FileConfiguration Name=\"Debug|" << getMSVCConfigName(arch) << "\">\n"
368 	            << toolLine
369 	            << indentString << "\t</FileConfiguration>\n"
370 	            << indentString << "\t<FileConfiguration Name=\"Analysis|" << getMSVCConfigName(arch) << "\">\n"
371 	            << toolLine
372 	            << indentString << "\t</FileConfiguration>\n"
373 	            << indentString << "\t<FileConfiguration Name=\"LLVM|" << getMSVCConfigName(arch) << "\">\n"
374 	            << toolLine
375 	            << indentString << "\t</FileConfiguration>\n"
376 	            << indentString << "\t<FileConfiguration Name=\"Release|" << getMSVCConfigName(arch) << "\">\n"
377 	            << toolLine
378 	            << indentString << "\t</FileConfiguration>\n"
379 	            << indentString << "</File>\n";
380 }
381 
382 } // namespace CreateProjectTool
383