blob: 2a2eb4989b1d44db035b850b14f0dce885278de8 [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
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030026# Function to extract the last path component from package line in .proto files
27define java_package_path
28$(shell grep package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
29endef
30
Zack Williams52209662019-02-07 10:15:31 -070031# Variables
Zack Williams43bfd9e2019-04-12 13:09:31 -070032PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto))
33
Zack Williams52209662019-02-07 10:15:31 -070034PROTO_PYTHON_DEST_DIR := python/voltha_protos
35PROTO_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 -050036PROTO_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 -070037PROTO_GO_DEST_DIR := go
38PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_GO_DEST_DIR)/$(call go_package_path,$(f))/%.pb.go,$(f)))
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030039PROTO_JAVA_DEST_DIR := java
40PROTO_JAVA_PB := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_JAVA_DEST_DIR)/$(call java_package_path,$(f))/%.pb.java,$(f)))
Zack Williams25e0a322019-06-07 14:07:21 -070041# Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid
Scott Baker39210792019-10-03 15:00:52 -070042.PHONY: voltha.pb protoc_check
Matt Jeanneret15249fa2019-04-12 20:25:31 -040043
Zack Williams52209662019-02-07 10:15:31 -070044print:
Zack Williams43bfd9e2019-04-12 13:09:31 -070045 @echo "Proto files: $(PROTO_FILES)"
46 @echo "Python PB2 files: $(PROTO_PYTHON_PB2)"
47 @echo "Go PB files: $(PROTO_GO_PB)"
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030048 @echo "JAVA PB files: $(PROTO_JAVA_PB)"
Zack Williams52209662019-02-07 10:15:31 -070049
50# Generic targets
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030051protos: python-protos go-protos java-protos
Zack Williams52209662019-02-07 10:15:31 -070052
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030053build: protos python-build go-protos java-protos
Zack Williams52209662019-02-07 10:15:31 -070054
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030055test: python-test go-test java-test
Zack Williams52209662019-02-07 10:15:31 -070056
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030057clean: python-clean go-clean java-clean
Zack Williams52209662019-02-07 10:15:31 -070058
59# Python targets
Zack Williams25e0a322019-06-07 14:07:21 -070060python-protos: protoc_check $(PROTO_PYTHON_PB2)
Zack Williams52209662019-02-07 10:15:31 -070061
62venv_protos:
63 virtualenv $@;\
64 source ./$@/bin/activate ; set -u ;\
65 pip install grpcio-tools googleapis-common-protos
66
67$(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/voltha_protos/%.proto Makefile venv_protos
68 source ./venv_protos/bin/activate ; set -u ;\
69 python -m grpc_tools.protoc \
Zack Williams43bfd9e2019-04-12 13:09:31 -070070 -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 $<
Zack Williams52209662019-02-07 10:15:31 -070077
Zack Williams43bfd9e2019-04-12 13:09:31 -070078python-build: setup.py python-protos
William Kurkian4ae78492019-06-11 20:33:05 -040079 rm -rf dist/
Zack Williams52209662019-02-07 10:15:31 -070080 python ./setup.py sdist
81
82python-test: tox.ini setup.py python-protos
83 tox
84
85python-clean:
Matt Jeanneret37e0fc62019-03-07 12:33:21 -050086 find python/ -name '*.pyc' | xargs rm -f
Zack Williams43bfd9e2019-04-12 13:09:31 -070087 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)
Zack Williams52209662019-02-07 10:15:31 -0700100
101# Go targets
Scott Baker39210792019-10-03 15:00:52 -0700102go-protos: protoc_check $(PROTO_GO_PB) voltha.pb
Zack Williams43bfd9e2019-04-12 13:09:31 -0700103
104go_temp:
105 mkdir -p go_temp
106
107$(PROTO_GO_PB): $(PROTO_FILES) go_temp
108 @echo "Creating $@"
109 cd protos && protoc \
110 --go_out=MAPS=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor,plugins=grpc,paths=source_relative:../go_temp \
111 -I . voltha_protos/$$(echo $@ | sed -n 's/.*\/\(.*\).pb.go/\1.proto/p' )
112 mkdir -p $(dir $@)
113 mv go_temp/voltha_protos/$(notdir $@) $@
114
Scott Baker39210792019-10-03 15:00:52 -0700115voltha.pb: ${PROTO_FILES}
Zack Williams43bfd9e2019-04-12 13:09:31 -0700116 @echo "Creating $@"
117 protoc -I protos -I protos/google/api \
118 --include_imports --include_source_info \
119 --descriptor_set_out=$@ \
120 ${PROTO_FILES}
Zack Williams52209662019-02-07 10:15:31 -0700121
Zack Williams25e0a322019-06-07 14:07:21 -0700122go-test: protoc_check
William Kurkianad745652019-03-20 08:45:51 -0400123 test/test-go-proto-consistency.sh
Scott Bakerb6b797c2019-10-04 15:50:09 -0700124 GO111MODULE=on go mod verify
Zack Williams52209662019-02-07 10:15:31 -0700125
126go-clean:
Zack Williams43bfd9e2019-04-12 13:09:31 -0700127 rm -rf go_temp
Zack Williams52209662019-02-07 10:15:31 -0700128
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +0300129# Java targets
130java-protos: protoc_check $(PROTO_JAVA_PB) move-java-protos voltha.pb
131
132java_temp:
133 mkdir -p java_temp/src/main/java
134
135$(PROTO_JAVA_PB): $(PROTO_FILES) java_temp
136 @echo "Creating $@"
137 cd protos && protoc \
138 --java_out=../java_temp/src/main/java \
139 -I . voltha_protos/$$(echo $@ | sed -n 's/.*\/\(.*\).pb.java/\1.proto/p' )
140
141move-java-protos: protoc_check
142 cp -r java_temp/src/main/java/* java/
143
144# Tests if the generated java classes are compilable
145java-test: java-protos
146 cp test/pom.xml java_temp
147 cd java_temp && mvn compile
148
149java-clean:
150 rm -rf java
151 rm -rf java_temp
152
153
Zack Williams25e0a322019-06-07 14:07:21 -0700154# Protobuf compiler helper functions
155protoc_check:
156ifeq ("", "$(shell which protoc)")
157 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
158 @echo "It looks like you don't have a version of protocol buffer tools."
159 @echo "To install the protocol buffer toolchain on Linux, you can run:"
160 @echo " make install-protoc"
161 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
162 exit 1
163endif
164ifneq ("libprotoc 3.7.0", "$(shell protoc --version)")
165 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
166 @echo "You have the wrong version of protoc installed"
167 @echo "Please install version 3.7.0"
168 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
169 exit 1
170endif
171
William Kurkian6ea97f82019-03-13 15:51:55 -0400172install-protoc:
Zack Williams25e0a322019-06-07 14:07:21 -0700173 @echo "Downloading and installing protocol buffer support (Linux amd64 only)"
174ifneq ("Linux", "$(shell uname -s)")
175 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
176 @echo "Automated installation of protoc not supported on $(shell uname -s)"
177 @echo "Please install protoc v3.7.0 from:"
178 @echo " https://github.com/protocolbuffers/protobuf/releases"
179 @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
180 exit 1
181endif
Matt Jeanneret61e94872019-03-22 16:16:01 -0400182 @echo "Installation will require sudo priviledges"
William Kurkian6ea97f82019-03-13 15:51:55 -0400183 @echo "This will take a few minutes."
Zack Williams25e0a322019-06-07 14:07:21 -0700184 @echo "Asking for sudo credentials now so we can install at the end"
William Kurkian6ea97f82019-03-13 15:51:55 -0400185 sudo echo "Thanks"; \
Zack Williams25e0a322019-06-07 14:07:21 -0700186 PROTOC_VERSION="3.7.0" ;\
187 PROTOC_SHA256SUM="a1b8ed22d6dc53c5b8680a6f1760a305b33ef471bece482e92728f00ba2a2969" ;\
188 curl -L -o /tmp/protoc-$${PROTOC_VERSION}-linux-x86_64.zip https://github.com/google/protobuf/releases/download/v$${PROTOC_VERSION}/protoc-$${PROTOC_VERSION}-linux-x86_64.zip ;\
189 echo "$${PROTOC_SHA256SUM} /tmp/protoc-$${PROTOC_VERSION}-linux-x86_64.zip" | sha256sum -c - ;\
190 unzip /tmp/protoc-$${PROTOC_VERSION}-linux-x86_64.zip -d /tmp/protoc3 ;\
191 sudo mv /tmp/protoc3/bin/* /usr/local/bin/ ;\
192 sudo mv /tmp/protoc3/include/* /usr/local/include/ ;\
193 sudo chmod -R a+rx /usr/local/bin/* ;\
194 sudo chmod -R a+rX /usr/local/include/