Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 1 | # Copyright 2020-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 device-management-interface |
| 16 | default: test |
| 17 | |
| 18 | # set default shell options |
| 19 | SHELL = bash -e -o pipefail |
| 20 | |
| 21 | # tool containers |
| 22 | VOLTHA_TOOLS_VERSION ?= 2.0.0 |
| 23 | |
| 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 |
| 25 | PROTOC_SH = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/go/src/github.com/opencord/device-management-interface/v3 $(shell test -t 0 && echo "-it") --workdir=/go/src/github.com/opencord/device-management-interface/v3 voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc sh -c |
| 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 |
| 27 | |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 28 | # Function to extract the last path component from package line in .proto files |
| 29 | define if_package_path |
| 30 | $(shell grep if_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p') |
Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 31 | endef |
| 32 | |
| 33 | # Variables |
| 34 | PROTO_FILES := $(sort $(wildcard protos/dmi/*.proto)) |
| 35 | |
| 36 | PROTO_GO_DEST_DIR := go |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 37 | PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/dmi/%.proto,$(PROTO_GO_DEST_DIR)/$(call if_package_path,$(f))/%.pb.go,$(f))) |
| 38 | |
Andrea Campanella | add9944 | 2020-10-02 12:02:42 +0200 | [diff] [blame] | 39 | PROTO_PYTHON_DEST_DIR := python/dmi |
| 40 | PROTO_PYTHON_PB2 := $(foreach f, $(PROTO_FILES), $(patsubst protos/dmi/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2.py,$(f))) |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 41 | |
Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 42 | # Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid |
| 43 | .PHONY: dmi.pb |
| 44 | |
| 45 | print: |
| 46 | @echo "Proto files: $(PROTO_FILES)" |
| 47 | @echo "Go PB files: $(PROTO_GO_PB)" |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 48 | @echo "python PB files: $(PROTO_PYTHON_PB)" |
Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 49 | |
| 50 | # Generic targets |
uwe ottrembka | 5ea5c0a | 2020-08-31 10:37:35 +0200 | [diff] [blame] | 51 | protos: go-protos python-protos |
Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 52 | |
| 53 | build: protos |
| 54 | |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 55 | test: go-test python-test |
| 56 | |
Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 57 | |
| 58 | venv_protos: |
| 59 | virtualenv -p python3 $@;\ |
| 60 | source ./$@/bin/activate ; set -u ;\ |
| 61 | pip install grpcio-tools googleapis-common-protos |
| 62 | |
Andrea Campanella | add9944 | 2020-10-02 12:02:42 +0200 | [diff] [blame] | 63 | # Python targets |
| 64 | python-protos: $(PROTO_PYTHON_PB2) |
| 65 | |
| 66 | $(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/dmi/%.proto Makefile venv_protos |
uwe ottrembka | 5ea5c0a | 2020-08-31 10:37:35 +0200 | [diff] [blame] | 67 | source ./venv_protos/bin/activate ; set -u ;\ |
Andrea Campanella | add9944 | 2020-10-02 12:02:42 +0200 | [diff] [blame] | 68 | python -m grpc_tools.protoc \ |
| 69 | -I protos \ |
| 70 | --python_out=python \ |
| 71 | --grpc_python_out=python \ |
| 72 | --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \ |
| 73 | --include_imports \ |
| 74 | --include_source_info \ |
| 75 | $< |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 76 | |
Andrea Campanella | 031ea1e | 2020-06-08 11:37:44 +0200 | [diff] [blame] | 77 | # Go targets |
| 78 | go-protos: dmi.pb |
| 79 | @echo "Creating *.go.pb files" |
| 80 | @${PROTOC_SH} " \ |
| 81 | set -e -o pipefail; \ |
| 82 | for x in ${PROTO_FILES}; do \ |
| 83 | echo \$$x; \ |
| 84 | protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \ |
| 85 | done" |
| 86 | |
| 87 | dmi.pb: |
| 88 | @echo "Creating $@" |
| 89 | @${PROTOC} -I protos \ |
| 90 | --include_imports --include_source_info \ |
| 91 | --descriptor_set_out=$@ \ |
| 92 | ${PROTO_FILES} |
| 93 | |
| 94 | go-test: |
| 95 | test/test-go-proto-consistency.sh |
| 96 | ${GO} mod verify |
uwe ottrembka | 66d91ea | 2020-08-11 10:50:55 +0200 | [diff] [blame] | 97 | |
| 98 | python-test: tox.ini |
| 99 | test/test-python-proto-consistency.sh |
Andrea Campanella | add9944 | 2020-10-02 12:02:42 +0200 | [diff] [blame] | 100 | tox |
| 101 | |
| 102 | python-build: setup.py python-protos |
| 103 | rm -rf dist/ |
| 104 | python ./setup.py sdist |