cmake: set icon for a Mac OS X app

2019-04-07 17:07发布

问题:

Installing a Mac OS X app using cmake environment, I want to set and install the icon in the process of installation.

Therefore, I try to set

set( MACOSX_BUNDLE_ICON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/images/myAopImage.icns )
ADD_EXECUTABLE(MYAPP MACOSX_BUNDLE ${MACOSX_BUNDLE_ICON_FILE} ${allSources})
#(I could set it in the source directory to any other path)

However, this does not work, it writes the complete path into the Info.plist, and installs nothing into the Resources directory (the resources dir even does not get created in the app folder which gets installed)

Anyhow, even if I put by hand into the Info.plist the information

<key>CFBundleIconFile</key>
<string>myAppImage.icns</string>

and create the Resources directory by hand, putting into there the icns File,

the icon appears anyhow for the app.

How do I solve this problem? At first to do it by hand maybe, but also within the cmake context?

回答1:

To copy the icns file to your Resources folder, you need to set a source property on it.

set_source_files_properties(myAppImage.icns PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")

Also, I read somewhere that the string in the plist does not require the .icns, so

<string>myAppImage</string> 

should be fine.



回答2:

There are two pieces to bundling the .icns file from CMake: adding the source file itself, and configuring it in the .plist. If you want to specify a path to your .icns file (e.g. because it is in a subdirectory), then these two properties will refer to the .icns file slightly differently:

# NOTE: Don't include the path in MACOSX_BUNDLE_ICON_FILE -- this is
# the property added to Info.plist
set(MACOSX_BUNDLE_ICON_FILE myAppImage.icns)

# And this part tells CMake where to find and install the file itself
set(myApp_ICON ${CMAKE_CURRENT_SOURCE_DIR}/images/myAppImage.icns)
set_source_files_properties(${myApp_ICON} PROPERTIES
       MACOSX_PACKAGE_LOCATION "Resources")

add_executable(myApp MACOSX_BUNDLE ${myApp_ICON} ${allSources})