0

Trying to write a Makefile that compiles python gRPC protos but only when the source proto file changed. I ran through it multiple times and can't figure out why I'm getting that error (when running make build). Supposedly it's because a file is matching multiple targets, but I don't see how:

# All proto source files
PROTOS_SRC = $(wildcard api/*.proto)
# The three types of generated files per proto source
PROTOS_PY = $(patsubst api/%.proto, api/%_pb2.py, $(PROTO_SRC))
PROTOS_GRPC_PY = $(patsubst api/%.proto, api/%_pb2_grpc.py, $(PROTO_SRC))
PROTOS_PYI = $(patsubst api/%.proto, api/%_pb2.pyi, $(PROTO_SRC))

# All the generated files we have to depend on
PROTOS_OUT = $(PROTOS_PY) $(PROTOS_GRPC_PY) $(PROTOS_PYI)

# How to obtain generated sources from a proto file
api/%_pb2.py api/%_pb2_grpc.py api/%_pb2.pyi: api/%.proto:
    python -m grpc_tools.protoc -I./api --python_out=./api --grpc_python_out=./api --mypy_out=./api $<

build: $(PROTOS_OUT)

clean:
    rm -rf api/*_pb2.py api/*_pb2_grpc.py

.PHONY: clean

The proto source code is in the api/ subdirectory and that's where I'm outputting the generated code too.

I wrote the conversion rule based on this, so i think it's correct: GNU Makefile rule generating a few targets from a single source file

jeanluc
  • 1,608
  • 1
  • 14
  • 28
  • You have a syntax error: `api/%_pb2.py api/%_pb2_grpc.py api/%_pb2.pyi: api/%.proto:` note that you accidentally put 2 `:` ... – OrenIshShalom Jun 19 '23 at 06:14
  • side note: I always prefer a *single* target file – OrenIshShalom Jun 19 '23 at 06:16
  • When posting questions please always **cut and paste** the _exact_ and _complete_ error message, with code block formatting, just like source code. Paraphrased/retyped error messages can be misleading at best. – MadScientist Jun 19 '23 at 12:52
  • @OrenIshShalom a single target file makes sense when the build command generates a single output file. It doesn't work when, as in this case, the build command generates multiple output files. – MadScientist Jun 19 '23 at 12:53

0 Answers0