1# Copyright (c) 2022, Google, Inc. All rights reserved 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 16# Invoke a protoc command with a custom plugin for protobuf-driven code 17# generation and build a library from the generated sources. 18# 19# args: 20# MODULE : module name (required) 21# MODULE_PROTOC_PLUGIN: path to a python protoc plugin (required) 22# MODULE_PROTOC_PLUGIN_FLAGS: optional flags for the custom plugin 23# shared via env variable 24# MODULE_PROTOS: list of PROTO files 25# MODULE_PROTO_PACKAGE: a path that matches the directory structure of 26# the PROTO package utilized in the module. 27 28PROTOC_TOOL ?= $(firstword $(wildcard out/host/linux-x86/bin/aprotoc prebuilts/libprotobuf/bin/protoc)) 29 30ifeq ($(PROTOC_TOOL),) 31$(error No PROTOC_TOOL. Please build the aprotoc or checkout with trusty manifest) 32endif 33 34ifeq ($(MODULE_PROTOC_PLUGIN),) 35$(error No MODULE_PROTOC_PLUGIN provided for $(MODULE)) 36endif 37 38# WARNING: this implies all sources are under the same package. 39# TODO(b/259511922): support multiple packages. 40MODULE_SRCS := $(call TOBUILDDIR,$(patsubst %.proto,%.c,$(MODULE_PROTOS))) 41MODULE_PROTO_OUT_DIR := $(sort $(dir $(subst $(MODULE_PROTO_PACKAGE),,$(MODULE_SRCS)))) 42 43# TODO: support multiple, disparate packages; 44# the output directory for the tool should be at the root of 45# the package path. 46$(MODULE_SRCS): PROTOC_TOOL := $(PROTOC_TOOL) 47$(MODULE_SRCS): MODULE_PROTOC_PLUGIN := $(MODULE_PROTOC_PLUGIN) 48$(MODULE_SRCS): MODULE_PROTOC_PLUGIN_FLAGS := $(MODULE_PROTOC_PLUGIN_FLAGS) 49$(MODULE_SRCS): MODULE_PROTO_PACKAGE := $(MODULE_PROTO_PACKAGE) 50$(MODULE_SRCS): MODULE_PROTO_OUT_DIR := $(MODULE_PROTO_OUT_DIR) 51$(MODULE_SRCS): $(BUILDDIR)/%.c: %.proto $(MODULE_PROTOC_PLUGIN) 52 @$(MKDIR) 53 @echo generating $@ from PROTO 54 $(NOECHO)$(PROTOC_TOOL) \ 55 --plugin=protoc-gen-custom-plugin=$(MODULE_PROTOC_PLUGIN) \ 56 --custom-plugin_out=$(MODULE_PROTO_OUT_DIR) \ 57 --custom-plugin_opt=pkg:$(MODULE_PROTO_PACKAGE),$(MODULE_PROTOC_PLUGIN_FLAGS) \ 58 $< 59 60MODULE_EXPORT_INCLUDES += $(MODULE_PROTO_OUT_DIR)/include 61 62# Ensure that all auto-generated code, including headers, is 63# emitted before downstream dependencies 64MODULE_EXPORT_SRCDEPS += $(MODULE_SRCS) 65 66# Build the PROTO module into a library 67include make/library.mk 68 69MODULE_PROTOS := 70PROTOC_TOOL := 71MODULE_PROTO_OUT_DIR := 72MODULE_PROTOC_PLUGIN := 73MODULE_PROTOC_PLUGIN_FLAGS := 74MODULE_PROTO_PACKAGE := 75