Starting the protobuf

Hello!

I am trying to use the Google Protocol Buffer library for SWI-Prolog by following this tutorial: ('packages/protobufs.html').

At first, I was doing everything wrong and installed the protocol for Windows and tried to compile the proto file with the built-in SWI-Prolog packages. After a few hours of various errors, I checked the developer’s documentation and found out that it could not be processed on Windows.

So, I created a Docker image with this Dockerfile:

FROM debian:buster
RUN apt-get update && apt-get install -y \
    swi-prolog
RUN apt-get install -y wget
WORKDIR /tmp
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v27.0/protoc-27.0-linux-x86_64.zip
RUN unzip protoc-27.0-linux-x86_64.zip -d protoc
RUN mv protoc/include /usr/local/include && \
    mv protoc/bin/* /usr/local/bin
RUN rm -rf /tmp/*
WORKDIR /app
CMD ["bash"]

Then, I ran these commands in my container:

1 Add "/usr/lib/swi-prolog/library/protobufs " to path 
2 Create gen_pb directory
3 protoc -I/usr/local/include --swipl_out=gen_pb google/protobuf/descriptor.proto google/protobuf/compiler/plugin.proto

I have the plugin.proto file, but this command did not create:

gen_pb/google/protobuf/descriptor_pb.pl
gen_pb/google/protobuf/compiler/plugin_pb.pl

Now I am not sure about windows. I found addressbook.proto in my swipl files in windows and I read in docs that I need make run_addressbook. May I run it on Windows? And how?

Therefore, I need some advice from someone who has already gone through the entire process.

It appears that there are some missing pieces in the documentation – I’ll try to update these soon. (I think that the plugin needs to be specified when using protoc, for example)

I’m unable to help with docker or Windows; hopefully when I fix the documentation for using protoc it’ll be obvious how to do things with docker.

Here’s a partial answer; the protobuf package seems to have become out of date, so I need to make some changes to it. Also, there are some typos in the documentation. :frowning:

The following worked on Debian and should also work on Ubuntu (after installing swi-prolog, protobuf-compiler, pkg-config). It is possible that it would also work on Windows; there is a comment with commit 01e748fa3bac23e15830b978af9fbc14ab191146 that it has cleaned up float32 and int32 for MSVC.

protoc -I/usr/share/doc/protobuf-compiler/examples -I/usr/include --swipl_out=/tmp/ --plugin=protoc-gen-swipl=/usr/lib/swi-prolog/library/protobufs/protoc-gen-swipl /usr/share/doc/protobuf-compiler/examples/addressbook.proto

This generated /tmp/addressbook_pb.pl … you should change the --swipl-out= specification to be the directory you want (e.g., interop).

Note that protoc requires a -I item that encompasses the .proto file, so this also works:

protoc -I/usr/share/doc/protobuf-compiler/examples -I/usr/include --swipl_out=/tmp/ --plugin=protoc-gen-swipl=/usr/lib/swi-prolog/library/protobufs/protoc-gen-swipl addressbook.proto

It’s possible that your installation puts files in a different place; you can find the addressbook example with:

dpkg -L protobuf-compiler | grep addressbook

To test this, I ran the following (there’s a typo in address book.pl for the “decode” command):

swipl -g test_write -g test_write -g test_read -t halt addressbook.pl  # outputs to addressbook.wire
protoc --decode=tutorial.AddressBook addressbook.proto <addressbook.wire 

I’ve updated the documentation: DOC: examples+tests using updated protobuf-compiler package by kamahen · Pull Request #18 · SWI-Prolog/contrib-protobufs · GitHub

1 Like

FYI

For others like me finding the GitHub page and expecting more info. The GitHub repository has a few subdirectories with further documentation in them.

Here are the directories with documentation pages AFAIK

https://github.com/SWI-Prolog/contrib-protobufs
https://github.com/SWI-Prolog/contrib-protobufs/tree/master/bootstrap
https://github.com/SWI-Prolog/contrib-protobufs/tree/master/bootstrap/gen_pb
https://github.com/SWI-Prolog/contrib-protobufs/tree/master/demo
https://github.com/SWI-Prolog/contrib-protobufs/tree/master/interop

The SWI-Prolog index is misleading: “protobufs” leads to SWI-Prolog -- library(protobufs): Google's Protocol Buffers ("protobufs") but there’s more here (with a rather strange name):
section('packages/protobufs.html')

@jan - Can this indexing be fixed, please? (Also, the author’s info and maybe it needs a copyright?).
The file is swipl-devel/packages/protobufs/protobufs.doc.

There ought to be a section describing the various directories, such as /usr/share/swi-prolog/doc/packages/examples/protobufs (which includes demo, interop and bootstrap). Where should this be added?

There are a lot of hits in the search. protobuf is only used for the library name though. The docs talk about Google Protocol Buffers and you find that using “protocol”. I guess we can add a tag “protobufs”. I see the tagging interface is a bit broken after the website migration though :frowning:

I don’t know. You’re guess is most likely better than mine :slight_smile: