blob: a0f1ad6244de415d14b240afb463fa66cb0c992f [file] [log] [blame]
Matt Jeanneret9fba8652019-04-02 12:00:17 -04001# 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
16default: test
17
David Bainbridge142516e2019-04-19 01:34:58 +000018# set default shell options
19SHELL = bash -e -o pipefail
20
21# Function to extract the last path component from go_package line in .proto files
22define go_package_path
23$(shell grep go_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
24endef
25
Matt Jeanneret9fba8652019-04-02 12:00:17 -040026# Variables
David Bainbridge142516e2019-04-19 01:34:58 +000027PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto))
28
Matt Jeanneret9fba8652019-04-02 12:00:17 -040029PROTO_PYTHON_DEST_DIR := python/voltha_protos
30PROTO_PYTHON_PB2 := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2.py,$(f)))
31PROTO_PYTHON_PB2_GRPC := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2_grpc.py,$(f)))
David Bainbridge142516e2019-04-19 01:34:58 +000032PROTO_GO_DEST_DIR := go
33PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_GO_DEST_DIR)/$(call go_package_path,$(f))/%.pb.go,$(f)))
Matt Jeanneret9fba8652019-04-02 12:00:17 -040034
35PROTOC_PREFIX := /usr/local
36PROTOC_VERSION := "3.7.0"
37PROTOC_DOWNLOAD_PREFIX := "https://github.com/google/protobuf/releases/download"
38PROTOC_DIR := protobuf-$(PROTOC_VERSION)
39PROTOC_TARBALL := protobuf-python-$(PROTOC_VERSION).tar.gz
40PROTOC_DOWNLOAD_URI := $(PROTOC_DOWNLOAD_PREFIX)/v$(PROTOC_VERSION)/$(PROTOC_TARBALL)
41PROTOC_BUILD_TMP_DIR := "/tmp/protobuf-build-$(shell uname -s | tr '[:upper:]' '[:lower:]')"
42
David Bainbridge142516e2019-04-19 01:34:58 +000043# Force pb file to be regenrated every time. Otherwise the make process assumes whats there is still ok
44.PHONY: go/voltha.pb
45
Matt Jeanneret9fba8652019-04-02 12:00:17 -040046print:
David Bainbridge142516e2019-04-19 01:34:58 +000047 @echo "Proto files: $(PROTO_FILES)"
48 @echo "Python PB2 files: $(PROTO_PYTHON_PB2)"
49 @echo "Go PB files: $(PROTO_GO_PB)"
Matt Jeanneret9fba8652019-04-02 12:00:17 -040050
51# Generic targets
52protos: python-protos go-protos
53
54build: protos python-build go-protos
55
56test: python-test go-test
57
58clean: python-clean go-clean
59
60# Python targets
61python-protos: $(PROTO_PYTHON_PB2)
62
63venv_protos:
64 virtualenv $@;\
65 source ./$@/bin/activate ; set -u ;\
66 pip install grpcio-tools googleapis-common-protos
67
68$(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/voltha_protos/%.proto Makefile venv_protos
69 source ./venv_protos/bin/activate ; set -u ;\
70 python -m grpc_tools.protoc \
David Bainbridge142516e2019-04-19 01:34:58 +000071 -I protos \
72 --python_out=python \
73 --grpc_python_out=python \
74 --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \
75 --include_imports \
76 --include_source_info \
77 $<
Matt Jeanneret9fba8652019-04-02 12:00:17 -040078
David Bainbridge142516e2019-04-19 01:34:58 +000079python-build: setup.py python-protos
Matt Jeanneret9fba8652019-04-02 12:00:17 -040080 python ./setup.py sdist
81
82python-test: tox.ini setup.py python-protos
83 tox
84
85python-clean:
Matt Jeanneret9fba8652019-04-02 12:00:17 -040086 find python/ -name '*.pyc' | xargs rm -f
David Bainbridge142516e2019-04-19 01:34:58 +000087 rm -rf \
88 .coverage \
89 .tox \
90 coverage.xml \
91 dist \
92 nose2-results.xml \
93 python/__pycache__ \
94 python/test/__pycache__ \
95 python/voltha_protos.egg-info \
96 venv_protos \
97 $(PROTO_PYTHON_DEST_DIR)/*.desc \
98 $(PROTO_PYTHON_PB2) \
99 $(PROTO_PYTHON_PB2_GRPC)
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400100
101# Go targets
David Bainbridge142516e2019-04-19 01:34:58 +0000102go-protos: protoc_check_version $(PROTO_GO_PB) go/voltha.pb
103
104protoc_check_version:
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400105ifeq ("", "$(shell which protoc)")
106 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
107 @echo "It looks like you don't have a version of protocol buffer tools."
108 @echo "To install the protocol buffer toolchain, you can run:"
109 @echo " make install-protoc"
110 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
William Kurkianc91266e2019-04-05 14:21:20 -0400111 exit 1
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400112endif
David Bainbridge142516e2019-04-19 01:34:58 +0000113
114go_temp:
115 mkdir -p go_temp
116
117$(PROTO_GO_PB): $(PROTO_FILES) go_temp
118 @echo "Creating $@"
119 cd protos && protoc \
120 --go_out=MAPS=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor,plugins=grpc,paths=source_relative:../go_temp \
121 -I . voltha_protos/$$(echo $@ | sed -n 's/.*\/\(.*\).pb.go/\1.proto/p' )
122 mkdir -p $(dir $@)
123 mv go_temp/voltha_protos/$(notdir $@) $@
124
125go/voltha.pb: ${PROTO_FILES}
126 @echo "Creating $@"
127 protoc -I protos -I protos/google/api \
128 --include_imports --include_source_info \
129 --descriptor_set_out=$@ \
130 ${PROTO_FILES}
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400131
132go-test:
133ifneq ("libprotoc 3.7.0", "$(shell protoc --version)")
134 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
135 @echo "It looks like you don't have protocol buffer tools ${PROTOC_VERSION} installed."
David Bainbridge142516e2019-04-19 01:34:58 +0000136 @echo "To install this version, you can run:"
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400137 @echo " make install-protoc"
138 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
William Kurkianc91266e2019-04-05 14:21:20 -0400139 exit 1
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400140endif
141 test/test-go-proto-consistency.sh
142
143go-clean:
David Bainbridge142516e2019-04-19 01:34:58 +0000144 rm -rf go_temp
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400145
146install-protoc:
147 @echo "Downloading and installing protocol buffer support."
William Kurkianc91266e2019-04-05 14:21:20 -0400148 @echo "Installation will require sudo priviledges"
Matt Jeanneret9fba8652019-04-02 12:00:17 -0400149 @echo "This will take a few minutes."
150 mkdir -p $(PROTOC_BUILD_TMP_DIR)
151 @echo "We ask for sudo credentials now so we can install at the end"; \
152 sudo echo "Thanks"; \
David Bainbridge142516e2019-04-19 01:34:58 +0000153 cd $(PROTOC_BUILD_TMP_DIR); \
154 wget $(PROTOC_DOWNLOAD_URI); \
155 tar xzvf $(PROTOC_TARBALL); \
156 cd $(PROTOC_DIR); \
157 ./configure --prefix=$(PROTOC_PREFIX); \
158 make; \
159 sudo make install; \
160 sudo ldconfig