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# Build an AIDL module for Trusty
17#
18# args:
19# MODULE : module name (required)
20# MODULE_AIDLS: list of AIDL files
21# MODULE_AIDL_FLAGS: optional flags for the AIDL_TOOL binary
22# MODULE_AIDL_PACKAGE: a path that matches the directory structure of the AIDL
23#     package utilized in the module. For example, declaring
24#     `package com.android.trusty.binder` should correspond to a
25#     MODULE_AIDL_PACKAGE of com/android/trusty/binder. If MODULE_AIDL_PACKAGE
26#     is not defined, it implies that there is no top-level `package`
27#     declaration.
28# MODULE_AIDL_LANGUAGE: the language to auto-generate the files for. Current
29#     options are `cpp` and `rust`.
30# MODULE_AIDL_RUST_DEPS: names of Rust AIDL crates that the current module
31#     depends on. Until we find a way to automatically infer these, users
32#     will have to specify them manually.
33
34# Check that there are is at most one package specified
35ifeq ($(filter $(words $(MODULE_AIDL_PACKAGE)),0 1),)
36$(error $(MODULE) has the following packages $(MODULE_AIDL_PACKAGE), but only one is supported)
37endif
38
39ifeq ($(MODULE_AIDL_LANGUAGE),)
40MODULE_AIDL_LANGUAGE := cpp
41endif
42
43ifeq ($(MODULE_AIDL_LANGUAGE),cpp)
44AIDL_EXT := cpp
45AIDL_HEADER_DIR := $(BUILDDIR)/include
46else ifeq ($(MODULE_AIDL_LANGUAGE),rust)
47AIDL_EXT := rs
48AIDL_HEADER_DIR :=
49else
50$(error "Unsupported AIDL language: $(MODULE_AIDL_LANGUAGE)")
51endif
52
53# TODO: this implies all sources are in MODULE_AIDL_PACKAGE or are subpackages
54# of MODULE_AIDL_PACKAGE; support multiple packages
55GET_AIDL_PACKAGE_ROOT = $(if $(MODULE_AIDL_PACKAGE),$(firstword $(subst $(MODULE_AIDL_PACKAGE), ,$1)),$(dir $1))
56
57ifneq (,$(wildcard out/host/linux-x86/bin/aidl))
58# Use the aidl tool from the build output if it exists
59AIDL_TOOL := out/host/linux-x86/bin/aidl
60else
61AIDL_TOOL := prebuilts/build-tools/linux-x86/bin/aidl
62endif
63
64MODULE_AIDL_INCLUDES ?=
65AIDL_SRCS := $(call TOBUILDDIR,$(patsubst %.aidl,%.$(AIDL_EXT),$(MODULE_AIDLS)))
66AIDL_RUST_GLUE_TOOL := system/tools/aidl/build/aidl_rust_glue.py
67MODULE_AIDL_INCLUDES += $(foreach dir,$(sort $(foreach src,$(MODULE_AIDLS),$(call GET_AIDL_PACKAGE_ROOT,$(src)))), -I $(patsubst %/,%,$(dir)))
68
69# TODO: support multiple, disparate packages; for AIDL interfaces with package paths,
70# the output directory for the tool should be at the root of
71# the package path. The compiler creates one subdirectory
72# per package component, e.g., com.foo.IFoo goes into com/foo/IFoo.cpp.
73# Luckily the .aidl files are also required to follow this structure,
74# so the input file is also com/foo/IFoo.aidl.
75$(AIDL_SRCS): AIDL_HEADER_DIR := $(AIDL_HEADER_DIR)
76$(AIDL_SRCS): AIDL_EXT := $(AIDL_EXT)
77$(AIDL_SRCS): AIDL_TOOL := $(AIDL_TOOL)
78$(AIDL_SRCS): MODULE_AIDL_INCLUDES := $(MODULE_AIDL_INCLUDES)
79$(AIDL_SRCS): MODULE_AIDL_FLAGS := $(MODULE_AIDL_FLAGS)
80$(AIDL_SRCS): MODULE_AIDL_LANGUAGE := $(MODULE_AIDL_LANGUAGE)
81$(AIDL_SRCS): MODULE_AIDL_PACKAGE := $(MODULE_AIDL_PACKAGE)
82$(AIDL_SRCS): MODULE := $(MODULE)
83$(AIDL_SRCS): $(BUILDDIR)/%.$(AIDL_EXT): %.aidl
84	@$(MKDIR)
85	@if [ -n "$(AIDL_HEADER_DIR)" ]; then mkdir -p $(AIDL_HEADER_DIR); fi
86	@$(call ECHO,$(MODULE),generating from AIDL,$@)
87	$(NOECHO)$(AIDL_TOOL) --lang=$(MODULE_AIDL_LANGUAGE) --structured $(MODULE_AIDL_INCLUDES) \
88		$(foreach dir,$(AIDL_HEADER_DIR),-h $(dir)) -o $(call GET_AIDL_PACKAGE_ROOT,$@) $(MODULE_AIDL_FLAGS) $<
89	@$(call ECHO_DONE_SILENT,$(MODULE),generating from AIDL,$@)
90
91ifeq ($(MODULE_AIDL_LANGUAGE),cpp)
92MODULE_SRCS += $(AIDL_SRCS)
93
94# AIDL generates .cpp files which depend on the binder and C++ modules
95ifeq ($(call TOBOOL,$(TRUSTY_NEW_MODULE_SYSTEM)),false)
96MODULE_DEPS += \
97	trusty/kernel/lib/libcxx-trusty \
98	frameworks/native/libs/binder/trusty/kernel
99else
100MODULE_LIBRARY_DEPS += \
101	trusty/user/base/lib/libstdc++-trusty \
102	frameworks/native/libs/binder/trusty
103endif
104
105MODULE_EXPORT_INCLUDES += $(AIDL_HEADER_DIR)
106
107# Ensure that all auto-generated code, including headers, is
108# emitted before downstream dependencies
109MODULE_EXPORT_SRCDEPS += $(AIDL_SRCS)
110else # Rust
111AIDL_ROOT_RS := $(sort $(foreach src,$(AIDL_SRCS),$(call GET_AIDL_PACKAGE_ROOT,$(src))/$(MODULE_CRATE_NAME).rs))
112
113ifneq ($(words $(AIDL_ROOT_RS)),1)
114$(error Unable to determine root AIDL .rs file for $(MODULE))
115endif
116
117# Generate the top-level aidl_lib.rs for this module
118$(AIDL_ROOT_RS): AIDL_RUST_GLUE_TOOL := $(AIDL_RUST_GLUE_TOOL)
119$(AIDL_ROOT_RS): MODULE_AIDL_RUST_DEPS := $(foreach crate,$(MODULE_AIDL_RUST_DEPS),-I $(crate))
120$(AIDL_ROOT_RS): $(AIDL_SRCS)
121	@echo generating $@ from AIDL Rust glue
122	$(NOECHO)$(AIDL_RUST_GLUE_TOOL) $(MODULE_AIDL_RUST_DEPS) $@ $(dir $@) $^
123
124MODULE_LIBRARY_DEPS += \
125	frameworks/native/libs/binder/trusty/rust \
126	external/rust/crates/async-trait \
127	external/rust/crates/lazy_static \
128
129# The AIDL compiler marks an aidl_data variable as mutable and rustc complains
130MODULE_RUSTFLAGS += -Aunused-mut -Aunused-variables
131
132MODULE_SRCS += $(AIDL_ROOT_RS)
133MODULE_EXPORT_SRCDEPS += $(AIDL_ROOT_RS)
134endif
135
136# Build the AIDL module into a library
137include make/library.mk
138
139MODULE_AIDLS :=
140MODULE_AIDL_INCLUDES :=
141MODULE_AIDL_FLAGS :=
142MODULE_AIDL_PACKAGE :=
143MODULE_AIDL_LANGUAGE :=
144MODULE_AIDL_RUST_DEPS :=
145AIDL_EXT :=
146AIDL_HEADER_DIR :=
147AIDL_SRCS :=
148AIDL_TOOL :=
149AIDL_RUST_GLUE_TOOL :=
150AIDL_ROOT_RS :=
151