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 |
Kent Hagerman | 9864c14 | 2020-02-26 10:42:16 -0500 | [diff] [blame] | 22 | VOLTHA_TOOLS_VERSION ?= 2.0.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 |
Matteo Scandolo | b3c08ae | 2020-10-14 13:15:43 -0700 | [diff] [blame] | 25 | PROTOC_SH = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/go/src/github.com/opencord/voltha-protos/v4 $(shell test -t 0 && echo "-it") --workdir=/go/src/github.com/opencord/voltha-protos/v4 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 | |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 64 | clean: python-clean java-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 ;\ |
| 72 | pip install grpcio-tools googleapis-common-protos |
| 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 | |
| 108 | # Go targets |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 109 | go-protos: voltha.pb |
| 110 | @echo "Creating *.go.pb files" |
| 111 | @${PROTOC_SH} " \ |
| 112 | set -e -o pipefail; \ |
| 113 | for x in ${PROTO_FILES}; do \ |
| 114 | echo \$$x; \ |
| 115 | protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \ |
| 116 | done" |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 117 | |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 118 | voltha.pb: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 119 | @echo "Creating $@" |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 120 | @${PROTOC} -I protos -I protos/google/api \ |
| 121 | --include_imports --include_source_info \ |
| 122 | --descriptor_set_out=$@ \ |
| 123 | ${PROTO_FILES} |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 124 | |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 125 | go-test: |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 126 | test/test-go-proto-consistency.sh |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 127 | ${GO} mod verify |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 128 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 129 | # Java targets |
Kent Hagerman | 868dc38 | 2020-01-20 11:24:09 -0500 | [diff] [blame] | 130 | java-protos: voltha.pb |
| 131 | @echo "Creating java files" |
| 132 | @mkdir -p java_temp/src/main/java |
| 133 | @${PROTOC_SH} " \ |
| 134 | set -e -o pipefail; \ |
| 135 | for x in ${PROTO_FILES}; do \ |
| 136 | echo \$$x; \ |
| 137 | protoc --java_out=java_temp/src/main/java -I protos \$$x; \ |
| 138 | done" |
| 139 | #TODO: generate directly to the final location |
| 140 | @mkdir -p java |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 141 | cp -r java_temp/src/main/java/* java/ |
| 142 | |
| 143 | # Tests if the generated java classes are compilable |
| 144 | java-test: java-protos |
| 145 | cp test/pom.xml java_temp |
| 146 | cd java_temp && mvn compile |
| 147 | |
| 148 | java-clean: |
| 149 | rm -rf java |
| 150 | rm -rf java_temp |