TWRP_REQUIRED_MODULES: add more criteria to exclude packages

Only include hwservicemanager if CRYPTO flag is set and
only include servicemanager if FBE is included.

Only include health-hal if device uses dynamic partitions

Change-Id: I056d0246e406dc9f00bda65247455741ef986a37
diff --git a/Android.mk b/Android.mk
index ce86deb..86a53d3 100755
--- a/Android.mk
+++ b/Android.mk
@@ -80,11 +80,6 @@
 
 LOCAL_STATIC_LIBRARIES += libavb
 LOCAL_SHARED_LIBRARIES += libfs_mgr libinit
-ifeq ($(TW_INCLUDE_CRYPTO),true)
-    LOCAL_CFLAGS += -DUSE_FSCRYPT -Wno-macro-redefined
-    LOCAL_C_INCLUDES += bootable/recovery/crypto/fscrypt \
-        bootable/recovery/crypto
-endif
 LOCAL_C_INCLUDES += \
     system/core/fs_mgr/libfs_avb/include/ \
     system/core/fs_mgr/include_fstab/ \
@@ -151,6 +146,7 @@
 
 ifeq ($(PRODUCT_USE_DYNAMIC_PARTITIONS),true)
     LOCAL_CFLAGS += -DPRODUCT_USE_DYNAMIC_PARTITIONS=1
+    TWRP_REQUIRED_MODULES += android.hardware.health@2.0-service android.hardware.health@2.0-service.rc
 endif
 
 ifeq ($(TW_USES_VENDOR_LIBS),true)
@@ -292,9 +288,10 @@
     TW_INCLUDE_CRYPTO := true
 endif
 ifeq ($(TW_INCLUDE_CRYPTO), true)
-    LOCAL_CFLAGS += -DTW_INCLUDE_CRYPTO
+    LOCAL_CFLAGS += -DTW_INCLUDE_CRYPTO -DUSE_FSCRYPT -Wno-macro-redefined
     LOCAL_SHARED_LIBRARIES += libcryptfsfde libgpt_twrp
-    LOCAL_C_INCLUDES += external/boringssl/src/include
+    LOCAL_C_INCLUDES += external/boringssl/src/include bootable/recovery/crypto/fscrypt \
+        bootable/recovery/crypto
     TW_INCLUDE_CRYPTO_FBE := true
     LOCAL_CFLAGS += -DTW_INCLUDE_FBE
     LOCAL_SHARED_LIBRARIES += libtwrpfscrypt android.frameworks.stats@1.0 android.hardware.authsecret@1.0 \
@@ -396,27 +393,28 @@
     init.recovery.hlthchrg.rc \
     init.recovery.service.rc \
     init.recovery.ldconfig.rc \
-    hwservicemanager \
-    hwservicemanager.rc \
-    servicemanager \
-    servicemanager.rc \
     awk \
     toybox \
     toolbox \
     mkshrc_twrp \
-    android.hardware.health@2.0-service \
-    android.hardware.health@2.0-service.rc \
     plat_hwservice_contexts \
     vendor_hwservice_contexts
 
 ifneq ($(TW_INCLUDE_CRYPTO),)
 TWRP_REQUIRED_MODULES += \
-    plat_service_contexts \
+    hwservicemanager \
+    hwservicemanager.rc \
     vndservicemanager \
     vndservicemanager.rc \
     vold_prepare_subdirs \
     task_recovery_profiles.json \
     fscryptpolicyget
+    ifneq ($(TW_INCLUDE_CRYPTO_FBE),)
+    TWRP_REQUIRED_MODULES += \
+        plat_service_contexts \
+        servicemanager \
+        servicemanager.rc
+    endif
 endif
 
 ifneq ($(wildcard external/zip/Android.mk),)
diff --git a/etc/Android.mk b/etc/Android.mk
index f07a2c6..26d86d6 100755
--- a/etc/Android.mk
+++ b/etc/Android.mk
@@ -58,33 +58,6 @@
 LOCAL_SRC_FILES := init.recovery.ldconfig.rc
 include $(BUILD_PREBUILT)
 
-include $(CLEAR_VARS)
-LOCAL_MODULE := hwservicemanager.rc
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
-
-LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
-include $(BUILD_PREBUILT)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := servicemanager.rc
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
-
-LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
-include $(BUILD_PREBUILT)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := android.hardware.health@2.0-service.rc
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
-LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
-
-LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
-include $(BUILD_PREBUILT)
-
 ifeq ($(AB_OTA_UPDATER),true)
 	include $(CLEAR_VARS)
 	LOCAL_MODULE := android.hardware.boot@1.0-service.rc
@@ -96,8 +69,28 @@
 	include $(BUILD_PREBUILT)
 endif
 
+ifeq ($(PRODUCT_USE_DYNAMIC_PARTITIONS),true)
+	include $(CLEAR_VARS)
+	LOCAL_MODULE := android.hardware.health@2.0-service.rc
+	LOCAL_MODULE_TAGS := optional
+	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
+
+	LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
+	include $(BUILD_PREBUILT)
+endif
+
 ifneq ($(TW_INCLUDE_CRYPTO),)
 	include $(CLEAR_VARS)
+	LOCAL_MODULE := hwservicemanager.rc
+	LOCAL_MODULE_TAGS := optional
+	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+	LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
+
+	LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
+	include $(BUILD_PREBUILT)
+
+	include $(CLEAR_VARS)
 	LOCAL_MODULE := vndservicemanager.rc
 	LOCAL_MODULE_TAGS := optional
 	LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
@@ -105,6 +98,17 @@
 
 	LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
 	include $(BUILD_PREBUILT)
+
+	ifneq ($(TW_INCLUDE_CRYPTO_FBE),)
+		include $(CLEAR_VARS)
+		LOCAL_MODULE := servicemanager.rc
+		LOCAL_MODULE_TAGS := optional
+		LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+		LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/etc/init
+
+		LOCAL_SRC_FILES := init/$(LOCAL_MODULE)
+		include $(BUILD_PREBUILT)
+	endif
 endif
 
 ifeq ($(TWRP_INCLUDE_LOGCAT), true)