From b56eab1682529cf0a7feb0360d5e362c935831c7 Mon Sep 17 00:00:00 2001
From: zhangfei <115525066+zhangfeiv0@users.noreply.github.com>
Date: Sat, 21 Dec 2024 00:45:30 +0800
Subject: [PATCH] README: add cross-compile (#607)
* README: add cross-compile
---------
Signed-off-by: Zhang fei
---
CMakeLists.txt | 4 ++
README.md | 4 ++
docs/1-user-guide/README.md | 85 +++++++++++++++++++++++++++++--------
3 files changed, 75 insertions(+), 18 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c978973e..f9912bc4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -246,6 +246,10 @@ function(add_host_executable TARGETNAME)
target_compile_options(${TARGETNAME} PRIVATE -arch "${CMAKE_HOST_SYSTEM_PROCESSOR}")
target_link_options(${TARGETNAME} PRIVATE -arch "${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
+ elseif (DEFINED ENV{SLEEF_TARGET_EXEC_USE_QEMU})
+ if($ENV{SLEEF_TARGET_EXEC_USE_QEMU})
+ add_executable(${TARGETNAME} ${ARGN})
+ endif()
else()
add_executable(${TARGETNAME} IMPORTED GLOBAL)
set_property(TARGET ${TARGETNAME} PROPERTY IMPORTED_LOCATION ${NATIVE_BUILD_DIR}/bin/${TARGETNAME})
diff --git a/README.md b/README.md
index ad18449b..7a2934c5 100644
--- a/README.md
+++ b/README.md
@@ -190,6 +190,10 @@ ctest --test-dir build -j
For more detailed build instructions please refer to the [dedicated section on CMake](./docs/1-user-guide/build-with-cmake) or to [our web page][build_info_url].
+## How to cross-compile SLEEF
+
+For more detailed please refer to [cross-compile SLEEF](./docs/1-user-guide#cross_linux)
+
## Install SLEEF
### From source
diff --git a/docs/1-user-guide/README.md b/docs/1-user-guide/README.md
index cf15bb43..5f98aea4 100644
--- a/docs/1-user-guide/README.md
+++ b/docs/1-user-guide/README.md
@@ -18,6 +18,7 @@ Guidelines on how to compile and install the library.
* [Compiling the library with Microsoft Visual C++](#MSVC)
* [Compiling and running "Hello SLEEF"](#hello)
* [Importing SLEEF into your project](#import)
+* [Cross compilation for Linux](#cross_linux)
* [Cross compilation for iOS and Android](#cross)
Preliminaries
@@ -204,6 +205,62 @@ target_link_libraries(hellox86 sleef)
+Cross compilation for Linux
+
+Two methods are used for cross-compiling SLEEF. Both rely on existing toolchain
+files provided in the `toolchains/` directory.
+
+Here are examples of cross-compiling SLEEF for the AArch64 on a platform with
+x86_64 and Linux OS:
+
+Method 1
+
+Please run the following from the root directory of SLEEF:
+
+1. First, compile the native SLEEF.
+```bash
+cmake -S . -B build-native
+
+cmake --build build-native -j --clean-first
+```
+
+2. Cross-compile the target platform's SLEEF.
+```bash
+cmake -DCMAKE_TOOLCHAIN_FILE=./toolchains/aarch64-gcc.cmake -DNATIVE_BUILD_DIR=$(pwd)/build-native/ -S . -B build
+
+cmake --build build -j --clean-first
+```
+
+Method 2
+
+If running via an emulator like QEMU, there is no need to compile the native SLEEF.
+
+Please run the following from the root directory of SLEEF:
+
+1. Install qemu on Ubuntu.
+```bash
+sudo apt install -y qemu-user-static binfmt-support
+```
+
+2. Set the environment variable.
+```bash
+export SLEEF_TARGET_EXEC_USE_QEMU=ON
+```
+
+3. Set the dynamic linker/loader path.
+```bash
+# for AArch64
+export QEMU_LD_PREFIX=/usr/aarch64-linux-gnu/
+```
+
+4. Cross-compile the target platform's SLEEF.
+```bash
+cmake -DCMAKE_TOOLCHAIN_FILE=./toolchains/aarch64-gcc.cmake -S . -B build
+
+cmake --build build -j --clean-first
+```
+
+
Cross compilation for iOS and Android
SLEEF has preliminary support for iOS and Android. Here, "preliminary" means
@@ -217,29 +274,21 @@ the library for the host computer, then for the target OS. Below is an example
sequence of commands for cross compiling the library for iOS.
```sh
-mkdir build-native
-cd build-native
-cmake -GNinja ..
-ninja
-cd ..
-mkdir build-cross
-cd build-cross
-cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DNATIVE_BUILD_DIR=`pwd`/../build-native -DSLEEF_DISABLE_MPFR=TRUE -DSLEEF_DISABLE_SSL=TRUE ..
-ninja
+# Build natively first
+cmake -S . -B build-native
+cmake --build build-native -j --clean-first
+# Then cross-compile for iOS
+cmake -S . -B build-cross -DCMAKE_TOOLCHAIN_FILE=./toolchains/ios.toolchain.cmake -DNATIVE_BUILD_DIR=$(pwd)/build-native -DSLEEF_DISABLE_MPFR=TRUE -DSLEEF_DISABLE_SSL=TRUE
```
Below is an example sequence of commands for cross compiling the library for
Android.
```sh
-mkdir build-native
-cd build-native
-cmake -GNinja ..
-ninja
-cd ..
-mkdir build-cross
-cd build-cross
-cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=/opt/android-ndk-r21d/build/cmake/android.toolchain.cmake -DNATIVE_BUILD_DIR=`pwd`/../build-native -DANDROID_ABI=arm64-v8a ..
-ninja
+# Build natively first
+cmake -S . -B build-native
+cmake --build build-native -j --clean-first
+# Then cross-compile for Android
+cmake -S . -B build-cross -DCMAKE_TOOLCHAIN_FILE=/opt/android-ndk-r21d/build/cmake/android.toolchain.cmake -DNATIVE_BUILD_DIR=$(pwd)/build-native -DANDROID_ABI=arm64-v8a
```