# Cross Compiling VTK for Mobile Architectures

**URL:** https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193
**Category:** Development
**Created:** [February 15, 2021, 7:41pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193 "2021-02-15T19:41:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![timt](https://discourse.vtk.org/user_avatar/discourse.vtk.org/timt/32/3018_2.png) [@timt](https://discourse.vtk.org/u/timt)
#### Post date: [February 15, 2021, 7:42pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/1 "2021-02-15T19:42:00Z")

</div>

I am interested in consolidating our current functionality for cross-compiling VTK for mobile architectures (iOS, tvOS, Android, etc.). This is likely to have a lot of overlap with the new Apple M1 (arm64) chip support. In addition, we can deprecate support for many previous ARM architectures (armv7, armv7s, etc.).

**Previous/Current Development**

The last time I visited this topic was six years ago, with the results outlined in this [external blog post](https://blog.kitware.com/building-vtk-for-mobile-architectures/). The gist is that I created custom toolchain files for [device](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/ios.device.toolchain.cmake.in) and [simulator](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/ios.simulator.toolchain.cmake.in) that could be configured to produce a minimal subset of VTK via a global CMake flag `VTK_IOS_BUILD=ON` which invokes a configuration path specified in [vtkiOS.cmake](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/vtkiOS.cmake). This configuration will build the VTK compile tools then build a subset of VTK for each specified mobile architecture. [Another CMake script](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/MakeFramework.cmake.in) will then consolidate all of the static libraries and headers to make an iOS framework, which can then be included directly in the relevant Xcode project.

The Android path works similar in spirit, albeit with a different toolchain.

The simplest way to build is:

```bash
cmake -DVTK_IOS_BUILD=1 -S vtk -B vtk-ios-build

```

which produces `usr/local/frameworks/VTK.framework`. Something similar happens with `VTK_ANDROID_BUILD=1` to produce a consumable Android library.

One downside of this is that only a [small subset](https://gitlab.kitware.com/vtk/vtk/-/blob/master/CMake/vtkiOS.cmake#L100-116) of VTK modules are currently available. I am unsure which modules cross-compile correctly for iOS/Android and which do not. In addition, if a developer wants to add another module, they have to edit `vtkiOS.cmake` directly.

**Present/Future Development**

Modern CMake has a lot of nice built-in functionality for cross compiling for these architectures now. It would be great if we could simply rely on CMake to handle all of this, and delete the toolchain and iOS- and Android-specific CMake files from the VTK source.

For VTK, this means first building the compile tools:

```auto
cmake -S vtk -B vtk-compile-tools \
-DVTK_BUILD_COMPILE_TOOLS_ONLY=ON \
-DCMAKE_INSTALL_PREFIX=`pwd`/vtk-compile-tools-install

cmake --build vtk-compile-tools --config Release --target install

```

Then using modern CMake to cross compile:

```auto
cmake -S vtk -B vtk-ios-build -G Xcode \
-DCMAKE_SYSTEM_NAME=iOS \
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
-DCMAKE_INSTALL_PREFIX=vtk-ios-install \
-DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO \
-DVTKCompileTools_DIR=`pwd`/vtk-compile-tools-install
-DCMAKE_IOS_INSTALL_COMBINED=YES

```

However, at first pass CMake has an issue with finding a valid threading library. Alexis has a fix for this; I will post again to this thread when I have made progress.

Alexis and Julien have also noted interest around building VTK as an XCFramework, something of which I currently have little knowledge. This seems like the ideal end goal at present.

This is the current state of affairs. Opening up this discussion to track any progress and to list any issues that those working on this run into.

Thanks!

CC: @agirault @brad.king

---

<div class="post-metadata">

### Author: ![timt](https://discourse.vtk.org/user_avatar/discourse.vtk.org/timt/32/3018_2.png) [@timt](https://discourse.vtk.org/u/timt)
#### Post date: [February 15, 2021, 7:44pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/2 "2021-02-15T19:44:15Z")

</div>

More information, as I was limited in URLs and user tags as a “new user.” (LOL)

[Relevant CMake documentation](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-ios-tvos-or-watchos)

For more recent versions of VTK, it seems [this merge request](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7608) is necessary independent of how we’re compiling.

[WWDC presentation](https://developer.apple.com/videos/play/wwdc2019/416/) on XCFrameworks.

Additional CCs: @jjomier, @ben.boeckel

---

<div class="post-metadata">

### Author: ![agirault](https://discourse.vtk.org/user_avatar/discourse.vtk.org/agirault/32/508_2.png) [@agirault](https://discourse.vtk.org/u/agirault)
#### Post date: [February 15, 2021, 8:46pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/3 "2021-02-15T20:46:39Z")

</div>

> [@timt](#):
>
> Modern CMake has a lot of nice built-in functionality for cross compiling for these architectures now. It would be great if we could simply rely on CMake to handle all of this, and delete the toolchain and iOS- and Android-specific CMake files from the VTK source.

+1 on dumping the custom CMake toolchains in VTK and using the native CMake support instead. Obviously a lot easier to maintain, and more scalable when needing to configure more CMake properties than the ones currently hardcoded. Some useful links:

- [https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-ios-tvos-or-watchos](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-ios-tvos-or-watchos)
- [https://cmake.org/cmake/help/latest/prop\_tgt/FRAMEWORK.html](https://cmake.org/cmake/help/latest/prop_tgt/FRAMEWORK.html)

I don’t know anything about the **Android** build, but it looks like there was support even before: [https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android)

> [@timt](#):
>
> However, at first pass CMake has an issue with finding a valid threading library

Those tickets might help with dependency issues on iOS:

- FindThreads: [https://gitlab.kitware.com/cmake/cmake/-/issues/18993#note\_631486](https://gitlab.kitware.com/cmake/cmake/-/issues/18993#note_631486)
- findPackage with CONFIG: [https://gitlab.kitware.com/cmake/cmake/-/issues/19655#note\_619698](https://gitlab.kitware.com/cmake/cmake/-/issues/19655#note_619698)

> [@timt](#):
>
> Alexis and Julien have also noted interest around building VTK as an XCFramework, something of which I currently have little knowledge. This seems like the ideal end goal at present.

See this comment on the CMake’s issue tracker to follow on that topic:  
[https://gitlab.kitware.com/cmake/cmake/-/issues/21752#note\_903018](https://gitlab.kitware.com/cmake/cmake/-/issues/21752#note_903018)

* * *

@ken-martin @ben.boeckel I thought VTK had an iOS job with tests on [CDash](https://open.cdash.org/index.php?project=VTK) but I can’t seem to find it. I haven’t done any vtk development for iOS in a while: has it been removed, or am I misremembering?

---

<div class="post-metadata">

### Author: ![agirault](https://discourse.vtk.org/user_avatar/discourse.vtk.org/agirault/32/508_2.png) [@agirault](https://discourse.vtk.org/u/agirault)
#### Post date: [February 15, 2021, 8:50pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/4 "2021-02-15T20:50:19Z")

</div>

FYI, looks like multiple people had issues with building VTK for iOS in that thread:

> [@iOS framework](https://discourse.vtk.org/t/ios-framework/1607):
>
> I want to use VTK in ios. According to the instructions, I plan to Build the vtk.framework of ios, but it is prompted in make that VTK not installed. Build the source then Build the ‘install’ target。The vtk.framework generated after I manually created the corresponding directory is 0 bytes. Why is this? Thank you。 Before calling this, ensure that the following variables are set: VTK\_GLOB\_LIBS - a path containing a wildcard suitable for globbing VTK\_INSTALLED\_HEADERS - the path containing all h…

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [February 15, 2021, 9:03pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/5 "2021-02-15T21:03:44Z")

</div>

> [@agirault](#):
>
> @ken-martin @ben.boeckel I thought VTK had an iOS job with tests on [CDash](https://open.cdash.org/index.php?project=VTK) but I can’t seem to find it. I haven’t done any vtk development for iOS in a while: has it been removed, or am I misremembering?

Ken had been in charge of it, but we can add a build to CI [once basic macOS is done](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7613).

---

<div class="post-metadata">

### Author: ![agirault](https://discourse.vtk.org/user_avatar/discourse.vtk.org/agirault/32/508_2.png) [@agirault](https://discourse.vtk.org/u/agirault)
#### Post date: [February 15, 2021, 9:08pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/6 "2021-02-15T21:08:45Z")

</div>

> [@ben.boeckel](#):
>
> but we can add a build to CI [once basic macOS is done](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7613).

Nice. What Xcode version will be used there? You might already be aware, but CMake’s behavior on macOS before and after XCode 12 differs quite a lot. I can follow-up on the GitLab ticket if that’s something you’d like to discuss.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [February 15, 2021, 9:11pm UTC](https://discourse.vtk.org/t/cross-compiling-vtk-for-mobile-architectures/5193/7 "2021-02-15T21:11:08Z")

</div>

It is using Xcode 12.3 (as can be seen by the tagset and the `DEVELOPER_DIR` setting). This is to facilitate universal binaries for building once and testing on both architectures to save CI time.
