0

I'm trying to create a CMakeLists.txt file that will build a cross-platform library, I had no issues making it work on Linux and Windows, but making it work on iOS (real device and simulator) and Android is turning out to be a pain.

The main issue relies on my need to include a prebuilt external library that comes in the following directory structure:

├── lib
│   └── EXAMPLE
│       ├── include
│       │   └── EXAMPLE
│       │       ├── EXAMPLE_API.h
│       │       ├── EXAMPLE_Error.h
│       │       └── EXAMPLE_Type.h
│       └── lib
│           ├── Android_Static_All
│           │   ├── arm64-v8a
│           │   │   └── libEXAMPLE_API.a
│           │   ├── armeabi
│           │   │   └── libEXAMPLE_API.a
│           │   ├── armeabi-v7a
│           │   │   └── libEXAMPLE_API.a
│           │   ├── mips
│           │   │   └── libEXAMPLE_API.a
│           │   ├── mips64
│           │   │   └── libEXAMPLE_API.a
│           │   ├── x86
│           │   │   └── libEXAMPLE_API.a
│           │   └── x86_64
│           │       └── libEXAMPLE_API.a
│           ├── Linux
│           │   ├── x64
│           │   │   ├── libEXAMPLE_API.a
│           │   │   └── libEXAMPLE_API.so
│           │   └── x86
│           │       ├── libEXAMPLE_API.a
│           │       └── libEXAMPLE_API.so
│           ├── Windows
│           │   ├── x64
│           │   │   ├── EXAMPLE_API.dll
│           │   │   └── EXAMPLE_API.lib
│           │   └── x86
│           │       ├── EXAMPLE_API.dll
│           │       └── EXAMPLE_API.lib
│           ├── iOS64
│           │   ├── Release-iphoneos
│           │   │   └── libEXAMPLE_API.a
│           │   └── Release-iphonesimulator
│           │       └── libEXAMPLE_API.a
│           └── osX
│               ├── x64
│               │   ├── libEXAMPLE_API.a
│               │   └── libEXAMPLE_API.dylib
│               └── x86
│                   ├── libEXAMPLE_API.a
│                   └── libEXAMPLE_API.dylib

I've managed to build an Xcode project with the help of this great project (https://github.com/leetal/ios-cmake) by adjusting the following section of the CMake file:

# Comment one of the following line depending on what you are building
set(PPCS_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/EXAMPLE/lib/iOS64/Release-iphonesimulator")
# set(PPCS_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/EXAMPLE/lib/iOS64/Release-iphoneos")
# set(PPCS_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/EXAMPLE/lib/Android_Static_All/arm64-v8a")
# set(PPCS_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/EXAMPLE/lib/Android_Static_All/armeabi")
# etc...

find_library(EXAMPLE_API_LIBRARY 
    NAMES 
        EXAMPLE_API
        libEXAMPLE_API 
    HINTS 
        "${EXAMPLE_LIB_PATH}"
    NO_DEFAULT_PATH
)

target_link_libraries(${PROJECT_NAME} ${EXAMPLE_API_LIBRARY})

And then configuring the build folder with the command:

$ cmake .. -G Xcode -DCMAKE_TOOLCHAIN_FILE=../../ios-cmake/ios.toolchain.cmake -DPLATFORM=OS64COMBINED -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=0000000000

However this is not really feasible for a couple of reasons:

  1. Manually updating the CMake file isn't an option but there are ways to hardcode the correct path based on the build target.
  2. When including a static library for a specific platform (e.g. iOS), multiple versions need to be included in the build project (e.g. both x86_64 and arm64) but this method only includes the fist one it finds, ignoring the other architectures for the said target.

Is there a way to automate the linking of the correct libraries (especially with iOS and iOSSimulator) and create an according Xcode and Android project?

  • What is a problem with selecting a library based on information about the target (e.g. be reading `CMAKE_SYSTEM_NAME` variable)? Why one need to link with the multiple versions of the same library at the same time? – Tsyvarev Jun 08 '21 at 21:58
  • @Tsyvarev `CMAKE_SYSTEM_NAME` only provides the target name (`iOS`), without the platform (`arm64;x86_64`). It should be possible to create a build project for each platform of each target (no big deal on iOS, where we have only 2 platforms, but on Android we would have 7 build directories). Is this the right path to follow? – Davide Maggioni Jun 09 '21 at 07:31
  • [That answer](https://stackoverflow.com/a/47571204/3440745) tells, that you can check `ANDROID_ABI` for determine target architecture for Android. There is also [CMAKE_ANDROID_ARCH_ABI](https://cmake.org/cmake/help/latest/variable/CMAKE_ANDROID_ARCH_ABI.html) CMake variable. – Tsyvarev Jun 09 '21 at 09:42

0 Answers0