Problems With MacOS Cross Platform Build

Hello, so I’m attempting to build my game for my friend who uses a mac and found that he couldn’t run the file built from my machine (Windows 10). I figured it was a problem with execution so I rebuilt the game from a Linux machine (Ubuntu 20.04) and my friend was able to run the program but it ran into an error of “Build is Damaged” whenever he launched it. I’m using Unity version 2020.3.2f1 for my project right now. Thinking that the cause may have been some configuration or error, I built an new project with no settings changes, sent it over to my friend, and ran into the same errors. Is this a known bug with the current version of 2020 unity editors?

In an attempt to work around and test this without having to send my game files to my friend constantly, I found a macOS VM that I could run from my Linux machine (GitHub - foxlet/macOS-Simple-KVM: Tools to set up a quick macOS VM in QEMU, accelerated by KVM.). While running through the error messages, I found this old forum post Unity Issue Tracker - [macOS] "build is damaged and cannot be opened" error when downloading Unity build from internet and that mentioned using the codesign command. Through these convoluted steps and a custom build workflow, I was able to build the project, sign it with the codesign command on the macOS VM, and send it to my friend.

Commands used to build project, whole script here - PropHunt-Mirror/Assets/Editor/ScriptBatch.cs at main · nicholas-maltbie/PropHunt-Mirror · GitHub

/Applications/Unity/Hub/Editor/2020.3.2f1/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod ScriptBatch.MacOSBuild
# Use the --deep parameter because I have sub bundle files that need to be signed
codesign -s - -f --deep Builds/MacOS/PropHunt.app
codesign -s - -f Builds/MacOS/PropHunt.app

Code for configuring build

public class ScriptBatch : IPostprocessBuildWithReport
{
    public int callbackOrder { get { return 0; } }

    public static string[] GetScenes()
    {
        return new string[] { "Assets/Scenes/BasicHouse.unity" };
    }

    public void OnPostprocessBuild(BuildReport report)
    {
#if UNITY_STANDALONE_OSX
        UnityEngine.Debug.Log("Signing files for MacOS Build");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath + "/Contents/PlugIns/steam_api.bundle");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath + "/Contents/PlugIns/phonon.bundle");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath + "/Contents/PlugIns/audioplugin_phonon.bundle");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath);
#endif
        UnityEngine.Debug.Log("MyCustomBuildProcessor.OnPostprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
    }

    [MenuItem("Build/MacOS Build")]
    public static void MacOSBuild()
    {
        // Get filename.
        string path = "Builds/MacOS";
        string[] levels = GetScenes();

        string appFolder = path + "/PropHunt.app";

        PlayerSettings.SetArchitecture(BuildTargetGroup.Standalone, 1);

        // Build player.
        BuildPipeline.BuildPlayer(levels, appFolder, BuildTarget.StandaloneOSX, BuildOptions.Development);
    }
}

I’m using some extra bundles for steam_api and phonon and thought they had some configuration problems but ran into the same errors even I used an empty project with no plugins, bundles, or other odd configurations. I could only get the compiled program to run on a macOS when built and signed from a macOS machine. The cross platform builds from windows to Linux (Ubuntu 20.04) or Linux (Ubuntu 20.04) to windows worked just fine. This is the part that threw me off the most, even a basic, empty unity project could not be built cross platform for a Mac. This might just be a problem with my macOS VM and my friend’s individual machine but I feel like there must be something I’m missing.

Here is the link to my whole repository if you want to know more about the individual configuration settings - GitHub - nicholas-maltbie/PropHunt-Mirror: PropHunt project made using Unity and Mirror Networking

Is there a better way to build the project for macOS, I remember it being more straightforward.

Is there a simple configuration setting I’m forgetting or have done incorrectly? I tried to look for resources online but didn’t find anything for addressing the issue me and my friends ran into. This set of build steps using a macOS VM is rather convoluted but I can probably mostly automate it so I’m not too worried but would like to know if there is a better option available.

Have you upgraded Unity after you ran into this issue? There was this known issue: Unity Issue Tracker - macOS builds no longer run when with a quarantine attribute due to incorrect codesigning

However, it was fixed in 2020.3.0f1. Furthermore, the fix also introduced the signing APIs that you use, and Unity actually invokes those same APIs at the end of the build. Are you sure that nothing else is modifying the built .app bundle after Unity builds it but before your signing code runs? Unity signing the build should prevent the “App is damaged” error.

If you can still reproduce it without the signing code, could you ask your friend to capture a screenshot of that error?

Hello, could you link to that documentation? My friend just got the error message of “damaged and can’t be opened, You should move it to the Trash” with no extra information and just wanted to close.

