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

📄 codeblocks.cpp

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		for ( i = 0; i < defs.size(); i++ )
		{
			if ( defs[i]->value[0] )
			{
				const string& escaped = _replace_str(defs[i]->value, "\"","&quot;");
				common_defines.push_back( defs[i]->name + "=" + escaped );
				windres_defines += "-D" + defs[i]->name + "=" + escaped + " ";
			}
			else
			{
				common_defines.push_back( defs[i]->name );
				windres_defines += "-D" + defs[i]->name + " ";
			}
		}
		/*const vector<Property*>& variables = data.properties;
		for ( i = 0; i < variables.size(); i++ )
		{
			vars.push_back( variables[i]->name );
			values.push_back( variables[i]->value );
		}*/
		for ( i = 0; i < data.properties.size(); i++ )
		{
			Property& prop = *data.properties[i];
			if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
				baseaddr = prop.value;
		}
	}

	if ( !module.allowWarnings )
		compiler_flags.push_back ( "-Werror" );

	if ( module.type == StaticLibrary && module.isStartupLib )
		compiler_flags.push_back ( "-Wno-main" );


	FILE* OUT = fopen ( cbproj_file.c_str(), "wb" );

	fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
	fprintf ( OUT, "<CodeBlocks_project_file>\r\n" );
	fprintf ( OUT, "\t<FileVersion major=\"1\" minor=\"6\" />\r\n" );
	fprintf ( OUT, "\t<Project>\r\n" );
	fprintf ( OUT, "\t\t<Option title=\"%s\" />\r\n", module.name.c_str() );
	fprintf ( OUT, "\t\t<Option pch_mode=\"2\" />\r\n" );
	fprintf ( OUT, "\t\t<Option default_target=\"\" />\r\n" );
	fprintf ( OUT, "\t\t<Option compiler=\"gcc\" />\r\n" );
	fprintf ( OUT, "\t\t<Option extended_obj_names=\"1\" />\r\n" );
	fprintf ( OUT, "\t\t<Option virtualFolders=\"\" />\r\n" );
	fprintf ( OUT, "\t\t<Build>\r\n" );

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

	for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
	{
		const CBConfiguration& cfg = *m_configurations[icfg];
		fprintf ( OUT, "\t\t\t<Target title=\"%s\">\r\n", cfg.name.c_str() );

		if ( configuration.UseConfigurationInPath )
		{
			if ( module.type == StaticLibrary ||module.type == ObjectLibrary )
				fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), module.GetBasePath ().c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str());
			else
				fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), module.GetBasePath ().c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str());
			fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s%s\" />\r\n", intdir.c_str(), module.GetBasePath ().c_str (), cfg.name.c_str() );
		}
		else
		{
			if ( module.type == StaticLibrary || module.type == ObjectLibrary )
				fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), module.GetBasePath ().c_str (), module.name.c_str(), module_type.c_str() );
			else
				fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), module.GetBasePath ().c_str (), module.name.c_str(), module_type.c_str() );
			fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s\" />\r\n", intdir.c_str(), module.GetBasePath ().c_str () );
		}

		if ( lib )
		{
			fprintf ( OUT, "\t\t\t\t<Option type=\"2\" />\r\n" );
		}
		else if ( dll )		
			fprintf ( OUT, "\t\t\t\t<Option type=\"3\" />\r\n" );
		else if ( sys )
			fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
		else if ( exe )
		{
			if ( module.type == Kernel )
				fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
			else if ( module.type == NativeCUI )
				fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
			else if ( module.type == Win32CUI || module.type == Win32GUI || module.type == Win32SCR)
			{
				if ( console )
					fprintf ( OUT, "\t\t\t\t<Option type=\"1\" />\r\n" );
				else
					fprintf ( OUT, "\t\t\t\t<Option type=\"0\" />\r\n" );
			}
		}
		
		fprintf ( OUT, "\t\t\t\t<Option compiler=\"gcc\" />\r\n" );

		if ( module_type == ".cpl" )
		{
			fprintf ( OUT, "\t\t\t\t<Option parameters=\"shell32,Control_RunDLL &quot;$exe_output&quot;,@\" />\r\n" );
			fprintf ( OUT, "\t\t\t\t<Option host_application=\"rundll32.exe\" />\r\n" );
		}
		fprintf ( OUT, "\t\t\t\t<Compiler>\r\n" );
		
		bool debug = ( cfg.optimization == Debug );

		if ( debug )
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-g\" />\r\n" );

		/* compiler flags */
		for ( i = 0; i < compiler_flags.size(); i++ )
		{
			const string& cflag = compiler_flags[i];
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", cflag.c_str() );
		}

		/* defines */
		for ( i = 0; i < common_defines.size(); i++ )
		{
			const string& define = common_defines[i];
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-D%s\" />\r\n", define.c_str() );
		}
		/* includes */
		for ( i = 0; i < includes.size(); i++ )
		{
			const string& include = includes[i];
			fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
		}
		fprintf ( OUT, "\t\t\t\t</Compiler>\r\n" );

		/* includes */
		fprintf ( OUT, "\t\t\t\t<ResourceCompiler>\r\n" );
		for ( i = 0; i < includes.size(); i++ )
		{
			const string& include = includes[i];
			fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
		}
		fprintf ( OUT, "\t\t\t\t</ResourceCompiler>\r\n" );
		
		fprintf ( OUT, "\t\t\t\t<Linker>\r\n" );
		fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", project_linker_flags.c_str() );

		if ( sys )
		{
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false) == "" ? "DriverEntry@8" : module.GetEntryPoint(false).c_str ());
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str () );
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
		}
		else if ( exe )
		{
			if ( module.type == Kernel )
			{
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_KiSystemStartup\" />\r\n" );
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
			}
			else if ( module.type == NativeCUI )
			{
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_NtProcessStartup@4\" />\r\n" );
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
			}
			else
			{
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.useHostStdlib ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
				fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
			}
		}
		else if ( dll )
		{
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false).c_str () );
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str () );

			if ( module.type == Win32DLL )
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
			else if ( module.type == NativeDLL)
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
			else if ( module.type == NativeDLL)
				fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--shared\" />\r\n" );

			fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.useHostStdlib ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
			fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
		}

		fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--file-alignment,0x1000\" />\r\n" );
		fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--section-alignment,0x1000\" />\r\n" );

		if ( dll )
			fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s.temp.exp\" />\r\n", module.name.c_str() );

		/* libraries */
		for ( i = 0; i < libraries.size(); i++ )
		{
			const string& lib = libraries[i];
			fprintf ( OUT, "\t\t\t\t\t<Add library=\"%s\" />\r\n", lib.c_str() );
		}
		for ( i = 0; i < libpaths.size(); i++ )
		{
			const string& lib = libpaths[i];
			fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", lib.c_str() );
		}
		fprintf ( OUT, "\t\t\t\t</Linker>\r\n" );

		fprintf ( OUT, "\t\t\t\t<ExtraCommands>\r\n" );

		if ( module.type == StaticLibrary && module.importLibrary )
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"dlltool --dllname %s --def %s --output-lib $exe_output; %s -U\" />\r\n", module.importLibrary->dllname.c_str (), module.importLibrary->definition.c_str(), module.mangledSymbols ? "" : "--kill-at" );
		else if ( module.importLibrary != NULL )
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"dlltool --dllname %s --def %s --output-lib &quot;$(TARGET_OBJECT_DIR)lib$(TARGET_OUTPUT_BASENAME).a&quot; %s\" />\r\n", module.GetTargetName ().c_str(), module.importLibrary->definition.c_str(), module.mangledSymbols ? "" : "--kill-at" );


		for ( i = 0; i < resource_files.size(); i++ )
		{
			const string& resource_file = resource_files[i];
#ifdef WIN32
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del $(TARGET_OBJECT_DIR)\\%s.rci.tmp 2&gt;NUL\" />\r\n", resource_file.c_str() );
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del $(TARGET_OBJECT_DIR)\\%s.res.tmp 2&gt;NUL\" />\r\n", resource_file.c_str() );
#else
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm $(TARGET_OBJECT_DIR)/%s.rci.tmp 2&gt;/dev/null\" />\r\n", resource_file.c_str() );
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm $(TARGET_OBJECT_DIR)/%s.res.tmp 2&gt;/dev/null\" />\r\n", resource_file.c_str() );
#endif
		}

		if ( dll )
		{
			if (IsWineModule( module ))
				fprintf ( OUT, "\t\t\t\t\t<Add before=\"%s\\tools\\winebuild\\winebuild.exe -o %s --def -E %s.spec\" />\r\n", outdir.c_str(), module.importLibrary->definition.c_str(),  module.name.c_str());
			fprintf ( OUT, "\t\t\t\t\t<Add before=\"dlltool --dllname %s --def %s --output-exp %s.temp.exp %s\" />\r\n", module.GetTargetName ().c_str(), module.importLibrary->definition.c_str(), module.name.c_str(), module.mangledSymbols ? "" : "--kill-at" );
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"%s\\tools\\pefixup $exe_output -exports\" />\r\n", outdir.c_str() );
#ifdef WIN32
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del %s.temp.exp 2&gt;NUL\" />\r\n", module.name.c_str() );
#else
			fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm %s.temp.exp 2&gt;/dev/null\" />\r\n", module.name.c_str() );
