Skip to content

Commit

Permalink
Install libomp.so if needed with ndk-build.
Browse files Browse the repository at this point in the history
Test: ./run_tests.py --filter openmp_shared
Bug: android/ndk#1028
Change-Id: I965aeb3286d1ee141d8b0157941f86e8544c81d0
  • Loading branch information
DanAlbert committed Sep 24, 2019
1 parent 69cf04a commit d4b04f1
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 8 deletions.
47 changes: 47 additions & 0 deletions build/core/openmp.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Generates rules to install the dynamic OpenMP runtime libraries to the out
# directory if the runtime is requested by the app's ldflags.

NDK_APP_NEEDS_DYNAMIC_OMP := false
$(foreach __module,$(__ndk_modules),\
$(eval __module_ldflags := \
$(NDK_APP_LDFLAGS) $(__ndk_modules.$(__module).LDFLAGS))\
$(if $(filter -fopenmp,$(__module_ldflags)),\
$(if $(filter -static-openmp,$(__module_ldflags)),,\
$(eval NDK_APP_NEEDS_DYNAMIC_OMP := true)\
)\
)\
)

ifeq ($(NDK_APP_NEEDS_DYNAMIC_OMP),true)
NDK_APP_OMP := $(NDK_APP_DST_DIR)/libomp.so
installed_modules: $(NDK_APP_OMP)

NDK_OMP_LIB_PATH := \
$(NDK_TOOLCHAIN_LIB_DIR)/$(TARGET_TOOLCHAIN_ARCH_LIB_DIR)/libomp.so

$(NDK_APP_OMP): PRIVATE_ABI := $(TARGET_ARCH_ABI)
$(NDK_APP_OMP): PRIVATE_SRC := $(NDK_OMP_LIB_PATH)
$(NDK_APP_OMP): PRIVATE_DST := $(NDK_APP_OMP)

$(call generate-file-dir,$(NDK_APP_$(NDK_SANITIZER_NAME)))

$(NDK_APP_OMP): clean-installed-binaries
$(call host-echo-build-step,$(PRIVATE_ABI),OpenMP "$(call pretty-dir,$(PRIVATE_DST))")
$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
endif
8 changes: 0 additions & 8 deletions build/core/sanitizers.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
# limitations under the License.
#

NDK_TOOLCHAIN_LIB_SUFFIX := 64
ifeq ($(HOST_ARCH64),x86)
NDK_TOOLCHAIN_LIB_SUFFIX :=
endif

NDK_TOOLCHAIN_RESOURCE_DIR := $(shell $(TARGET_CXX) -print-resource-dir)
NDK_TOOLCHAIN_LIB_DIR := $(strip $(NDK_TOOLCHAIN_RESOURCE_DIR))/lib/linux

NDK_APP_ASAN := $(NDK_APP_DST_DIR)/$(TARGET_ASAN_BASENAME)
NDK_APP_UBSAN := $(NDK_APP_DST_DIR)/$(TARGET_UBSAN_BASENAME)

Expand Down
4 changes: 4 additions & 0 deletions build/core/setup-toolchain.mk
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ SYSROOT_LINK_ARG := -L $(SYSROOT_API_LIB_DIR) -L $(SYSROOT_LIB_DIR)
SYSROOT_ARCH_INC_ARG := \
-isystem $(SYSROOT_INC)/usr/include/$(TOOLCHAIN_NAME)

NDK_TOOLCHAIN_RESOURCE_DIR := $(shell $(TARGET_CXX) -print-resource-dir)
NDK_TOOLCHAIN_LIB_DIR := $(strip $(NDK_TOOLCHAIN_RESOURCE_DIR))/lib/linux

clean-installed-binaries::

include $(BUILD_SYSTEM)/gdb.mk
Expand All @@ -131,6 +134,7 @@ ifeq (,$(DUMP_VAR))
# Comes after NDK_APP_BUILD_SCRIPT because we need to know if *any* module
# has -fsanitize in its ldflags.
include $(BUILD_SYSTEM)/sanitizers.mk
include $(BUILD_SYSTEM)/openmp.mk

ifneq ($(NDK_APP_WRAP_SH_$(TARGET_ARCH_ABI)),)
include $(BUILD_SYSTEM)/install_wrap_sh.mk
Expand Down
1 change: 1 addition & 0 deletions build/core/toolchains/aarch64-linux-android-clang/setup.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TOOLCHAIN_NAME := aarch64-linux-android
LLVM_TRIPLE := aarch64-none-linux-android

TARGET_TOOLCHAIN_ARCH_LIB_DIR := aarch64
TARGET_ASAN_BASENAME := libclang_rt.asan-aarch64-android.so
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-aarch64-android.so

Expand Down
1 change: 1 addition & 0 deletions build/core/toolchains/arm-linux-androideabi-clang/setup.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TOOLCHAIN_NAME := arm-linux-androideabi
LLVM_TRIPLE := armv7-none-linux-androideabi

TARGET_TOOLCHAIN_ARCH_LIB_DIR := arm
TARGET_ASAN_BASENAME := libclang_rt.asan-arm-android.so
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-arm-android.so

Expand Down
1 change: 1 addition & 0 deletions build/core/toolchains/x86-clang/setup.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TOOLCHAIN_NAME := i686-linux-android
LLVM_TRIPLE := i686-none-linux-android

TARGET_TOOLCHAIN_ARCH_LIB_DIR := i386
TARGET_ASAN_BASENAME := libclang_rt.asan-i686-android.so
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-i686-android.so

Expand Down
1 change: 1 addition & 0 deletions build/core/toolchains/x86_64-clang/setup.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TOOLCHAIN_NAME := x86_64-linux-android
LLVM_TRIPLE := x86_64-none-linux-android

TARGET_TOOLCHAIN_ARCH_LIB_DIR := x86_64
TARGET_ASAN_BASENAME := libclang_rt.asan-x86_64-android.so
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-x86_64-android.so

Expand Down
8 changes: 8 additions & 0 deletions tests/device/openmp_shared/jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := openmp
LOCAL_SRC_FILES := openmp.c
LOCAL_CFLAGS := -fopenmp
LOCAL_LDFLAGS := -fopenmp
include $(BUILD_EXECUTABLE)
1 change: 1 addition & 0 deletions tests/device/openmp_shared/jni/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ABI := all
22 changes: 22 additions & 0 deletions tests/device/openmp_shared/jni/openmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(int argc, char *argv[])
{
int iam = 0, np = 1;

if (!getenv("OMP_NUM_THREADS"))
omp_set_num_threads(4);

#pragma omp parallel default(shared) private(iam, np)
{
#if defined(_OPENMP)
np = omp_get_num_threads();
iam = omp_get_thread_num();
#endif
printf("Hello from thread %d out of %d\n", iam, np);
}

return 0;
}

0 comments on commit d4b04f1

Please sign in to comment.