From af7ac3986eff70e23b2af833bca7901d5e7a3497 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 9 Nov 2022 15:52:56 -0800 Subject: [PATCH] Add basic musl support to make There are still devices that set BUILD_BROKEN_USES_BUILD_HOST_* and build host modules in Make. Add basic support for building against libc_musl in Make when USE_HOST_MUSL is set. Bug: 258535366 Test: build a host tool defined in Android.mk file in internal branch Change-Id: I9150be749bbeaac2ac5a33b2cf915004aa3033df --- core/binary.mk | 12 +++++++++--- core/config.mk | 8 -------- core/definitions.mk | 6 ++++++ core/host_executable_internal.mk | 15 +++++++++++++++ core/host_shared_library_internal.mk | 11 +++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/core/binary.mk b/core/binary.mk index 1ad9be8744..6320726d6a 100644 --- a/core/binary.mk +++ b/core/binary.mk @@ -19,7 +19,11 @@ include $(BUILD_SYSTEM)/use_lld_setup.mk # supply that, for example, when building libc itself. ifdef LOCAL_IS_HOST_MODULE ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none) + ifdef USE_HOST_MUSL + my_system_shared_libraries := libc_musl + else my_system_shared_libraries := + endif else my_system_shared_libraries := $(LOCAL_SYSTEM_SHARED_LIBRARIES) endif @@ -348,9 +352,11 @@ my_ldlibs := $(filter $(my_allowed_ldlibs),$(my_ldlibs)) else # LOCAL_IS_HOST_MODULE # Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of # device builds - my_ldlibs += -ldl -lpthread -lm - ifneq ($(HOST_OS),darwin) - my_ldlibs += -lrt + ifndef USE_HOST_MUSL + my_ldlibs += -ldl -lpthread -lm + ifneq ($(HOST_OS),darwin) + my_ldlibs += -lrt + endif endif endif diff --git a/core/config.mk b/core/config.mk index 631ba34e8b..5160762ea3 100644 --- a/core/config.mk +++ b/core/config.mk @@ -1049,14 +1049,6 @@ endif # PRODUCT_USE_DYNAMIC_PARTITIONS BOARD_PREBUILT_HIDDENAPI_DIR ?= .KATI_READONLY := BOARD_PREBUILT_HIDDENAPI_DIR -ifdef USE_HOST_MUSL - ifneq (,$(or $(BUILD_BROKEN_USES_BUILD_HOST_EXECUTABLE),\ - $(BUILD_BROKEN_USES_BUILD_HOST_SHARED_LIBRARY),\ - $(BUILD_BROKEN_USES_BUILD_HOST_STATIC_LIBRARY))) - $(error USE_HOST_MUSL can't be set when native host builds are enabled in Make with BUILD_BROKEN_USES_BUILD_HOST_*) - endif -endif - # ############################################################### # Set up final options. # ############################################################### diff --git a/core/definitions.mk b/core/definitions.mk index afa7f7bc78..31f30289da 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -2121,6 +2121,7 @@ $(hide) $(PRIVATE_CXX_LINK) \ $(PRIVATE_HOST_GLOBAL_LDFLAGS) \ ) \ $(PRIVATE_LDFLAGS) \ + $(PRIVATE_CRTBEGIN) \ $(PRIVATE_ALL_OBJECTS) \ -Wl,--whole-archive \ $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \ @@ -2129,8 +2130,10 @@ $(hide) $(PRIVATE_CXX_LINK) \ $(PRIVATE_ALL_STATIC_LIBRARIES) \ $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \ $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \ + $(PRIVATE_LIBCRT_BUILTINS) \ $(PRIVATE_ALL_SHARED_LIBRARIES) \ -o $@ \ + $(PRIVATE_CRTEND) \ $(PRIVATE_LDLIBS) endef endif @@ -2264,6 +2267,7 @@ endef ifneq ($(HOST_CUSTOM_LD_COMMAND),true) define transform-host-o-to-executable-inner $(hide) $(PRIVATE_CXX_LINK) \ + $(PRIVATE_CRTBEGIN) \ $(PRIVATE_ALL_OBJECTS) \ -Wl,--whole-archive \ $(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES) \ @@ -2272,6 +2276,7 @@ $(hide) $(PRIVATE_CXX_LINK) \ $(PRIVATE_ALL_STATIC_LIBRARIES) \ $(if $(PRIVATE_GROUP_STATIC_LIBRARIES),-Wl$(comma)--end-group) \ $(if $(filter true,$(NATIVE_COVERAGE)),$(PRIVATE_HOST_LIBPROFILE_RT)) \ + $(PRIVATE_LIBCRT_BUILTINS) \ $(PRIVATE_ALL_SHARED_LIBRARIES) \ $(foreach path,$(PRIVATE_RPATHS), \ -Wl,-rpath,\$$ORIGIN/$(path)) \ @@ -2280,6 +2285,7 @@ $(hide) $(PRIVATE_CXX_LINK) \ ) \ $(PRIVATE_LDFLAGS) \ -o $@ \ + $(PRIVATE_CRTEND) \ $(PRIVATE_LDLIBS) endef endif diff --git a/core/host_executable_internal.mk b/core/host_executable_internal.mk index 0cf62a4323..2ff9ff217a 100644 --- a/core/host_executable_internal.mk +++ b/core/host_executable_internal.mk @@ -39,6 +39,21 @@ $(LOCAL_BUILT_MODULE): PRIVATE_RPATHS := ../$(my_libdir) $(my_libdir) endif my_libdir := +my_crtbegin := +my_crtend := +my_libcrt_builtins := +ifdef USE_HOST_MUSL + my_crtbegin := $(SOONG_$(LOCAL_2ND_ARCH_VAR_PREFIX)HOST_OBJECT_libc_musl_crtbegin_dynamic) + my_crtend := $(SOONG_$(LOCAL_2ND_ARCH_VAR_PREFIX)HOST_OBJECT_libc_musl_crtend) + my_libcrt_builtins := $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)LIBCRT_BUILTINS) + $(LOCAL_BUILT_MODULE): PRIVATE_LDFLAGS += -Wl,--no-dynamic-linker +endif + +$(LOCAL_BUILT_MODULE): PRIVATE_CRTBEGIN := $(my_crtbegin) +$(LOCAL_BUILT_MODULE): PRIVATE_CRTEND := $(my_crtend) +$(LOCAL_BUILT_MODULE): PRIVATE_LIBCRT_BUILTINS := $(my_libcrt_builtins) +$(LOCAL_BUILT_MODULE): $(my_crtbegin) $(my_crtend) $(my_libcrt_builtins) + $(LOCAL_BUILT_MODULE): $(all_objects) $(all_libraries) $(CLANG_CXX) $(transform-host-o-to-executable) diff --git a/core/host_shared_library_internal.mk b/core/host_shared_library_internal.mk index da2087478d..ae8b798c3c 100644 --- a/core/host_shared_library_internal.mk +++ b/core/host_shared_library_internal.mk @@ -36,6 +36,17 @@ include $(BUILD_SYSTEM)/binary.mk my_host_libprofile_rt := $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)LIBPROFILE_RT) $(LOCAL_BUILT_MODULE): PRIVATE_HOST_LIBPROFILE_RT := $(my_host_libprofile_rt) +ifdef USE_HOST_MUSL + my_crtbegin := $(SOONG_$(LOCAL_2ND_ARCH_VAR_PREFIX)HOST_OBJECT_libc_musl_crtbegin_so) + my_crtend := $(SOONG_$(LOCAL_2ND_ARCH_VAR_PREFIX)HOST_OBJECT_libc_musl_crtend_so) + my_libcrt_builtins := $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)LIBCRT_BUILTINS) +endif + +$(LOCAL_BUILT_MODULE): PRIVATE_CRTBEGIN := $(my_crtbegin) +$(LOCAL_BUILT_MODULE): PRIVATE_CRTEND := $(my_crtend) +$(LOCAL_BUILT_MODULE): PRIVATE_LIBCRT_BUILTINS := $(my_libcrt_builtins) +$(LOCAL_BUILT_MODULE): $(my_crtbegin) $(my_crtend) $(my_libcrt_builtins) + $(LOCAL_BUILT_MODULE): \ $(all_objects) \ $(all_libraries) \