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+ # Helper for tools that need comma-separated package paths
37+ API_PKGS_COMMA := $(subst $(SPACE ) ,$(COMMA ) ,$(API_PKGS ) )
38+
39+ # Common
40+ BOILERPLATE := hack/boilerplate.go.txt
41+
1942# Directories
20- PROTO_DIR := proto
21- GEN_DIR := gen/go
43+ PROTO_DIR := proto
44+ GEN_DIR := gen/go
2245
2346# Shell setup
2447SHELL = /usr/bin/env bash -o pipefail
@@ -29,9 +52,17 @@ LOCALBIN ?= $(shell pwd)/bin
2952$(LOCALBIN ) :
3053 mkdir -p $(LOCALBIN )
3154
55+ # Go Version
56+ GO_VERSION := $(shell grep "^toolchain " go.mod | awk '{print $$2}')
57+ ifeq ($(GO_VERSION ) ,)
58+ GO_VERSION := $(shell go env GOVERSION)
59+ endif
60+
3261# Tool Binaries
33- PROTOC_GEN_GO ?= $(LOCALBIN ) /protoc-gen-go
34- PROTOC_GEN_GO_GRPC ?= $(LOCALBIN ) /protoc-gen-go-grpc
62+ PROTOC_GEN_GO ?= $(LOCALBIN ) /protoc-gen-go
63+ PROTOC_GEN_GO_GRPC ?= $(LOCALBIN ) /protoc-gen-go-grpc
64+ DEEPCOPY_GEN ?= $(LOCALBIN ) /deepcopy-gen
65+ GOVERTER ?= $(LOCALBIN ) /goverter
3566
3667# Tool Versions (load from ../.versions.yaml)
3768# Requires yq to be installed: brew install yq (macOS) or see https://github.com/mikefarah/yq
@@ -42,15 +73,17 @@ endif
4273
4374VERSIONS_FILE := ../.versions.yaml
4475
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 ) )
76+ PROTOC_GEN_GO_VERSION := $(shell $(YQ ) '.protobuf.protoc_gen_go' $(VERSIONS_FILE ) )
77+ PROTOC_GEN_GO_GRPC_VERSION := $(shell $(YQ ) '.protobuf.protoc_gen_go_grpc' $(VERSIONS_FILE ) )
78+ KUBERNETES_CODEGEN_VERSION := $(shell $(YQ ) '.go_tools.kubernetes_code_gen' $(VERSIONS_FILE ) )
79+ GOVERTER_VERSION := $(shell $(YQ ) '.go_tools.goverter' $(VERSIONS_FILE ) )
4780
4881# ==============================================================================
4982# Targets
5083# ==============================================================================
5184
5285.PHONY : all
53- all : protos-generate build
86+ all : code-gen test build # # Run code generation, compile all code, and execute tests.
5487
5588# #@ General
5689
@@ -60,42 +93,100 @@ help: ## Display this help.
6093
6194# #@ Development
6295
96+ .PHONY : code-gen
97+ code-gen : install-tools protos-generate deepcopy-gen conversion-gen # # Run all code generation targets.
98+ @echo " Synchronizing module dependencies..."
99+ go mod tidy
100+
63101.PHONY : build
64- build :
102+ build : code-gen # # Compile all Go code after generation to verify type safety.
103+ @echo " Compiling..."
65104 go build -v ./...
66105
106+ .PHONY : test
107+ test : code-gen # # Run unit tests and generate coverage after code generation.
108+ @echo " Testing..."
109+ go test -v $$(go list ./... | grep -v /gen/ ) -coverprofile cover.out
110+
67111.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 ..."
112+ protos-generate : $(PROTOC_GEN_GO ) $(PROTOC_GEN_GO_GRPC ) clean-protos # # Generate Protobuf Go bindings .
113+ @echo " Generating protobuf bindings for $( API_VERSIONS_COMMA ) ..."
70114 @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..."
115+ @$(foreach version,$(API_VERSIONS ) , \
116+ cd $(PROTO_DIR ) && protoc \
117+ -I . \
118+ --plugin=" protoc-gen-go=$( PROTOC_GEN_GO) " \
119+ --plugin=" protoc-gen-go-grpc=$( PROTOC_GEN_GO_GRPC) " \
120+ --go_out=" ../$( GEN_DIR) " \
121+ --go_opt=paths=" source_relative" \
122+ --go-grpc_out=" ../$( GEN_DIR) " \
123+ --go-grpc_opt=paths=" source_relative" \
124+ $(version ) /* .proto; \
125+ )
126+
127+ .PHONY : deepcopy-gen
128+ deepcopy-gen : $(DEEPCOPY_GEN ) clean-deepcopy # # Generate deepcopy code.
129+ @echo " Generating deepcopy code for $( API_VERSIONS_COMMA) ..."
130+ $(DEEPCOPY_GEN ) \
131+ --bounding-dirs=" $( API_PKGS_COMMA) " \
132+ --go-header-file=" $( BOILERPLATE) " \
133+ --output-file=" zz_generated.deepcopy.go" \
134+ $(API_PKGS )
135+
136+ .PHONY : conversion-gen
137+ conversion-gen : $(GOVERTER ) clean-conversion # # Generate conversion code.
138+ @echo " Generating conversion code for $( API_VERSIONS_COMMA) ..."
139+ @$(foreach version,$(API_VERSIONS ) , \
140+ $(GOVERTER ) gen ./$(version ) ; \
141+ )
142+
143+ .PHONY : clean
144+ clean : clean-protos clean-conversion clean-deepcopy # # Remove all generated code.
145+
146+ .PHONY : purge
147+ purge : clean clean-bin # # Remove all generated code and downloaded tools.
148+
149+ .PHONY : clean-protos
150+ clean-protos : # # Remove generated protobuf Go bindings.
151+ @echo " Removing generated protobuf Go bindings..."
88152 rm -rf $(GEN_DIR )
89- @echo " Done."
153+
154+ .PHONY : clean-deepcopy
155+ clean-deepcopy : # # Remove generated deepcopy code.
156+ @echo " Removing generated deepcopy code..."
157+ find . -name " zz_generated.deepcopy.go" -delete
158+
159+ .PHONY : clean-conversion
160+ clean-conversion : # # Remove generated conversion code.
161+ @echo " Removing generated conversion code..."
162+ find . -name " zz_generated.conversion.go" -delete
163+
164+ .PHONY : clean-bin
165+ clean-bin : # # Remove all downloaded tools in localbin.
166+ @echo " Removing downloaded build tools from $( LOCALBIN) ..."
167+ rm -rf $(LOCALBIN )
90168
91169# #@ Build Dependencies
92170
171+ .PHONY : install-tools
172+ install-tools : install-tools-echo $(PROTOC_GEN_GO ) $(PROTOC_GEN_GO_GRPC ) $(DEEPCOPY_GEN ) $(GOVERTER ) # # Ensure all necessary build tools are downloaded and installed.
173+
174+ .PHONY : install-tools-echo
175+ install-tools-echo :
176+ @echo " Installing dependencies..."
177+
93178$(PROTOC_GEN_GO ) :
94179 $(call go-install-tool,$(PROTOC_GEN_GO ) ,google.golang.org/protobuf/cmd/protoc-gen-go,$(PROTOC_GEN_GO_VERSION ) )
95180
96181$(PROTOC_GEN_GO_GRPC ) :
97182 $(call go-install-tool,$(PROTOC_GEN_GO_GRPC ) ,google.golang.org/grpc/cmd/protoc-gen-go-grpc,$(PROTOC_GEN_GO_GRPC_VERSION ) )
98183
184+ $(DEEPCOPY_GEN ) :
185+ $(call go-install-tool,$(DEEPCOPY_GEN ) ,k8s.io/code-generator/cmd/deepcopy-gen,$(KUBERNETES_CODEGEN_VERSION ) )
186+
187+ $(GOVERTER ) :
188+ $(call go-install-tool,$(GOVERTER ) ,github.com/jmattheis/goverter/cmd/goverter,$(GOVERTER_VERSION ) )
189+
99190# go-install-tool macro
100191# $1 - target path with name of binary
101192# $2 - package url which can be installed
@@ -107,7 +198,7 @@ mkdir -p $(LOCALBIN); \
107198package=$(2 ) @$(3 ) ;\
108199echo "Downloading $${package}" ;\
109200rm -f $(1 ) || true ;\
110- GOBIN=$(LOCALBIN ) go install $${package} ;\
201+ GOBIN=$(LOCALBIN ) GOTOOLCHAIN= $( GO_VERSION ) go install $${package} ;\
111202mv $(1 ) $(1 ) -$(3 ) ;\
112203} ;\
113204ln -sf $(1 ) -$(3 ) $(1 )
0 commit comments