0

I have the following project structure minified.

├── Makefile
├── pkg  
│   ├── proto
│   │   ├── auth
│   │   │   └── auth.proto
│   │   ├── common
│   │   │   └── common.proto
│   │   ├── google
│   │   │   ├── descriptor
│   │   │   │   └── descriptor.proto
│   │   │   ├── duration
│   │   │   │   └── duration.proto
│   │   │   └── timestamp
│   │   │       └── timestamp.proto
│   │   ├── user
│   │   │   └── user.proto
│   │   └── validate
│   │       └── validate.proto
│   └── transport

Now I have the following Makefile

PROTO_DIR := ./pkg/proto
TRANSPORT_DIR := ./pkg/transport

PROTOS := $(wildcard $(PROTO_DIR)/**/*.proto)
GOOGLE_PROTO_FILES := $(wildcard $(PROTO_DIR)/google/**/*.proto)

STUBS := $(patsubst $(PROTO_DIR)/%.proto,$(TRANSPORT_DIR)/%/pb/%.pb.go,$(PROTOS))
GOOGLE_STUBS := $(patsubst $(PROTO_DIR)/google/%.proto,$(TRANSPORT_DIR)/google/%/pb/%.pb.go,$(GOOGLE_PROTO_FILES))

gen-all: $(STUBS) $(GOOGLE_STUBS)

$(TRANSPORT_DIR)/%/pb/%.pb.go: $(PROTO_DIR)/%.proto
    mkdir -p $(TRANSPORT_DIR)/$(dir $*)/pb
    protoc --proto_path=$(PROTO_DIR) --go_out=$(TRANSPORT_DIR)/$(dir $*)/pb --go_opt=paths=source_relative $<

$(TRANSPORT_DIR)/google/%.pb.go: $(PROTO_DIR)/google/%.proto
    mkdir -p $(TRANSPORT_DIR)/google
    protoc --proto_path=$(PROTO_DIR) --go_out=$(TRANSPORT_DIR)/google --go_opt=paths=source_relative,plugins=grpc:$(TRANSPORT_DIR)/goog $<

clean:
    rm -rf $(TRANSPORT_DIR)/*

.PHONY: gen-all clean

I don't want to modify the Makefile multiple time hence I had to write to so complex. Now when I do make gen-all, it is creating the stubs in the following paths

pkg/transport
├── auth
│   └── pb
│       └── auth
│           └── auth.pb.go
├── common
│   └── pb
│       └── common
│           └── common.pb.go
├── google
│   ├── descriptor
│   │   └── pb
│   │       └── google
│   │           └── descriptor
│   │               └── descriptor.pb.go
│   ├── duration
│   │   └── pb
│   │       └── google
│   │           └── duration
│   │               └── duration.pb.go
│   └── timestamp
│       └── pb
│           └── google
│               └── timestamp
│                   └── timestamp.pb.go
├── user
│   └── pb
│       └── user
│           └── user.pb.go
└── validate
    └── pb
        └── validate
            └── validate.pb.go

I need the stubs to be present in the following paths

pkg/transport/google/descriptor/pb/descriptor.pb.go
pkg/transport/common/pb/common.pb.go
pkg/transport/auth/pb/auth.pb.go
pkg/transport/google/timestamp/pb/timestamp.pb.go

and so on...

How can I change my Makefile?? Also is there any way that I can simplify my Makefile?

Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38

0 Answers0