xref: /aosp_15_r20/external/protobuf/python/protobuf_distutils/README.md (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1*1b3f573fSAndroid Build Coastguard Worker# Python setuptools extension
2*1b3f573fSAndroid Build Coastguard Worker
3*1b3f573fSAndroid Build Coastguard WorkerThis is an extension for Python setuptools which uses an installed protobuf
4*1b3f573fSAndroid Build Coastguard Workercompiler (`protoc`) to generate Python sources.
5*1b3f573fSAndroid Build Coastguard Worker
6*1b3f573fSAndroid Build Coastguard Worker## Installing
7*1b3f573fSAndroid Build Coastguard Worker
8*1b3f573fSAndroid Build Coastguard WorkerTo use this extension, it needs to be installed so it can be imported by other
9*1b3f573fSAndroid Build Coastguard Workerprojects' setup.py.
10*1b3f573fSAndroid Build Coastguard Worker
11*1b3f573fSAndroid Build Coastguard Worker```shell
12*1b3f573fSAndroid Build Coastguard Worker$ python setup.py build
13*1b3f573fSAndroid Build Coastguard Worker$ python -m pip install .
14*1b3f573fSAndroid Build Coastguard Worker```
15*1b3f573fSAndroid Build Coastguard Worker
16*1b3f573fSAndroid Build Coastguard Worker(If you want to test changes to the extension, you can use `python setup.py
17*1b3f573fSAndroid Build Coastguard Workerdevelop`.)
18*1b3f573fSAndroid Build Coastguard Worker
19*1b3f573fSAndroid Build Coastguard Worker## Usage
20*1b3f573fSAndroid Build Coastguard Worker
21*1b3f573fSAndroid Build Coastguard Worker### Example setup.py configuration
22*1b3f573fSAndroid Build Coastguard Worker
23*1b3f573fSAndroid Build Coastguard Worker```python
24*1b3f573fSAndroid Build Coastguard Workerfrom setuptools import setup
25*1b3f573fSAndroid Build Coastguard Workersetup(
26*1b3f573fSAndroid Build Coastguard Worker    # ...
27*1b3f573fSAndroid Build Coastguard Worker    name='example_project',
28*1b3f573fSAndroid Build Coastguard Worker
29*1b3f573fSAndroid Build Coastguard Worker    # Require this package, but only for setup (not installation):
30*1b3f573fSAndroid Build Coastguard Worker    setup_requires=['protobuf_distutils'],
31*1b3f573fSAndroid Build Coastguard Worker
32*1b3f573fSAndroid Build Coastguard Worker    options={
33*1b3f573fSAndroid Build Coastguard Worker        # See below for details.
34*1b3f573fSAndroid Build Coastguard Worker        'generate_py_protobufs': {
35*1b3f573fSAndroid Build Coastguard Worker            'source_dir':        'path/to/protos',
36*1b3f573fSAndroid Build Coastguard Worker            'extra_proto_paths': ['path/to/other/project/protos'],
37*1b3f573fSAndroid Build Coastguard Worker            'output_dir':        'path/to/project/sources',  # default '.'
38*1b3f573fSAndroid Build Coastguard Worker            'proto_files':       ['relative/path/to/just_this_file.proto'],
39*1b3f573fSAndroid Build Coastguard Worker            'protoc':            'path/to/protoc.exe',
40*1b3f573fSAndroid Build Coastguard Worker        },
41*1b3f573fSAndroid Build Coastguard Worker    },
42*1b3f573fSAndroid Build Coastguard Worker)
43*1b3f573fSAndroid Build Coastguard Worker```
44*1b3f573fSAndroid Build Coastguard Worker
45*1b3f573fSAndroid Build Coastguard Worker### Example build invocation
46*1b3f573fSAndroid Build Coastguard Worker
47*1b3f573fSAndroid Build Coastguard WorkerThese steps will generate protobuf sources so they are included when building
48*1b3f573fSAndroid Build Coastguard Workerand installing `example_project` (see above):
49*1b3f573fSAndroid Build Coastguard Worker
50*1b3f573fSAndroid Build Coastguard Worker```shell
51*1b3f573fSAndroid Build Coastguard Worker$ python setup.py generate_py_protobufs
52*1b3f573fSAndroid Build Coastguard Worker$ python setup.py build
53*1b3f573fSAndroid Build Coastguard Worker$ python -m pip install .
54*1b3f573fSAndroid Build Coastguard Worker```
55*1b3f573fSAndroid Build Coastguard Worker
56*1b3f573fSAndroid Build Coastguard Worker## Options
57*1b3f573fSAndroid Build Coastguard Worker
58*1b3f573fSAndroid Build Coastguard Worker- `source_dir`:
59*1b3f573fSAndroid Build Coastguard Worker
60*1b3f573fSAndroid Build Coastguard Worker  This is the directory holding .proto files to be processed.
61*1b3f573fSAndroid Build Coastguard Worker
62*1b3f573fSAndroid Build Coastguard Worker  The default behavior is to generate sources for all .proto files found under
63*1b3f573fSAndroid Build Coastguard Worker  `source_dir`, recursively. This behavior can be controlled with options below.
64*1b3f573fSAndroid Build Coastguard Worker
65*1b3f573fSAndroid Build Coastguard Worker- `proto_root_path`:
66*1b3f573fSAndroid Build Coastguard Worker
67*1b3f573fSAndroid Build Coastguard Worker  This is the root path for resolving imports in source .proto files.
68*1b3f573fSAndroid Build Coastguard Worker
69*1b3f573fSAndroid Build Coastguard Worker  The default is the shortest prefix of `source_dir` among `[source_dir] +
70*1b3f573fSAndroid Build Coastguard Worker  self.extra_proto_paths`.
71*1b3f573fSAndroid Build Coastguard Worker
72*1b3f573fSAndroid Build Coastguard Worker- `extra_proto_paths`:
73*1b3f573fSAndroid Build Coastguard Worker
74*1b3f573fSAndroid Build Coastguard Worker  Specifies additional paths that should be used to find imports, in
75*1b3f573fSAndroid Build Coastguard Worker  addition to `source_dir`.
76*1b3f573fSAndroid Build Coastguard Worker
77*1b3f573fSAndroid Build Coastguard Worker  This option can be used to specify the path to other protobuf sources,
78*1b3f573fSAndroid Build Coastguard Worker  which are imported by files under `source_dir`.  No Python code will
79*1b3f573fSAndroid Build Coastguard Worker  be generated for .proto files under `extra_proto_paths`.
80*1b3f573fSAndroid Build Coastguard Worker
81*1b3f573fSAndroid Build Coastguard Worker- `output_dir`:
82*1b3f573fSAndroid Build Coastguard Worker
83*1b3f573fSAndroid Build Coastguard Worker  Specifies where generated code should be placed.
84*1b3f573fSAndroid Build Coastguard Worker
85*1b3f573fSAndroid Build Coastguard Worker  Typically, this should be the root package that generated Python modules
86*1b3f573fSAndroid Build Coastguard Worker  should be below.
87*1b3f573fSAndroid Build Coastguard Worker
88*1b3f573fSAndroid Build Coastguard Worker  The generated files will be placed under `output_dir` according to the
89*1b3f573fSAndroid Build Coastguard Worker  relative source paths under `proto_root_path`. For example, the source file
90*1b3f573fSAndroid Build Coastguard Worker  `${proto_root_path}/subdir/message.proto` will be generated as the Python
91*1b3f573fSAndroid Build Coastguard Worker  module `${output_dir}/subdir/message_pb2.py`.
92*1b3f573fSAndroid Build Coastguard Worker
93*1b3f573fSAndroid Build Coastguard Worker- `proto_files`:
94*1b3f573fSAndroid Build Coastguard Worker
95*1b3f573fSAndroid Build Coastguard Worker  A list of strings, specific .proto file paths for generating code, instead of
96*1b3f573fSAndroid Build Coastguard Worker  searching for all .proto files under `source_path`.
97*1b3f573fSAndroid Build Coastguard Worker
98*1b3f573fSAndroid Build Coastguard Worker  These paths are relative to `source_dir`. For example, to generate code
99*1b3f573fSAndroid Build Coastguard Worker  for just `${source_dir}/subdir/message.proto`, specify
100*1b3f573fSAndroid Build Coastguard Worker  `['subdir/message.proto']`.
101*1b3f573fSAndroid Build Coastguard Worker
102*1b3f573fSAndroid Build Coastguard Worker- `protoc`:
103*1b3f573fSAndroid Build Coastguard Worker
104*1b3f573fSAndroid Build Coastguard Worker  By default, the protoc binary (the Protobuf compiler) is found by
105*1b3f573fSAndroid Build Coastguard Worker  searching the environment path. To use a specific protoc binary, its
106*1b3f573fSAndroid Build Coastguard Worker  path can be specified. Resolution of the `protoc` value is as follows:
107*1b3f573fSAndroid Build Coastguard Worker  1. If the `--protoc=VALUE` flag is passed to `generate_py_protobufs`,
108*1b3f573fSAndroid Build Coastguard Worker     then `VALUE` will be used.
109*1b3f573fSAndroid Build Coastguard Worker     For example:
110*1b3f573fSAndroid Build Coastguard Worker     ```shell
111*1b3f573fSAndroid Build Coastguard Worker     $ python setup.py generate_py_protobufs --protoc=/path/to/protoc
112*1b3f573fSAndroid Build Coastguard Worker     ```
113*1b3f573fSAndroid Build Coastguard Worker  2. Otherwise, if a value was set in the `options`, it will be used.
114*1b3f573fSAndroid Build Coastguard Worker     (See "Example setup.py configuration," above.)
115*1b3f573fSAndroid Build Coastguard Worker  3. Otherwise, if the `PROTOC` environment variable is set, it will be
116*1b3f573fSAndroid Build Coastguard Worker     used. For example:
117*1b3f573fSAndroid Build Coastguard Worker     For example:
118*1b3f573fSAndroid Build Coastguard Worker     ```shell
119*1b3f573fSAndroid Build Coastguard Worker     $ PROTOC=/path/to/protoc python setup.py generate_py_protobufs
120*1b3f573fSAndroid Build Coastguard Worker     ```
121*1b3f573fSAndroid Build Coastguard Worker  4. Otherwise, `$PATH` will be searched.
122