I run into this error even if I build from a mac with an empty unity project. I tried using more recent versions of unity, is the issue resolved in unity version 2021, I can upgrade if so?

With my project in specific, I have three app bundles for extra libraries. Could you link to those examples of extra resources? I linked the github project and my settings currently and can send more detailed information if you need it.

We tried reproducing the error again and upgrading the version of unity to 2020.3.4 and ran into similar issues.

When building from mac, we got the same errors, here are screenshots from our computers attempting to run the file


In order to try an isolate the error, I made a fresh unity project with no settings changed just to see if the project would run and it did end up running. So I think the problem is with the bundle files that are added to the project. This is really weird and the codesign command seems to work as a hotfix workaround to run the project but I’m not sure why this works and couldn’t find any useful resources online relating to how unity builds.

Right now I’m using three bundles for steam_api and two for phonon (Steam Audio (valvesoftware.github.io))

All of the bundles seem to have issues when exported as the codesign command has some message about the signatures being incorrect or improperly configured

/Library/Developer/CommandLineTools/usr/bin/codesign_allocate: file not in an order that can be processed (code signature data out of place): /path/to/.app/.../.bundle

I don’t know if this is a problem with the .bundle files I imported, unity’s build process, or something else that has gone wrong. I know I may need to sign the file with a proper apple id if I want to export it to the mac store but is this required just to run a development version of the project?

I would think not because I was able to run the basic test project with no bundle files but then again I’m not sure how mac projects work in general :frowning: Any help or resources linked would be much appreciated.

Could you try running “codesign -vvv --deep /path/to/game.app” on the broken build? It should show exactly what macOS doesn’t like about the application.

This is actually due to Unity signing the bundle prevents it from being re-signed with system codesign tool in certain scenarios. This was fixed in Unity 2020.3.5f1 (https://issuetracker.unity3d.com/issues/resigning-unity-built-macos-app-results-in-code-signature-data-out-of-place-out-of-place-error). If you update your Unity version, it should be able to resign it without any issues. You don’t need an Apple ID for this.

In summary, you are running into several different issues:

  1. Building from Unity results in a damaged build. This is mostly likely because something is modifying the app bundle after the game is built. I’m not sure what it is, but “codesign -vvv --deep /path/to/game.app” might give you some clues. Resigning the bundle using the Unity signing API or via the codesign tool will work around it (UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle).
  2. Resigning using the codesign tool fails with “file not in an order that can be processed (code signature data out of place)” error. This issue was fixed in Unity 2020.3.5f1.

Updating to unity version 2020.3.5f1 fixed the issues with the codesign command along with using the UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle api command. This allows the build to work properly when building on a MacOS machine and the codesign command no longer creates those errors.

I’m still running into errors when building for cross platform from linux to mac (error message posted below)

I followed your advice and ran the codesign -vvv project.app command and it showed that a .bundle directory was modified so I’ll try and re-import and fix that error with the build order. I think this may have to do with its placement in the project as it’s not in the Plugins directory so hopefully I’ll be able to sort out that error and fix the cross platform build errors.

It might take a while to fully sort this out so I’ll post another update later today or tomorrow when I have further information. For some reason building a basic, empty project from this version of unity is also having problems running on a mac but I will use the codesign -vvv command to investigate that as well, it may be a configuration or install error so I will do my best to sort it out. Thank you for all of the help.

Couple of weird things about this: “UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle” should work just fine on Windows and Linux editors. I’m surprised that it works when you do it in Mac editor but not Linux editor.

Furthermore, that error you’re showing does not indicate a signing error. It looks like a crash. Does the build generate a player log at all?

Hello, it seems like you are correct. When building the program with linux or mac, there is a problem with the steam_api.bundle listed below

Linux build

Nicks-iMac:Builds nick$ codesign -vvv ~/Desktop/MacOS/PropHunt.app/
--prepared:/Users/nick/Desktop/MacOS/PropHunt.app/Contents/PlugIns/audioplugin_phonon.bundle
...
/Users/nick/Desktop/MacOS/PropHunt.app/: nested code is modified or invalid
file modified: /Users/nick/Desktop/MacOS/PropHunt.app/Contents/PlugIns/steam_api.bundle

MacOS build

Nicks-iMac:Builds nick$ codesign -vvv MacOS/PropHunt.app/
MacOS/PropHunt.app/: invalid or unsupported format for signature
In subcomponent: /Users/nick/Projects/PropHunt-Mirror/Builds/MacOS/PropHunt.app/Contents/PlugIns/steam_api.bundle

Although, I did notice that I was able to run the codesign command after the project was built and have the project operate properly and have all the files be correctly signed.

Nicks-iMac:Builds nick$ codesign -s - -f --deep MacOS/PropHunt.app
MacOS/PropHunt.app: replacing existing signature
Nicks-iMac:Builds nick$ codesign -vvv MacOS/PropHunt.app
--prepared:/Users/nick/Projects/PropHunt-Mirror/Builds/MacOS/PropHunt.app/Contents/PlugIns/steam_api.bundle
--validated:/Users/nick/Projects/PropHunt-Mirror/Builds/MacOS/PropHunt.app/Contents/PlugIns/steam_api.bundle
...
MacOS/PropHunt.app: valid on disk
MacOS/PropHunt.app: satisfies its Designated Requirement

Good news is that it fails on both mac and linux equally just I can correct it after it fails on linux. I’m not sure what would be modifying that steam_api.bundle in the project after it is built. I’m not as familiar with MacOS so I’m attempting to look into the problem and modifying my build script.

This is how I’m using the signing API by the way, is this configured properly to sign the generated files?

public class ScriptBatch : IPostprocessBuildWithReport
{
    public int callbackOrder { get { return 0; } }

    public static string[] GetScenes()
    {
        return new string[] { "Assets/Scenes/BasicHouse.unity" };
    }

    public void OnPostprocessBuild(BuildReport report)
    {
#if UNITY_STANDALONE_OSX
        UnityEngine.Debug.Log("Signing files for MacOS Build");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath + "/Contents/PlugIns/phonon.bundle");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath + "/Contents/PlugIns/audioplugin_phonon.bundle");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath + "/Contents/PlugIns/steam_api.bundle");
        UnityEditor.OSXStandalone.MacOSCodeSigning.CodeSignAppBundle(report.summary.outputPath);
#endif
        UnityEngine.Debug.Log("MyCustomBuildProcessor.OnPostprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
    }

    [MenuItem("Build/MacOS Build")]
    public static void MacOSBuild()
    {
        // Get filename.
        string path = "Builds/MacOS";
        string[] levels = GetScenes();

        string appFolder = path + "/PropHunt.app";

        // Build player.
        BuildPipeline.BuildPlayer(levels, appFolder, BuildTarget.StandaloneOSX, BuildOptions.Development);
    }
}

Upon looking into the player logs (didn’t know where they were stored but was able to lookup and find them at ~/Library/Logs/Company Name/Product Name/Player.log https://docs.unity3d.com/Manual/LogFiles.html), I see that my VM I setup does not have graphical library setup so that may be causing the errors since it is missing a graphics driver and all rendering is done by the CPU. I will look into setting up that link now and seeing if that helps with launching the linux program. It makes sense that it would crash if it doesn’t have access to graphics drivers and they are trying to be accessed by the unity program.

Thanks again for the help, I’ll post an update if I find anything else soon and keep on working on the current bugs with these new tools. Do you have any idea what would be causing the steam_api.bundle to be modified after the build process, is there a way to check if the signature is valid or corrupted before the build? because if it’s corrupted before the program is built it may not be able to be fixed by the unity signing api (I don’t know how this works very well, not sure what would modify it after it is built).

Honestly, this sounds like another bug in Unity code signing logic. The code you showed looks correct. Would you mind filing a bug report? I’d ask that you’d include the plugin that Unity fails to sign in the report so that we could reproduce it locally.

Hello, after looking into the crash reports more I think the crashes are partially due to the low performance nature of my MacOS vm, may have to invest in a full machine or test more with my friend in the future to find the sources of those errors.

I filed a bug report with an attached zip of the failing bundle file and how I’m currently utilizing the signing api. https://support.unity.com/hc/en-us/requests/1030038 This is my first time filing a bug report so if I need to do anything else to file it properly please send a response and I’ll do my best to fix any mistakes.

I can make an example project with just this bundle file added and showcasing how the error occurs and how the codesign command helps circumvent this issue if that would help. Upgrading the version of unity and using the codesign command seems to be a good enough workaround for me for now. If you need anything else for or more information send me a message. Otherwise I’ll wait on the bug report response and help there however I can.

Thanks for all the help resolving this issue.

I was referring to the signing the bundle as a bug, not the crashes.

Oh, sorry. Here are the instructions to file bug reports: https://unity3d.com/unity/qa/bug-reporting

support@unity3d.com email is mostly about product purchase issues.

Sounds good, I will recreate an empty project to re-produce the bug and post the relevant information through that bug reporting.

Did OP ever find a solution to this? I’ve been trying to use Unity Cloud Build to build the mac version and keep running into the same issue.

I would like to know, too. I don’t even have any OnPostprocessBuild and it still happens. It was fine on 2019.4, but ever since I upgraded to 2021.1 and then 2021.2 it keeps happening, and I didn’t change the codebase at all between upgrading.