Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 1 | # Copyright 2019-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # Makefile for voltha-protos |
| 16 | default: test |
| 17 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 18 | # set default shell options |
| 19 | SHELL = bash -e -o pipefail |
| 20 | |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 21 | # tool containers |
David K. Bainbridge | c4bf538 | 2021-04-08 16:06:54 +0000 | [diff] [blame] | 22 | VOLTHA_TOOLS_VERSION ?= 2.4.0 |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 23 | |
Kent Hagerman | ea09e42 | 2020-02-20 18:56:54 -0500 | [diff] [blame] | 24 | PROTOC = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc protoc |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame^] | 25 | PROTOC_SH = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/go/src/github.com/opencord/voltha-protos/v5 $(shell test -t 0 && echo "-it") --workdir=/go/src/github.com/opencord/voltha-protos/v5 voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc sh -c |
Kent Hagerman | ea09e42 | 2020-02-20 18:56:54 -0500 | [diff] [blame] | 26 | GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app $(shell test -t 0 && echo "-it") -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 27 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 28 | # Function to extract the last path component from go_package line in .proto files |
| 29 | define go_package_path |
| 30 | $(shell grep go_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p') |
| 31 | endef |
| 32 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 33 | # Function to extract the last path component from package line in .proto files |
| 34 | define java_package_path |
| 35 | $(shell grep package $(1) | sed -n 's/.*\/\(.*\)";/\1/p') |
| 36 | endef |
| 37 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 38 | # Variables |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 39 | PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto)) |
| 40 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 41 | PROTO_PYTHON_DEST_DIR := python/voltha_protos |
| 42 | PROTO_PYTHON_PB2 := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2.py,$(f))) |
Matt Jeanneret | 37e0fc6 | 2019-03-07 12:33:21 -0500 | [diff] [blame] | 43 | PROTO_PYTHON_PB2_GRPC := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2_grpc.py,$(f))) |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 44 | PROTO_GO_DEST_DIR := go |
| 45 | PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_GO_DEST_DIR)/$(call go_package_path,$(f))/%.pb.go,$(f))) |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 46 | PROTO_JAVA_DEST_DIR := java |
| 47 | PROTO_JAVA_PB := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_JAVA_DEST_DIR)/$(call java_package_path,$(f))/%.pb.java,$(f))) |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 48 | # Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 49 | .PHONY: voltha.pb |
Matt Jeanneret | 15249fa | 2019-04-12 20:25:31 -0400 | [diff] [blame] | 50 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 51 | print: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 52 | @echo "Proto files: $(PROTO_FILES)" |
| 53 | @echo "Python PB2 files: $(PROTO_PYTHON_PB2)" |
| 54 | @echo "Go PB files: $(PROTO_GO_PB)" |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 55 | @echo "JAVA PB files: $(PROTO_JAVA_PB)" |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 56 | |
| 57 | # Generic targets |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 58 | protos: python-protos go-protos java-protos |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 59 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 60 | build: protos python-build go-protos java-protos |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 61 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 62 | test: python-test go-test java-test |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 63 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame^] | 64 | clean: python-clean java-clean go-clean |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 65 | |
| 66 | # Python targets |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 67 | python-protos: $(PROTO_PYTHON_PB2) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 68 | |
| 69 | venv_protos: |
Dinesh Belwalkar | ed6da5e | 2020-02-25 11:23:57 -0800 | [diff] [blame] | 70 | virtualenv -p python3 $@;\ |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 71 | source ./$@/bin/activate ; set -u ;\ |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame^] | 72 | pip install grpcio==1.39.0 protobuf==3.17.3 grpcio-tools==1.39.0 googleapis-common-protos==1.52.0 |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 73 | |
| 74 | $(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/voltha_protos/%.proto Makefile venv_protos |
| 75 | source ./venv_protos/bin/activate ; set -u ;\ |
| 76 | python -m grpc_tools.protoc \ |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 77 | -I protos \ |
| 78 | --python_out=python \ |
| 79 | --grpc_python_out=python \ |
| 80 | --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \ |
| 81 | --include_imports \ |
| 82 | --include_source_info \ |
| 83 | $< |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 84 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 85 | python-build: setup.py python-protos |
William Kurkian | 4ae7849 | 2019-06-11 20:33:05 -0400 | [diff] [blame] | 86 | rm -rf dist/ |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 87 | python ./setup.py sdist |
| 88 | |
| 89 | python-test: tox.ini setup.py python-protos |
| 90 | tox |
| 91 | |
| 92 | python-clean: |
Matt Jeanneret | 37e0fc6 | 2019-03-07 12:33:21 -0500 | [diff] [blame] | 93 | find python/ -name '*.pyc' | xargs rm -f |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 94 | rm -rf \ |
| 95 | .coverage \ |
| 96 | .tox \ |
| 97 | coverage.xml \ |
| 98 | dist \ |
| 99 | nose2-results.xml \ |
| 100 | python/__pycache__ \ |
| 101 | python/test/__pycache__ \ |
| 102 | python/voltha_protos.egg-info \ |
| 103 | venv_protos \ |
| 104 | $(PROTO_PYTHON_DEST_DIR)/*.desc \ |
| 105 | $(PROTO_PYTHON_PB2) \ |
| 106 | $(PROTO_PYTHON_PB2_GRPC) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 107 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame^] | 108 | go-clean: |
| 109 | rm -rf go/* |
| 110 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 111 | # Go targets |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 112 | go-protos: voltha.pb |
| 113 | @echo "Creating *.go.pb files" |
| 114 | @${PROTOC_SH} " \ |
| 115 | set -e -o pipefail; \ |
| 116 | for x in ${PROTO_FILES}; do \ |
| 117 | echo \$$x; \ |
| 118 | protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \ |
| 119 | done" |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 120 | |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 121 | voltha.pb: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 122 | @echo "Creating $@" |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 123 | @${PROTOC} -I protos -I protos/google/api \ |
| 124 | --include_imports --include_source_info \ |
| 125 | --descriptor_set_out=$@ \ |
| 126 | ${PROTO_FILES} |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 127 | |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 128 | go-test: |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 129 | test/test-go-proto-consistency.sh |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 130 | ${GO} mod verify |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 131 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 132 | # Java targets |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 133 | java-protos: voltha.pb |
| 134 | @echo "Creating java files" |
| 135 | @mkdir -p java_temp/src/main/java |
| 136 | @${PROTOC_SH} " \ |
| 137 | set -e -o pipefail; \ |
| 138 | for x in ${PROTO_FILES}; do \ |
| 139 | echo \$$x; \ |
| 140 | protoc --java_out=java_temp/src/main/java -I protos \$$x; \ |
| 141 | done" |
| 142 | #TODO: generate directly to the final location |
| 143 | @mkdir -p java |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 144 | cp -r java_temp/src/main/java/* java/ |
| 145 | |
| 146 | # Tests if the generated java classes are compilable |
| 147 | java-test: java-protos |
| 148 | cp test/pom.xml java_temp |
| 149 | cd java_temp && mvn compile |
| 150 | |
| 151 | java-clean: |
| 152 | rm -rf java |
| 153 | rm -rf java_temp |