如何使用 CMake 将 Info.plist 捆绑在 MacOSX 应用程序中?

How to have Info.plist bundled in a MacOSX application with CMake?

我正在开发一个访问相机的应用程序。项目是用C++写的,我用CMake打包的。

要在Mac中部署项目,我使用下面的命令,然后在Xcode中打开项目:

cmake -G Xcode ../src

在上次更新之前一直运行良好,当它开始抱怨:

[access] This app has crashed because it attempted to access 
privacy-sensitive data without a usage description.  The app's 
Info.plist must contain an NSCameraUsageDescription key with 
a string value explaining to the user how the app uses this data.
Program ended with exit code: 9

所以我创建了一个包含以下内容的新 Info.plist 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
    <dict>
        <key>CFBundleIconFile</key>
        <string></string>
        <key>NSCameraUsageDescription</key>
        <string>This app requires to access your camera 
            to retrieve images and perform the demo</string>
    </dict>
</plist>

我的问题是:我应该在 CMakeLists.txt 中添加什么来获取这个文件并将其放在适当的位置?而且... cmake -G Xcode 是否有可能将其正确包含在 Xcode 项目中?

编辑 根据建议,我尝试了这个:

  # Compile files:
  add_executable(fpv 
    main.cpp

    files.cpp
    files.hpp

    more-files.cpp
    more-files.hpp
 )

# Link files:
target_link_libraries(fpv
   fpv-lib
   ${GTKMM_LIBRARIES}  
   ${OpenCV_LIBS} )

# Lets bundle it:
set_target_properties(fpv PROPERTIES
  FRAMEWORK TRUE
  FRAMEWORK_VERSION C
  MACOSX_FRAMEWORK_IDENTIFIER com.cmake.dynamicFramework
  MACOSX_FRAMEWORK_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist
  # "current version" in semantic format in Mach-O binary file
  VERSION 16.4.0
  # "compatibility version" in semantic format in Mach-O binary file
  SOVERSION 1.0.0
  # XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Jean-Michel Gonet"
)

编辑 为了确保 Info.plist 是正确的,我在 Xcode 项目中手动添加了它:

然后成功了。

但我还是想在CMake中进行项目配置。

官方 Cmake 文档(参见 cmake-properties-7)提到了 MACOSX 目标属性的 2 个系列:

  • FRAMEWORKFRAMEWORK_VERSIONMACOSX_FRAMEWORK_INFO_PLIST
  • MACOSX_BUNDLE_INFO_PLISTMACOSX_BUNDLE

现在,Info.plist 的包含由后者 Bundle 家族管理。

举个完整的例子,假设您的应用程序名为 fpv,您的目录结构为:

/src   <-- Root folder
 /lib  <-- There you may add sources for your application's library
      CMakeLists.txt <-- Library has its own configuration
      more-files.cpp
      lots-of-files.cpp
      etc.cpp
      ...
 /app  <-- This is where executable resides
      CMakeLists.txt <-- This is where you need to configure the bundle
      Info.plist    <-- I've placed the plist file just besides.
      main.cpp
      second.cpp
      more.cpp

这就是您的应用程序 CMakeLists.txt 的样子:

# src/app
project( fpv )

# GTKMM has to be linked/included via pkg_config:
find_package(PkgConfig)
pkg_check_modules(GTKMM gtkmm-3.0) # Defines variables GTKMM_INCLUDE_DIRS, GTKMM_LIBRARY_DIRS and GTKMM_LIBRARIES.
link_directories( ${GTKMM_LIBRARY_DIRS} )
include_directories( ${GTKMM_INCLUDE_DIRS} )

# OpenCV can be linked as usual:
find_package( OpenCV REQUIRED )

# Compile files:
add_executable(fpv 
    main.cpp

    main-window.cpp
    main-window.hpp

    auto-viseur.cpp
    auto-viseur.hpp
 )

# Link files:
target_link_libraries(fpv
   fpv-lib
   ${GTKMM_LIBRARIES}  
   ${OpenCV_LIBS} )

# Lets bundle it:
set_target_properties(fpv PROPERTIES
  MACOSX_BUNDLE TRUE
  MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist
)

要构建 XCode 项目,请在主 src 文件夹之外创建一个 xcode 文件夹:

mkdir xcode
cd xcode
cmake -G Xcode ../src

现在您可以将其作为来自 Xcode 的项目打开。要在调试模式下执行:

  • Select 您应用程序的目标(左上角,默认设置为 ALL_BUILD)。您会注意到该图标已更改为程式化的 A。
  • 运行吧。
  • 如果一切顺利,您应该被要求允许您的应用程序访问相机。
  • 此外,您应该将 Info.plist 文件视为资源