简体   繁体   中英

CMAKE create OSX bundle for dylib?

I need some help from from someone that unlike me actually knows something about 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. It's setup to build as a MODULE like this:

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. 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. 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. On other platforms the resources just go into a folder next to the dylib. And then have the bundle installed, or dll and folder, into whatever destination they are destined for.

So how to bundle a module? Is it even possible or am I wasting my time?

this is all quite possible with 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). Here is one of its CMakeLists.txt files that covers most of what you need (around line 38).

If you want cmake to crate the bundle for you, you need to pass MACOSX_BUNDLE when you add the executable ( ADD_EXECUTABLE ). Note that this is for the Application itself and not for the 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. dito for plugins if you build them with the BUNDLE property.

you can also manually assign the MACOSX_PACKAGE_LOCATION property to whatever you like

I believe you also may have to do something like SET(CMAKE_INSTALL_RPATH @loader_path/../Frameworks) on the target.

Let me know if you have any questions and i will update my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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