Compiling XIBs with CMake without Xcode

I’ve been enjoying using the JetBrains IDE CLion to do some refactoring and improvements to the Auctions code base. However, when I tried to build the Mac app bundle with it, the app failed to launch:

2022-07-30 19:54:15.117 Auctions[80371:16543044] Unable to load nib file: Auctions, exiting

The XIB files were definitely part of the CMake project. I later learned that CMake does not automatically add XIB compilation targets to a project. It relies on the Xcode generator to do that.

I found a long-archived documentation page from CMake on the Kitware GitLab that described a method to build NIB files from XIBs, and have modified it to make it simpler for Auctions.

You can see the change in the commit diff, but I’ll include the snippet here for posterity.

First, you define an array with the XIB file names with no suffix. For instance, I’ve done set(COCOA_UI_XIBS AXAccountsWindow AXSignInWindow Auctions) for the three XIB files presently in the codebase.

Then we have the loop to build them:

find_program(IBTOOL ibtool REQUIRED)
foreach(XIBFILE ${COCOA_UI_XIBS})
add_custom_command(TARGET Auctions POST_BUILD
COMMAND ${IBTOOL} --compile ${CMAKE_CURRENT_BINARY_DIR}/Auctions.app/Contents/Resources/${XIBFILE}.nib ${CMAKE_CURRENT_SOURCE_DIR}/${XIBFILE}.xib
COMMENT "Compiling NIB file ${XIBFILE}.nib")
endforeach()

Now it starts correctly and works properly when built from within CLion. This was surprisingly difficult to debug and fix, so I hope this post can help others avoid the hours of dead ends that I endured.

Until next time, Happy Hacking!

One thought on “Compiling XIBs with CMake without Xcode”

  1. Thanks for this! I combined this idea with a couple other answers I found to produce a drop-in helper script that lets you add xibs from anywhere in the project with “add_xib([filename])”: https://gist.github.com/cubicvoid/676d4d64dedd73e073dc02483e6c3c23

    It makes the handling a little cleaner:
    – Don’t need to list all xibs for a large project in a single top-level CMakeLists.txt
    – Defines the nibs as file rather than target dependencies so they’re only rebuilt when needed (the version in the post rebuilds all nibs when anything in the target changes)
    – Defers to CMake’s built-in resource copying for the RESOURCE field instead of doing it manually
    – Plays nicely with Xcode’s resource handling (can optionally use either Xcode or non-Xcode generator with same results)

    Thanks again, I couldn’t have gotten this working without your post.

    Liked by 1 person

Leave a comment