#endif
			fprintf ( OUT, "\t\t\t\t\t<Mode after=\"always\" />\r\n" );
		}

		fprintf ( OUT, "\t\t\t\t</ExtraCommands>\r\n" );

		fprintf ( OUT, "\t\t\t</Target>\r\n" );

	}

	/* vars
	fprintf ( OUT, "\t\t\t<Environment>\r\n" );
	for ( i = 0; i < vars.size(); i++ )
	{
		const string& var = vars[i];
		const string& value = values[i];
		fprintf ( OUT, "\t\t\t\t<Variable name=\"%s\" value=\"%s\" />\r\n", var.c_str(), value.c_str()  );
	}
	fprintf ( OUT, "\t\t\t</Environment>\r\n" ); */

	fprintf ( OUT, "\t\t</Build>\r\n" );

#ifdef FORCE_CPP
	CompilerVar = "CPP"
#else
	if ( module.cplusplus )
		CompilerVar = "CPP";
	else
		CompilerVar = "CC";
#endif

	/* header files */
	for ( i = 0; i < header_files.size(); i++ )
	{
		const string& header_file = header_files[i];
		fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", header_file.c_str() );
		fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
		fprintf ( OUT, "\t\t\t<Option compile=\"0\" />\r\n" );
		fprintf ( OUT, "\t\t\t<Option link=\"0\" />\r\n" );
		for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
		{
			const CBConfiguration& cfg = *m_configurations[icfg];
			fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
		}
		fprintf ( OUT, "\t\t</Unit>\r\n" );
	}

	/* source files */
	for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
	{
		string source_file = DosSeparator(source_files[isrcfile]);
		fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", source_file.c_str() );
		fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );

		string extension = GetExtension ( source_file );
		if ( extension == ".s" || extension == ".S" )
		{
			fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"gcc -x assembler-with-cpp -c $file -o $link_objects $includes -D__ASM__ $options\" />\r\n" );
		}
		else if ( extension == ".asm" || extension == ".ASM" )		
		{
			fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"nasm -f win32 $file -o $link_objects\" />\r\n" );
		}
		else if ( extension == ".idl" || extension == ".IDL" )		
		{
			fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s\\tools\\widl\\widl.exe %s %s -h -H &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.h&quot; -c -C &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.c&quot; $file\\ngcc %s -c &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.c&quot; -o &quot;$(TARGET_OUTPUT_DIR)$file_c.o&quot;\" />\r\n", outdir.c_str(), widl_options.c_str(), windres_defines.c_str(), widl_options.c_str() );
		}
		else if ( extension == ".spec" || extension == ".SPEC" )		
		{
			fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
			fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s\\tools\\winebuild\\winebuild.exe -o $file.stubs.c --pedll $file\\n$compiler -c $options $includes $file.stubs.c -o $(TARGET_OBJECT_DIR)\\$file.o\" />\r\n", outdir.c_str() );
		}

		for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
		{
			const CBConfiguration& cfg = *m_configurations[icfg];
			fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
		}
		fprintf ( OUT, "\t\t</Unit>\r\n" );
	}

	/* resource files */
	for ( i = 0; i < resource_files.size(); i++ )
	{
		const string& resource_file = resource_files[i];
		fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", resource_file.c_str() );
		fprintf ( OUT, "\t\t\t<Option compilerVar=\"WINDRES\" />\r\n" );
		string extension = GetExtension ( resource_file );
		fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"gcc -xc -E -DRC_INVOKED $includes %s $file -o $(TARGET_OBJECT_DIR)\\$file.rci.tmp\\n%s\\tools\\wrc\\wrc.exe $includes %s $(TARGET_OBJECT_DIR)\\$file.rci.tmp $(TARGET_OBJECT_DIR)\\$file.res.tmp\\n$rescomp --output-format=coff $(TARGET_OBJECT_DIR)\\$file.res.tmp -o $resource_output\" />\r\n" , windres_defines.c_str(), outdir.c_str(),  windres_defines.c_str() );
		for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
		{
			const CBConfiguration& cfg = *m_configurations[icfg];
			fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
		}
		fprintf ( OUT, "\t\t</Unit>\r\n" );
	}

	fprintf ( OUT, "\t\t<Extensions />\r\n" );
	fprintf ( OUT, "\t</Project>\r\n" );
	fprintf ( OUT, "</CodeBlocks_project_file>\r\n" );


	fclose ( OUT );
}

