blob: 1eea24f2ca2fca00dbeef98cd0a0060fb31c902a [file] [log] [blame]
Zack Williams52209662019-02-07 10:15:31 -07001# 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
Zack Williams43bfd9e2019-04-12 13:09:31 -070018# 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
Zack Williams52209662019-02-07 10:15:31 -070026# Variables
Zack Williams43bfd9e2019-04-12 13:09:31 -070027PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto))
28
Zack Williams52209662019-02-07 10:15:31 -070029PROTO_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)))
Matt Jeanneret37e0fc62019-03-07 12:33:21 -050031PROTO_PYTHON_PB2_GRPC := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2_grpc.py,$(f)))
Zack Williams43bfd9e2019-04-12 13:09:31 -070032PROTO_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)))
Zack Williams52209662019-02-07 10:15:31 -070034
William Kurkian6ea97f82019-03-13 15:51:55 -040035PROTOC_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
Zack Williams52209662019-02-07 10:15:31 -070043print:
Zack Williams43bfd9e2019-04-12 13:09:31 -070044 @echo "Proto files: $(PROTO_FILES)"
45 @echo "Python PB2 files: $(PROTO_PYTHON_PB2)"
46 @echo "Go PB files: $(PROTO_GO_PB)"
Zack Williams52209662019-02-07 10:15:31 -070047
48# Generic targets
49protos: python-protos go-protos
50
William Kurkianbd3736d2019-03-08 12:20:40 -050051build: protos python-build go-protos
Zack Williams52209662019-02-07 10:15:31 -070052
53test: python-test go-test
54
55clean: python-clean go-clean
56
57# Python targets
58python-protos: $(PROTO_PYTHON_PB2)
59
60venv_protos:
61 virtualenv $@;\
62 source ./$@/bin/activate ; set -u ;\
63 pip install grpcio-tools googleapis-common-protos
64
65$(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/voltha_protos/%.proto Makefile venv_protos
66 source ./venv_protos/bin/activate ; set -u ;\
67 python -m grpc_tools.protoc \
Zack Williams43bfd9e2019-04-12 13:09:31 -070068 -I protos \
69 --python_out=python \
70 --grpc_python_out=python \
71 --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \
72 --include_imports \
73 --include_source_info \
74 $<
Zack Williams52209662019-02-07 10:15:31 -070075
Zack Williams43bfd9e2019-04-12 13:09:31 -070076python-build: setup.py python-protos
Zack Williams52209662019-02-07 10:15:31 -070077 python ./setup.py sdist
78
79python-test: tox.ini setup.py python-protos
80 tox
81
82python-clean:
Matt Jeanneret37e0fc62019-03-07 12:33:21 -050083 find python/ -name '*.pyc' | xargs rm -f
Zack Williams43bfd9e2019-04-12 13:09:31 -070084 rm -rf \
85 .coverage \
86 .tox \
87 coverage.xml \
88 dist \
89 nose2-results.xml \
90 python/__pycache__ \
91 python/test/__pycache__ \
92 python/voltha_protos.egg-info \
93 venv_protos \
94 $(PROTO_PYTHON_DEST_DIR)/*.desc \
95 $(PROTO_PYTHON_PB2) \
96 $(PROTO_PYTHON_PB2_GRPC)
Zack Williams52209662019-02-07 10:15:31 -070097
98# Go targets
Zack Williams43bfd9e2019-04-12 13:09:31 -070099go-protos: protoc_check_version $(PROTO_GO_PB) go/voltha.pb
100
101protoc_check_version:
William Kurkianad745652019-03-20 08:45:51 -0400102ifeq ("", "$(shell which protoc)")
William Kurkian6ea97f82019-03-13 15:51:55 -0400103 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
William Kurkianad745652019-03-20 08:45:51 -0400104 @echo "It looks like you don't have a version of protocol buffer tools."
William Kurkian6ea97f82019-03-13 15:51:55 -0400105 @echo "To install the protocol buffer toolchain, you can run:"
106 @echo " make install-protoc"
107 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
Matt Jeanneret61e94872019-03-22 16:16:01 -0400108 exit 1
William Kurkian6ea97f82019-03-13 15:51:55 -0400109endif
Zack Williams43bfd9e2019-04-12 13:09:31 -0700110
111go_temp:
112 mkdir -p go_temp
113
114$(PROTO_GO_PB): $(PROTO_FILES) go_temp
115 @echo "Creating $@"
116 cd protos && protoc \
117 --go_out=MAPS=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor,plugins=grpc,paths=source_relative:../go_temp \
118 -I . voltha_protos/$$(echo $@ | sed -n 's/.*\/\(.*\).pb.go/\1.proto/p' )
119 mkdir -p $(dir $@)
120 mv go_temp/voltha_protos/$(notdir $@) $@
121
122go/voltha.pb: ${PROTO_FILES}
123 @echo "Creating $@"
124 protoc -I protos -I protos/google/api \
125 --include_imports --include_source_info \
126 --descriptor_set_out=$@ \
127 ${PROTO_FILES}
Zack Williams52209662019-02-07 10:15:31 -0700128
129go-test:
William Kurkianad745652019-03-20 08:45:51 -0400130ifneq ("libprotoc 3.7.0", "$(shell protoc --version)")
131 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
132 @echo "It looks like you don't have protocol buffer tools ${PROTOC_VERSION} installed."
Zack Williams43bfd9e2019-04-12 13:09:31 -0700133 @echo "To install this version, you can run:"
William Kurkianad745652019-03-20 08:45:51 -0400134 @echo " make install-protoc"
135 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
Matt Jeanneret61e94872019-03-22 16:16:01 -0400136 exit 1
William Kurkianad745652019-03-20 08:45:51 -0400137endif
138 test/test-go-proto-consistency.sh
Zack Williams52209662019-02-07 10:15:31 -0700139
140go-clean:
Zack Williams43bfd9e2019-04-12 13:09:31 -0700141 rm -rf go_temp
Zack Williams52209662019-02-07 10:15:31 -0700142
William Kurkian6ea97f82019-03-13 15:51:55 -0400143install-protoc:
144 @echo "Downloading and installing protocol buffer support."
Matt Jeanneret61e94872019-03-22 16:16:01 -0400145 @echo "Installation will require sudo priviledges"
William Kurkian6ea97f82019-03-13 15:51:55 -0400146 @echo "This will take a few minutes."
147 mkdir -p $(PROTOC_BUILD_TMP_DIR)
148 @echo "We ask for sudo credentials now so we can install at the end"; \
149 sudo echo "Thanks"; \
Zack Williams43bfd9e2019-04-12 13:09:31 -0700150 cd $(PROTOC_BUILD_TMP_DIR); \
151 wget $(PROTOC_DOWNLOAD_URI); \
152 tar xzvf $(PROTOC_TARBALL); \
153 cd $(PROTOC_DIR); \
154 ./configure --prefix=$(PROTOC_PREFIX); \
155 make; \
156 sudo make install; \
157 sudo ldconfig