1616# Configuration
1717# ==============================================================================
1818
19+ # Helpers for string manipulation
20+ COMMA := ,
21+ EMPTY :=
22+ SPACE := $(EMPTY ) $(EMPTY )
23+
24+ # Modules
25+ MODULE_NAME := github.com/nvidia/nvsentinel/api
26+
27+ # API Versions (space-separated)
28+ API_VERSIONS := device/v1alpha1
29+
30+ # Helper for tools that need comma-separated Group/Versions
31+ API_VERSIONS_COMMA := $(subst $(SPACE ) ,$(COMMA ) ,$(API_VERSIONS ) )
32+
33+ # Helper for tools that need full package paths
34+ API_PKGS := $(foreach version,$(API_VERSIONS ) ,$(MODULE_NAME ) /$(version ) )
35+
36+ # Common
37+ BOILERPLATE := hack/boilerplate.go.txt
38+
1939# Directories
20- PROTO_DIR := proto
21- GEN_DIR := gen/go
40+ PROTO_DIR := proto
41+ GEN_DIR := gen/go
2242
2343# Shell setup
2444SHELL = /usr/bin/env bash -o pipefail
@@ -29,9 +49,17 @@ LOCALBIN ?= $(shell pwd)/bin
2949$(LOCALBIN ) :
3050 mkdir -p $(LOCALBIN )
3151
52+ # Go Version
53+ GO_VERSION := $(shell grep "^toolchain " go.mod | awk '{print $$2}')
54+ ifeq ($(GO_VERSION ) ,)
55+ GO_VERSION := $(shell go env GOVERSION)
56+ endif
57+
3258# Tool Binaries
33- PROTOC_GEN_GO ?= $(LOCALBIN ) /protoc-gen-go
34- PROTOC_GEN_GO_GRPC ?= $(LOCALBIN ) /protoc-gen-go-grpc
59+ PROTOC_GEN_GO ?= $(LOCALBIN ) /protoc-gen-go
60+ PROTOC_GEN_GO_GRPC ?= $(LOCALBIN ) /protoc-gen-go-grpc
61+ DEEPCOPY_GEN ?= $(LOCALBIN ) /deepcopy-gen
62+ GOVERTER ?= $(LOCALBIN ) /goverter
3563
3664# Tool Versions (load from ../.versions.yaml)
3765# Requires yq to be installed: brew install yq (macOS) or see https://github.com/mikefarah/yq
@@ -42,15 +70,17 @@ endif
4270
4371VERSIONS_FILE := ../.versions.yaml
4472
45- PROTOC_GEN_GO_VERSION := $(shell $(YQ ) '.protobuf.protoc_gen_go' $(VERSIONS_FILE ) )
46- PROTOC_GEN_GO_GRPC_VERSION := $(shell $(YQ ) '.protobuf.protoc_gen_go_grpc' $(VERSIONS_FILE ) )
73+ PROTOC_GEN_GO_VERSION := $(shell $(YQ ) '.protobuf.protoc_gen_go' $(VERSIONS_FILE ) )
74+ PROTOC_GEN_GO_GRPC_VERSION := $(shell $(YQ ) '.protobuf.protoc_gen_go_grpc' $(VERSIONS_FILE ) )
75+ KUBERNETES_CODEGEN_VERSION := $(shell $(YQ ) '.go_tools.kubernetes_code_gen' $(VERSIONS_FILE ) )
76+ GOVERTER_VERSION := $(shell $(YQ ) '.go_tools.goverter' $(VERSIONS_FILE ) )
4777
4878# ==============================================================================
4979# Targets
5080# ==============================================================================
5181
5282.PHONY : all
53- all : protos-generate build
83+ all : code-gen test build # # Run code generation, compile all code, and execute tests.
5484
5585# #@ General
5686
@@ -60,42 +90,100 @@ help: ## Display this help.
6090
6191# #@ Development
6292
93+ .PHONY : code-gen
94+ code-gen : install-tools protos-generate deepcopy-gen conversion-gen # # Run all code generation targets.
95+ @echo " Synchronizing module dependencies..."
96+ go mod tidy
97+
6398.PHONY : build
64- build :
99+ build : code-gen # # Compile all Go code after generation to verify type safety.
100+ @echo " Compiling..."
65101 go build -v ./...
66102
103+ .PHONY : test
104+ test : code-gen # # Run unit tests and generate coverage after code generation.
105+ @echo " Testing..."
106+ go test -v $$(go list ./... | grep -v /gen/ ) -coverprofile cover.out
107+
67108.PHONY : protos-generate
68- protos-generate : $(PROTOC_GEN_GO ) $(PROTOC_GEN_GO_GRPC ) protos- clean # # Generate Go code from Proto definitions .
69- @echo " Generating Proto code ..."
109+ protos-generate : $(PROTOC_GEN_GO ) $(PROTOC_GEN_GO_GRPC ) clean-protos # # Generate Protobuf Go bindings .
110+ @echo " Generating protobuf bindings for $( API_VERSIONS_COMMA ) ..."
70111 @mkdir -p $(GEN_DIR )
71- cd proto && protoc \
72- -I . \
73- -I ../$(THIRD_PARTY_DIR ) \
74- --plugin=" protoc-gen-go=$( PROTOC_GEN_GO) " \
75- --plugin=" protoc-gen-go-grpc=$( PROTOC_GEN_GO_GRPC) " \
76- --go_out=../$(GEN_DIR ) \
77- --go_opt=paths=source_relative \
78- --go-grpc_out=../$(GEN_DIR ) \
79- --go-grpc_opt=paths=source_relative \
80- device/v1alpha1/* .proto
81- @echo " Cleaning up dependencies..."
82- go mod tidy
83- @echo " Done."
84-
85- .PHONY : protos-clean
86- protos-clean : # # Remove generated code.
87- @echo " Cleaning old generated Proto code..."
112+ @$(foreach version,$(API_VERSIONS ) , \
113+ cd $(PROTO_DIR ) && protoc \
114+ -I . \
115+ --plugin=" protoc-gen-go=$( PROTOC_GEN_GO) " \
116+ --plugin=" protoc-gen-go-grpc=$( PROTOC_GEN_GO_GRPC) " \
117+ --go_out=" ../$( GEN_DIR) " \
118+ --go_opt=paths=" source_relative" \
119+ --go-grpc_out=" ../$( GEN_DIR) " \
120+ --go-grpc_opt=paths=" source_relative" \
121+ $(version ) /* .proto; \
122+ )
123+
124+ .PHONY : deepcopy-gen
125+ deepcopy-gen : $(DEEPCOPY_GEN ) clean-deepcopy # # Generate deepcopy code.
126+ @echo " Generating deepcopy code for $( API_VERSIONS_COMMA) ..."
127+ $(DEEPCOPY_GEN ) \
128+ --bounding-dirs=" $( API_PKGS_COMMA) " \
129+ --go-header-file=" $( BOILERPLATE) " \
130+ --output-file=" zz_generated.deepcopy.go" \
131+ $(API_PKGS )
132+
133+ .PHONY : conversion-gen
134+ conversion-gen : $(GOVERTER ) clean-conversion # # Generate conversion code..
135+ @echo " Generating conversion code for $( API_VERSIONS_COMMA) ..."
136+ @$(foreach version,$(API_VERSIONS ) , \
137+ $(GOVERTER ) gen ./$(version ) ; \
138+ )
139+
140+ .PHONY : clean
141+ clean : clean-protos clean-conversion clean-deepcopy # # Remove all generated code.
142+
143+ .PHONY : purge
144+ purge : clean clean-bin # # Remove all generated code and downloaded tools.
145+
146+ .PHONY : clean-protos
147+ clean-protos : # # Remove generated protobuf Go bindings.
148+ @echo " Removing generated protobuf Go bindings..."
88149 rm -rf $(GEN_DIR )
89- @echo " Done."
150+
151+ .PHONY : clean-deepcopy
152+ clean-deepcopy : # # Remove generated deepcopy code.
153+ @echo " Removing generated deepcopy code..."
154+ find . -name " zz_generated.deepcopy.go" -delete
155+
156+ .PHONY : clean-conversion
157+ clean-conversion : # # Remove generated conversion code.
158+ @echo " Removing generated conversion code..."
159+ find . -name " zz_generated.conversion.go" -delete
160+
161+ .PHONY : clean-bin
162+ clean-bin : # # Remove all downloaded tools in localbin.
163+ @echo " Removing downloaded build tools from $( LOCALBIN) ..."
164+ rm -rf $(LOCALBIN )
90165
91166# #@ Build Dependencies
92167
168+ .PHONY : install-tools
169+ install-tools : install-tools-echo $(PROTOC_GEN_GO ) $(PROTOC_GEN_GO_GRPC ) $(DEEPCOPY_GEN ) $(GOVERTER ) # # Ensure all necessary build tools are downloaded and installed.
170+
171+ .PHONY : install-tools-echo
172+ install-tools-echo :
173+ @echo " Installing dependencies..."
174+
93175$(PROTOC_GEN_GO ) :
94176 $(call go-install-tool,$(PROTOC_GEN_GO ) ,google.golang.org/protobuf/cmd/protoc-gen-go,$(PROTOC_GEN_GO_VERSION ) )
95177
96178$(PROTOC_GEN_GO_GRPC ) :
97179 $(call go-install-tool,$(PROTOC_GEN_GO_GRPC ) ,google.golang.org/grpc/cmd/protoc-gen-go-grpc,$(PROTOC_GEN_GO_GRPC_VERSION ) )
98180
181+ $(DEEPCOPY_GEN ) :
182+ $(call go-install-tool,$(DEEPCOPY_GEN ) ,k8s.io/code-generator/cmd/deepcopy-gen,$(KUBERNETES_CODEGEN_VERSION ) )
183+
184+ $(GOVERTER ) :
185+ $(call go-install-tool,$(GOVERTER ) ,github.com/jmattheis/goverter/cmd/goverter,$(GOVERTER_VERSION ) )
186+
99187# go-install-tool macro
100188# $1 - target path with name of binary
101189# $2 - package url which can be installed
@@ -107,7 +195,7 @@ mkdir -p $(LOCALBIN); \
107195package=$(2 ) @$(3 ) ;\
108196echo "Downloading $${package}" ;\
109197rm -f $(1 ) || true ;\
110- GOBIN=$(LOCALBIN ) go install $${package} ;\
198+ GOBIN=$(LOCALBIN ) GOTOOLCHAIN= $( GO_VERSION ) go install $${package} ;\
111199mv $(1 ) $(1 ) -$(3 ) ;\
112200} ;\
113201ln -sf $(1 ) -$(3 ) $(1 )
0 commit comments