protobuf 下载编译,拷贝,proto文件生成c# c++代码

Posted 青蛙~~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了protobuf 下载编译,拷贝,proto文件生成c# c++代码相关的知识,希望对你有一定的参考价值。

下载并编译protobuf源码

echo off & color 0A


set "WORK_DIR=%cd%"
echo %WORK_DIR%
 
 rem 这里可以替换版本号
set "PROTOBUF_VESION=v3.12.4"
echo %PROTOBUF_VESION%
set "PROTOBUF_PATH=protobuf_%PROTOBUF_VESION%"
echo %PROTOBUF_PATH%

git clone -b %PROTOBUF_VESION% git://github.com/google/protobuf.git %PROTOBUF_PATH%


cd %PROTOBUF_PATH%
git clone -b release-1.7.0 git://github.com/google/googlemock.git gmock


cd gmock
git clone -b release-1.7.0 git://github.com/google/googletest.git gtest

cd %WORK_DIR%

set VS_DEV_CMD="D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\Tools\\VsDevCmd.bat"

set "BUILD_PATH=protobuf_%PROTOBUF_VESION%_vs2019_sln"
echo %BUILD_PATH%
if not exist %BUILD_PATH% md %BUILD_PATH%
cd %BUILD_PATH%
rem 设置编译版本 Debug Or Release
set "MODE=Release"
echo %MODE%
if not exist %MODE% md %MODE%
cd %MODE%
echo %cd%

set "CMAKELISTS_DIR=%WORK_DIR%\\%PROTOBUF_PATH%\\cmake"
echo %CMAKELISTS_DIR%

rem 开始构建和编译
call %VS_DEV_CMD%
cmake %CMAKELISTS_DIR% -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=%MODE% -Dprotobuf_BUILD_TESTS=OFF
call extract_includes.bat
nmake /f Makefile

xcopy include\\* ..\\..\\include /y /e /i /q
xcopy lib* ..\\..\\lib /y /i /q

if not exist %WORK_DIR%\\bin (
	mkdir %WORK_DIR%\\bin
)

xcopy /Y /Q *.exe %WORK_DIR%\\bin


pause

proto文件生成c# c++代码

@echo off & color 0A

set CURRENT_DIR=%cd%
set PROTO_TOOLS_PATH=%CURRENT_DIR%\\Tools
set PROTO_FILES_PATH=%CURRENT_DIR%\\ProtoFiles
echo %PROTO_FILES_PATH%
set CPP_OUTPUT_PATH=%CURRENT_DIR%\\.CppOut
set TARGET_PATH=%CURRENT_DIR%\\..\\..\\GameCommon\\PacketCommon\\Proto

set CSHARP_OUTPUT_PATH=%CURRENT_DIR%\\.CsharpOut

:: 设置ProtobufSDK路径
set PROTOBUF_SDK_ROOT=%CURRENT_DIR%\\..\\..\\ThirdParty\\protobuf-3.12.4
echo %PROTOBUF_SDK_ROOT%


:: 1. 编译proto文件成cpp ,cshap文件
:CompileProto
python "%PROTO_TOOLS_PATH%/CompileProto.py" --proto_sdk "%PROTOBUF_SDK_ROOT%" --proto_path "%PROTO_FILES_PATH%" --cpp_out "%CPP_OUTPUT_PATH%" --csharp_out "%CSHARP_OUTPUT_PATH%"
if errorlevel 1 goto CompileProtoError
goto CopyToDest

:CompileProtoError
echo Error: Compile proto failed!
pause
exit /B 1

:: 4. 将生成的pb.h和pb.cc拷贝到目标目录
:CopyToDest
REM cd "%CPP_OUTPUT_PATH%"
REM if exist "%TARGET_PATH%" (
	REM xcopy /Y /Q *.h "%TARGET_PATH%"
	REM xcopy /Y /Q *.cc "%TARGET_PATH%"
REM )

cd "%CSHARP_OUTPUT_PATH%"
if exist "%TARGET_PATH%" (
	xcopy /Y /Q *.cs "%TARGET_PATH%"
)
cd "%CURRENT_DIR%"

echo Done
pause

exit /B 0


对应的py源码

#-*- coding: utf-8 -*-
import os
import sys
import io
import subprocess
import shutil
import json
from stat import *
from argparse import ArgumentParser
		
def walk_tree_file(top_most_path, ext_filter, callback, param):
	totalCounts = 0
	for f in os.listdir(top_most_path):
		pathname = os.path.join(top_most_path, f)
		mode = os.stat(pathname)[ST_MODE]
		if S_ISREG(mode):
			fullname = os.path.realpath(pathname);
			[dirname, filename]=os.path.split(fullname)
			filename_ext = os.path.splitext(filename)[1]
			if(filename_ext.lower() != ext_filter):
				continue
			ret = callback(fullname, dirname, filename, param)
			if(ret != 0):
				return [ret, 0]
			totalCounts += 1
	return [0, totalCounts]
	
def build_cpp_proto(fullname, dirname, filename, param) :
	cmd = "\\"%s\\" --proto_path=\\"%s\\" --proto_path=. --cpp_out=\\"%s\\" \\"%s\\"" % (os.path.join(param["proto_sdk"], "bin/protoc.exe"), os.path.join(param["proto_sdk"], "include"), param["cpp_out"], filename)
	print(cmd)
	p = subprocess.Popen(cmd, cwd=dirname)
	p.wait()
	return p.returncode
    
def build_csharp_proto(fullname, dirname, filename, param) :
    cmd = "\\"%s\\" --proto_path=\\"%s\\" --proto_path=. --csharp_out=\\"%s\\" \\"%s\\"" % (os.path.join(param["proto_sdk"], "bin/protoc.exe"), os.path.join(param["proto_sdk"], "include"), param["csharp_out"], filename)
    print(cmd)
    p = subprocess.Popen(cmd, cwd=dirname)
    p.wait()
    return p.returncode


if __name__ == "__main__":
	parser = ArgumentParser(description="Compile Proto Files")
	parser.add_argument("--proto_sdk", help="where is protobuf sdk installed")
	parser.add_argument("--proto_path", help="proto directory. all *.proto files will be processed.")
	parser.add_argument("--cpp_out", help="the cpp output directory for cpp generator.")
	parser.add_argument("--csharp_out", help="the csharp output directory for csharp generator.")

	option = parser.parse_args()
	if (not(option.proto_sdk and option.proto_path and option.cpp_out and option.csharp_out)) :
		parser.print_help()
	
	#build param
	param =  
		"proto_sdk" : option.proto_sdk,
		"proto_path" : option.proto_path,
		"cpp_out" : option.cpp_out,
		"csharp_out" : option.csharp_out
		
	#make sure cpp out is exist
	if(not os.path.exists(param["cpp_out"])):
		os.mkdir(param["cpp_out"])
		if(not os.path.exists(param["cpp_out"])):
			print("Error, create '%s' failed" % param["cpp_out"])
			exit(1)
            
    #make sure csharp out is exist
	if(not os.path.exists(param["csharp_out"])):
		os.mkdir(param["csharp_out"])
		if(not os.path.exists(param["csharp_out"])):
			print("Error, create '%s' failed" % param["csharp_out"])
			exit(1)
            
	[ret, totalCounts] = walk_tree_file(param["proto_path"], ".proto", build_cpp_proto, param)
	if(ret!=0) :
		print("Error: Build cpp_proto failed!")
		exit(ret)
	print("Build %d cpp_proto file(s)"% totalCounts)

	[ret, totalCounts] = walk_tree_file(param["proto_path"], ".proto", build_csharp_proto, param)
	if(ret!=0) :
		print("Error: Build csharp_proto failed!")
		exit(ret)
	print("Build %d csharp_proto file(s)"% totalCounts)
	
	

编译处理工具指导

消息协议处理工具


这个目录保存协议文件.proto以及相应的处理工具

所需环境

  • 需要安装Python3.x版本来运行编译脚本,请到Python官网 下载安装,安装之后确认python --version命令能正确执行

编译proto 对应的c# c++指导方法

直接运行此目录下的BuildProto.bat编译proto文件,这个过程会执行如下动作

  1. 检查是否安装Python3
  2. 调用proto.exe编译proto文件
  3. 在编译后的.pb.cc文件中加入自定义代码
  4. 将编译后的.pb.h.pb.cc拷贝到源码目标目录

编译protobuf源代码指导方法

protobuf源码编译


这个目录用来下载和编译protobuf源码生成protoc.exe,lib

所需环境

  • 需要安装git cmake 并配置好各自环境变量 并支持nmake编译代码

改变protobuf版本号

set “PROTOBUF_VESION=v3.12.4” 改变3.12.4这个数字就可以编译对应的protobuf版本

编译方法

直接运行此目录下的down_and_build_protobuf.bat之后会自动从github下载你配置的protobuf版本源码并编译,这个过程会执行如下动作

  1. git clone protobuf源码 gmock源码 gtest源码
  2. 用cmake加nmake编译源码
  3. 将生成的lib,exe,include 拷贝到相应的目标目录

以上是关于protobuf 下载编译,拷贝,proto文件生成c# c++代码的主要内容,如果未能解决你的问题,请参考以下文章

protobuf 下载编译,拷贝,proto文件生成c# c++代码

protobuf在linux下载编译和使用

protobuf在linux下载编译和使用

protobuf在linux下载编译和使用

Protobuf 编译工具转换 Java 类

使用protobuf编译onnx.proto过程中的一些问题总结