在 OSX 上使用 CMake 构建 Unity 原生包 [英] Build Unity native bundle with CMake on OSX

查看:31
本文介绍了在 OSX 上使用 CMake 构建 Unity 原生包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xcode 项目,它构建了一个包含库的包.我使用以下菜单创建了项目:

I have an Xcode project that builds a bundle that contains a library. I created the project using the following menu:

新项目 -> 框架 &图书馆 -> 捆绑

New Project -> Framework & Library -> Bundle

为了提供更多上下文,我想创建一个 Unity 原生插件.目前头文件非常简单,只包含一个头文件和一个源文件.

To give a bit more context I want to create a native plugin for Unity. At the moment the header is very simple and just contains a header and a source file.

我现在希望能够使用 CMake 生成一个类似的项目,但我正在努力使其工作.

I now want to be able to generate a similar project using CMake and I am struggling to make it work.

我拥有的 CMake 文件归结为:

The CMake file I have boils down to this:

cmake_minimum_required(VERSION 3.3)
project(Plugin)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES Plugin.cpp)
add_library(Plugin SHARED ${SOURCE_FILES})

上面的脚本构建了一个 .dylib 文件,我想让它构建一个 .bundle 文件.

The above script builds a .dylib file and I would like to get it to build a .bundle file.

我发现 add_executable 有一个 MACOSX_BUNDLE 选项,但它对我不起作用,因为我想捆绑一个图书馆.还有一个名为 BUNDLE 的目标属性,但它没有似乎对我的项目进行了任何更改 - 当我希望获得 .bundle 文件时,它仍然会创建一个 .dylib 文件.

I found that add_executable has a MACOSX_BUNDLE option but it doesn't work for me because I want to bundle a library. There is also a target property called BUNDLE but it doesn't seem to change anything to my project - it still creates a .dylib file when I would expect to get a .bundle file.

我阅读了这个问题,这似乎是建议一个有效的解决方案,但我无法让它适用于我的项目.

I read this question that seems to suggest a valid solution but I can't make it work for my project.

感谢任何帮助.

推荐答案

我不使用 CMake,但我已经为 iOS 和 OSX 构建了一个 Unity 原生库.我使用构建脚本通过调用 xcodebuild 为两个平台构建.它为 iOS 构建了一个静态库,为 OSX 构建了一个动态库,当将构建的库复制到位时,.dylib 被简单地重命名为 .bundle.

I don't use CMake, but I have built a Unity native library for both iOS and OSX. I use a build script to build for both platforms by calling xcodebuild. It builds a static library for iOS and a dynamic library for OSX and when copying the built libraries into place, the .dylib is simply renamed to .bundle.

这是我的构建脚本,如果有帮助的话:

Here is my build script, if that helps:

#!/bin/sh

PROJECT=UnityPlugin_UserDefaults.xcodeproj
CONFIGURATION=Release
BUILDDIR=build
OSXARCHIVEDIR=UserDefaults_OSX.xcarchive
IOSLIB=../Plugins/iOS/libUserDefaults.a
OSXLIB=../Plugins/UserDefaults.bundle

rm -rf $BUILDDIR $OSXARCHIVEDIR

for sdk in iphoneos iphonesimulator
do
    xcodebuild -project $PROJECT -configuration $CONFIGURATION -scheme UserDefaults_iOS -derivedDataPath $BUILDDIR -sdk $sdk build || exit 2
done

mkdir -p $(dirname $IOSLIB)
rm -f $IOSLIB
xcrun lipo \
    $BUILDDIR/Build/Products/$CONFIGURATION-iphoneos/libUserDefaults.a \
    $BUILDDIR/Build/Products/$CONFIGURATION-iphonesimulator/libUserDefaults.a \
    -output $IOSLIB \
    -create || exit 3

echo Created $IOSLIB:
file $IOSLIB

xcodebuild -project $PROJECT -configuration $CONFIGURATION -scheme UserDefaults_OSX -derivedDataPath $BUILDDIR -archivePath $OSXARCHIVEDIR -sdk macosx archive || exit 4

mkdir -p $(dirname $OSXLIB)
cp $OSXARCHIVEDIR/Products/usr/local/lib/libUserDefaults.dylib $OSXLIB || exit 5

echo Created $OSXLIB:
file $OSXLIB

rm -rf $BUILDDIR

Xcode 项目包含每个平台的目标,其中Scheme 用于定义要构建的目标.

The Xcode project contains a target for each platform with a Scheme used to define which to build.

这篇关于在 OSX 上使用 CMake 构建 Unity 原生包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