Skip to content

Commit 03f7bf5

Browse files
committed
[android] Add libopencilk-abi.mk file to build the OpenCilk bitcode ABI for Android, and update the Android README.
1 parent 88cb908 commit 03f7bf5

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

android/README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,30 @@ These instructions describe how to build the OpenCilk runtime system for Android
88
## Setup Android Studio and NDK
99

1010
1. Install [Android Studio](https://developer.android.com/studio) with [NDK](https://developer.android.com/studio/projects/install-ndk) onto your system.
11-
2. Replace NDK's copy of `clang-<version>` with OpenCilk's `clang-<version>`. For example, for NDK version 28, which includes a prebuilt LLVM toolchain based on LLVM 19, replace the `clang-19` binary in that NDK with OpenCilk's `clang-19` binary.
11+
2. Replace [NDK's copy of `clang-<version>`](https://developer.android.com/ndk/guides/other_build_systems#overview) with OpenCilk's `clang-<version>` binary. For example, for NDK version 28, which includes a prebuilt LLVM toolchain based on LLVM 19, replace the `clang-19` binary in that NDK with OpenCilk's `clang-19` binary.
1212
> [!TIP]
1313
> Save the distributed `clang-<version>` binary in the NDK somewhere before replacing it with the OpenCilk version, so you can restore it if anything goes wrong.
1414
1515
## Build the runtime system
1616

17-
1. Run `ndk-build` inside the `android/jni` subdirectory of this repository.
18-
2. Make sure your Android project contains a `jniLibs` subdirectory in the [correct place](https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs), for example, as a subdirectory within `src/main`.
19-
3. Copy `android/libs/*` into your Android project under the `jniLibs` subdirectory.
17+
1. Export the `NDK` environment variable set to the path to the NDK in Android Studio.
18+
2. Run `$NDK/ndk-build` inside the `android/jni` subdirectory of this repository.
19+
3. Run `make -f libopencilk-abi.mk` inside the `android/jni` subdirectory.
20+
21+
## Use OpenCilk in your Android project
22+
23+
1. Make sure your Android project contains a `jniLibs` subdirectory in the [correct place](https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs), for example, as a subdirectory within `src/main`.
24+
2. Copy `android/libs/*` in your copy of this repository into the `jniLibs` subdirectory of your Android project.
25+
3. To compile the Cilk parts of your Android project, add these flags to `CMAKE_C_FLAGS` or `CMAKE_CXX_FLAGS`:
26+
27+
```cmake
28+
-fopencilk --opencilk-abi-bitcode=${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libopencilk-abi.bc -femulated-tls
29+
```
30+
31+
4. To link your Android project with Cilk code, add these flags to `CMAKE_SHARED_LINKER_FLAGS`:
32+
33+
```cmake
34+
-fopencilk -L${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}
35+
```
36+
37+
5. [Optional] To use OpenCilk runtime headers, first copy the contents of the `include/cilk` subdirectory in this repository into an `include/cilk` subdirectory in your Android project, such as in `app/src/main/cpp/include/cilk`. Then use `include_directories()` to add that `include` directory, such as via `include_directories(${CMAKE_SOURCE_DIR}/include)`.

android/jni/libopencilk-abi.mk

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
DIR?=$(shell pwd)
2+
LOCAL_PATH := $(DIR)
3+
LIBOPENCILK_ROOT_REL := ../..
4+
LIBOPENCILK_ROOT_ABS := $(LOCAL_PATH)/../..
5+
6+
UNAME := $(shell uname -s)
7+
ifneq (,$(findstring Linux,$(UNAME)))
8+
HOST_OS := linux
9+
endif
10+
ifneq (,$(findstring Darwin,$(UNAME)))
11+
HOST_OS := darwin
12+
endif
13+
14+
TOOLCHAIN=${NDK}/toolchains/llvm/prebuilt/$(HOST_OS)-x86_64
15+
CC=$(TOOLCHAIN)/bin/clang
16+
CXX=$(TOOLCHAIN)/bin/clang++
17+
18+
# libopencilk-abi.bc
19+
20+
MODULE := libopencilk-abi
21+
OUT_DIR := $(LOCAL_PATH)/../libs
22+
TARGET_AARCH64_DIR := $(OUT_DIR)/arm64-v8a
23+
TARGET_X86_64_DIR := $(OUT_DIR)/x86_64
24+
TARGET_AARCH64 := $(TARGET_AARCH64_DIR)/$(MODULE).bc
25+
TARGET_X86_64 := $(TARGET_X86_64_DIR)/$(MODULE).bc
26+
API := 28
27+
28+
all: $(TARGET_AARCH64) $(TARGET_X86_64)
29+
30+
SRC_FILES := $(LIBOPENCILK_ROOT_ABS)/runtime/cilk2c_inlined.c
31+
CFLAGS += \
32+
-DCHEETAH_API="" \
33+
-DCHEETAH_INTERNAL_NORETURN='__attribute__((noreturn))' \
34+
-DCHEETAH_INTERNAL="" \
35+
-DCILK_DEBUG=0 \
36+
-g -gdwarf-4
37+
38+
C_INCLUDES := -I$(LIBOPENCILK_ROOT_ABS)/include
39+
40+
ifeq (${DEBUG},1)
41+
CFLAGS += -O0
42+
else
43+
CFLAGS += -O3
44+
endif
45+
46+
$(TARGET_AARCH64_DIR) $(TARGET_X86_64_DIR):
47+
@mkdir -p $@
48+
49+
$(TARGET_AARCH64) : $(TARGET_AARCH64_DIR)
50+
$(TARGET_X86_64) : $(TARGET_X86_64_DIR)
51+
52+
$(TARGET_AARCH64) : $(SRC_FILES)
53+
$(CC) --target=aarch64-linux-android$(API) $(CFLAGS) $(C_INCLUDES) -c -emit-llvm -o $@ $<
54+
55+
$(TARGET_X86_64) : $(SRC_FILES)
56+
$(CC) --target=x86_64-linux-android$(API) $(CFLAGS) $(C_INCLUDES) -c -emit-llvm -o $@ $<
57+
58+
clean:
59+
rm $(TARGET_AARCH64) $(TARGET_X86_64)

0 commit comments

Comments
 (0)