# How to generate FAT STATIC lib with CMake Preset

**URL:** https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655
**Category:** Code
**Tags:** gen:xcode, os:ios
**Created:** [October 13, 2022, 10:01am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655 "2022-10-13T10:01:42Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![DrAuraHxC](https://discourse.cmake.org/user_avatar/discourse.cmake.org/draurahxc/32/2827_2.png) [@DrAuraHxC](https://discourse.cmake.org/u/DrAuraHxC)
#### Post date: [October 13, 2022, 10:01am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/1 "2022-10-13T10:01:42Z")

</div>

Hi,

I would like to be able to compile some libraries in “fat” mode, i.e. to have a library that combines the arm64 and x86\_64 version in one file.

I can do it very well for macOS by only setting `CMAKE_OSX_ARCHITECTURES` variable:

```auto
{
    "name": "release-macOS",
    "inherits": ["release"],
    "cacheVariables": {
        "CMAKE_OSX_ARCHITECTURES": "arm64;x86_64",
        "CMAKE_INSTALL_PREFIX": "${sourceParentDir}/_install/macOS/release"
    },
    "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Darwin"
    }
},
{
    "name": "debug-macOS",
    "inherits": ["debug", "release-macOS"],
    "cacheVariables": {
        "CMAKE_INSTALL_PREFIX": "${sourceParentDir}/_install/macOS/debug"
    }
}

```

Result:

```auto
❯ lipo -info libCatch2d.a
Architectures in the fat file: libCatch2d.a are: x86_64 arm64

```

But for iOS doing this is apparently not enough because the result is always a library only for arm64:

```auto
❯ lipo -info libCatch2d.a
Non-fat file: libCatch2d.a is architecture: arm64

```

So I would like to know how to configure through the presets system and CMake variables to get what I want for iOS. Knowing that the difference between macOS and iOS for me is the use of Ninja for macOS and XCode for iOS via the “generator” variable in the presets.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 16, 2022, 2:40pm UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/2 "2022-10-16T14:40:42Z")

</div>

`OSX` implies that it works for macOS. iOS is not macOS. I suspect that support isn’t too hard to add, but just hasn’t been done yet.

---

<div class="post-metadata">

### Author: ![DrAuraHxC](https://discourse.cmake.org/user_avatar/discourse.cmake.org/draurahxc/32/2827_2.png) [@DrAuraHxC](https://discourse.cmake.org/u/DrAuraHxC)
#### Post date: [October 19, 2022, 9:47am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/3 "2022-10-19T09:47:30Z")

</div>

However, if we look at the CMake documentation in the cmake-toolchains section, we can see a paragraph where it is explained that we can generate a library with several architecture: [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)

Now I just tried adding the variable `CMAKE_IOS_INSTALL_COMBINED` and I get something interesting.  
After compiling and installing the arm64 version, I get this type of line:

```auto
-- [iOS combined] Target: Catch2
-- [iOS combined] Config: Debug
-- [iOS combined] Destination: /Users/naubry/xxx/NextVersion/publibs/_install/iOS/debug/lib/libCatch2d.a
-- [iOS combined] Architectures (iphoneos): arm64 arm64e armv7 armv7s
-- [iOS combined] Architectures (iphonesimulator): arm64 arm64e i386 x86_64
-- [iOS combined] Architectures (iphonesimulator) after pruning: i386 x86_64
-- [iOS combined] Build `Catch2` for `iphonesimulator`
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project Catch2.xcodeproj build -target Catch2 -parallelizeTargets -configuration Debug -hideShellScriptEnvironment -sdk iphonesimulator

```

It looks like he’s actually trying to make a library with multiple architectures. However, I get an error pretty quickly like:

```auto
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project Catch2.xcodeproj build -target Catch2 -parallelizeTargets -configuration Debug -hideShellScriptEnvironment -sdk iphonesimulator

User defaults from command line:
    HideShellScriptEnvironment = YES
    IDEPackageSupportUseBuiltinSCM = YES

Build settings from command line:
    SDKROOT = iphonesimulator16.0
    TOOLCHAINS = com.apple.dt.toolchain.XcodeDefault

Computing target dependency graph and provisioning inputs

Create build description
Build description signature: ac7b261392e652e6dcf24b6b17682a4a
Build description path: /Users/naubry/xxx/NextVersion/publibs/catch2/_build/build/build/XCBuildData/ac7b261392e652e6dcf24b6b17682a4a-desc.xcbuild

note: Building targets in dependency order
error: unable to attach DB: error: accessing build database "/Users/naubry/xxx/NextVersion/publibs/catch2/_build/build/build/XCBuildData/build.db": database is locked Possibly there are two concurrent builds running in the same filesystem location.
note: Run script build phase 'Generate CMakeFiles/ZERO_CHECK' will be run during every build because the option to run the script phase "Based on dependency analysis" is unchecked. (in target 'ZERO_CHECK' from project 'Catch2')
**BUILD FAILED**

CMake Error at /usr/local/Cellar/cmake/3.24.2/share/cmake/Modules/CMakeIOSInstallCombined.cmake:144 (message):
  Build failed
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.24.2/share/cmake/Modules/CMakeIOSInstallCombined.cmake:299 (_ios_install_combined_build)
  src/cmake_install.cmake:66 (ios_install_combined)
  cmake_install.cmake:91 (include)

**BUILD FAILED**

The following build commands failed:
        PhaseScriptExecution CMake\ PostBuild\ Rules /Users/naubry/xxx/NextVersion/publibs/catch2/_build/build/Catch2.build/Debug-iphoneos/install.build/Script-93426DA6A4AC2C2AC9CE883C.sh (in target 'install' from project 'Catch2')
(1 failure)

```

