⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msvcmaker.cpp

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2002 Patrik Stridvall
 * Copyright (C) 2005 Royce Mitchell III
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif//_MSC_VER

#include <string>
#include <vector>
#include <set>

#include <stdio.h>

#include "msvc.h"

using std::string;
using std::vector;
using std::set;

typedef set<string> StringSet;

#ifdef OUT
#undef OUT
#endif//OUT

void
MSVCBackend::_generate_dsp ( const Module& module )
{
	size_t i;
	// TODO FIXME wine hack?
	const bool wine = false;

	string dsp_file = DspFileName(module);
	printf ( "Creating MSVC project: '%s'\n", dsp_file.c_str() );
	FILE* OUT = fopen ( dsp_file.c_str(), "wb" );

	vector<string> imports;
	for ( i = 0; i < module.non_if_data.libraries.size(); i++ )
	{
		imports.push_back ( module.non_if_data.libraries[i]->name );
	}

	string module_type = GetExtension(module.GetTargetName());
	bool lib = (module_type == ".lib") || (module_type == ".a");
	bool dll = (module_type == ".dll") || (module_type == ".cpl");
	bool exe = (module_type == ".exe") || (module_type == ".scr");
	// TODO FIXME - need more checks here for 'sys' and possibly 'drv'?

	bool console = exe && (module.type == Win32CUI);

	// TODO FIXME - not sure if the count here is right...
	int parts = 0;
	const char* p = strpbrk ( dsp_file.c_str(), "/\\" );
	while ( p )
	{
		++parts;
		p = strpbrk ( p+1, "/\\" );
	}
	string msvc_wine_dir = "..";
	while ( --parts )
		msvc_wine_dir += "\\..";

	string wine_include_dir = msvc_wine_dir + "\\include";

	//$progress_current++;
	//$output->progress("$dsp_file (file $progress_current of $progress_max)");

	// TODO FIXME - what's diff. betw. 'c_srcs' and 'source_files'?
	string dsp_path = module.GetBasePath();
	vector<string> c_srcs, source_files, resource_files, includes, libraries;
	StringSet common_defines;
	vector<const IfableData*> ifs_list;
	ifs_list.push_back ( &module.project.non_if_data );
	ifs_list.push_back ( &module.non_if_data );

	// this is a define in MinGW w32api, but not Microsoft's headers
	common_defines.insert ( "STDCALL=__stdcall" );

	while ( ifs_list.size() )
	{
		const IfableData& data = *ifs_list.back();
		ifs_list.pop_back();
		// TODO FIXME - refactor needed - we're discarding if conditions
		for ( i = 0; i < data.ifs.size(); i++ )
			ifs_list.push_back ( &data.ifs[i]->data );
		const vector<File*>& files = data.files;
		for ( i = 0; i < files.size(); i++ )
		{
			// TODO FIXME - do we want the full path of the file here?
			string file = string(".") + &files[i]->name[dsp_path.size()];

			source_files.push_back ( file );
			if ( !stricmp ( Right(file,2).c_str(), ".c" ) )
				c_srcs.push_back ( file );
			if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
				resource_files.push_back ( file );
		}
		const vector<Include*>& incs = data.includes;
		for ( i = 0; i < incs.size(); i++ )
		{

			// explicitly omit win32api directories
			if ( !strncmp(incs[i]->directory.c_str(), "w32api", 6 ) )
				continue;

			// explicitly omit include/wine directories
			if ( !strncmp(incs[i]->directory.c_str(), "include\\wine", 12 ) )
				continue;

			string path = Path::RelativeFromDirectory (
				incs[i]->directory,
				module.GetBasePath() );
			includes.push_back ( path );
		}
		const vector<Library*>& libs = data.libraries;
		for ( i = 0; i < libs.size(); i++ )
		{
			libraries.push_back ( libs[i]->name + ".lib" );
		}
		const vector<Define*>& defs = data.defines;
		for ( i = 0; i < defs.size(); i++ )
		{
			if ( defs[i]->value[0] )
				common_defines.insert( defs[i]->name + "=" + defs[i]->value );
			else
				common_defines.insert( defs[i]->name );
		}
	}
	// TODO FIXME - we don't include header files in our build system
	//my @header_files = @{module->{header_files}};
	vector<string> header_files;

	// TODO FIXME - wine hack?
	/*if (module.name !~ /^wine(?:_unicode|build|runtests|test)?$/ &&
		module.name !~ /^(?:gdi32)_.+?$/ &&
		Right ( module.name, 5 ) == "_test" )
	{
		source_files.push_back ( module.name + ".spec" );
		@source_files = sort(@source_files);
	}*/

	bool no_cpp = true;
	bool no_msvc_headers = true;
	// TODO FIXME - wine hack?
	/*if (module.name =~ /^wine(?:runtests|test)$/
		|| Right ( module.name, 5 ) == "_test" )
	{
		no_msvc_headers = false;
	}*/

	std::vector<std::string> cfgs;

	cfgs.push_back ( module.name + " - Win32" );

	if (!no_cpp)
	{
		std::vector<std::string> _cfgs;
		for ( i = 0; i < cfgs.size(); i++ )
		{
			_cfgs.push_back ( cfgs[i] + " C" );
			_cfgs.push_back ( cfgs[i] + " C++" );
		}
		cfgs.resize(0);
		cfgs = _cfgs;
	}

	// TODO FIXME - wine hack?
	/*if (!no_release)
	{
		std::vector<std::string> _cfgs;
		for ( i = 0; i < cfgs.size(); i++ )
		{
			_cfgs.push_back ( cfgs[i] + " Debug" );
			_cfgs.push_back ( cfgs[i] + " Release" );
		}
		cfgs.resize(0);
		cfgs = _cfgs;
	}*/

	if (!no_msvc_headers)
	{
		std::vector<std::string> _cfgs;
		for ( i = 0; i < cfgs.size(); i++ )
		{
			_cfgs.push_back ( cfgs[i] + " MSVC Headers" );
			_cfgs.push_back ( cfgs[i] + " Wine Headers" );
		}
		cfgs.resize(0);
		cfgs = _cfgs;
	}

	string default_cfg = cfgs.back();

	fprintf ( OUT, "# Microsoft Developer Studio Project File - Name=\"%s\" - Package Owner=<4>\r\n", module.name.c_str() );
	fprintf ( OUT, "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n" );
	fprintf ( OUT, "# ** DO NOT EDIT **\r\n" );
	fprintf ( OUT, "\r\n" );

	if ( lib )
	{
		fprintf ( OUT, "# TARGTYPE \"Win32 (x86) Static Library\" 0x0104\r\n" );
	}
	else if ( dll )
	{
		fprintf ( OUT, "# TARGTYPE \"Win32 (x86) Dynamic-Link Library\" 0x0102\r\n" );
	}
	else
	{
		fprintf ( OUT, "# TARGTYPE \"Win32 (x86) Console Application\" 0x0103\r\n" );
	}
	fprintf ( OUT, "\r\n" );

	fprintf ( OUT, "CFG=%s\r\n", default_cfg.c_str() );
	fprintf ( OUT, "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n" );
	fprintf ( OUT, "!MESSAGE use the Export Makefile command and run\r\n" );
	fprintf ( OUT, "!MESSAGE \r\n" );
	fprintf ( OUT, "!MESSAGE NMAKE /f \"%s.mak\".\r\n", module.name.c_str() );
	fprintf ( OUT, "!MESSAGE \r\n" );
	fprintf ( OUT, "!MESSAGE You can specify a configuration when running NMAKE\r\n" );
	fprintf ( OUT, "!MESSAGE by defining the macro CFG on the command line. For example:\r\n" );
	fprintf ( OUT, "!MESSAGE \r\n" );
	fprintf ( OUT, "!MESSAGE NMAKE /f \"%s.mak\" CFG=\"%s\"\r\n", module.name.c_str(), default_cfg.c_str() );
	fprintf ( OUT, "!MESSAGE \r\n" );
	fprintf ( OUT, "!MESSAGE Possible choices for configuration are:\r\n" );
	fprintf ( OUT, "!MESSAGE \r\n" );
	for ( i = 0; i < cfgs.size(); i++ )
	{
		const string& cfg = cfgs[i];
		if ( lib )
		{
			fprintf ( OUT, "!MESSAGE \"%s\" (based on \"Win32 (x86) Static Library\")\r\n", cfg.c_str() );
		}
		else if ( dll )
		{
			fprintf ( OUT, "!MESSAGE \"%s\" (based on \"Win32 (x86) Dynamic-Link Library\")\r\n", cfg.c_str() );
		}
		else
		{
			fprintf ( OUT, "!MESSAGE \"%s\" (based on \"Win32 (x86) Console Application\")\r\n", cfg.c_str() );
		}
	}
	fprintf ( OUT, "!MESSAGE \r\n" );
	fprintf ( OUT, "\r\n" );

	fprintf ( OUT, "# Begin Project\r\n" );
	fprintf ( OUT, "# PROP AllowPerConfigDependencies 0\r\n" );
	fprintf ( OUT, "# PROP Scc_ProjName \"\"\r\n" );
	fprintf ( OUT, "# PROP Scc_LocalPath \"\"\r\n" );
	fprintf ( OUT, "CPP=cl.exe\r\n" );
	if ( !lib && !exe ) fprintf ( OUT, "MTL=midl.exe\r\n" );
	fprintf ( OUT, "RSC=rc.exe\r\n" );

	std::string output_dir;
	for ( size_t icfg = 0; icfg < cfgs.size(); icfg++ )
	{
		std::string& cfg = cfgs[icfg];
		if ( icfg == 0 )
		{
			fprintf ( OUT, "!IF  \"$(CFG)\" == \"%s\"\r\n", cfg.c_str() );
			fprintf ( OUT, "\r\n" );
		}
		else
		{
			fprintf ( OUT, "\r\n" );
			fprintf ( OUT, "!ELSEIF  \"$(CFG)\" == \"%s\"\r\n", cfg.c_str() );
			fprintf ( OUT, "\r\n" );
		}

		bool debug = !strstr ( cfg.c_str(), "Release" );
		bool msvc_headers = ( 0 != strstr ( cfg.c_str(), "MSVC Headers" ) );

		fprintf ( OUT, "# PROP BASE Use_MFC 0\r\n" );

		if ( debug )
		{
			fprintf ( OUT, "# PROP BASE Use_Debug_Libraries 1\r\n" );
		}
		else
		{
			fprintf ( OUT, "# PROP BASE Use_Debug_Libraries 0\r\n" );
		}

		output_dir = Replace(cfg,module.name + " - ","");
		output_dir = Replace(output_dir," ","_");
		output_dir = Replace(output_dir,"C++","Cxx");
		// TODO FIXME - wine hack?
		//if ( output_prefix_dir.size() )
		//	output_dir = output_prefix_dir + "\\" + output_dir;

		fprintf ( OUT, "# PROP BASE Output_Dir \"%s\"\r\n", output_dir.c_str() );
		fprintf ( OUT, "# PROP BASE Intermediate_Dir \"%s\"\r\n", output_dir.c_str() );

		fprintf ( OUT, "# PROP BASE Target_Dir \"\"\r\n" );

		fprintf ( OUT, "# PROP Use_MFC 0\r\n" );
		if ( debug )
		{
			fprintf ( OUT, "# PROP Use_Debug_Libraries 1\r\n" );
		}
		else
		{
			fprintf ( OUT, "# PROP Use_Debug_Libraries 0\r\n" );
		}
		fprintf ( OUT, "# PROP Output_Dir \"%s\"\r\n", output_dir.c_str() );
		fprintf ( OUT, "# PROP Intermediate_Dir \"%s\"\r\n", output_dir.c_str() );

		if ( dll ) fprintf ( OUT, "# PROP Ignore_Export_Lib 0\r\n" );
		fprintf ( OUT, "# PROP Target_Dir \"\"\r\n" );

		StringSet defines = common_defines;

		if ( debug )
		{
			defines.insert ( "_DEBUG" );
			if ( lib || exe )
			{
				fprintf ( OUT, "# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od" );
				defines.insert ( "_LIB" );
			}
			else
			{
				fprintf ( OUT, "# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /Zi /Od" );
				defines.insert ( "_WINDOWS" );
				defines.insert ( "_USRDLL" );
				// TODO FIXME - wine hack?
				//defines.insert ( string("\U") + module.name + "\E_EXPORTS" );
			}
		}
		else
		{
			defines.insert ( "NDEBUG" );
			if ( lib || exe )
			{
				fprintf ( OUT, "# ADD BASE CPP /nologo /W3 /GX /O2" );
				defines.insert ( "_LIB" );
			}
			else
			{
				fprintf ( OUT, "# ADD BASE CPP /nologo /MT /W3 /GX /O2" );
				defines.insert ( "_WINDOWS" );
				defines.insert ( "_USRDLL" );
				// TODO FIXME - wine hack?
				//defines.insert ( string("\U") + module.name + "\E_EXPORTS" );
			}
		}

		for ( StringSet::const_iterator it1=defines.begin(); it1!=defines.end(); it1++ )
		{
			fprintf ( OUT, " /D \"%s\"", it1->c_str() );
		}
		if ( lib || exe ) fprintf ( OUT, " /YX" );
		fprintf ( OUT, " /FD" );
		if ( debug )
		{
			fprintf ( OUT, " /GZ" );
			if ( lib || exe ) fprintf ( OUT, " " );
		}
		fprintf ( OUT, " /c" );
		fprintf ( OUT, "\r\n" );

		if ( debug )
		{
			defines.insert ( "_DEBUG" );
			if(lib)
			{
				fprintf ( OUT, "# ADD CPP /nologo /MTd /W3 /Gm /GX /Zi /Od" );
				defines.insert ( "_LIB" );
			}
			else
			{
				fprintf ( OUT, "# ADD CPP /nologo /MTd /W3 /Gm /GX /Zi /Od" );
				defines.insert ( "_USRDLL" );
			}
		}
		else
		{
			defines.insert ( "NDEBUG" );
			if(lib)
			{
				fprintf ( OUT, "# ADD CPP /nologo /MT /W3 /GX /O2" );
				defines.insert ( "_LIB" );
			}
			else
			{
				fprintf ( OUT, "# ADD CPP /nologo /MT /W3 /GX /O2" );
				defines.insert ( "_USRDLL" );
			}
		}

		// TODO FIXME - wine hack?
		if ( wine )
		{
			// TODO FIXME - wine hack?
			//defines.insert ( string("_\U") + module.name + "\E_" );
			// TODO FIXME - wine hack?
			/*if ( module.name !~ /^(?:wine(?:build|test)|.*?_test)$/ )
				defines.insert ( "__WINESRC__" );*/
			if ( msvc_headers )
				defines.insert ( "__WINE_USE_NATIVE_HEADERS" );
			string output_dir2 = Replace(output_dir,"\\","\\\\");
			defines.insert ( ssprintf("__WINETEST_OUTPUT_DIR=\\\"%s\\\"",output_dir.c_str()) );
			defines.insert ( "__i386__" );
			defines.insert ( "_X86_" );

			// TODO FIXME - wine hacks?
			/*if(module.name =~ /^gdi32_(?:enhmfdrv|mfdrv)$/) {
				push @includes, ".." );
			}

			if ( strstr ( module.name.c_str(), "_test" )
			{
				include.push_back ( msvc_wine_dir + "\\" + output_dir );
			}

			if (!msvc_headers || module.name == "winetest")
			{
				includes.push_back ( wine_include_dir );
			}*/
		}

		//if ( wine )
		{
			for ( i = 0; i < includes.size(); i++ )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -