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

📄 modulehandler.cpp

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
MingwModuleHandler::GenerateWidlCommands (
	const CompilationUnit& compilationUnit,
	const string& widlflagsMacro )
{
	if ( module.type == RpcServer )
		GenerateWidlCommandsServer ( compilationUnit,
		                             widlflagsMacro );
	else if ( module.type == RpcClient )
		GenerateWidlCommandsClient ( compilationUnit,
		                             widlflagsMacro );
	else if ( module.type == EmbeddedTypeLib )
		GenerateWidlCommandsEmbeddedTypeLib ( compilationUnit,
										widlflagsMacro );
	else // applies also for other module.types which include idl files
		GenerateWidlCommandsIdlHeader ( compilationUnit,
		                                widlflagsMacro );
}

void
MingwModuleHandler::GenerateCommands (
	const CompilationUnit& compilationUnit,
	const string& cc,
	const string& cppc,
	const string& cflagsMacro,
	const string& nasmflagsMacro,
	const string& windresflagsMacro,
	const string& widlflagsMacro )
{
	FileLocation* sourceFileLocation = compilationUnit.GetFilename ( backend->intermediateDirectory );
	string filename = sourceFileLocation->filename;
	string extension = GetExtension ( filename );
	if ( extension == ".c" || extension == ".C" )
	{
		GenerateGccCommand ( sourceFileLocation,
		                     GetCompilationUnitDependencies ( compilationUnit ),
		                     cc,
		                     cflagsMacro );
		return;
	}
	else if ( extension == ".cc" || extension == ".CC" ||
	          extension == ".cpp" || extension == ".CPP" ||
	          extension == ".cxx" || extension == ".CXX" )
	{
		GenerateGccCommand ( sourceFileLocation,
		                     GetCompilationUnitDependencies ( compilationUnit ),
		                     cppc,
		                     cflagsMacro );
		return;
	}
	else if ( extension == ".s" || extension == ".S" )
	{
		GenerateGccAssemblerCommand ( sourceFileLocation,
		                              cc,
		                              cflagsMacro );
		return;
	}
	else if ( extension == ".asm" || extension == ".ASM" )
	{
		GenerateNasmCommand ( sourceFileLocation,
		                      nasmflagsMacro );
		return;
	}
	else if ( extension == ".rc" || extension == ".RC" )
	{
		GenerateWindresCommand ( sourceFileLocation,
		                         windresflagsMacro );
		return;
	}
	else if ( extension == ".spec" || extension == ".SPEC" )
	{
		GenerateWinebuildCommands ( sourceFileLocation );
		GenerateGccCommand ( GetActualSourceFilename ( sourceFileLocation ),
		                     "",
		                     cc,
		                     cflagsMacro );
		return;
	}
	else if ( extension == ".idl" || extension == ".IDL" )
	{
		GenerateWidlCommands ( compilationUnit,
		                       widlflagsMacro );
		if ( (module.type == RpcServer) || (module.type == RpcClient) )
		{
			GenerateGccCommand ( GetActualSourceFilename ( sourceFileLocation ),
			                     GetExtraDependencies ( filename ),
		    	                 cc,
		        	             cflagsMacro );
		}
		return;
	}

	throw InvalidOperationException ( __FILE__,
	                                  __LINE__,
	                                  "Unsupported filename extension '%s' in file '%s'",
	                                  extension.c_str (),
	                                  filename.c_str () );
}

void
MingwModuleHandler::GenerateBuildMapCode ( const char *mapTarget )
{
	fprintf ( fMakefile,
	          "ifeq ($(ROS_BUILDMAP),full)\n" );

	string mapFilename = PassThruCacheDirectory (
		GetBasename ( module.GetPath () ) + ".map",
		backend->outputDirectory );
	CLEAN_FILE ( mapFilename );

	fprintf ( fMakefile,
	          "\t$(ECHO_OBJDUMP)\n" );
	fprintf ( fMakefile,
	          "\t$(Q)${objdump} -d -S %s > %s\n",
			  mapTarget ? mapTarget :  "$@",
	          mapFilename.c_str () );

	fprintf ( fMakefile,
	          "else\n" );
	fprintf ( fMakefile,
	          "ifeq ($(ROS_BUILDMAP),yes)\n" );

	fprintf ( fMakefile,
	          "\t$(ECHO_NM)\n" );
	fprintf ( fMakefile,
	          "\t$(Q)${nm} --numeric-sort %s > %s\n",
			  mapTarget ? mapTarget :  "$@",
	          mapFilename.c_str () );

	fprintf ( fMakefile,
	          "endif\n" );

	fprintf ( fMakefile,
	          "endif\n" );
}

