Skip to content

iOS: native support #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 57 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
1ca5f10
added build post process for xcode
bitsandfoxes Jul 13, 2021
e7e8479
fixed pathing
bitsandfoxes Jul 13, 2021
88ef4d5
moved ios post processing into new proj
bitsandfoxes Jul 15, 2021
0908f67
cleanup
bitsandfoxes Jul 15, 2021
61e7737
Merge branch 'main' into feat/ios_native
bitsandfoxes Jul 15, 2021
1bbc257
added Editor.iOS to solution
bitsandfoxes Jul 15, 2021
ef0fa15
renamed post process
bitsandfoxes Jul 15, 2021
c01b417
stabilize
bitsandfoxes Jul 22, 2021
75326d3
stabilize cleanup
bitsandfoxes Jul 22, 2021
64f6b30
first wip dictionary
bitsandfoxes Jul 23, 2021
20c4fbd
Merge branch 'main' into feat/ios_native
bitsandfoxes Jul 26, 2021
f929999
split build post process up
bitsandfoxes Jul 26, 2021
e2d6653
split main modifier and added tests
bitsandfoxes Jul 27, 2021
1996527
removed temp files
bitsandfoxes Jul 27, 2021
dd91db5
added cocoa as submodule
bitsandfoxes Jul 27, 2021
65c0381
added cocoa build to buildtargets
bitsandfoxes Jul 29, 2021
5d716e9
removed .framework from assets
bitsandfoxes Jul 29, 2021
644ba52
removed framework meta from gitignore for iOS targeting
bitsandfoxes Jul 29, 2021
62884dd
fixed cocoa clean
bitsandfoxes Jul 29, 2021
3849c0f
fixed plugin path
bitsandfoxes Jul 29, 2021
9a99d5c
added native options generation tests
bitsandfoxes Jul 29, 2021
45af472
tweaked native options generation
bitsandfoxes Jul 29, 2021
fc025b2
added more options to native options
bitsandfoxes Jul 30, 2021
db2d3bb
newlines are the worst.. -.-
bitsandfoxes Jul 30, 2021
718108a
csproj cleanup
bitsandfoxes Jul 30, 2021
bd2d0e2
cocoa build script cleanup
bitsandfoxes Jul 30, 2021
1fa6170
added test xcodeproj
bitsandfoxes Aug 2, 2021
9359b58
added sentry xcode project tests & added opt out options for native
bitsandfoxes Aug 2, 2021
79cc104
cleanup
bitsandfoxes Aug 2, 2021
de78c19
added ios opt out to options
bitsandfoxes Aug 3, 2021
c4a3ae2
cleanup
bitsandfoxes Aug 3, 2021
bb2f52d
removed dummy template
bitsandfoxes Aug 3, 2021
bd663e8
updated .gitignore
bitsandfoxes Aug 3, 2021
5cd5353
updated .gitignore
bitsandfoxes Aug 3, 2021
54e1e5d
package dev cleanup
bitsandfoxes Aug 3, 2021
dbef5f6
reverted unity project settings
bitsandfoxes Aug 3, 2021
7ba5cb0
removed unused variable
bitsandfoxes Aug 3, 2021
875fa0e
added framework validation
bitsandfoxes Aug 3, 2021
d380e9b
cleanup
bitsandfoxes Aug 3, 2021
3db5ce8
CI iOS module test
bitsandfoxes Aug 3, 2021
a8a5ac5
review
bitsandfoxes Aug 3, 2021
ca080e7
review #2
bitsandfoxes Aug 3, 2021
0d16af3
Merge branch 'main' into feat/ios_native
bruno-garcia Aug 3, 2021
867fec3
review
bitsandfoxes Aug 4, 2021
edd6ec7
Merge branch 'feat/ios_native' of https://github.com/getsentry/sentry…
bitsandfoxes Aug 4, 2021
67382e8
moved building cocoa into build.targets
bitsandfoxes Aug 5, 2021
7c45a87
moar review
bitsandfoxes Aug 5, 2021
f9896b2
fixed ios tool path for editor.ios
bitsandfoxes Aug 5, 2021
253484a
fixed pii option name in native options
bitsandfoxes Aug 5, 2021
a206db2
skip copying unchanged files
bitsandfoxes Aug 5, 2021
dc822a9
replaced copy testfiles target with built-in itemgroup
bitsandfoxes Aug 5, 2021
484fd99
excluded clang tests for non MacOSX platforms
bitsandfoxes Aug 5, 2021
75b5d44
review fixes
bitsandfoxes Aug 5, 2021
5a07dec
updated CHANGELOG.md
bitsandfoxes Aug 5, 2021
1d8fbde
added missing cocoa submodule restoration
bitsandfoxes Aug 5, 2021
801f50f
fixed inconsistent paths
bitsandfoxes Aug 5, 2021
36b135c
fixed restoring submodules
bitsandfoxes Aug 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ jobs:
unity-version: ${{ env.UNITY_VERSION }}
unity-modules: android

