1# Install jni libraries for one arch.
2# Input variables:
3#   my_2nd_arch_prefix: indicate if this is for TARGET_2ND_ARCH.
4#   my_embed_jni: indicate if we want to embed the jni libs in the apk.
5#   my_prebuilt_jni_libs
6#   my_installed_module_stem (from configure_module_stem.mk)
7#   partition_tag (from base_rules.mk)
8#   my_prebuilt_src_file (from prebuilt_internal.mk)
9#
10# Output variables:
11#   my_jni_shared_libraries, my_jni_shared_libraries_abi, if we are going to embed the libraries into the apk;
12#   my_embedded_prebuilt_jni_libs, prebuilt jni libs embedded in prebuilt apk.
13#
14
15my_sdk_variant = $(1)
16ifneq (,$(and $(my_embed_jni),$(LOCAL_SDK_VERSION)))
17  # Soong produces $(lib).so in $(lib).sdk_intermediates so that the library
18  # has the correct name for embedding in an APK.  Append .sdk to the name
19  # of the intermediates directory, but not the .so name.
20  my_sdk_variant = $(call use_soong_sdk_libraries,$(1))
21endif
22
23my_jni_shared_libraries := $(strip \
24  $(foreach lib,$(LOCAL_JNI_SHARED_LIBRARIES), \
25    $(call intermediates-dir-for,SHARED_LIBRARIES,$(call my_sdk_variant,$(lib)),,,$(my_2nd_arch_prefix))/$(lib).so))
26
27
28# App-specific lib path.
29my_app_lib_path := $(dir $(LOCAL_INSTALLED_MODULE))lib/$(TARGET_$(my_2nd_arch_prefix)ARCH)
30my_embedded_prebuilt_jni_libs :=
31
32ifdef my_embed_jni
33  # App explicitly requires the prebuilt NDK stl shared libraies.
34  # The NDK stl shared libraries should never go to the system image.
35  ifeq ($(LOCAL_NDK_STL_VARIANT),c++_shared)
36    ifndef LOCAL_SDK_VERSION
37      $(error LOCAL_SDK_VERSION must be defined with LOCAL_NDK_STL_VARIANT, \
38          LOCAL_PACKAGE_NAME=$(LOCAL_PACKAGE_NAME))
39    endif
40    my_jni_shared_libraries += \
41        $(HISTORICAL_NDK_VERSIONS_ROOT)/$(LOCAL_NDK_VERSION)/sources/cxx-stl/llvm-libc++/libs/$(TARGET_$(my_2nd_arch_prefix)CPU_ABI)/libc++_shared.so
42  endif
43
44  # Set the abi directory used by the local JNI shared libraries.
45  # (Doesn't change how the local shared libraries are compiled, just
46  # sets where they are stored in the apk.)
47  ifeq ($(LOCAL_JNI_SHARED_LIBRARIES_ABI),)
48    my_jni_shared_libraries_abi := $(TARGET_$(my_2nd_arch_prefix)CPU_ABI)
49  else
50    my_jni_shared_libraries_abi := $(LOCAL_JNI_SHARED_LIBRARIES_ABI)
51  endif
52
53else ifneq ($(my_jni_shared_libraries),) # not my_embed_jni
54
55  # The jni libaries will be installed to the system.img.
56  my_jni_filenames := $(notdir $(my_jni_shared_libraries))
57  # Make sure the JNI libraries get installed
58  my_shared_library_path := $(call get_non_asan_path,\
59      $($(my_2nd_arch_prefix)TARGET_OUT$(partition_tag)_SHARED_LIBRARIES))
60
61  bit_suffix := $(if $(filter %64,$(TARGET_$(my_2nd_arch_prefix)ARCH)),:64,:32)
62  ALL_MODULES.$(my_register_name).REQUIRED_FROM_TARGET += $(addsuffix $(bit_suffix),$(LOCAL_JNI_SHARED_LIBRARIES))
63
64  # Create symlink in the app specific lib path
65  # Skip creating this symlink when running the second part of a target sanitization build.
66  ifeq ($(filter address,$(SANITIZE_TARGET)),)
67    my_symlink_target_dir := $(patsubst $(PRODUCT_OUT)%,%,\
68      $(my_shared_library_path))
69    $(foreach lib,$(my_jni_filenames),\
70      $(call symlink-file, \
71        $(my_shared_library_path)/$(lib), \
72        $(my_symlink_target_dir)/$(lib), \
73        $(my_app_lib_path)/$(lib)) \
74      $(eval $$(LOCAL_INSTALLED_MODULE) : $$(my_app_lib_path)/$$(lib)) \
75      $(eval ALL_MODULES.$(my_register_name).INSTALLED += $$(my_app_lib_path)/$$(lib)))
76  endif
77
78  # Clear jni_shared_libraries to not embed it into the apk.
79  my_jni_shared_libraries :=
80endif  # my_embed_jni
81
82ifdef my_prebuilt_jni_libs
83  # Files like @lib/<abi>/libfoo.so (path inside the apk) are JNI libs embedded prebuilt apk;
84  # Files like path/to/libfoo.so (path relative to LOCAL_PATH) are prebuilts in the source tree.
85  my_embedded_prebuilt_jni_libs := $(patsubst @%,%, \
86      $(filter @%, $(my_prebuilt_jni_libs)))
87
88  # prebuilt JNI exsiting as separate source files.
89  my_prebuilt_jni_libs := $(addprefix $(LOCAL_PATH)/, \
90      $(filter-out @%, $(my_prebuilt_jni_libs)))
91  ifdef my_prebuilt_jni_libs
92    ifdef my_embed_jni
93      # Embed my_prebuilt_jni_libs to the apk
94      my_jni_shared_libraries += $(my_prebuilt_jni_libs)
95    else # not my_embed_jni
96      # Install my_prebuilt_jni_libs as separate files.
97      $(foreach lib, $(my_prebuilt_jni_libs), \
98          $(eval $(call copy-one-file, $(lib), $(my_app_lib_path)/$(notdir $(lib)))))
99
100      my_installed_library := $(addprefix $(my_app_lib_path)/, $(notdir $(my_prebuilt_jni_libs)))
101      $(LOCAL_INSTALLED_MODULE) : $(my_installed_library)
102
103      ALL_MODULES.$(my_register_name).INSTALLED += $(my_installed_library)
104    endif  # my_embed_jni
105  endif  # inner my_prebuilt_jni_libs
106endif  # outer my_prebuilt_jni_libs
107
108# Verify that all included libraries are built against the NDK
109include $(BUILD_SYSTEM)/allowed_ndk_types.mk
110
111ifneq ($(strip $(LOCAL_JNI_SHARED_LIBRARIES)),)
112  ifneq ($(LOCAL_SDK_VERSION),)
113    my_link_type := app:sdk
114    my_warn_types := native:platform $(my_warn_ndk_types)
115    my_allowed_types := $(my_allowed_ndk_types)
116    ifneq (,$(filter true,$(LOCAL_VENDOR_MODULE) $(LOCAL_ODM_MODULE) $(LOCAL_PROPRIETARY_MODULE)))
117      my_allowed_types += native:vendor native:vndk native:platform_vndk
118    else ifeq ($(LOCAL_PRODUCT_MODULE),true)
119      my_allowed_types += native:product native:vndk native:platform_vndk
120    endif
121  else
122    my_link_type := app:platform
123    my_warn_types := $(my_warn_ndk_types)
124    my_allowed_types := $(my_allowed_ndk_types) native:platform native:product native:vendor native:vndk native:vndk_private native:platform_vndk
125  endif
126
127  ifeq ($(SOONG_ANDROID_MK),$(LOCAL_MODULE_MAKEFILE))
128    # SOONG_SDK_VARIANT_MODULES isn't complete yet while parsing Soong modules, and Soong has
129    # already ensured that apps link against the correct SDK variants, don't check them.
130  else
131    ifneq (,$(LOCAL_SDK_VERSION))
132      my_link_deps := $(addprefix SHARED_LIBRARIES:,$(call use_soong_sdk_libraries,$(LOCAL_JNI_SHARED_LIBRARIES)))
133    else
134      my_link_deps := $(addprefix SHARED_LIBRARIES:,$(LOCAL_JNI_SHARED_LIBRARIES))
135    endif
136  endif
137
138  my_common :=
139  include $(BUILD_SYSTEM)/link_type.mk
140endif
141