Using asset catalogs for macOS/iOS projects

Is it possible to get CMake to generate a macOS/iOS Xcode project that uses asset catalogs to set the application icon?

I’ve been trying to get it to work for days now without success, and I currently have the below code:

elseif(${TARGET_PLATFORM_FAMILY} STREQUAL Apple)
    configure_file(templates/Info.plist ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist)

    set(ICON_FILES
        Assets.xcassets/
        Assets.xcassets/Contents.json
        Assets.xcassets/Appicon.appiconset/
        Assets.xcassets/Appicon.appiconset/Contents.json
        Assets.xcassets/Appicon.appiconset/Icon512.png
    )

    target_sources(${TARGETNAME} PRIVATE ${ICON_FILES})
    source_group("Resources" FILES ${ICON_FILES})

    set_target_properties(${TARGETNAME} PROPERTIES
        MACOSX_BUNDLE TRUE
        MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist
        XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER ${APP_PACKAGE_ID}
        XCODE_ATTRIBUTE_PRODUCT_NAME ${APP_DISPLAY_NAME}
        XCODE_ATTRIBUTE_ASSETCATALOG_COMPILER_APPICON_NAME AppIcon;
        XCODE_ATTRIBUTE_COMBINE_HIDPI_IMAGES YES;
        #RESOURCE "${ICON_FILES}"
    )

When I open the generated project in Xcode I have an asset catalog folder and the Target/General page also sees the icon name (AppIcon) I set as valid (clicking on the grey arrow takes me to the asset catalog page with images appearing), but no matter what I try the app itself never gets the icon.

1 Like

Rather than adding each part of the asset catalog individually, see if adding it as a whole directory works instead:

target_sources(${TARGETNAME} PRIVATE Assets.xcassets)
set_source_files_properties(Assets.xcassets PROPERTIES
    MACOSX_PACKAGE_LOCATION Resources
)

Adding directories rather than files is not officially supported by CMake as far as I’m aware, but doing it this way should allow Xcode to see the asset catalog and compile it for you. I don’t recall if setting the source file properties is required or not, but give it a go and see.

I have tried doing that, with no luck.

After digging yet more, I am noticing that, compared with a standard Xcode project (a default one I spun up for testing), the build log never mentions the “Compile Asset Catalogs” step.

After adding a default MainMenu.xib file that too never gets picked up for compilation, in spite of me adding it to the project’s sources (default project build log has a "Compile XIB file step that is missing in my CMake one). And of course the resulting app has no menu.

It sounds like you might have something else going on in your project. The approach I mentioned above does compile an asset catalog for me. I suggest you strip down your project to the bare minimum that reproduces the problem and post it somewhere so others can take a look. More often than not, the process of reducing the problem down to a minimal project that reproduces the issue uncovers the underlying reason.