Skip to content

Commit

Permalink
Merge pull request #184 from CesiumGS/android-x64
Browse files Browse the repository at this point in the history
Build native binaries for Android on x86_64 (notably Magic Leap 2)
  • Loading branch information
kring authored Jan 29, 2023
2 parents cb21853 + 80d2b87 commit c9ce261
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Editor/BuildCesiumForUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public static void CompileForAndroidAndExit()
Directory.CreateDirectory(buildPath);
try
{
PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP);
PlayerSettings.Android.targetArchitectures = AndroidArchitecture.ARM64 | AndroidArchitecture.X86_64;
BuildPlayer(BuildTargetGroup.Android, BuildTarget.Android, Path.Combine(buildPath, "Android"));
}
finally
Expand Down
107 changes: 84 additions & 23 deletions Editor/CompileCesiumForUnityNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ public void OnPreprocessBuild(BuildReport report)
AssetDatabase.StartAssetEditing();
try
{
CreatePlaceholders(
GetLibraryToBuild(report.summary),
"CesiumForUnityNative-Runtime"
);
LibraryToBuild[] libraries = GetLibrariesToBuildForPlatform(report.summary, finalLibrariesOnly: true);
foreach (LibraryToBuild library in libraries)
{
CreatePlaceholders(library, "CesiumForUnityNative-Runtime");
}
}
finally
{
Expand Down Expand Up @@ -145,7 +146,7 @@ private static void ConfigurePlugin(LibraryToBuild library, PluginImporter impor

if (library.Platform == BuildTarget.Android)
{
importer.SetPlatformData(BuildTarget.Android, "CPU", "ARM64");
importer.SetPlatformData(BuildTarget.Android, "CPU", library.Cpu == "arm64" ? "ARM64" : library.Cpu);
}
else if (library.Platform == BuildTarget.StandaloneOSX)
{
Expand Down Expand Up @@ -188,35 +189,82 @@ private static void OnPostprocessAllAssets(
/// <param name="report"></param>
public void OnPostBuildPlayerScriptDLLs(BuildReport report)
{
LibraryToBuild[] libraries = GetLibrariesToBuildForPlatform(report.summary, finalLibrariesOnly: false);
foreach (LibraryToBuild library in libraries)
{
BuildNativeLibrary(library);
}

if (report.summary.platform == BuildTarget.StandaloneOSX)
{
// On macOS, build for both ARM64 and x64, and then use the lipo tool to combine
// the libraries for the two CPUs into a single library.
LibraryToBuild x64 = GetLibraryToBuild(report.summary, "x86_64");
BuildNativeLibrary(x64);
LibraryToBuild arm64 = GetLibraryToBuild(report.summary, "arm64");
BuildNativeLibrary(arm64);

string[] args = new[]
List<string> args = new List<string>();
args.Add("-create");
foreach (LibraryToBuild library in libraries)
{
"-create",
Path.Combine(x64.InstallDirectory, "libCesiumForUnityNative-Runtime.dylib"),
Path.Combine(arm64.InstallDirectory, "libCesiumForUnityNative-Runtime.dylib"),
"-output",
Path.GetFullPath(Path.Combine(x64.InstallDirectory, "..", "libCesiumForUnityNative-Runtime.dylib"))
};
args.Add(Path.Combine(library.InstallDirectory, "libCesiumForUnityNative-Runtime.dylib"));
}
args.Add("-output");
args.Add(Path.GetFullPath(Path.Combine(libraries[0].InstallDirectory, "..", "libCesiumForUnityNative-Runtime.dylib")));
Process p = Process.Start("lipo", string.Join(' ', args));
p.WaitForExit();
if (p.ExitCode != 0)
throw new Exception("lipo failed");
Directory.Delete(x64.InstallDirectory, true);
Directory.Delete(arm64.InstallDirectory, true);

foreach (LibraryToBuild library in libraries)
{
Directory.Delete(library.InstallDirectory, true);
}
}
}

/// <summary>
///
/// </summary>
/// <param name="summary"></param>
/// <param name="finalLibrariesOnly">
/// True if only the final libraries shipped to end users should be included, not any intermediate libraries created along the way.
/// This affects the macOS libraries, where two libraries are built (for x64 and ARM64), but then they are compiled into a
/// single library supporting both platforms.
/// </param>
/// <returns></returns>
private static LibraryToBuild[] GetLibrariesToBuildForPlatform(BuildSummary summary, bool finalLibrariesOnly)
{
List<LibraryToBuild> result = new List<LibraryToBuild>();

if (summary.platform == BuildTarget.StandaloneOSX)
{
if (finalLibrariesOnly)
{
result.Add(GetLibraryToBuild(summary, "x86_64"));
}
else
{
result.Add(GetLibraryToBuild(summary, "x86_64"));
result.Add(GetLibraryToBuild(summary, "arm64"));
}
}
else if (summary.platform == BuildTarget.Android)
{
// We support ARM64 and x86_64. If any other architectures are enabled, log a warning.
AndroidArchitecture supported = AndroidArchitecture.ARM64 | AndroidArchitecture.X86_64;
if ((PlayerSettings.Android.targetArchitectures & ~supported) != 0)
UnityEngine.Debug.LogWarning("Cesium for Unity only supports the ARM64 and x86_64 CPU architectures on Android. Other architectures will not work.");

if (PlayerSettings.Android.targetArchitectures.HasFlag(AndroidArchitecture.ARM64))
result.Add(GetLibraryToBuild(summary, "arm64"));
if (PlayerSettings.Android.targetArchitectures.HasFlag(AndroidArchitecture.X86_64))
result.Add(GetLibraryToBuild(summary, "x86_64"));
}
else
{
BuildNativeLibrary(GetLibraryToBuild(report.summary));
result.Add(GetLibraryToBuild(summary));
}

return result.ToArray();
}

public static LibraryToBuild GetLibraryToBuild(BuildSummary summary, string cpu = null)
{
return GetLibraryToBuild(new PlatformToBuild()
Expand Down Expand Up @@ -253,17 +301,30 @@ public static LibraryToBuild GetLibraryToBuild(PlatformToBuild platform, string
library.ExtraConfigureArgs.Add("-DEDITOR=on");

if (platform.platformGroup == BuildTargetGroup.Android)
library.Toolchain = "extern/android-toolchain.cmake";
{
library.Toolchain = $"extern/android-toolchain.cmake";
if (cpu == null)
cpu = "arm64";
if (cpu == "x86_64")
library.ExtraConfigureArgs.Add("-DCMAKE_ANDROID_ARCH_ABI=x86_64");
else
library.ExtraConfigureArgs.Add("-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a");
}

if (platform.platformGroup == BuildTargetGroup.iOS)
{
library.Toolchain = "extern/ios-toolchain.cmake";
library.ExtraConfigureArgs.Add("-GXcode");
}

if (platform.platform == BuildTarget.StandaloneOSX && cpu != null)
if (platform.platform == BuildTarget.StandaloneOSX)
{
if (cpu != null)
library.ExtraConfigureArgs.Add("-DCMAKE_OSX_ARCHITECTURES=" + cpu);
}

if (cpu != null)
{
library.ExtraConfigureArgs.Add("-DCMAKE_OSX_ARCHITECTURES=" + cpu);
library.InstallDirectory = Path.Combine(library.InstallDirectory, cpu);
library.BuildDirectory += "-" + cpu;
}
Expand Down
17 changes: 8 additions & 9 deletions native~/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ set( SUPPORT_CONSOLE_APP OFF)
add_subdirectory(extern/tidy-html5 EXCLUDE_FROM_ALL)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Android")
set(CESIUM_ARCHITECTURE "aarch64")
set(HTTPLIB_USE_OPENSSL_IF_AVAILABLE OFF)
set(ANDROID_ABI ${CMAKE_ANDROID_ARCH_ABI})
set(ANDROID_NDK ${CMAKE_ANDROID_NDK})
Expand All @@ -45,10 +44,10 @@ endif()

# Specify all targets that need to compile bitcode
if (${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
set (ALL_TARGETS
set (ALL_TARGETS
CesiumForUnityNative-Runtime
Async++
Cesium3DTilesSelection
Cesium3DTilesSelection
CesiumAsync
CesiumGeospatial
CesiumGeometry
Expand All @@ -58,15 +57,15 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
CesiumUtility
draco_attributes
draco_compression_attributes_dec
draco_compression_bit_coders
draco_compression_entropy
draco_compression_bit_coders
draco_compression_entropy
draco_compression_decode
draco_compression_mesh_dec
draco_compression_point_cloud_dec
draco_core
draco_mesh
draco_metadata
draco_metadata_dec
draco_metadata
draco_metadata_dec
draco_point_cloud
draco_points_dec
draco_static
Expand All @@ -81,9 +80,9 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
tinyxml2
tidy-static
uriparser
webpdecode
webpdecode
webpdecoder
webpdspdecode
webpdspdecode
webputils
webputilsdecode
)
Expand Down
1 change: 0 additions & 1 deletion native~/extern/android-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
SET(CMAKE_SYSTEM_NAME Android)
SET(CMAKE_SYSTEM_VERSION 21)

SET(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
SET(CMAKE_ANDROID_NDK "$ENV{ANDROID_NDK_ROOT}")
SET(CMAKE_ANDROID_STL_TYPE c++_static)

Expand Down

0 comments on commit c9ce261

Please sign in to comment.