blob: 5db361cd18e70fd5333c37abcd055f19f3176042 [file] [log] [blame]
Andrea Campanella031ea1e2020-06-08 11:37:44 +02001# 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
16default: test
17
18# set default shell options
19SHELL = bash -e -o pipefail
20
21# tool containers
22VOLTHA_TOOLS_VERSION ?= 2.0.0
23
24PROTOC = 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
25PROTOC_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
26GO = 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 ottrembka66d91ea2020-08-11 10:50:55 +020028# Function to extract the last path component from package line in .proto files
29define if_package_path
30$(shell grep if_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
Andrea Campanella031ea1e2020-06-08 11:37:44 +020031endef
32
33# Variables
34PROTO_FILES := $(sort $(wildcard protos/dmi/*.proto))
35
36PROTO_GO_DEST_DIR := go
uwe ottrembka66d91ea2020-08-11 10:50:55 +020037PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/dmi/%.proto,$(PROTO_GO_DEST_DIR)/$(call if_package_path,$(f))/%.pb.go,$(f)))
38
39PROTO_PYTHON_DEST_DIR := python
40PROTO_PYTHON_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/dmi/%.proto,$(PROTO_PYTHON_DEST_DIR)/$(call if_package_path,$(f))/%.pb2.py,$(f)))
41
Andrea Campanella031ea1e2020-06-08 11:37:44 +020042# Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid
43.PHONY: dmi.pb
44
45print:
46 @echo "Proto files: $(PROTO_FILES)"
47 @echo "Go PB files: $(PROTO_GO_PB)"
uwe ottrembka66d91ea2020-08-11 10:50:55 +020048 @echo "python PB files: $(PROTO_PYTHON_PB)"
Andrea Campanella031ea1e2020-06-08 11:37:44 +020049
50# Generic targets
uwe ottrembka5ea5c0a2020-08-31 10:37:35 +020051protos: go-protos python-protos
Andrea Campanella031ea1e2020-06-08 11:37:44 +020052
53build: protos
54
uwe ottrembka66d91ea2020-08-11 10:50:55 +020055test: go-test python-test
56
Andrea Campanella031ea1e2020-06-08 11:37:44 +020057
58venv_protos:
59 virtualenv -p python3 $@;\
60 source ./$@/bin/activate ; set -u ;\
61 pip install grpcio-tools googleapis-common-protos
62
uwe ottrembka66d91ea2020-08-11 10:50:55 +020063# python targets
uwe ottrembka5ea5c0a2020-08-31 10:37:35 +020064python-protos: venv_protos
65 @echo "Creating *.pb2.pv and *.pb2_grpc.py files"
66 source ./venv_protos/bin/activate ; set -u ;\
67 for x in $(PROTO_FILES) ; do \
68 echo $$x; \
69 python -m grpc_tools.protoc \
70 -I protos \
71 --python_out=python \
72 --grpc_python_out=python \
73 --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \
74 --include_imports \
75 --include_source_info \
76 $$x;\
77 done
uwe ottrembka66d91ea2020-08-11 10:50:55 +020078
Andrea Campanella031ea1e2020-06-08 11:37:44 +020079# Go targets
80go-protos: dmi.pb
81 @echo "Creating *.go.pb files"
82 @${PROTOC_SH} " \
83 set -e -o pipefail; \
84 for x in ${PROTO_FILES}; do \
85 echo \$$x; \
86 protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \
87 done"
88
89dmi.pb:
90 @echo "Creating $@"
91 @${PROTOC} -I protos \
92 --include_imports --include_source_info \
93 --descriptor_set_out=$@ \
94 ${PROTO_FILES}
95
96go-test:
97 test/test-go-proto-consistency.sh
98 ${GO} mod verify
uwe ottrembka66d91ea2020-08-11 10:50:55 +020099
100python-test: tox.ini
101 test/test-python-proto-consistency.sh
102 tox