Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README: add cross-compile #607

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 67 additions & 18 deletions docs/1-user-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<h2 id="preliminaries">Preliminaries</h2>
Expand Down Expand Up @@ -204,6 +205,62 @@ target_link_libraries(hellox86 sleef)
</p>


<h2 id="cross_linux">Cross compilation for Linux</h2>

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:

<h3 id="method1">Method 1</h3>

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
```

<h3 id="method2">Method 2</h3>

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
```


<h2 id="cross">Cross compilation for iOS and Android</h2>

SLEEF has preliminary support for iOS and Android. Here, "preliminary" means
Expand All @@ -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
```

Loading