blob: 09c25312e25d7cf37b0f962189164f919fe56638 [file] [log] [blame]
Joey Armstronge6cdd8e2022-12-29 11:58:15 -05001# -*- makefile -*-
2# -----------------------------------------------------------------------
Joey Armstronge66eaaf2023-01-15 18:58:52 -05003# Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors
Zack Williams52209662019-02-07 10:15:31 -07004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050016# -----------------------------------------------------------------------
17
Joey Armstronge66eaaf2023-01-15 18:58:52 -050018.PHONY: test
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050019.DEFAULT_GOAL := test
20
Joey Armstronge66eaaf2023-01-15 18:58:52 -050021##-------------------##
22##---] GLOBALS [---##
23##-------------------##
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050024TOP ?= .
25MAKEDIR ?= $(TOP)/makefiles
26
27$(if $(VERBOSE),$(eval export VERBOSE=$(VERBOSE))) # visible to include(s)
28
Joey Armstrong761579c2023-05-08 11:59:57 -040029##--------------------------
30## Enable setup.py debugging
31##--------------------------
32# https://docs.python.org/3/distutils/setupscript.html#debugging-the-setup-script
33export DISTUTILS_DEBUG := 1
34
Joey Armstrongc2277652023-01-11 17:41:36 -050035# Makefile for voltha-protos
36default: test
37
Joey Armstronge66eaaf2023-01-15 18:58:52 -050038## Library linting
39# NO-LINT-MAKEFILE := true # cleanup needed
40NO-LINT-SHELL := true # cleanup needed
41
Joey Armstronge6cdd8e2022-12-29 11:58:15 -050042##--------------------##
43##---] INCLUDES [---##
44##--------------------##
Joey Armstronge66eaaf2023-01-15 18:58:52 -050045include $(MAKEDIR)/include.mk
Zack Williams52209662019-02-07 10:15:31 -070046
Zack Williams43bfd9e2019-04-12 13:09:31 -070047# Function to extract the last path component from go_package line in .proto files
48define go_package_path
49$(shell grep go_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
50endef
51
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030052# Function to extract the last path component from package line in .proto files
53define java_package_path
54$(shell grep package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
55endef
56
Zack Williams52209662019-02-07 10:15:31 -070057# Variables
Zack Williams43bfd9e2019-04-12 13:09:31 -070058PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto))
59
Zack Williams52209662019-02-07 10:15:31 -070060PROTO_PYTHON_DEST_DIR := python/voltha_protos
61PROTO_PYTHON_PB2 := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2.py,$(f)))
Matt Jeanneret37e0fc62019-03-07 12:33:21 -050062PROTO_PYTHON_PB2_GRPC := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2_grpc.py,$(f)))
Zack Williams43bfd9e2019-04-12 13:09:31 -070063PROTO_GO_DEST_DIR := go
64PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_GO_DEST_DIR)/$(call go_package_path,$(f))/%.pb.go,$(f)))
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030065PROTO_JAVA_DEST_DIR := java
66PROTO_JAVA_PB := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_JAVA_DEST_DIR)/$(call java_package_path,$(f))/%.pb.java,$(f)))
Joey Armstrong761579c2023-05-08 11:59:57 -040067
Zack Williams25e0a322019-06-07 14:07:21 -070068# Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid
Kent Hagerman868dc382020-01-20 11:24:09 -050069.PHONY: voltha.pb
Matt Jeanneret15249fa2019-04-12 20:25:31 -040070
Zack Williams52209662019-02-07 10:15:31 -070071print:
Zack Williams43bfd9e2019-04-12 13:09:31 -070072 @echo "Proto files: $(PROTO_FILES)"
73 @echo "Python PB2 files: $(PROTO_PYTHON_PB2)"
74 @echo "Go PB files: $(PROTO_GO_PB)"
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030075 @echo "JAVA PB files: $(PROTO_JAVA_PB)"
Zack Williams52209662019-02-07 10:15:31 -070076
77# Generic targets
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030078protos: python-protos go-protos java-protos
Zack Williams52209662019-02-07 10:15:31 -070079
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030080build: protos python-build go-protos java-protos
Zack Williams52209662019-02-07 10:15:31 -070081
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030082test: python-test go-test java-test
Zack Williams52209662019-02-07 10:15:31 -070083
Joey Armstrong761579c2023-05-08 11:59:57 -040084clean :: python-clean java-clean go-clean
Zack Williams52209662019-02-07 10:15:31 -070085
Joey Armstrong761579c2023-05-08 11:59:57 -040086sterile :: clean
Joey Armstrongc2277652023-01-11 17:41:36 -050087
Zack Williams52209662019-02-07 10:15:31 -070088# Python targets
Kent Hagerman868dc382020-01-20 11:24:09 -050089python-protos: $(PROTO_PYTHON_PB2)
Zack Williams52209662019-02-07 10:15:31 -070090
Joey Armstrong761579c2023-05-08 11:59:57 -040091## -----------------------------------------------------------------------
92## -----------------------------------------------------------------------
93$(PROTO_PYTHON_DEST_DIR)/%_pb2.py: \
94 protos/voltha_protos/%.proto \
95 Makefile \
96 $(venv-activate-script)
97 $(activate) \
98 && python -m grpc_tools.protoc \
Zack Williams43bfd9e2019-04-12 13:09:31 -070099 -I protos \
100 --python_out=python \
101 --grpc_python_out=python \
102 --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \
103 --include_imports \
104 --include_source_info \
105 $<
Zack Williams52209662019-02-07 10:15:31 -0700106
Zack Williams43bfd9e2019-04-12 13:09:31 -0700107python-build: setup.py python-protos
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500108 $(RM) -r dist/
Zack Williams52209662019-02-07 10:15:31 -0700109 python ./setup.py sdist
110
111python-test: tox.ini setup.py python-protos
112 tox
113
114python-clean:
Joey Armstronge66eaaf2023-01-15 18:58:52 -0500115 find python -name '__pycache__' -type d -print0 \
116 | xargs -0 --no-run-if-empty $(RM) -r
Joey Armstrongc2277652023-01-11 17:41:36 -0500117 find python -name '*.pyc' -type f -print0 \
118 | xargs -0 --no-run-if-empty $(RM)
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500119 $(RM) -r \
Zack Williams43bfd9e2019-04-12 13:09:31 -0700120 .coverage \
121 .tox \
122 coverage.xml \
123 dist \
124 nose2-results.xml \
125 python/__pycache__ \
126 python/test/__pycache__ \
127 python/voltha_protos.egg-info \
Joey Armstrong761579c2023-05-08 11:59:57 -0400128 "$(venv-abs-path)" \
Zack Williams43bfd9e2019-04-12 13:09:31 -0700129 $(PROTO_PYTHON_DEST_DIR)/*.desc \
130 $(PROTO_PYTHON_PB2) \
131 $(PROTO_PYTHON_PB2_GRPC)
Zack Williams52209662019-02-07 10:15:31 -0700132
Joey Armstrong761579c2023-05-08 11:59:57 -0400133## -----------------------------------------------------------------------
134## Intent: Revert go to a clean state.
135## o TODO - go/ directory should not be placed under revision control.
136## o Build should retrieve versioned sources from a central repo.
137## -----------------------------------------------------------------------
khenaidoo5fc5cea2021-08-11 17:39:16 -0400138go-clean:
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500139 $(RM) -r go/*
Joey Armstrong761579c2023-05-08 11:59:57 -0400140 $(HIDE)$(MAKE) repair
141
142## -----------------------------------------------------------------------
143## Intent: Recover from a fatal failed build state:
144## o build removes go/ while regenerating prototypes.
145## o chicken-n-egg: make becomes fatal when go/ is removed and proten fails.
146## -----------------------------------------------------------------------
147repair:
148 /usr/bin/env git checkout go
149
khenaidoo5fc5cea2021-08-11 17:39:16 -0400150
Zack Williams52209662019-02-07 10:15:31 -0700151# Go targets
Kent Hagerman868dc382020-01-20 11:24:09 -0500152go-protos: voltha.pb
153 @echo "Creating *.go.pb files"
Joey Armstrong761579c2023-05-08 11:59:57 -0400154 @echo "PROTO_FILES=$(PROTO_FILES)" | tr ' ' '\n'
155 ${PROTOC_SH} $(quote-double)\
Kent Hagerman868dc382020-01-20 11:24:09 -0500156 set -e -o pipefail; \
157 for x in ${PROTO_FILES}; do \
158 echo \$$x; \
159 protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \
Joey Armstrong761579c2023-05-08 11:59:57 -0400160 done$(quote-double)
Zack Williams43bfd9e2019-04-12 13:09:31 -0700161
Kent Hagerman868dc382020-01-20 11:24:09 -0500162voltha.pb:
Zack Williams43bfd9e2019-04-12 13:09:31 -0700163 @echo "Creating $@"
Joey Armstrong761579c2023-05-08 11:59:57 -0400164 $(HIDE)${PROTOC} -I protos -I protos/google/api \
Kent Hagerman868dc382020-01-20 11:24:09 -0500165 --include_imports --include_source_info \
166 --descriptor_set_out=$@ \
167 ${PROTO_FILES}
Zack Williams43bfd9e2019-04-12 13:09:31 -0700168
Kent Hagerman868dc382020-01-20 11:24:09 -0500169go-test:
William Kurkianad745652019-03-20 08:45:51 -0400170 test/test-go-proto-consistency.sh
Kent Hagerman868dc382020-01-20 11:24:09 -0500171 ${GO} mod verify
Zack Williams52209662019-02-07 10:15:31 -0700172
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +0300173# Java targets
Kent Hagerman868dc382020-01-20 11:24:09 -0500174java-protos: voltha.pb
175 @echo "Creating java files"
176 @mkdir -p java_temp/src/main/java
Joey Armstrong761579c2023-05-08 11:59:57 -0400177 @${PROTOC_SH} $(quote-double) \
Kent Hagerman868dc382020-01-20 11:24:09 -0500178 set -e -o pipefail; \
179 for x in ${PROTO_FILES}; do \
180 echo \$$x; \
181 protoc --java_out=java_temp/src/main/java -I protos \$$x; \
Joey Armstrong761579c2023-05-08 11:59:57 -0400182 done$(quote-double)
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500183 #TODO: generate directly to the final location
Kent Hagerman868dc382020-01-20 11:24:09 -0500184 @mkdir -p java
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +0300185 cp -r java_temp/src/main/java/* java/
186
187# Tests if the generated java classes are compilable
188java-test: java-protos
189 cp test/pom.xml java_temp
190 cd java_temp && mvn compile
191
192java-clean:
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500193 $(RM) -r java
194 $(RM) -r java_temp
195
Joey Armstronge66eaaf2023-01-15 18:58:52 -0500196# placeholder for library targets
197lint :
198
Joey Armstronge6cdd8e2022-12-29 11:58:15 -0500199# [EOF]