blob: 9c803bd956332ff986ea0eb41c5f456315c593bb [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
Kent Hagerman868dc382020-01-20 11:24:09 -050021# tool containers
22VOLTHA_TOOLS_VERSION ?= 1.0.3
23
24PROTOC = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app 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/voltha-protos/v3 --workdir=/go/src/github.com/opencord/voltha-protos/v3 voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-protoc sh -c
26GO = docker run --rm --user $$(id -u):$$(id -g) -v ${CURDIR}:/app -v gocache:/.cache -v gocache-${VOLTHA_TOOLS_VERSION}:/go/pkg voltha/voltha-ci-tools:${VOLTHA_TOOLS_VERSION}-golang go
27
Zack Williams43bfd9e2019-04-12 13:09:31 -070028# Function to extract the last path component from go_package line in .proto files
29define go_package_path
30$(shell grep go_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
31endef
32
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030033# Function to extract the last path component from package line in .proto files
34define java_package_path
35$(shell grep package $(1) | sed -n 's/.*\/\(.*\)";/\1/p')
36endef
37
Zack Williams52209662019-02-07 10:15:31 -070038# Variables
Zack Williams43bfd9e2019-04-12 13:09:31 -070039PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto))
40
Zack Williams52209662019-02-07 10:15:31 -070041PROTO_PYTHON_DEST_DIR := python/voltha_protos
42PROTO_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 -050043PROTO_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 -070044PROTO_GO_DEST_DIR := go
45PROTO_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 +030046PROTO_JAVA_DEST_DIR := java
47PROTO_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 -070048# Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid
Kent Hagerman868dc382020-01-20 11:24:09 -050049.PHONY: voltha.pb
Matt Jeanneret15249fa2019-04-12 20:25:31 -040050
Zack Williams52209662019-02-07 10:15:31 -070051print:
Zack Williams43bfd9e2019-04-12 13:09:31 -070052 @echo "Proto files: $(PROTO_FILES)"
53 @echo "Python PB2 files: $(PROTO_PYTHON_PB2)"
54 @echo "Go PB files: $(PROTO_GO_PB)"
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030055 @echo "JAVA PB files: $(PROTO_JAVA_PB)"
Zack Williams52209662019-02-07 10:15:31 -070056
57# Generic targets
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030058protos: python-protos go-protos java-protos
Zack Williams52209662019-02-07 10:15:31 -070059
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030060build: protos python-build go-protos java-protos
Zack Williams52209662019-02-07 10:15:31 -070061
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +030062test: python-test go-test java-test
Zack Williams52209662019-02-07 10:15:31 -070063
Kent Hagerman868dc382020-01-20 11:24:09 -050064clean: python-clean java-clean
Zack Williams52209662019-02-07 10:15:31 -070065
66# Python targets
Kent Hagerman868dc382020-01-20 11:24:09 -050067python-protos: $(PROTO_PYTHON_PB2)
Zack Williams52209662019-02-07 10:15:31 -070068
69venv_protos:
70 virtualenv $@;\
71 source ./$@/bin/activate ; set -u ;\
72 pip install grpcio-tools googleapis-common-protos
73
74$(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/voltha_protos/%.proto Makefile venv_protos
75 source ./venv_protos/bin/activate ; set -u ;\
76 python -m grpc_tools.protoc \
Zack Williams43bfd9e2019-04-12 13:09:31 -070077 -I protos \
78 --python_out=python \
79 --grpc_python_out=python \
80 --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \
81 --include_imports \
82 --include_source_info \
83 $<
Zack Williams52209662019-02-07 10:15:31 -070084
Zack Williams43bfd9e2019-04-12 13:09:31 -070085python-build: setup.py python-protos
William Kurkian4ae78492019-06-11 20:33:05 -040086 rm -rf dist/
Zack Williams52209662019-02-07 10:15:31 -070087 python ./setup.py sdist
88
89python-test: tox.ini setup.py python-protos
90 tox
91
92python-clean:
Matt Jeanneret37e0fc62019-03-07 12:33:21 -050093 find python/ -name '*.pyc' | xargs rm -f
Zack Williams43bfd9e2019-04-12 13:09:31 -070094 rm -rf \
95 .coverage \
96 .tox \
97 coverage.xml \
98 dist \
99 nose2-results.xml \
100 python/__pycache__ \
101 python/test/__pycache__ \
102 python/voltha_protos.egg-info \
103 venv_protos \
104 $(PROTO_PYTHON_DEST_DIR)/*.desc \
105 $(PROTO_PYTHON_PB2) \
106 $(PROTO_PYTHON_PB2_GRPC)
Zack Williams52209662019-02-07 10:15:31 -0700107
108# Go targets
Kent Hagerman868dc382020-01-20 11:24:09 -0500109go-protos: voltha.pb
110 @echo "Creating *.go.pb files"
111 @${PROTOC_SH} " \
112 set -e -o pipefail; \
113 for x in ${PROTO_FILES}; do \
114 echo \$$x; \
115 protoc --go_out=plugins=grpc:/go/src -I protos \$$x; \
116 done"
Zack Williams43bfd9e2019-04-12 13:09:31 -0700117
Kent Hagerman868dc382020-01-20 11:24:09 -0500118voltha.pb:
Zack Williams43bfd9e2019-04-12 13:09:31 -0700119 @echo "Creating $@"
Kent Hagerman868dc382020-01-20 11:24:09 -0500120 @${PROTOC} -I protos -I protos/google/api \
121 --include_imports --include_source_info \
122 --descriptor_set_out=$@ \
123 ${PROTO_FILES}
Zack Williams43bfd9e2019-04-12 13:09:31 -0700124
Kent Hagerman868dc382020-01-20 11:24:09 -0500125go-test:
William Kurkianad745652019-03-20 08:45:51 -0400126 test/test-go-proto-consistency.sh
Kent Hagerman868dc382020-01-20 11:24:09 -0500127 ${GO} mod verify
Zack Williams52209662019-02-07 10:15:31 -0700128
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +0300129# Java targets
Kent Hagerman868dc382020-01-20 11:24:09 -0500130java-protos: voltha.pb
131 @echo "Creating java files"
132 @mkdir -p java_temp/src/main/java
133 @${PROTOC_SH} " \
134 set -e -o pipefail; \
135 for x in ${PROTO_FILES}; do \
136 echo \$$x; \
137 protoc --java_out=java_temp/src/main/java -I protos \$$x; \
138 done"
139 #TODO: generate directly to the final location
140 @mkdir -p java
Serkant Uluderyacbcfaa42019-10-18 13:25:08 +0300141 cp -r java_temp/src/main/java/* java/
142
143# Tests if the generated java classes are compilable
144java-test: java-protos
145 cp test/pom.xml java_temp
146 cd java_temp && mvn compile
147
148java-clean:
149 rm -rf java
150 rm -rf java_temp