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 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 26 | # Function to extract the last path component from package line in .proto files |
| 27 | define java_package_path |
| 28 | $(shell grep package $(1) | sed -n 's/.*\/\(.*\)";/\1/p') |
| 29 | endef |
| 30 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 31 | # Variables |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 32 | PROTO_FILES := $(sort $(wildcard protos/voltha_protos/*.proto)) |
| 33 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 34 | PROTO_PYTHON_DEST_DIR := python/voltha_protos |
| 35 | 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] | 36 | 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] | 37 | PROTO_GO_DEST_DIR := go |
| 38 | PROTO_GO_PB:= $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_GO_DEST_DIR)/$(call go_package_path,$(f))/%.pb.go,$(f))) |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 39 | PROTO_JAVA_DEST_DIR := java |
| 40 | PROTO_JAVA_PB := $(foreach f, $(PROTO_FILES), $(patsubst protos/voltha_protos/%.proto,$(PROTO_JAVA_DEST_DIR)/$(call java_package_path,$(f))/%.pb.java,$(f))) |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 41 | # Force pb file to be regenrated every time. Otherwise the make process assumes generated version is still valid |
Scott Baker | 3921079 | 2019-10-03 15:00:52 -0700 | [diff] [blame] | 42 | .PHONY: voltha.pb protoc_check |
Matt Jeanneret | 15249fa | 2019-04-12 20:25:31 -0400 | [diff] [blame] | 43 | |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 44 | print: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 45 | @echo "Proto files: $(PROTO_FILES)" |
| 46 | @echo "Python PB2 files: $(PROTO_PYTHON_PB2)" |
| 47 | @echo "Go PB files: $(PROTO_GO_PB)" |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 48 | @echo "JAVA PB files: $(PROTO_JAVA_PB)" |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 49 | |
| 50 | # Generic targets |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 51 | protos: python-protos go-protos java-protos |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 52 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 53 | build: protos python-build go-protos java-protos |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 54 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 55 | test: python-test go-test java-test |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 56 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 57 | clean: python-clean go-clean java-clean |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 58 | |
| 59 | # Python targets |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 60 | python-protos: protoc_check $(PROTO_PYTHON_PB2) |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 61 | |
| 62 | venv_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 Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 70 | -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 Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 77 | |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 78 | python-build: setup.py python-protos |
William Kurkian | 4ae7849 | 2019-06-11 20:33:05 -0400 | [diff] [blame] | 79 | rm -rf dist/ |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 80 | python ./setup.py sdist |
| 81 | |
| 82 | python-test: tox.ini setup.py python-protos |
| 83 | tox |
| 84 | |
| 85 | python-clean: |
Matt Jeanneret | 37e0fc6 | 2019-03-07 12:33:21 -0500 | [diff] [blame] | 86 | find python/ -name '*.pyc' | xargs rm -f |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 87 | 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 Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 100 | |
| 101 | # Go targets |
Scott Baker | 3921079 | 2019-10-03 15:00:52 -0700 | [diff] [blame] | 102 | go-protos: protoc_check $(PROTO_GO_PB) voltha.pb |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 103 | |
| 104 | go_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 Baker | 3921079 | 2019-10-03 15:00:52 -0700 | [diff] [blame] | 115 | voltha.pb: ${PROTO_FILES} |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 116 | @echo "Creating $@" |
| 117 | protoc -I protos -I protos/google/api \ |
| 118 | --include_imports --include_source_info \ |
| 119 | --descriptor_set_out=$@ \ |
| 120 | ${PROTO_FILES} |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 121 | |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 122 | go-test: protoc_check |
William Kurkian | ad74565 | 2019-03-20 08:45:51 -0400 | [diff] [blame] | 123 | test/test-go-proto-consistency.sh |
Scott Baker | b6b797c | 2019-10-04 15:50:09 -0700 | [diff] [blame] | 124 | GO111MODULE=on go mod verify |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 125 | |
| 126 | go-clean: |
Zack Williams | 43bfd9e | 2019-04-12 13:09:31 -0700 | [diff] [blame] | 127 | rm -rf go_temp |
Zack Williams | 5220966 | 2019-02-07 10:15:31 -0700 | [diff] [blame] | 128 | |
Serkant Uluderya | cbcfaa4 | 2019-10-18 13:25:08 +0300 | [diff] [blame] | 129 | # Java targets |
| 130 | java-protos: protoc_check $(PROTO_JAVA_PB) move-java-protos voltha.pb |
| 131 | |
| 132 | java_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 | |
| 141 | move-java-protos: protoc_check |
| 142 | cp -r java_temp/src/main/java/* java/ |
| 143 | |
| 144 | # Tests if the generated java classes are compilable |
| 145 | java-test: java-protos |
| 146 | cp test/pom.xml java_temp |
| 147 | cd java_temp && mvn compile |
| 148 | |
| 149 | java-clean: |
| 150 | rm -rf java |
| 151 | rm -rf java_temp |
| 152 | |
| 153 | |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 154 | # Protobuf compiler helper functions |
| 155 | protoc_check: |
| 156 | ifeq ("", "$(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 |
| 163 | endif |
| 164 | ifneq ("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 |
| 170 | endif |
| 171 | |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 172 | install-protoc: |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 173 | @echo "Downloading and installing protocol buffer support (Linux amd64 only)" |
| 174 | ifneq ("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 |
| 181 | endif |
Matt Jeanneret | 61e9487 | 2019-03-22 16:16:01 -0400 | [diff] [blame] | 182 | @echo "Installation will require sudo priviledges" |
William Kurkian | 6ea97f8 | 2019-03-13 15:51:55 -0400 | [diff] [blame] | 183 | @echo "This will take a few minutes." |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 184 | @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] | 185 | sudo echo "Thanks"; \ |
Zack Williams | 25e0a32 | 2019-06-07 14:07:21 -0700 | [diff] [blame] | 186 | 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/ |