- name: Setup Unity Modules - iOS
uses: kuler90/setup-unity@f44e3f35ce3737316a15321ec3468161940e23e8
with:
unity-version: ${{ env.UNITY_VERSION }}
unity-modules: ios

# Need to write to $GITHUB_PATH to make the environment variable
# available to other steps.
- name: Add Unity on PATH
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ samples/unity-of-bugs/Assets/InitTestScene*
# Ignore build and Unity artifacts
package-dev/**/*.dll
package-dev/**/*.meta
package-dev/**/*.framework
# required to be makred as iOS only
!package-dev/**/*.framework.meta
!package-dev/Runtime/Sentry.Unity.dll.meta
!package-dev/Tests/*.meta
# required to be marked as Editor only
!package-dev/Tests/Runtime/Sentry.Unity.Tests.dll.meta
!package-dev/Tests/**/*.asmdef.meta
!package-dev/Tests/**/*.asmdef
!package-dev/Tests/**/*.asmdef.meta
package-dev/Tests/Editor/TestFiles/
package-dev/**/*.pdb
package-dev/**/*.xml
package-dev/**/TestSentryOptions.json
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "src/sentry-dotnet"]
path = src/sentry-dotnet
url = https://github.com/getsentry/sentry-dotnet.git
[submodule "src/sentry-cocoa"]
path = src/sentry-cocoa
url = https://github.com/getsentry/sentry-cocoa
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- iOS native support ([#254](https://github.com/getsentry/sentry-unity/pull/254))
- Compile Initialization with the game ([#272](https://github.com/getsentry/sentry-unity/pull/272))
- Native crash in sample ([#270](https://github.com/getsentry/sentry-unity/pull/270))
- Cache, background threads and data for UnityEventProcessor ([#268](https://github.com/getsentry/sentry-unity/pull/268))
Expand Down
28 changes: 28 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<UnitySampleProjectUnityVersion>$(SolutionDir)samples/unity-of-bugs/ProjectSettings/ProjectVersion.txt</UnitySampleProjectUnityVersion>
<UnityTestPlayModeResultFilePath>../../artifacts/test/playmode/results.xml</UnityTestPlayModeResultFilePath>
<UnityTestEditModeResultFilePath>../../artifacts/test/editmode/results.xml</UnityTestEditModeResultFilePath>
<!-- Cocoa-->
<CocoaRoot>$(SolutionDir)src/sentry-cocoa/</CocoaRoot>
<CocoaFrameworkDestination>$(SolutionDir)package-dev/Plugins/iOS/Sentry.framework/</CocoaFrameworkDestination>
</PropertyGroup>

<!-- Use the Unity Editor version set in the sample project of the repo -->
Expand Down Expand Up @@ -53,6 +56,31 @@
<RemoveDir Directories="$(UnityPackageProject)" />
</Target>

<Target Name="CleanCocoaSDK" AfterTargets="Clean" Condition="Exists('$(CocoaFrameworkDestination)')">
<RemoveDir Directories="$(CocoaFrameworkPath)Sentry.framework" />
</Target>

<!-- Build the iOS SDK: dotnet msbuild /t:BuildCocoaSDK -->
<Target Name="BuildCocoaSDK"
Condition="$([MSBuild]::IsOSPlatform('OSX'))
And '$(MSBuildProjectName)' == 'Sentry.Unity'
And !Exists('$(CocoaFrameworkDestination)')"
BeforeTargets="BeforeBuild">
<Error Condition="!Exists('$(CocoaRoot)')" Text="Couldn't find the Cocoa root at $(CocoaRoot)."></Error>

<Exec WorkingDirectory="$(CocoaRoot)" Command="carthage build --use-xcframeworks --no-skip-current --platform iOS"></Exec>

<!-- Itemgroup for the output Sentry.framework so we have access to '%(RecursiveDir)' when copying -->
<ItemGroup>
<CocoaBuildPath Include="$(CocoaRoot)Carthage/Build/Sentry.xcframework/ios-arm64_armv7/Sentry.framework/**/*" />
</ItemGroup>
<Copy SourceFiles="@(CocoaBuildPath)"
DestinationFiles="@(CocoaBuildPath->'$(CocoaFrameworkDestination)%(RecursiveDir)%(Filename)%(Extension)')">
</Copy>

<Error Condition="!Exists('$(CocoaFrameworkDestination)')" Text="Failed to build the Cocoa SDK."></Error>
</Target>

<!-- Even with a successful build, Unity will error on 'usbmuxd' or log out to std-error which breaks msbuild.
We need to run a unity build to restore the test packages and for that reason we'll ignore errors here and assume a later step will validate the build is actually working:
The offending error:
Expand Down
30 changes: 30 additions & 0 deletions Sentry.Unity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ ProjectSection(SolutionItems) = preProject
src\Directory.Build.props = src\Directory.Build.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Unity.Editor.iOS", "src\Sentry.Unity.Editor.iOS\Sentry.Unity.Editor.iOS.csproj", "{703A3ECC-048C-443A-B624-0D17DFC674AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Unity.Editor.iOS.Tests", "test\Sentry.Unity.Editor.iOS.Tests\Sentry.Unity.Editor.iOS.Tests.csproj", "{A43C866F-FA99-4B01-B480-6A7901569FAB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -101,6 +105,30 @@ Global
{B3F2AFD5-E075-43CD-BE89-626330A60457}.Release|x64.Build.0 = Release|Any CPU
{B3F2AFD5-E075-43CD-BE89-626330A60457}.Release|x86.ActiveCfg = Release|Any CPU
{B3F2AFD5-E075-43CD-BE89-626330A60457}.Release|x86.Build.0 = Release|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Debug|x64.ActiveCfg = Debug|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Debug|x64.Build.0 = Debug|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Debug|x86.ActiveCfg = Debug|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Debug|x86.Build.0 = Debug|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Release|Any CPU.Build.0 = Release|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Release|x64.ActiveCfg = Release|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Release|x64.Build.0 = Release|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Release|x86.ActiveCfg = Release|Any CPU
{703A3ECC-048C-443A-B624-0D17DFC674AA}.Release|x86.Build.0 = Release|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Debug|x64.ActiveCfg = Debug|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Debug|x64.Build.0 = Debug|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Debug|x86.ActiveCfg = Debug|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Debug|x86.Build.0 = Debug|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Release|Any CPU.Build.0 = Release|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Release|x64.ActiveCfg = Release|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Release|x64.Build.0 = Release|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Release|x86.ActiveCfg = Release|Any CPU
{A43C866F-FA99-4B01-B480-6A7901569FAB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -114,5 +142,7 @@ Global
{A1406740-5D00-4272-9B22-FA919A10EC5C} = {36979A3D-4741-4D8A-82BF-4010474388E1}
{1325A3DF-F3C9-4EF4-94F7-B283FE9DA18D} = {36979A3D-4741-4D8A-82BF-4010474388E1}
{583BBF7E-054D-414D-AAA8-47C400AB3914} = {36979A3D-4741-4D8A-82BF-4010474388E1}
{703A3ECC-048C-443A-B624-0D17DFC674AA} = {36979A3D-4741-4D8A-82BF-4010474388E1}
{A43C866F-FA99-4B01-B480-6A7901569FAB} = {6A85CE0A-6FE4-4190-ABEE-7E8BCFF9AF4D}
EndGlobalSection
EndGlobal
9 changes: 5 additions & 4 deletions before.Sentry.Unity.sln.targets
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project InitialTargets="RestoreSubmodule">
<Project InitialTargets="RestoreSubmodules">
<!-- If sentry-dotnet is not found, restore git submodules -->
<Target Name="RestoreSubmodule"
Condition="!Exists('src/sentry-dotnet/src/Sentry/Sentry.csproj')">
<Message Importance="High" Text="sentry-dotnet not found. Restoring git submodules."></Message>
<Target Name="RestoreSubmodules"
Condition="!Exists('src/sentry-dotnet/src/Sentry/Sentry.csproj')
OR !Exists('src/sentry-cocoa/Sentry.xcodeproj')">
<Message Importance="High" Text="Restoring git submodules."></Message>
<Exec Command="git submodule update --init --recursive"></Exec>
</Target>
</Project>
81 changes: 81 additions & 0 deletions package-dev/Plugins/iOS/Sentry.framework.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 2 additions & 23 deletions package-dev/Runtime/SentryInitialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,10 @@ public static class SentryInitialization
public static void Init()
{
var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions();
if (options is null)
if (options.ShouldInitializeSdk())
{
new UnityLogger(new SentryOptions()).LogWarning(
"Sentry has not been configured. You can do that through the editor: Tools -> Sentry");
return;
SentryUnity.Init(options);
}

if (!options.Enabled)
{
return;
}

if (!options.CaptureInEditor && Application.isEditor)
{
options.DiagnosticLogger?.LogInfo("Disabled while in the Editor.");
return;
}

if (string.IsNullOrWhiteSpace(options.Dsn))
{
options.DiagnosticLogger?.LogWarning("No Sentry DSN configured. Sentry will be disabled.");
return;
}

SentryUnity.Init(options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ MonoBehaviour:
<IsEnvironmentUser>k__BackingField: 0
<EnableOfflineCaching>k__BackingField: 1
<MaxCacheItems>k__BackingField: 30
<InitCacheFlushTimeout>k__BackingField: 1000
<InitCacheFlushTimeout>k__BackingField: 2000
<ShutdownTimeout>k__BackingField: 2000
<MaxQueueItems>k__BackingField: 30
<IOSNativeSupportEnabled>k__BackingField: 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to consider the upgrade path. Where this field doesn't exist and will default to 0.
Perhaps have it nullable so we can tell if the user opt-ed out or if it's just an upgrading scriptable objet

<Debug>k__BackingField: 1
<DebugOnlyInEditor>k__BackingField: 1
<DiagnosticLevel>k__BackingField: 2
<DiagnosticLevel>k__BackingField: 0
43 changes: 43 additions & 0 deletions src/Sentry.Unity.Editor.iOS/BuildPostProcess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Sentry.Extensibility;
using UnityEditor;
using UnityEditor.Callbacks;

namespace Sentry.Unity.Editor.iOS
{
public static class BuildPostProcess
{
[PostProcessBuild(1)]
public static void OnPostProcessBuild(BuildTarget target, string pathToProject)
{
if (target != BuildTarget.iOS)
{
return;
}

var options = ScriptableSentryUnityOptions.LoadSentryUnityOptions();
if (!options.ShouldInitializeSdk())
{
return;
}

if (!options!.IOSNativeSupportEnabled)
{
options.DiagnosticLogger?.LogDebug("iOS Native support disabled. Won't modify the xcode project");
return;
}

try
{
using var sentryXcodeProject = SentryXcodeProject.Open(pathToProject, options);
sentryXcodeProject.AddSentryFramework();
sentryXcodeProject.AddNativeOptions();
sentryXcodeProject.AddSentryToMain();
}
catch (Exception e)
{
options.DiagnosticLogger?.LogError("Failed to add Sentry to the xcode project", e);
}
}
}
}
64 changes: 64 additions & 0 deletions src/Sentry.Unity.Editor.iOS/NativeMain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using Sentry.Extensibility;

namespace Sentry.Unity.Editor.iOS
{
internal interface INativeMain
{
public void AddSentry(string pathToMain, IDiagnosticLogger? logger);
}

internal class NativeMain : INativeMain
{
private const string Include = @"#include <Sentry/Sentry.h>
#include ""SentryOptions.m""
";
private const string Init = @"
[SentrySDK startWithOptions:getSentryOptions()];
";

public void AddSentry(string pathToMain, IDiagnosticLogger? logger)
{
if (!File.Exists(pathToMain))
{
throw new FileNotFoundException("Could not find main.", pathToMain);
}

var main = File.ReadAllText(pathToMain);
if (ContainsSentry(main, logger))
{
return;
}

var sentryMain = AddSentryToMain(main);
File.WriteAllText(pathToMain, sentryMain);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 3 exit conditions before we add Sentry. It could be useful when troubleshooting to log Debug (or error on AddSentryToMain returning null?) to help us understand customers issues when they report the logs

}

internal bool ContainsSentry(string main, IDiagnosticLogger? logger)
{
if (main.Contains(Include))
{
logger?.LogInfo("'main.mm' already contains Sentry.");
return true;
}

return false;
}

internal string AddSentryToMain(string main)
{
main = main.Insert(0, Include);

var initRegex = new Regex(@"int main\(int argc, char\* argv\[\]\)\s+{\s+@autoreleasepool\s+{");
var match = initRegex.Match(main);
if (match.Success)
{
return main.Insert(match.Index + match.Length, Init);
}

throw new ArgumentException($"Failed to add Sentry to main.\n{main}", nameof(main));
}
}
}
Loading