xref: /aosp_15_r20/external/cronet/third_party/protobuf/examples/README.md (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker# Protocol Buffers - Code Example
2*6777b538SAndroid Build Coastguard Worker
3*6777b538SAndroid Build Coastguard WorkerThis directory contains example code that uses Protocol Buffers to manage an
4*6777b538SAndroid Build Coastguard Workeraddress book. Two programs are provided for each supported language. The
5*6777b538SAndroid Build Coastguard Workeradd_person example adds a new person to an address book, prompting the user to
6*6777b538SAndroid Build Coastguard Workerinput the person's information. The list_people example lists people already in
7*6777b538SAndroid Build Coastguard Workerthe address book. The examples use the exact same format in all three languages,
8*6777b538SAndroid Build Coastguard Workerso you can, for example, use add_person_java to create an address book and then
9*6777b538SAndroid Build Coastguard Workeruse list_people_python to read it.
10*6777b538SAndroid Build Coastguard Worker
11*6777b538SAndroid Build Coastguard WorkerThese examples are part of the Protocol Buffers tutorial, located at:
12*6777b538SAndroid Build Coastguard Worker  https://developers.google.com/protocol-buffers/docs/tutorials
13*6777b538SAndroid Build Coastguard Worker
14*6777b538SAndroid Build Coastguard Worker## Build the example using bazel
15*6777b538SAndroid Build Coastguard Worker
16*6777b538SAndroid Build Coastguard WorkerThe example requires bazel 0.5.4 or newer to build. You can download/install
17*6777b538SAndroid Build Coastguard Workerthe latest version of bazel from bazel's release page:
18*6777b538SAndroid Build Coastguard Worker
19*6777b538SAndroid Build Coastguard Worker    https://github.com/bazelbuild/bazel/releases
20*6777b538SAndroid Build Coastguard Worker
21*6777b538SAndroid Build Coastguard WorkerOnce you have bazel installed, simply run the following command in this examples
22*6777b538SAndroid Build Coastguard Workerdirectory to build the code:
23*6777b538SAndroid Build Coastguard Worker
24*6777b538SAndroid Build Coastguard Worker    $ bazel build :all
25*6777b538SAndroid Build Coastguard Worker
26*6777b538SAndroid Build Coastguard WorkerThen you can run the built binary:
27*6777b538SAndroid Build Coastguard Worker
28*6777b538SAndroid Build Coastguard Worker    $ bazel-bin/add_person_cpp addressbook.data
29*6777b538SAndroid Build Coastguard Worker
30*6777b538SAndroid Build Coastguard WorkerTo use protobuf in your own bazel project, please follow instructions in the
31*6777b538SAndroid Build Coastguard Worker[BUILD](BUILD) file and [WORKSPACE](WORKSPACE) file.
32*6777b538SAndroid Build Coastguard Worker
33*6777b538SAndroid Build Coastguard Worker## Build the example using make
34*6777b538SAndroid Build Coastguard Worker
35*6777b538SAndroid Build Coastguard WorkerYou must install the protobuf package before you can build it using make. The
36*6777b538SAndroid Build Coastguard Workerminimum requirement is to install protocol compiler (i.e., the protoc binary)
37*6777b538SAndroid Build Coastguard Workerand the protobuf runtime for the language you want to build.
38*6777b538SAndroid Build Coastguard Worker
39*6777b538SAndroid Build Coastguard WorkerYou can simply run "make" to build the example for all languages (except for
40*6777b538SAndroid Build Coastguard WorkerGo). However, since different language has different installation requirement,
41*6777b538SAndroid Build Coastguard Workerit will likely fail. It's better to follow individual instructions below to
42*6777b538SAndroid Build Coastguard Workerbuild only the language you are interested in.
43*6777b538SAndroid Build Coastguard Worker
44*6777b538SAndroid Build Coastguard Worker### C++
45*6777b538SAndroid Build Coastguard Worker
46*6777b538SAndroid Build Coastguard WorkerYou can follow instructions in [../src/README.md](../src/README.md) to install
47*6777b538SAndroid Build Coastguard Workerprotoc and protobuf C++ runtime from source.
48*6777b538SAndroid Build Coastguard Worker
49*6777b538SAndroid Build Coastguard WorkerThen run "make cpp" in this examples directory to build the C++ example. It
50*6777b538SAndroid Build Coastguard Workerwill create two executables: add_person_cpp and list_people_cpp. These programs
51*6777b538SAndroid Build Coastguard Workersimply take an address book file as their parameter. The add_person_cpp
52*6777b538SAndroid Build Coastguard Workerprograms will create the file if it doesn't already exist.
53*6777b538SAndroid Build Coastguard Worker
54*6777b538SAndroid Build Coastguard WorkerTo run the examples:
55*6777b538SAndroid Build Coastguard Worker
56*6777b538SAndroid Build Coastguard Worker    $ ./add_person_cpp addressbook.data
57*6777b538SAndroid Build Coastguard Worker    $ ./list_people_cpp addressbook.data
58*6777b538SAndroid Build Coastguard Worker
59*6777b538SAndroid Build Coastguard WorkerNote that on some platforms you may have to edit the Makefile and remove
60*6777b538SAndroid Build Coastguard Worker"-lpthread" from the linker commands (perhaps replacing it with something else).
61*6777b538SAndroid Build Coastguard WorkerWe didn't do this automatically because we wanted to keep the example simple.
62*6777b538SAndroid Build Coastguard Worker
63*6777b538SAndroid Build Coastguard Worker### Python
64*6777b538SAndroid Build Coastguard Worker
65*6777b538SAndroid Build Coastguard WorkerFollow instructions in [../README.md](../README.md) to install protoc and then
66*6777b538SAndroid Build Coastguard Workerfollow [../python/README.md](../python/README.md) to install protobuf python
67*6777b538SAndroid Build Coastguard Workerruntime from source. You can also install python runtime using pip:
68*6777b538SAndroid Build Coastguard Worker
69*6777b538SAndroid Build Coastguard Worker    $ pip install protobuf
70*6777b538SAndroid Build Coastguard Worker
71*6777b538SAndroid Build Coastguard WorkerMake sure the runtime version is the same as protoc binary, or it may not work.
72*6777b538SAndroid Build Coastguard Worker
73*6777b538SAndroid Build Coastguard WorkerAfter you have install both protoc and python runtime, run "make python" to
74*6777b538SAndroid Build Coastguard Workerbuild two executables (shell scripts actually): add_person_python and
75*6777b538SAndroid Build Coastguard Workerlist_people_python. They work the same way as the C++ executables.
76*6777b538SAndroid Build Coastguard Worker
77*6777b538SAndroid Build Coastguard Worker### Java
78*6777b538SAndroid Build Coastguard Worker
79*6777b538SAndroid Build Coastguard WorkerFollow instructions in [../README.md](../README.md) to install protoc and then
80*6777b538SAndroid Build Coastguard Workerdownload protobuf Java runtime .jar file from maven:
81*6777b538SAndroid Build Coastguard Worker
82*6777b538SAndroid Build Coastguard Worker    https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
83*6777b538SAndroid Build Coastguard Worker
84*6777b538SAndroid Build Coastguard WorkerThen run the following:
85*6777b538SAndroid Build Coastguard Worker
86*6777b538SAndroid Build Coastguard Worker    $ export CLASSPATH=/path/to/protobuf-java-[version].jar
87*6777b538SAndroid Build Coastguard Worker    $ make java
88*6777b538SAndroid Build Coastguard Worker
89*6777b538SAndroid Build Coastguard WorkerThis will create the add_person_java/list_people_java executables (shell
90*6777b538SAndroid Build Coastguard Workerscripts) and can be used to create/display an address book data file.
91*6777b538SAndroid Build Coastguard Worker
92*6777b538SAndroid Build Coastguard Worker### Go
93*6777b538SAndroid Build Coastguard Worker
94*6777b538SAndroid Build Coastguard WorkerFollow instructions in [../README.md](../README.md) to install protoc. Then
95*6777b538SAndroid Build Coastguard Workerinstall the Go protoc plugin (protoc-gen-go):
96*6777b538SAndroid Build Coastguard Worker
97*6777b538SAndroid Build Coastguard Worker    $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
98*6777b538SAndroid Build Coastguard Worker
99*6777b538SAndroid Build Coastguard WorkerThe "go install" command will install protoc-gen-go into the GOBIN
100*6777b538SAndroid Build Coastguard Workerdirectory.  You can set the $GOBIN environment variable before
101*6777b538SAndroid Build Coastguard Workerrunning "go install" to change the install location.  Make sure the
102*6777b538SAndroid Build Coastguard Workerinstall directory is in your shell $PATH.
103*6777b538SAndroid Build Coastguard Worker
104*6777b538SAndroid Build Coastguard WorkerBuild the Go samples with "make go".  This creates the following
105*6777b538SAndroid Build Coastguard Workerexecutable files in the current directory:
106*6777b538SAndroid Build Coastguard Worker
107*6777b538SAndroid Build Coastguard Worker    add_person_go      list_people_go
108*6777b538SAndroid Build Coastguard Worker
109*6777b538SAndroid Build Coastguard WorkerTo run the example:
110*6777b538SAndroid Build Coastguard Worker
111*6777b538SAndroid Build Coastguard Worker    ./add_person_go addressbook.data
112*6777b538SAndroid Build Coastguard Worker
113*6777b538SAndroid Build Coastguard Workerto add a person to the protocol buffer encoded file addressbook.data.  The file
114*6777b538SAndroid Build Coastguard Workeris created if it does not exist.  To view the data, run:
115*6777b538SAndroid Build Coastguard Worker
116*6777b538SAndroid Build Coastguard Worker    ./list_people_go addressbook.data
117*6777b538SAndroid Build Coastguard Worker
118*6777b538SAndroid Build Coastguard WorkerObserve that the C++, Python, Java, and Dart examples in this directory run in a
119*6777b538SAndroid Build Coastguard Workersimilar way and can view/modify files created by the Go example and vice
120*6777b538SAndroid Build Coastguard Workerversa.
121*6777b538SAndroid Build Coastguard Worker
122*6777b538SAndroid Build Coastguard Worker### Dart
123*6777b538SAndroid Build Coastguard Worker
124*6777b538SAndroid Build Coastguard WorkerFirst, follow the instructions in [../README.md](../README.md) to install the Protocol Buffer Compiler (protoc).
125*6777b538SAndroid Build Coastguard Worker
126*6777b538SAndroid Build Coastguard WorkerThen, install the Dart Protocol Buffer plugin as described [here](https://github.com/dart-lang/dart-protoc-plugin#how-to-build-and-use).
127*6777b538SAndroid Build Coastguard WorkerNote, the executable `bin/protoc-gen-dart` must be in your `PATH` for `protoc` to find it.
128*6777b538SAndroid Build Coastguard Worker
129*6777b538SAndroid Build Coastguard WorkerBuild the Dart samples in this directory with `make dart`.
130*6777b538SAndroid Build Coastguard Worker
131*6777b538SAndroid Build Coastguard WorkerTo run the examples:
132*6777b538SAndroid Build Coastguard Worker
133*6777b538SAndroid Build Coastguard Worker```sh
134*6777b538SAndroid Build Coastguard Worker$ dart add_person.dart addressbook.data
135*6777b538SAndroid Build Coastguard Worker$ dart list_people.dart addressbook.data
136*6777b538SAndroid Build Coastguard Worker```
137*6777b538SAndroid Build Coastguard Worker
138*6777b538SAndroid Build Coastguard WorkerThe two programs take a protocol buffer encoded file as their parameter.
139*6777b538SAndroid Build Coastguard WorkerThe first can be used to add a person to the file. The file is created
140*6777b538SAndroid Build Coastguard Workerif it does not exist. The second displays the data in the file.
141