はじめに
protobufを利用する際に、protocを取得する手段の一つとしてGoogle.Protobuf.Toolsを利用するのは割とある方法だと思います。
しかし毎回その実行バイナリのパスを忘れてしまうのでメモがてらに書き残しておきたいと思います。
パス
NuGetでインストールされたものはcsprojでは$(NuGetPackageRoot)で取得することができます。その中の以下のパスにバイナリがあります。
google.protobuf.tools/<VersionName>/tools/<PlatformName>/protoc
VersionNameについて
具体的にVersionNameに何が入るのかというと、Google.Protobuf.Toolsのバージョンです。
# 例 /Users/<UserName>/.nuget/packages/google.protobuf.tools/3.28.3/tools/<PlatformName>/protoc
<ItemGroup> <PackageReference Include="Google.Protobuf" Version="3.28.3" /> <PackageReference Include="Google.Protobuf.Tools" Version="3.28.3"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup>
PlatformNameについて
PlatformNameには以下のどれかが入ります。
linux_x64linux_x86macosx_x64windows_x64windows_x86
# 例 /Users/<UserName>/.nuget/packages/google.protobuf.tools/<VersionName>/tools/macosx_x64/protoc
ちなみにwindowsのときはprotoc.exeなので注意です。
$ tree . ├── linux_x64 │ └── protoc ├── linux_x86 │ └── protoc ├── macosx_x64 │ └── protoc ├── windows_x64 │ └── protoc.exe └── windows_x86 └── protoc.exe
具体例
/Users/user/.nuget/packages/google.protobuf.tools/3.28.3/tools/macosx_x64/protoc
csprojで取得する
csprojを経由して利用することのほうが多いのかなと思います。
<PropertyGroup> <!-- Google.Protobuf.Toolsのバージョンに連動 --> <ProtobufVersion>3.28.3</ProtobufVersion> <!-- linux_x64 or linux_x86 or macosx_x64 or windows_x64 or windows_x86 --> <ProtobufPlatform>macosx_x64</ProtobufPlatform> </PropertyGroup> <Target Name="CustomAfterBuild" AfterTargets="Build"> <!-- protocのパスを表示 --> <Message Text="protoc's Path = $(NuGetPackageRoot)google.protobuf.tools/$(ProtobufVersion)/tools/$(ProtobufPlatform)/protoc.exe" Importance="high" Condition="'$(ProtobufPlatform)' == 'windows_x64' or '$(ProtobufPlatform)' == 'windows_x86'"/> <Message Text="protoc's Path = $(NuGetPackageRoot)google.protobuf.tools/$(ProtobufVersion)/tools/$(ProtobufPlatform)/protoc" Importance="high" Condition="'$(ProtobufPlatform)' != 'windows_x64' and '$(ProtobufPlatform)' != 'windows_x86'"/> </Target>