void
MingwModuleHandler::GenerateBuildNonSymbolStrippedCode ()
{
	fprintf ( fMakefile,
	          "ifeq ($(ROS_BUILDNOSTRIP),yes)\n" );

	string filename = module.GetPath ();
	string outputFilename = PassThruCacheDirectory (
		filename,
		backend->outputDirectory );
	string nostripFilename = PassThruCacheDirectory (
		GetBasename ( filename ) + ".nostrip" + GetExtension ( filename ),
		backend->outputDirectory );
	CLEAN_FILE ( nostripFilename );

	fprintf ( fMakefile,
	          "\t$(ECHO_CP)\n" );
	fprintf ( fMakefile,
			  "\t${cp} %s %s 1>$(NUL)\n",
			  outputFilename.c_str (),
	          nostripFilename.c_str () );

	fprintf ( fMakefile,
	          "endif\n" );
}

void
MergeStringVector ( const vector<string>& input,
                    vector<string>& output )
{
	int wrap_at = 25;
	string s;
	int wrap_count = -1;
	for ( size_t i = 0; i < input.size (); i++ )
	{
		if ( input[i].size () == 0 )
			continue;
		if ( wrap_count++ == wrap_at )
		{
			output.push_back ( s );
			s = "";
			wrap_count = 0;
		}
		else if ( s.size () > 0)
			s += " ";
		s += input[i];
	}
	if ( s.length () > 0 )
		output.push_back ( s );
}

void
MingwModuleHandler::GetObjectsVector ( const IfableData& data,
                                       vector<string>& objectFiles ) const
{
	for ( size_t i = 0; i < data.compilationUnits.size (); i++ )
	{
		CompilationUnit& compilationUnit = *data.compilationUnits[i];
		objectFiles.push_back ( GetObjectFilename ( compilationUnit.GetFilename ( backend->intermediateDirectory ), NULL ) );
	}
}

void
MingwModuleHandler::GenerateCleanObjectsAsYouGoCode () const
{
	if ( backend->configuration.CleanAsYouGo )
	{
		vector<string> objectFiles;
		GetObjectsVector ( module.non_if_data,
		                   objectFiles );
		vector<string> lines;
		MergeStringVector ( objectFiles,
		                    lines );
		for ( size_t i = 0; i < lines.size (); i++ )
		{
			fprintf ( fMakefile,
			          "\t-@${rm} %s 2>$(NUL)\n",
			          lines[i].c_str () );
		}
	}
}

void
MingwModuleHandler::GenerateRunRsymCode () const
{
	fprintf ( fMakefile,
	          "\t$(ECHO_RSYM)\n" );
	fprintf ( fMakefile,
	          "\t$(Q)$(RSYM_TARGET) $@ $@\n\n" );
}

void
MingwModuleHandler::GenerateRunStripCode () const
{
	fprintf ( fMakefile,
	          "ifeq ($(ROS_LEAN_AND_MEAN),yes)\n" );
	fprintf ( fMakefile,
	          "\t$(ECHO_STRIP)\n" );
	fprintf ( fMakefile,
	          "\t${strip} -s -x -X $@\n\n" );
	fprintf ( fMakefile,
	          "endif\n" );
}

void
MingwModuleHandler::GenerateLinkerCommand (
	const string& dependencies,
	const string& linker,
	const string& linkerParameters,
	const string& objectsMacro,
	const string& libsMacro,
	const string& pefixupParameters )
{
	string target ( GetTargetMacro ( module ) );
	string target_folder ( GetDirectory ( GetTargetFilename ( module, NULL ) ) );
	string definitionFilename = GetDefinitionFilename ();

	string linkerScriptArgument;
	if ( module.linkerScript != NULL )
		linkerScriptArgument = ssprintf ( "-Wl,-T,%s", module.linkerScript->directory.c_str () );
	else
		linkerScriptArgument = "";

	fprintf ( fMakefile,
		"%s: %s %s $(RSYM_TARGET) $(PEFIXUP_TARGET) | %s\n",
		target.c_str (),
		definitionFilename.c_str (),
		dependencies.c_str (),
		target_folder.c_str () );
	fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
	string targetName ( module.GetTargetName () );

	if ( !module.IsDLL () )
	{
		fprintf ( fMakefile,
		          "\t%s %s %s -o %s %s %s %s\n",
		          linker.c_str (),
		          linkerParameters.c_str (),
		          linkerScriptArgument.c_str (),
		          target.c_str (),
		          objectsMacro.c_str (),
		          libsMacro.c_str (),
		          GetLinkerMacro ().c_str () );
	}
	else if ( module.HasImportLibrary () )
	{
		string temp_exp = ros_temp + module.name + ".temp.exp";
		CLEAN_FILE ( temp_exp );

		fprintf ( fMakefile,
		          "\t${dlltool} --dllname %s --def %s --output-exp %s %s %s\n",
		          targetName.c_str (),
		          definitionFilename.c_str (),
		          temp_exp.c_str (),
		          module.mangledSymbols ? "" : "--kill-at",
		          module.underscoreSymbols ? "--add-underscore" : "" );

		fprintf ( fMakefile,
		          "\t%s %s %s %s -o %s %s %s %s\n",
		          linker.c_str (),
		          linkerParameters.c_str (),
		          linkerScriptArgument.c_str (),
		          temp_exp.c_str (),
		          target.c_str (),
		          objectsMacro.c_str (),
		          libsMacro.c_str (),
		          GetLinkerMacro ().c_str () );

		fprintf ( fMakefile,
		          "\t$(Q)$(PEFIXUP_TARGET) %s -exports %s\n",
		          target.c_str (),
		          pefixupParameters.c_str() );

		fprintf ( fMakefile,
		          "\t-@${rm} %s 2>$(NUL)\n",
		          temp_exp.c_str () );
	}
	else
	{
		/* XXX: need to workaround binutils bug, which exports
		 * all functions in a dll if no .def file or an empty
		 * one has been provided... */
		/* See bug 1244 */
		//printf ( "%s will have all its functions exported\n",
		//         module.GetTargetName ().c_str () );
		fprintf ( fMakefile,
		          "\t%s %s %s -o %s %s %s %s\n",
		          linker.c_str (),
		          linkerParameters.c_str (),
		          linkerScriptArgument.c_str (),
		          target.c_str (),
		          objectsMacro.c_str (),
		          libsMacro.c_str (),
		          GetLinkerMacro ().c_str () );
	}

	GenerateBuildMapCode ();
	GenerateBuildNonSymbolStrippedCode ();
	GenerateRunRsymCode ();
	GenerateRunStripCode ();
	GenerateCleanObjectsAsYouGoCode ();
}

