From 49a4e9b52e6878403e3ef57958f065691fd27ec4 Mon Sep 17 00:00:00 2001 From: Martin Stjernholm Date: Fri, 21 Aug 2020 16:36:23 +0100 Subject: [PATCH] Preserve symlinks in the Soong prebuilt install steps for host binaries and shared libs. If a prebuilt module file is a symlink in the .intermediates tree it is preserved when copied to the final install location. This preserves symlinks to prebuilt binaries, so that they are run from their prebuilt source location and can find libraries in relative paths from there. Symlinks are converted to absolute, to not fail if they are relative in the source tree. Test: m clean && m droid dist checkbuild with and without the change, then compare the lists of symlinks in the out/ trees Test: build/soong/soong_ui.bash --make-mode \ TARGET_PRODUCT=crosshatch_hwasan TARGET_BUILD_VARIANT=userdebug \ droid SANITIZE_TARGET=hwaddress \ continuous_instrumentation_tests_api_coverage \ continuous_native_tests device-tests platform_tests check that libclang_rt.hwasan_static-aarch64-android.a isn't a symlink in out/target/product/crosshatch/obj Bug: 145934348 Change-Id: I7600e5b0754f8ea6cd0ffc8e1ba6d39153f182aa --- core/base_rules.mk | 4 ++++ core/definitions.mk | 19 +++++++++++++++++++ core/soong_cc_prebuilt.mk | 8 ++++++++ core/soong_rust_prebuilt.mk | 4 ++++ 4 files changed, 35 insertions(+) diff --git a/core/base_rules.mk b/core/base_rules.mk index 739747046..2bc7f08fe 100644 --- a/core/base_rules.mk +++ b/core/base_rules.mk @@ -515,7 +515,11 @@ ifneq ($(LOCAL_INSTALLED_MODULE),$(my_default_test_module)) $(LOCAL_INSTALLED_MODULE): PRIVATE_POST_INSTALL_CMD := $(LOCAL_POST_INSTALL_CMD) $(LOCAL_INSTALLED_MODULE): $(LOCAL_BUILT_MODULE) @echo "Install: $@" +ifeq ($(LOCAL_MODULE_MAKEFILE),$(SOONG_ANDROID_MK)) + $(copy-file-or-link-to-new-target) +else $(copy-file-to-new-target) +endif $(PRIVATE_POST_INSTALL_CMD) endif diff --git a/core/definitions.mk b/core/definitions.mk index 2bf1ba6cc..2aff4881b 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -2551,6 +2551,18 @@ $(hide) rm -f $@ $(hide) cp $< $@ endef +# The same as copy-file-to-new-target, but preserve symlinks. Symlinks are +# converted to absolute to not break. +define copy-file-or-link-to-new-target +@mkdir -p $(dir $@) +$(hide) rm -f $@ +$(hide) if [ -h $< ]; then \ + ln -s $$(realpath $<) $@; \ +else \ + cp $< $@; \ +fi +endef + # Copy a prebuilt file to a target location. define transform-prebuilt-to-target @echo "$($(PRIVATE_PREFIX)DISPLAY) Prebuilt: $(PRIVATE_MODULE) ($@)" @@ -2563,6 +2575,13 @@ define transform-prebuilt-to-target-strip-comments $(copy-file-to-target-strip-comments) endef +# Copy a prebuilt file to a target location, but preserve symlinks rather than +# dereference them. +define copy-or-link-prebuilt-to-target +@echo "$($(PRIVATE_PREFIX)DISPLAY) Prebuilt: $(PRIVATE_MODULE) ($@)" +$(copy-file-or-link-to-new-target) +endef + # Copy a list of files/directories to target location, with sub dir structure preserved. # For example $(HOST_OUT_EXECUTABLES)/aapt -> $(staging)/bin/aapt . # $(1): the source list of files/directories. diff --git a/core/soong_cc_prebuilt.mk b/core/soong_cc_prebuilt.mk index c9b742a7b..a0315a542 100644 --- a/core/soong_cc_prebuilt.mk +++ b/core/soong_cc_prebuilt.mk @@ -142,8 +142,16 @@ ifeq ($(my_check_same_vndk_variants),true) $(LOCAL_BUILT_MODULE): $(same_vndk_variants_stamp) endif +# Use copy-or-link-prebuilt-to-target for host executables and shared libraries, +# to preserve symlinks to the source trees. They can then run directly from the +# prebuilt directories where the linker can load their dependencies using +# relative RUNPATHs. $(LOCAL_BUILT_MODULE): $(LOCAL_PREBUILT_MODULE_FILE) +ifeq ($(LOCAL_IS_HOST_MODULE) $(if $(filter EXECUTABLES SHARED_LIBRARIES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),true,),true true) + $(copy-or-link-prebuilt-to-target) +else $(transform-prebuilt-to-target) +endif ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),) $(hide) chmod +x $@ endif diff --git a/core/soong_rust_prebuilt.mk b/core/soong_rust_prebuilt.mk index 804e37edf..de6bafdc7 100644 --- a/core/soong_rust_prebuilt.mk +++ b/core/soong_rust_prebuilt.mk @@ -57,7 +57,11 @@ ifdef LOCAL_INSTALLED_MODULE endif $(LOCAL_BUILT_MODULE): $(LOCAL_PREBUILT_MODULE_FILE) +ifeq ($(LOCAL_IS_HOST_MODULE) $(if $(filter EXECUTABLES SHARED_LIBRARIES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),true,),true true) + $(copy-or-link-prebuilt-to-target) +else $(transform-prebuilt-to-target) +endif ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),) $(hide) chmod +x $@ endif