CBConfiguration::CBConfiguration ( const OptimizationType optimization, const std::string &name )
{
	this->optimization = optimization;
	if ( name != "" )
		this->name = name;
	else
	{
		if ( optimization == Debug )
			this->name = "Debug";
		else if ( optimization == Release )
			this->name = "Release";
		else
			this->name = "Unknown";
	}
}

std::string
CBBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
{
	std::string::size_type pos = string1.find(find_str, 0);
	int intLen = find_str.length();

	while(std::string::npos != pos)
	{
		string1.replace(pos, intLen, replace_str);
		pos = string1.find(find_str, intLen + pos);
	}

	return string1;
}

std::string
CBBackend::GenerateProjectLinkerFlags() const
{
	std::string lflags;
	for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
	{
		LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
		if ( lflags.length () > 0 )
			lflags += " ";
		lflags += linkerFlag.flag;
	}
	return lflags;
}

void
CBBackend::MingwAddImplicitLibraries( Module &module )
{
	Library* pLibrary;

	if ( !module.isDefaultEntryPoint )
		return;

	if ( module.IsDLL () )
	{
		//pLibrary = new Library ( module, "__mingw_dllmain" );
		//module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
	}
	else
	{
		pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
		module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
	}

	pLibrary = new Library ( module, "mingw_common" );
	module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() + 1, pLibrary );

	if ( module.name != "msvcrt" )
	{
		// always link in msvcrt to get the basic routines
		pLibrary = new Library ( module, "msvcrt" );
		module.non_if_data.libraries.push_back ( pLibrary );
	}
}

const Property* 
CBBackend::_lookup_property ( const Module& module, const std::string& name ) const
{
	/* Check local values */
	for ( size_t i = 0; i < module.non_if_data.properties.size(); i++ )
	{
		const Property& property = *module.non_if_data.properties[i];
		if ( property.name == name )
			return &property;
	}
	// TODO FIXME - should we check local if-ed properties?
	for ( size_t i = 0; i < module.project.non_if_data.properties.size(); i++ )
	{
		const Property& property = *module.project.non_if_data.properties[i];
		if ( property.name == name )
			return &property;
	}
	// TODO FIXME - should we check global if-ed properties?
	return NULL;
}

bool
CBBackend::IsWineModule ( const Module& module ) const
{
	if ( module.importLibrary == NULL)
		return false;

	size_t index = module.importLibrary->definition.rfind ( ".spec.def" );
	return ( index != string::npos );
}

⌨️ 快捷键说明

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