简体   繁体   English

CMAKE为dylib创建OSX包?

[英]CMAKE create OSX bundle for dylib?

I need some help from from someone that unlike me actually knows something about CMAKE. 我需要一些不同于我的人的帮助,实际上我知道CMAKE的一些事情。

Problem: I have this CMAKE project that produces a .so/.dylib/.dll (its a plugin for another application) and so far everything is fine it's compiling and linking and produces the expected output. 问题:我有这个CMAKE项目产生.so / .dylib / .dll(它是另一个应用程序的插件),到目前为止一切都很好,它正在编译和链接并产生预期的输出。 It's setup to build as a MODULE like this: 它的设置是像这样构建一个MODULE:

ADD_LIBRARY(${PROJECT_NAME} MODULE ${CORE_SRC} ${CORE_HEADERS})

with a healthy bunch of external dependencies and a few compiler/linker settings. 拥有一组健康的外部依赖项和一些编译器/链接器设置。

And builds using two custom targets: 并使用两个自定义目标构建:

ADD_CUSTOM_TARGET(debug
        COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR}
        COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
        COMMENT "Switch CMAKE_BUILD_TYPE to Debug")

ADD_CUSTOM_TARGET(release
        COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR}
        COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
        COMMENT "Switch CMAKE_BUILD_TYPE to Release")

My problem is that on osx this dylib must be packaged as a osx BUNDLE or it wont load. 我的问题是,在osx上,这个dylib必须打包为osx BUNDLE,否则它不会加载。 I could create the bundle directory structure manually but with my not so comprehensive CMAKE knowledge I do believe I should be able to have it generated for me. 我可以手动创建捆绑目录结构,但由于我不太全面的CMAKE知识,我相信我应该能够为我生成它。 I did try to follow some examples, but I could only get it to work for executable files. 我确实尝试过一些例子,但我只能让它用于可执行文件。

What I would like is to have my dylib installed into a bundle with its resources inside my out of source directory. 我想要的是将我的dylib安装到一个捆绑包中,其资源位于我的源目录之外。 On other platforms the resources just go into a folder next to the dylib. 在其他平台上,资源只会进入dylib旁边的文件夹。 And then have the bundle installed, or dll and folder, into whatever destination they are destined for. 然后将捆绑软件或dll和文件夹安装到目的地的任何目的地。

So how to bundle a module? 那么如何捆绑模块呢? Is it even possible or am I wasting my time? 它甚至可能还是我在浪费时间?

this is all quite possible with cmake; 这对cmake来说是完全可能的; tho I remember having a difficult time figuring it all out as well. 我记得我也很难搞清楚这一切。

I have a rather complex project that does this very thing with a dylib, a framework, and a series of plugins (which happen to also be .so s). 我有一个相当复杂的项目,它使用dylib,框架和一系列插件(碰巧也是.so s)来完成这件事。 Here is one of its CMakeLists.txt files that covers most of what you need (around line 38). 是其中一个CMakeLists.txt文件,涵盖了您需要的大部分内容(第38行)。

If you want cmake to crate the bundle for you, you need to pass MACOSX_BUNDLE when you add the executable ( ADD_EXECUTABLE ). 如果您希望cmake为您创建捆绑包,则需要在添加可执行文件( ADD_EXECUTABLE )时传递MACOSX_BUNDLE Note that this is for the Application itself and not for the dylib. 请注意,这是针对应用程序本身而不是针对dylib。

If it is possible for you to build the dylib as a framework ( FRAMEWORK property), cmake should automatically put it it the bundle's frameworks directory when you add it. 如果您可以将dylib构建为框架( FRAMEWORK属性),则在添加它时,cmake 自动将其作为bundle的frameworks目录。 dito for plugins if you build them with the BUNDLE property. 如果使用BUNDLE属性构建它们,则为dito提供插件。

you can also manually assign the MACOSX_PACKAGE_LOCATION property to whatever you like 您也可以手动将 MACOSX_PACKAGE_LOCATION属性分配给您喜欢的任何内容

I believe you also may have to do something like SET(CMAKE_INSTALL_RPATH @loader_path/../Frameworks) on the target. 我相信你也可能需要在目标上执行类似SET(CMAKE_INSTALL_RPATH @loader_path/../Frameworks)的操作。

Let me know if you have any questions and i will update my answer. 如果您有任何问题,请告诉我,我会更新我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM