Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 1 | # 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 |
| 16 | default: test |
| 17 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 18 | # set default shell options |
| 19 | SHELL = bash -e -o pipefail |
| 20 | |
| 21 | # Function to extract the last path component from go_package line in .proto files |
| 22 | define go_package_path |
| 23 | $(shell grep go_package $(1) | sed -n 's/.*\/\(.*\)";/\1/p') |
| 24 | endef |
| 25 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 26 | # Variables |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 27 | PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto)) |
| 28 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 29 | PROTO_PYTHON_DEST_DIR := python/voltha_protos |
| 30 | PROTO_PYTHON_PB2 := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2.py,$(f))) |
Matt Jeanneret | 37e0fc6 | 2019-03-07 12:33:21 -0500 | [diff] [blame] | 31 | PROTO_PYTHON_PB2_GRPC := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_PYTHON_DEST_DIR)/%_pb2_grpc.py,$(f))) |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 32 | PROTO_GO_DEST_DIR := go |
| 33 | PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_GO_DEST_DIR)/$(call go_package_path,$(f))/%.pb.go,$(f))) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 34 | |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 35 | # Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid |
| 36 | .PHONY: go/voltha.pb protoc_check |
Matt Jeanneret | 15249fa | 2019-04-12 20:25:31 -0400 | [diff] [blame] | 37 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 38 | print: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 39 | @echo "Proto files: $(PROTO_FILES)" |
| 40 | @echo "Python PB2 files: $(PROTO_PYTHON_PB2)" |
| 41 | @echo "Go PB files: $(PROTO_GO_PB)" |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 42 | |
| 43 | # Generic targets |
| 44 | protos: python-protos go-protos |
| 45 | |
William Kurkian | bd3736d | 2019-03-08 12:20:40 -0500 | [diff] [blame] | 46 | build: protos python-build go-protos |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 47 | |
| 48 | test: python-test go-test |
| 49 | |
| 50 | clean: python-clean go-clean |
| 51 | |
| 52 | # Python targets |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 53 | python-protos: protoc_check $(PROTO_PYTHON_PB2) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 54 | |
| 55 | venv_protos: |
| 56 | virtualenv $@;\ |
| 57 | source ./$@/bin/activate ; set -u ;\ |
| 58 | pip install grpcio-tools googleapis-common-protos |
| 59 | |
| 60 | $(PROTO_PYTHON_DEST_DIR)/%_pb2.py: protos/voltha_protos/%.proto Makefile venv_protos |
| 61 | source ./venv_protos/bin/activate ; set -u ;\ |
| 62 | python -m grpc_tools.protoc \ |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 63 | -I protos \ |
| 64 | --python_out=python \ |
| 65 | --grpc_python_out=python \ |
| 66 | --descriptor_set_out=$(PROTO_PYTHON_DEST_DIR)/$(basename $(notdir $<)).desc \ |
| 67 | --include_imports \ |
| 68 | --include_source_info \ |
| 69 | $< |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 70 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 71 | python-build: setup.py python-protos |
William Kurkian | 4ae7849 | 2019-06-11 20:33:05 -0400 | [diff] [blame^] | 72 | rm -rf dist/ |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 73 | python ./setup.py sdist |
| 74 | |
| 75 | python-test: tox.ini setup.py python-protos |
| 76 | tox |
| 77 | |
| 78 | python-clean: |
Matt Jeanneret | 37e0fc6 | 2019-03-07 12:33:21 -0500 | [diff] [blame] | 79 | find python/ -name '*.pyc' | xargs rm -f |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 80 | rm -rf \ |
| 81 | .coverage \ |
| 82 | .tox \ |
| 83 | coverage.xml \ |
| 84 | dist \ |
| 85 | nose2-results.xml \ |
| 86 | python/__pycache__ \ |
| 87 | python/test/__pycache__ \ |
| 88 | python/voltha_protos.egg-info \ |
| 89 | venv_protos \ |
| 90 | $(PROTO_PYTHON_DEST_DIR)/*.desc \ |
| 91 | $(PROTO_PYTHON_PB2) \ |
| 92 | $(PROTO_PYTHON_PB2_GRPC) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 93 | |
| 94 | # Go targets |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 95 | go-protos: protoc_check $(PROTO_GO_PB) go/voltha.pb |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 96 | |
| 97 | go_temp: |
| 98 | mkdir -p go_temp |
| 99 | |
| 100 | $(PROTO_GO_PB): $(PROTO_FILES) go_temp |
| 101 | @echo "Creating $@" |
| 102 | cd protos && protoc \ |
| 103 | --go_out=MAPS=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor,plugins=grpc,paths=source_relative:../go_temp \ |
| 104 | -I . voltha_protos/$$(echo $@ | sed -n 's/.*\/\(.*\).pb.go/\1.proto/p' ) |
| 105 | mkdir -p $(dir $@) |
| 106 | mv go_temp/voltha_protos/$(notdir $@) $@ |
| 107 | |
| 108 | go/voltha.pb: ${PROTO_FILES} |
| 109 | @echo "Creating $@" |
| 110 | protoc -I protos -I protos/google/api \ |
| 111 | --include_imports --include_source_info \ |
| 112 | --descriptor_set_out=$@ \ |
| 113 | ${PROTO_FILES} |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 114 | |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 115 | go-test: protoc_check |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 116 | test/test-go-proto-consistency.sh |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 117 | |
| 118 | go-clean: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 119 | rm -rf go_temp |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 120 | |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 121 | # Protobuf compiler helper functions |
| 122 | protoc_check: |
| 123 | ifeq ("", "$(shell which protoc)") |
| 124 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 125 | @echo "It looks like you don't have a version of protocol buffer tools." |
| 126 | @echo "To install the protocol buffer toolchain on Linux, you can run:" |
| 127 | @echo " make install-protoc" |
| 128 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 129 | exit 1 |
| 130 | endif |
| 131 | ifneq ("libprotoc 3.7.0", "$(shell protoc --version)") |
| 132 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 133 | @echo "You have the wrong version of protoc installed" |
| 134 | @echo "Please install version 3.7.0" |
| 135 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 136 | exit 1 |
| 137 | endif |
| 138 | |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 139 | install-protoc: |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 140 | @echo "Downloading and installing protocol buffer support (Linux amd64 only)" |
| 141 | ifneq ("Linux", "$(shell uname -s)") |
| 142 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 143 | @echo "Automated installation of protoc not supported on $(shell uname -s)" |
| 144 | @echo "Please install protoc v3.7.0 from:" |
| 145 | @echo " https://github.com/protocolbuffers/protobuf/releases" |
| 146 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 147 | exit 1 |
| 148 | endif |
Matt Jeanneret | 61e9487 | 2019-03-22 16:16:01 -0400 | [diff] [blame] | 149 | @echo "Installation will require sudo priviledges" |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 150 | @echo "This will take a few minutes." |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 151 | @echo "Asking for sudo credentials now so we can install at the end" |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 152 | sudo echo "Thanks"; \ |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 153 | PROTOC_VERSION="3.7.0" ;\ |
| 154 | PROTOC_SHA256SUM="a1b8ed22d6dc53c5b8680a6f1760a305b33ef471bece482e92728f00ba2a2969" ;\ |
| 155 | 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 ;\ |
| 156 | echo "$${PROTOC_SHA256SUM} /tmp/protoc-$${PROTOC_VERSION}-linux-x86_64.zip" | sha256sum -c - ;\ |
| 157 | unzip /tmp/protoc-$${PROTOC_VERSION}-linux-x86_64.zip -d /tmp/protoc3 ;\ |
| 158 | sudo mv /tmp/protoc3/bin/* /usr/local/bin/ ;\ |
| 159 | sudo mv /tmp/protoc3/include/* /usr/local/include/ ;\ |
| 160 | sudo chmod -R a+rx /usr/local/bin/* ;\ |
| 161 | sudo chmod -R a+rX /usr/local/include/ |