Skip to content

Commit d4b04f1

Browse files
committed
Install libomp.so if needed with ndk-build.
Test: ./run_tests.py --filter openmp_shared Bug: android/ndk#1028 Change-Id: I965aeb3286d1ee141d8b0157941f86e8544c81d0
1 parent 69cf04a commit d4b04f1

File tree

10 files changed

+86
-8
lines changed

10 files changed

+86
-8
lines changed

build/core/openmp.mk

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# Copyright (C) 2019 The Android Open Source Project
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# Generates rules to install the dynamic OpenMP runtime libraries to the out
18+
# directory if the runtime is requested by the app's ldflags.
19+
20+
NDK_APP_NEEDS_DYNAMIC_OMP := false
21+
$(foreach __module,$(__ndk_modules),\
22+
$(eval __module_ldflags := \
23+
$(NDK_APP_LDFLAGS) $(__ndk_modules.$(__module).LDFLAGS))\
24+
$(if $(filter -fopenmp,$(__module_ldflags)),\
25+
$(if $(filter -static-openmp,$(__module_ldflags)),,\
26+
$(eval NDK_APP_NEEDS_DYNAMIC_OMP := true)\
27+
)\
28+
)\
29+
)
30+
31+
ifeq ($(NDK_APP_NEEDS_DYNAMIC_OMP),true)
32+
NDK_APP_OMP := $(NDK_APP_DST_DIR)/libomp.so
33+
installed_modules: $(NDK_APP_OMP)
34+
35+
NDK_OMP_LIB_PATH := \
36+
$(NDK_TOOLCHAIN_LIB_DIR)/$(TARGET_TOOLCHAIN_ARCH_LIB_DIR)/libomp.so
37+
38+
$(NDK_APP_OMP): PRIVATE_ABI := $(TARGET_ARCH_ABI)
39+
$(NDK_APP_OMP): PRIVATE_SRC := $(NDK_OMP_LIB_PATH)
40+
$(NDK_APP_OMP): PRIVATE_DST := $(NDK_APP_OMP)
41+
42+
$(call generate-file-dir,$(NDK_APP_$(NDK_SANITIZER_NAME)))
43+
44+
$(NDK_APP_OMP): clean-installed-binaries
45+
$(call host-echo-build-step,$(PRIVATE_ABI),OpenMP "$(call pretty-dir,$(PRIVATE_DST))")
46+
$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
47+
endif

build/core/sanitizers.mk

-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
# limitations under the License.
1515
#
1616

17-
NDK_TOOLCHAIN_LIB_SUFFIX := 64
18-
ifeq ($(HOST_ARCH64),x86)
19-
NDK_TOOLCHAIN_LIB_SUFFIX :=
20-
endif
21-
22-
NDK_TOOLCHAIN_RESOURCE_DIR := $(shell $(TARGET_CXX) -print-resource-dir)
23-
NDK_TOOLCHAIN_LIB_DIR := $(strip $(NDK_TOOLCHAIN_RESOURCE_DIR))/lib/linux
24-
2517
NDK_APP_ASAN := $(NDK_APP_DST_DIR)/$(TARGET_ASAN_BASENAME)
2618
NDK_APP_UBSAN := $(NDK_APP_DST_DIR)/$(TARGET_UBSAN_BASENAME)
2719

build/core/setup-toolchain.mk

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ SYSROOT_LINK_ARG := -L $(SYSROOT_API_LIB_DIR) -L $(SYSROOT_LIB_DIR)
110110
SYSROOT_ARCH_INC_ARG := \
111111
-isystem $(SYSROOT_INC)/usr/include/$(TOOLCHAIN_NAME)
112112

113+
NDK_TOOLCHAIN_RESOURCE_DIR := $(shell $(TARGET_CXX) -print-resource-dir)
114+
NDK_TOOLCHAIN_LIB_DIR := $(strip $(NDK_TOOLCHAIN_RESOURCE_DIR))/lib/linux
115+
113116
clean-installed-binaries::
114117

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

135139
ifneq ($(NDK_APP_WRAP_SH_$(TARGET_ARCH_ABI)),)
136140
include $(BUILD_SYSTEM)/install_wrap_sh.mk

build/core/toolchains/aarch64-linux-android-clang/setup.mk

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
TOOLCHAIN_NAME := aarch64-linux-android
1717
LLVM_TRIPLE := aarch64-none-linux-android
1818

19+
TARGET_TOOLCHAIN_ARCH_LIB_DIR := aarch64
1920
TARGET_ASAN_BASENAME := libclang_rt.asan-aarch64-android.so
2021
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-aarch64-android.so
2122

build/core/toolchains/arm-linux-androideabi-clang/setup.mk

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
TOOLCHAIN_NAME := arm-linux-androideabi
1717
LLVM_TRIPLE := armv7-none-linux-androideabi
1818

19+
TARGET_TOOLCHAIN_ARCH_LIB_DIR := arm
1920
TARGET_ASAN_BASENAME := libclang_rt.asan-arm-android.so
2021
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-arm-android.so
2122

build/core/toolchains/x86-clang/setup.mk

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
TOOLCHAIN_NAME := i686-linux-android
1717
LLVM_TRIPLE := i686-none-linux-android
1818

19+
TARGET_TOOLCHAIN_ARCH_LIB_DIR := i386
1920
TARGET_ASAN_BASENAME := libclang_rt.asan-i686-android.so
2021
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-i686-android.so
2122

build/core/toolchains/x86_64-clang/setup.mk

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
TOOLCHAIN_NAME := x86_64-linux-android
1717
LLVM_TRIPLE := x86_64-none-linux-android
1818

19+
TARGET_TOOLCHAIN_ARCH_LIB_DIR := x86_64
1920
TARGET_ASAN_BASENAME := libclang_rt.asan-x86_64-android.so
2021
TARGET_UBSAN_BASENAME := libclang_rt.ubsan_standalone-x86_64-android.so
2122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
include $(CLEAR_VARS)
4+
LOCAL_MODULE := openmp
5+
LOCAL_SRC_FILES := openmp.c
6+
LOCAL_CFLAGS := -fopenmp
7+
LOCAL_LDFLAGS := -fopenmp
8+
include $(BUILD_EXECUTABLE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
APP_ABI := all
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <omp.h>
4+
5+
int main(int argc, char *argv[])
6+
{
7+
int iam = 0, np = 1;
8+
9+
if (!getenv("OMP_NUM_THREADS"))
10+
omp_set_num_threads(4);
11+
12+
#pragma omp parallel default(shared) private(iam, np)
13+
{
14+
#if defined(_OPENMP)
15+
np = omp_get_num_threads();
16+
iam = omp_get_thread_num();
17+
#endif
18+
printf("Hello from thread %d out of %d\n", iam, np);
19+
}
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)