I have two questions now, that is why it seems to do for all architectures:

```auto
-- [iOS combined] Architectures (iphoneos): arm64 arm64e armv7 armv7s
-- [iOS combined] Architectures (iphonesimulator): arm64 arm64e i386 x86_64
-- [iOS combined] Architectures (iphonesimulator) after pruning: i386 x86_64

```

while I ask only for arm64 and x86\_64?

```auto
{
    "name": "release-iOS",
    "inherits": ["release"],
    "generator": "Xcode",
    "cacheVariables": {
        "CMAKE_SYSTEM_NAME": "iOS",
        "CMAKE_OSX_ARCHITECTURES": "arm64;x86_64",
        "CMAKE_IOS_INSTALL_COMBINED": "YES",
        "CMAKE_INSTALL_PREFIX": "${sourceParentDir}/_install/iOS/release"
    },
    "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Darwin"
    }
},
{
    "name": "debug-iOS",
    "inherits": ["debug", "release-iOS"],
    "generator": "Xcode",
    "cacheVariables": {
        "CMAKE_INSTALL_PREFIX": "${sourceParentDir}/_install/iOS/debug"
    }
}

```

And the second question, why do I get a concurrency error when my python script first runs the configuration and then the compilation/installation?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 19, 2022, 10:18pm UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/4 "2022-10-19T22:18:01Z")

</div>

What CMake and Xcode versions are you using? The following merge request and issues linked therein may be relevant to you:

- [https://gitlab.kitware.com/cmake/cmake/-/merge\_requests/5785](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5785)

---

<div class="post-metadata">

### Author: ![DrAuraHxC](https://discourse.cmake.org/user_avatar/discourse.cmake.org/draurahxc/32/2827_2.png) [@DrAuraHxC](https://discourse.cmake.org/u/DrAuraHxC)
#### Post date: [October 20, 2022, 6:27am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/5 "2022-10-20T06:27:38Z")

</div>

It does look like my problem but the ticket is at least 1 year old and I am in version 3.24.2 for CMake and XCode 14.0.1 (14A400) so I think I have the fix in my version of CMake but it may not work now.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 21, 2022, 1:59am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/6 "2022-10-21T01:59:33Z")

</div>

How are you invoking/executing the install?

---

<div class="post-metadata">

### Author: ![DrAuraHxC](https://discourse.cmake.org/user_avatar/discourse.cmake.org/draurahxc/32/2827_2.png) [@DrAuraHxC](https://discourse.cmake.org/u/DrAuraHxC)
#### Post date: [October 21, 2022, 8:18am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/7 "2022-10-21T08:18:41Z")

</div>

To answer quickly – using a Python script that does this:

```python
subprocess.run(["cmake",
                "--preset={0}-{1}".format(args.build, args.target)], cwd=srcDir)
subprocess.run(["cmake",
                "--build", "{0}".format(buildDir),
                "-j", "{0}".format(nbCore),
                "-t", "install"], cwd=srcDir)

```

But I will do a test by running the compilation by hand and come back to give you the result.  
EDIT : I get exactly the same behavior as with the script.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 21, 2022, 9:09am UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/8 "2022-10-21T09:09:16Z")

</div>

The merge request I linked [mentioned](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5785#note_899090) that CMake no longer supports using `IOS_INSTALL_COMBINED` with building the `install` target due to limitations in Xcode 12+. Use `cmake --install` instead to avoid the “unable to attach DB” problem you are seeing.

---

<div class="post-metadata">

### Author: ![DrAuraHxC](https://discourse.cmake.org/user_avatar/discourse.cmake.org/draurahxc/32/2827_2.png) [@DrAuraHxC](https://discourse.cmake.org/u/DrAuraHxC)
#### Post date: [October 21, 2022, 2:16pm UTC](https://discourse.cmake.org/t/how-to-generate-fat-static-lib-with-cmake-preset/6655/9 "2022-10-21T14:16:08Z")

</div>

Very well by cutting out the build and installation phase, it works perfectly. Thanks a lot for the help.
