# Embedding binaries within macOS bundle

**URL:** https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989
**Category:** Usage
**Tags:** os:macos, gen:xcode
**Created:** [February 2, 2024, 10:12am UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989 "2024-02-02T10:12:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![j-boivie](https://discourse.cmake.org/user_avatar/discourse.cmake.org/j-boivie/32/4230_2.png) [@j-boivie](https://discourse.cmake.org/u/j-boivie)
#### Post date: [February 2, 2024, 10:12am UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/1 "2024-02-02T10:12:20Z")

</div>

Howdy folks, long time listener first time caller etc.

I’m looking for information on how to embed an executable binary within a macOS app bundle when using CMake with Xcode. My project is set up as follows:

Executable target Foo which is built as a “Command Line tool” in Xcode parlance.

Executable target Bar, which is an App target. Bar has a dependency on Foo.

When I build I get both my Foo binary and my `Bar.app` bundle in my build dir, so far all is well. However what I’d like to accomplish is to embed the Foo binary within the `Contents/MacOS` directory in the Bar.app bundle.

In Xcode this can easily be done “manually” by adding a Copy Files phase to the target’s “Build Phases” setting Destination to `Executables` and adding the Foo target to the list of files. This results in the appropriate `PBXCopyFilesBuildPhase` node in the `project.pbxcproj` file.

However I’ve been unable to figure out how to do this through CMake, I’ve managed to copy other bundle resources by setting the `MACOSX_PACKAGE_LOCATION` property on the files, but setting this on the Foo target has no effect. I’ve trawled the internet for solutions but haven’t been able to find anything so far, is there a “canonical” way of doing this with CMake?

Thanks!

---

<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: [February 4, 2024, 1:39pm UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/2 "2024-02-04T13:39:50Z")

</div>

CMake’s own packaging does something like this where these binaries live in `Contents/bin`. The only detail that I can think of is to install the main program first and then the helpers.

---

<div class="post-metadata">

### Author: ![j-boivie](https://discourse.cmake.org/user_avatar/discourse.cmake.org/j-boivie/32/4230_2.png) [@j-boivie](https://discourse.cmake.org/u/j-boivie)
#### Post date: [February 7, 2024, 1:31pm UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/3 "2024-02-07T13:31:41Z")

</div>

Thanks! Would you be able to point me in the general direction of where this is done in the CMake repository? I had a search for `bin/` but it wasn’t that fruitful.

---

<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: [February 7, 2024, 8:48pm UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/4 "2024-02-07T20:48:01Z")

</div>

I’m not sure exactly. @brad.king?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [February 7, 2024, 10:12pm UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/5 "2024-02-07T22:12:05Z")

</div>

They are not integrated in CMake’s build tree, but we use [a symlink](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.28.3/Source/QtDialog/CMakeLists.txt#L316-321) there to put `cmake-gui` next to the rest of the binaries in the `bin` directory.

The integration occurs during installation. `CMake.app` is installed by [this code](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.28.3/Source/QtDialog/CMakeLists.txt#L326) to the real install prefix, and then all the rest of the files go to the prefix with `CMake.app/Contents` appended. See [this code](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.28.3/CMakeLists.txt#L443-454).

The same installation process is used during packaging with CPack to produce our official binaries.

---

<div class="post-metadata">

### Author: ![j-boivie](https://discourse.cmake.org/user_avatar/discourse.cmake.org/j-boivie/32/4230_2.png) [@j-boivie](https://discourse.cmake.org/u/j-boivie)
#### Post date: [February 9, 2024, 9:06am UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/6 "2024-02-09T09:06:21Z")

</div>

Thanks for the answers both of you, that’s very helpful.

Symlinking when building and then doing the copying during installation seems like a good approach.

Can I ask why the “native” way isn’t supported? Given that we can populate other bundle directories like `Frameworks/` & `Resources/` using the `MACOSX_PACKAGE_LOCATION` property, why not do something similar for executables? It would be really nice to be able to set a property on a bundle target where I can specify an executable and have that copied into the bundle’s `MacOS/` directory.

Cheers!

---

<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: [February 16, 2024, 2:18pm UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/7 "2024-02-16T14:18:09Z")

</div>

That feels like it would be a property that ultimately depends on consumers. If I have a `not_a_bundle` executable with a property of `MACOSX_PACKAGE_LOCATION`, what is responsible for putting it into the bundle? Which bundle? Multiple?

---

<div class="post-metadata">

### Author: ![j-boivie](https://discourse.cmake.org/user_avatar/discourse.cmake.org/j-boivie/32/4230_2.png) [@j-boivie](https://discourse.cmake.org/u/j-boivie)
#### Post date: [February 21, 2024, 9:37am UTC](https://discourse.cmake.org/t/embedding-binaries-within-macos-bundle/9989/8 "2024-02-21T09:37:24Z")

</div>

That’s a good point. I’m not too familiar with CMake in this regard but I suppose the bundle concept doesn’t really make sense outside of Apple platforms, might be hard to express that an executable should contain another executable in a cross-platform way.

The benefit of doing it in XCode seems to be that the build system is a bit more intelligent in what it needs to copy and when, but symlinking is not an expensive operation so that isn’t really a big deal.

Thanks for the help!