void
MingwModuleHandler::GeneratePhonyTarget() const
{
	string targetMacro ( GetTargetMacro ( module ) );
	fprintf ( fMakefile,
	          ".PHONY: %s\n\n",
	          targetMacro.c_str ());
	fprintf ( fMakefile, "%s: | %s\n",
	          targetMacro.c_str (),
	          GetDirectory ( GetTargetFilename ( module, NULL ) ).c_str () );
}

void
MingwModuleHandler::GenerateObjectFileTargets (
	const IfableData& data,
	const string& cc,
	const string& cppc,
	const string& cflagsMacro,
	const string& nasmflagsMacro,
	const string& windresflagsMacro,
	const string& widlflagsMacro )
{
	size_t i;

	const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
	for ( i = 0; i < compilationUnits.size (); i++ )
	{
		GenerateCommands ( *compilationUnits[i],
		                   cc,
		                   cppc,
		                   cflagsMacro,
		                   nasmflagsMacro,
		                   windresflagsMacro,
		                   widlflagsMacro );
		fprintf ( fMakefile,
		          "\n" );
	}

	const vector<If*>& ifs = data.ifs;
	for ( i = 0; i < ifs.size(); i++ )
	{
		GenerateObjectFileTargets ( ifs[i]->data,
		                            cc,
		                            cppc,
		                            cflagsMacro,
		                            nasmflagsMacro,
		                            windresflagsMacro,
		                            widlflagsMacro );
	}

	vector<CompilationUnit*> sourceCompilationUnits;
	GetModuleSpecificCompilationUnits ( sourceCompilationUnits );
	for ( i = 0; i < sourceCompilationUnits.size (); i++ )
	{
		GenerateCommands ( *sourceCompilationUnits[i],
		                   cc,
		                   cppc,
		                   cflagsMacro,
		                   nasmflagsMacro,
		                   windresflagsMacro,
		                   widlflagsMacro );
	}
	CleanupCompilationUnitVector ( sourceCompilationUnits );
}

void
MingwModuleHandler::GenerateObjectFileTargets (
	const string& cc,
	const string& cppc,
	const string& cflagsMacro,
	const string& nasmflagsMacro,
	const string& windresflagsMacro,
	const string& widlflagsMacro )
{
	if ( module.pch && use_pch )
	{
		const string& baseHeaderFilename = module.pch->file.name;
		const string& pchFilename = GetPrecompiledHeaderFilename ();
		CLEAN_FILE(pchFilename);
		string dependencies = baseHeaderFilename;
		/* WIDL generated headers may be used */
		vector<string> rpcDependencies;
		GetRpcHeaderDependencies ( rpcDependencies );
		dependencies += " " + v2s ( rpcDependencies, 5 );
		fprintf ( fMakefile,
		          "%s: %s\n",
		          pchFilename.c_str(),
		          dependencies.c_str() );
		fprintf ( fMakefile, "\t$(ECHO_PCH)\n" );
		fprintf ( fMakefile,
		          "\t%s -o %s %s -g %s\n\n",
		          module.cplusplus ? cppc.c_str() : cc.c_str(),
		          pchFilename.c_str(),
		          cflagsMacro.c_str(),
		          baseHeaderFilename.c_str() );
	}

	GenerateObjectFileTargets ( module.non_if_data,
	                            cc,
	                            cppc,
	                            cflagsMacro,
	                            nasmflagsMacro,
	                            windresflagsMacro,
	                            widlflagsMacro );
	fprintf ( fMakefile, "\n" );
}

string
MingwModuleHandler::GenerateArchiveTarget ( const string& ar,
                                            const string& objs_macro ) const
{
	string archiveFilename ( GetModuleArchiveFilename () );

	fprintf ( fMakefile,
	          "%s: %s | %s\n",
	          archiveFilename.c_str (),

⌨️ 快捷键说明

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