Resource file gets included twice in Mac OS X application package built using CMake and CPack

StackOverflow https://stackoverflow.com/questions/23535638

  •  17-07-2023
  •  | 
  •  

質問

I am trying to use CMake and CPack to build and package a Mac OS X application, but I can't figure out the correct way to include resources.

This is what my CMakeLists.txt looks like:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(foo)
ADD_EXECUTABLE(foo foo.c foo.txt)
SET_TARGET_PROPERTIES(foo PROPERTIES MACOSX_BUNDLE TRUE)
SET_TARGET_PROPERTIES(foo PROPERTIES RESOURCE foo.txt)
INSTALL(TARGETS foo DESTINATION ".")
SET(CPACK_GENERATOR TGZ)
INCLUDE(CPack)

However, when I use it to build a package, foo.txt gets included twice: in the Resources directory of the bundle as expected, but also at the root:

$ cd build
$ cmake ..
$ make package
$ tar -xvzf foo-0.1.1-Darwin.tar.gz 
x foo-0.1.1-Darwin/foo.app/Contents/Info.plist
x foo-0.1.1-Darwin/foo.app/Contents/MacOS/foo
x foo-0.1.1-Darwin/foo.app/Contents/Resources/foo.txt
x foo-0.1.1-Darwin/foo.txt

What am I doing wrong?

EDIT

For easier reading, here is what the final, working, CMakeLists.txt looks like (as per Josh's answer and my comment on it):

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(foo)
ADD_EXECUTABLE(foo foo.c foo.txt)
SET_TARGET_PROPERTIES(foo PROPERTIES MACOSX_BUNDLE TRUE)
SET_SOURCE_FILES_PROPERTIES(foo.txt PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
INSTALL(TARGETS foo DESTINATION ".")
SET(CPACK_GENERATOR TGZ)
INCLUDE(CPack)
役に立ちましたか?

解決

Removing the line * PROPERTIES RESOURCE foo.txt) should solve the problem.

Since you have included cpack in your cmake file, the generated TGZ will contain all of the files which would be installed with make install. In this case, you have

SET_TARGET_PROPERTIES(foo PROPERTIES MACOSX_BUNDLE TRUE)

which will build foo as an OS X application bundle (doc), and

SET_TARGET_PROPERTIES(foo PROPERTIES RESOURCE TRUE)

specifies foo.txt as a resource files (doc). With both of those PROPERTIES set, make install will create two versions of foo.txt. One in the bundle, foo.app/Contents/Resources/foot.txt, and one in the top level directory, foo.txt. Therefore, CPack will also generate those two versions of foo.txt.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top