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 | |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 35 | PROTOC_PREFIX := /usr/local |
| 36 | PROTOC_VERSION := "3.7.0" |
| 37 | PROTOC_DOWNLOAD_PREFIX := "https://github.com/google/protobuf/releases/download" |
| 38 | PROTOC_DIR := protobuf-$(PROTOC_VERSION) |
| 39 | PROTOC_TARBALL := protobuf-python-$(PROTOC_VERSION).tar.gz |
| 40 | PROTOC_DOWNLOAD_URI := $(PROTOC_DOWNLOAD_PREFIX)/v$(PROTOC_VERSION)/$(PROTOC_TARBALL) |
| 41 | PROTOC_BUILD_TMP_DIR := "/tmp/protobuf-build-$(shell uname -s | tr '[:upper:]' '[:lower:]')" |
| 42 | |
Matt Jeanneret | 15249fa | 2019-04-12 20:25:31 -0400 | [diff] [blame] | 43 | # Force pb file to be regenrated every time. Otherwise the make process assumes whats there is still ok |
| 44 | .PHONY: go/voltha.pb |
| 45 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 46 | print: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 47 | @echo "Proto files: $(PROTO_FILES)" |
| 48 | @echo "Python PB2 files: $(PROTO_PYTHON_PB2)" |
| 49 | @echo "Go PB files: $(PROTO_GO_PB)" |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 50 | |
| 51 | # Generic targets |
| 52 | protos: python-protos go-protos |
| 53 | |
William Kurkian | bd3736d | 2019-03-08 12:20:40 -0500 | [diff] [blame] | 54 | build: protos python-build go-protos |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 55 | |
| 56 | test: python-test go-test |
| 57 | |
| 58 | clean: python-clean go-clean |
| 59 | |
| 60 | # Python targets |
| 61 | python-protos: $(PROTO_PYTHON_PB2) |
| 62 | |
| 63 | venv_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 \ |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 71 | -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 | $< |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 78 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 79 | python-build: setup.py python-protos |
William Kurkian | 1df4e71 | 2019-06-07 17:08:26 -0400 | [diff] [blame^] | 80 | rm -r dist/ |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 81 | python ./setup.py sdist |
| 82 | |
| 83 | python-test: tox.ini setup.py python-protos |
| 84 | tox |
| 85 | |
| 86 | python-clean: |
Matt Jeanneret | 37e0fc6 | 2019-03-07 12:33:21 -0500 | [diff] [blame] | 87 | find python/ -name '*.pyc' | xargs rm -f |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 88 | rm -rf \ |
| 89 | .coverage \ |
| 90 | .tox \ |
| 91 | coverage.xml \ |
| 92 | dist \ |
| 93 | nose2-results.xml \ |
| 94 | python/__pycache__ \ |
| 95 | python/test/__pycache__ \ |
| 96 | python/voltha_protos.egg-info \ |
| 97 | venv_protos \ |
| 98 | $(PROTO_PYTHON_DEST_DIR)/*.desc \ |
| 99 | $(PROTO_PYTHON_PB2) \ |
| 100 | $(PROTO_PYTHON_PB2_GRPC) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 101 | |
| 102 | # Go targets |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 103 | go-protos: protoc_check_version $(PROTO_GO_PB) go/voltha.pb |
| 104 | |
| 105 | protoc_check_version: |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 106 | ifeq ("", "$(shell which protoc)") |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 107 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 108 | @echo "It looks like you don't have a version of protocol buffer tools." |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 109 | @echo "To install the protocol buffer toolchain, you can run:" |
| 110 | @echo " make install-protoc" |
| 111 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
Matt Jeanneret | 61e9487 | 2019-03-22 16:16:01 -0400 | [diff] [blame] | 112 | exit 1 |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 113 | endif |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 114 | |
| 115 | go_temp: |
| 116 | mkdir -p go_temp |
| 117 | |
| 118 | $(PROTO_GO_PB): $(PROTO_FILES) go_temp |
| 119 | @echo "Creating $@" |
| 120 | cd protos && protoc \ |
| 121 | --go_out=MAPS=Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor,plugins=grpc,paths=source_relative:../go_temp \ |
| 122 | -I . voltha_protos/$$(echo $@ | sed -n 's/.*\/\(.*\).pb.go/\1.proto/p' ) |
| 123 | mkdir -p $(dir $@) |
| 124 | mv go_temp/voltha_protos/$(notdir $@) $@ |
| 125 | |
| 126 | go/voltha.pb: ${PROTO_FILES} |
| 127 | @echo "Creating $@" |
| 128 | protoc -I protos -I protos/google/api \ |
| 129 | --include_imports --include_source_info \ |
| 130 | --descriptor_set_out=$@ \ |
| 131 | ${PROTO_FILES} |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 132 | |
| 133 | go-test: |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 134 | ifneq ("libprotoc 3.7.0", "$(shell protoc --version)") |
| 135 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
| 136 | @echo "It looks like you don't have protocol buffer tools ${PROTOC_VERSION} installed." |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 137 | @echo "To install this version, you can run:" |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 138 | @echo " make install-protoc" |
| 139 | @echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" |
Matt Jeanneret | 61e9487 | 2019-03-22 16:16:01 -0400 | [diff] [blame] | 140 | exit 1 |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 141 | endif |
| 142 | test/test-go-proto-consistency.sh |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 143 | |
| 144 | go-clean: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 145 | rm -rf go_temp |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 146 | |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 147 | install-protoc: |
| 148 | @echo "Downloading and installing protocol buffer support." |
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." |
| 151 | mkdir -p $(PROTOC_BUILD_TMP_DIR) |
| 152 | @echo "We ask for sudo credentials now so we can install at the end"; \ |
| 153 | sudo echo "Thanks"; \ |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 154 | cd $(PROTOC_BUILD_TMP_DIR); \ |
| 155 | wget $(PROTOC_DOWNLOAD_URI); \ |
| 156 | tar xzvf $(PROTOC_TARBALL); \ |
| 157 | cd $(PROTOC_DIR); \ |
| 158 | ./configure --prefix=$(PROTOC_PREFIX); \ |
| 159 | make; \ |
| 160 | sudo make install; \ |
| 161 | sudo ldconfig |