1# Copyright (C) 2022 The Android Open Source Project 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# Generate Rust bindings for C headers 17# 18# Bindgen reads C headers and generates compatible Rust declarations of types 19# and APIs. This allows us to keep Rust code in sync with C code that it depends 20# on. 21# 22# Input variables: 23# MODULE_BINDGEN_ALLOW_FILES 24# MODULE_BINDGEN_ALLOW_FUNCTIONS 25# MODULE_BINDGEN_ALLOW_TYPES 26# MODULE_BINDGEN_ALLOW_VARS 27# MODULE_BINDGEN_CTYPES_PREFIX 28# MODULE_BINDGEN_FLAGS 29# MODULE_BINDGEN_OUTPUT_ENV_VAR 30# MODULE_BINDGEN_SRC_HEADER 31 32ifeq ($(strip $(MODULE_BINDGEN_SRC_HEADER)),) 33$(error $(MODULE): MODULE_BINDGEN_SRC_HEADER is required to use bindgen.mk) 34endif 35 36BINDGEN := $(CLANG_TOOLS_BINDIR)/bindgen 37 38MODULE_BINDGEN_OUTPUT_FILE := $(call TOBUILDDIR,$(patsubst %.h,%.rs,$(MODULE_BINDGEN_SRC_HEADER))) 39 40# Trusty rust is all no_std 41ifeq ($(MODULE_IS_KERNEL),true) 42MODULE_BINDGEN_FLAGS += --use-core --ctypes-prefix 'core::ffi' 43else 44MODULE_BINDGEN_FLAGS += --use-core --ctypes-prefix 'trusty_sys' 45endif 46 47ifneq ($(strip $(MODULE_BINDGEN_CTYPES_PREFIX)),) 48MODULE_BINDGEN_FLAGS += --ctypes-prefix $(MODULE_BINDGEN_CTYPES_PREFIX) 49endif 50 51MODULE_BINDGEN_FLAGS += $(addprefix --allowlist-var ,$(MODULE_BINDGEN_ALLOW_VARS)) 52MODULE_BINDGEN_FLAGS += $(addprefix --allowlist-type ,$(MODULE_BINDGEN_ALLOW_TYPES)) 53MODULE_BINDGEN_FLAGS += $(addprefix --allowlist-function ,$(MODULE_BINDGEN_ALLOW_FUNCTIONS)) 54MODULE_BINDGEN_FLAGS += $(addprefix --allowlist-file ,$(MODULE_BINDGEN_ALLOW_FILES)) 55# other sanitizer flags if present will cause bindgen to fail unless we pass 56# -fno-sanitize-ignorelist, see https://issues.chromium.org/issues/40265121 57BINDGEN_MODULE_COMPILEFLAGS := $(MODULE_COMPILEFLAGS) -fvisibility=default -fno-sanitize-ignorelist 58 59ifeq ($(call TOBOOL,$(TRUSTY_USERSPACE)),true) 60# library.mk overrides GLOBAL_COMPILEFLAGS and GLOBAL_INCLUDES for the .o, so we 61# must do the same here for bindgen 62$(MODULE_BINDGEN_OUTPUT_FILE): GLOBAL_COMPILEFLAGS := $(GLOBAL_SHARED_COMPILEFLAGS) $(GLOBAL_USER_COMPILEFLAGS) $(GLOBAL_USER_IN_TREE_COMPILEFLAGS) $(GLOBAL_USER_IN_TREE_CPPFLAGS) 63$(MODULE_BINDGEN_OUTPUT_FILE): GLOBAL_INCLUDES := $(addprefix -I,$(GLOBAL_UAPI_INCLUDES) $(GLOBAL_SHARED_INCLUDES) $(GLOBAL_USER_INCLUDES)) 64endif 65 66-include $(MODULE_BINDGEN_OUTPUT_FILE).d 67 68$(MODULE_BINDGEN_OUTPUT_FILE): BINDGEN := $(BINDGEN) 69$(MODULE_BINDGEN_OUTPUT_FILE): BINDGEN_MODULE_COMPILEFLAGS := $(BINDGEN_MODULE_COMPILEFLAGS) 70$(MODULE_BINDGEN_OUTPUT_FILE): BINDGEN_MODULE_INCLUDES := $(addprefix -I,$(MODULE_INCLUDES)) 71$(MODULE_BINDGEN_OUTPUT_FILE): ARCH_COMPILEFLAGS := $(ARCH_$(ARCH)_COMPILEFLAGS) 72$(MODULE_BINDGEN_OUTPUT_FILE): DEFINES := $(addprefix -D,$(MODULE_DEFINES)) 73$(MODULE_BINDGEN_OUTPUT_FILE): MODULE_BINDGEN_FLAGS := $(MODULE_BINDGEN_FLAGS) 74$(MODULE_BINDGEN_OUTPUT_FILE): $(MODULE_BINDGEN_SRC_HEADER) $(BINDGEN) $(MODULE_SRCDEPS) $(CONFIGHEADER) 75 @$(MKDIR) 76 $(NOECHO) 77 CLANG_PATH=$(BINDGEN_CLANG_PATH) \ 78 LIBCLANG_PATH=$(BINDGEN_LIBCLANG_PATH) \ 79 $(BINDGEN) $< -o $@.tmp $(MODULE_BINDGEN_FLAGS) --depfile $@.d -- $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) $(BINDGEN_MODULE_COMPILEFLAGS) $(BINDGEN_MODULE_INCLUDES) $(GLOBAL_INCLUDES) $(DEFINES) 80 @$(call TESTANDREPLACEFILE,$@.tmp,$@) 81 82MODULE_SRCDEPS += $(MODULE_BINDGEN_OUTPUT_FILE) 83 84ifeq ($(MODULE_BINDGEN_OUTPUT_ENV_VAR),) 85MODULE_BINDGEN_OUTPUT_ENV_VAR := BINDGEN_INC_FILE 86endif 87MODULE_RUST_ENV := $(MODULE_RUST_ENV) $(MODULE_BINDGEN_OUTPUT_ENV_VAR)=$(MODULE_BINDGEN_OUTPUT_FILE) 88 89MODULE_BINDGEN_ALLOW_FILES := 90MODULE_BINDGEN_ALLOW_FUNCTIONS := 91MODULE_BINDGEN_ALLOW_TYPES := 92MODULE_BINDGEN_ALLOW_VARS := 93MODULE_BINDGEN_CTYPES_PREFIX := 94MODULE_BINDGEN_OUTPUT_ENV_VAR := 95MODULE_BINDGEN_SRC_HEADER := 96 97BINDGEN := 98MODULE_BINDGEN_FLAGS := 99BINDGEN_MODULE_COMPILEFLAGS := 100MODULE_BINDGEN_OUTPUT_FILE := 101