Add non-fatal mode for verify_uses_libraries check.

The new mode is enabled with environment variable
RELAX_USES_LIBRARY_CHECK. If the variable is set to true, then a
verify_uses_libraries check failure does not fail the build, instead it
sets a special compiler filter "extract" for dexpreopt, which means that
the DEX file will be extracted, but it won't be compiled to native code.
Class loader context will be set to empty in this case (not &, as it is
going to be deprecated soon).

If the variable RELAX_USES_LIBRARY_CHECK is unset or set to something
other than "true", then the old behaviour of the verify_uses_libraries
check is preserved.

The intended use case for this flag is to have a smoother migration path
for the Java modules that need to add <uses-library> information in
the build files. The flag allows to quickly silence build errors. This
flag should be used with caution and only as a temporary measure, as it
masks real errors and affects performance.

verify_uses_libraries check is reworked so that it writes the error
message to a status file (which is used instead of the former timestamp
file). Currently the stored error message is not used, but it may be
used later to produce a warning. Dexpreopt command checks if the status
file exists and is nonempty; if that is the case, then compiler filter
is set to "extract".

Bug: 132357300

Test: Manually add some mismatch between the libraries in the Android.bp
      and Android.mk files for dexpreopted apps, build with
      RELAX_USES_LIBRARY_CHECK=true and obsserve that the build doesn't
      fail and they are compiled with compiler-filter "extract".
      Unset RELAX_USES_LIBRARY_CHECK and observe that the build fails.

Change-Id: Ie1a6298c4dba2f368ca9ac8b9fee2de0a26e68e8
This commit is contained in:
Ulya Trafimovich
2021-02-17 16:05:21 +00:00
parent 714c4bceb3
commit 5a09c2084d
5 changed files with 36 additions and 9 deletions

View File

@@ -104,17 +104,19 @@ endif
my_enforced_uses_libraries :=
ifdef LOCAL_ENFORCE_USES_LIBRARIES
my_enforced_uses_libraries := $(intermediates.COMMON)/enforce_uses_libraries.timestamp
my_enforced_uses_libraries := $(intermediates.COMMON)/enforce_uses_libraries.status
$(my_enforced_uses_libraries): PRIVATE_USES_LIBRARIES := $(LOCAL_USES_LIBRARIES)
$(my_enforced_uses_libraries): PRIVATE_OPTIONAL_USES_LIBRARIES := $(LOCAL_OPTIONAL_USES_LIBRARIES)
$(my_enforced_uses_libraries): PRIVATE_RELAX_CHECK := $(RELAX_USES_LIBRARY_CHECK)
$(my_enforced_uses_libraries): $(BUILD_SYSTEM)/verify_uses_libraries.sh $(AAPT)
$(my_enforced_uses_libraries): $(my_prebuilt_src_file)
@echo Verifying uses-libraries: $<
rm -f $@
aapt_binary=$(AAPT) \
uses_library_names="$(strip $(PRIVATE_USES_LIBRARIES))" \
optional_uses_library_names="$(strip $(PRIVATE_OPTIONAL_USES_LIBRARIES))" \
$(BUILD_SYSTEM)/verify_uses_libraries.sh $<
touch $@
relax_check="$(strip $(PRIVATE_RELAX_CHECK))" \
$(BUILD_SYSTEM)/verify_uses_libraries.sh $< $@
$(built_module) : $(my_enforced_uses_libraries)
endif

View File

@@ -479,6 +479,17 @@ USE_PREBUILT_SDK_TOOLS_IN_PLACE := true
USE_D8 := true
.KATI_READONLY := USE_D8
# Whether to fail immediately if verify_uses_libraries check fails, or to keep
# going and restrict dexpreopt to not compile any code for the failed module.
#
# The intended use case for this flag is to have a smoother migration path for
# the Java modules that need to add <uses-library> information in their build
# files. The flag allows to quickly silence build errors. This flag should be
# used with caution and only as a temporary measure, as it masks real errors
# and affects performance.
RELAX_USES_LIBRARY_CHECK ?= false
.KATI_READONLY := RELAX_USES_LIBRARY_CHECK
#
# Tools that are prebuilts for TARGET_BUILD_USE_PREBUILT_SDKS
#

View File

@@ -105,6 +105,7 @@ ifeq ($(WRITE_SOONG_VARIABLES),true)
$(call add_json_bool, IsEng, $(filter eng,$(TARGET_BUILD_VARIANT)))
$(call add_json_bool, SanitizeLite, $(SANITIZE_LITE))
$(call add_json_bool, DefaultAppImages, $(WITH_DEX_PREOPT_APP_IMAGE))
$(call add_json_bool, RelaxUsesLibraryCheck, $(filter true,$(RELAX_USES_LIBRARY_CHECK)))
$(call add_json_str, Dex2oatXmx, $(DEX2OAT_XMX))
$(call add_json_str, Dex2oatXms, $(DEX2OAT_XMS))
$(call add_json_str, EmptyDirectory, $(OUT_DIR)/empty)

View File

@@ -278,6 +278,7 @@ ifdef LOCAL_DEX_PREOPT
$(call add_json_list, PreoptFlags, $(LOCAL_DEX_PREOPT_FLAGS))
$(call add_json_str, ProfileClassListing, $(if $(my_process_profile),$(LOCAL_DEX_PREOPT_PROFILE)))
$(call add_json_bool, ProfileIsTextListing, $(my_profile_is_text_listing))
$(call add_json_str, EnforceUsesLibrariesStatusFile, $(intermediates.COMMON)/enforce_uses_libraries.status)
$(call add_json_bool, EnforceUsesLibraries, $(LOCAL_ENFORCE_USES_LIBRARIES))
$(call add_json_str, ProvidesUsesLibrary, $(firstword $(LOCAL_PROVIDES_USES_LIBRARY) $(LOCAL_MODULE)))
$(call add_json_map, ClassLoaderContexts)
@@ -345,6 +346,9 @@ ifdef LOCAL_DEX_PREOPT
$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/javalib.jar)
my_dexpreopt_deps += $(my_dexpreopt_images_deps)
my_dexpreopt_deps += $(DEXPREOPT_BOOTCLASSPATH_DEX_FILES)
ifeq ($(LOCAL_ENFORCE_USES_LIBRARIES),true)
my_dexpreopt_deps += $(intermediates.COMMON)/enforce_uses_libraries.status
endif
$(my_dexpreopt_zip): PRIVATE_MODULE := $(LOCAL_MODULE)
$(my_dexpreopt_zip): $(my_dexpreopt_deps)

View File

@@ -21,6 +21,7 @@
set -e
local_apk=$1
status_file=$2
badging=$(${aapt_binary} dump badging "${local_apk}")
export sdk_version=$(echo "${badging}" | grep "sdkVersion" | sed -n "s/sdkVersion:'\(.*\)'/\1/p")
# Export target_sdk_version to the caller.
@@ -28,20 +29,28 @@ export target_sdk_version=$(echo "${badging}" | grep "targetSdkVersion" | sed -n
uses_libraries=$(echo "${badging}" | grep "uses-library" | sed -n "s/uses-library:'\(.*\)'/\1/p")
optional_uses_libraries=$(echo "${badging}" | grep "uses-library-not-required" | sed -n "s/uses-library-not-required:'\(.*\)'/\1/p")
errmsg=
# Verify that the uses libraries match exactly.
# Currently we validate the ordering of the libraries since it matters for resolution.
single_line_libs=$(echo "${uses_libraries}" | tr '\n' ' ' | awk '{$1=$1}1')
if [[ "${single_line_libs}" != "${uses_library_names}" ]]; then
echo "LOCAL_USES_LIBRARIES (${uses_library_names})" \
"do not match (${single_line_libs}) in manifest for ${local_apk}"
exit 1
errmsg="LOCAL_USES_LIBRARIES (${uses_library_names}) do not match (${single_line_libs}) in manifest for ${local_apk}"
fi
# Verify that the optional uses libraries match exactly.
single_line_optional_libs=$(echo "${optional_uses_libraries}" | tr '\n' ' ' | awk '{$1=$1}1')
if [[ "${single_line_optional_libs}" != "${optional_uses_library_names}" ]]; then
echo "LOCAL_OPTIONAL_USES_LIBRARIES (${optional_uses_library_names}) " \
"do not match (${single_line_optional_libs}) in manifest for ${local_apk}"
exit 1
errmsg="LOCAL_OPTIONAL_USES_LIBRARIES (${optional_uses_library_names}) do not match (${single_line_optional_libs}) in manifest for ${local_apk}"
fi
if [[ ! -z "${errmsg}" ]]; then
echo "${errmsg}" > "${status_file}"
if [[ "${relax_check}" != true ]]; then
# fail immediately
echo "${errmsg}"
exit 1
fi
else
touch "${status_file}"
fi