Skip to content

Commit

Permalink
Enable LTO builds (#2239)
Browse files Browse the repository at this point in the history
* Enable LTO + backport iOS build fix

* Replace -Oz with -O2 if LTO is enabled

* Disable interprocedural_optimization for android

* Enable LTO for UWP builds

* Add changelog entry

* Disable LTO for PR builds
  • Loading branch information
nirinchev authored and LaPeste committed Feb 11, 2021
1 parent adcdde2 commit 8fa868c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
var obj = new MyObject();
obj.Denominations.Add("quarter", 0.25d);
```
* Reduced the size of the native binaries by ~5%. (PR [#2239](https://github.com/realm/realm-dotnet/pull/2239))

### Compatibility
* Realm Studio: 10.0.0 or later.

### Internal
* Using Core 10.3.3.
* Test projects updated to dotnetcore 3.1. This means that tests are no longer executed against dotnetcore 2.0.
* Using Core 11.x.y. (it's in flux)
* Enabled LTO builds for all platforms except Android. (PR [#2239](https://github.com/realm/realm-dotnet/pull/2239))

## 10.1.0 (2021-02-09)

Expand Down
22 changes: 16 additions & 6 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def WindowsPlatforms = [ 'Win32', 'x64' ]
def WindowsUniversalPlatforms = [ 'Win32', 'x64', 'ARM' ]

String versionSuffix = ''
boolean enableLTO = true

stage('Checkout') {
rlmNode('docker') {
Expand All @@ -28,6 +29,7 @@ stage('Checkout') {
}
else if (env.CHANGE_BRANCH == null || !env.CHANGE_BRANCH.startsWith('release')) {
versionSuffix = "PR-${env.CHANGE_ID}.${env.BUILD_ID}"
enableLTO = false
}

stash includes: '**', excludes: 'wrappers/**', name: 'dotnet-source', useDefaultExcludes: false
Expand All @@ -36,12 +38,20 @@ stage('Checkout') {
}

stage('Build wrappers') {
def bashExtraArgs = ''
def psExtraArgs = ''

if (enableLTO) {
bashExtraArgs = '-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON'
psExtraArgs = '-EnableLTO'
}

def jobs = [
'iOS': {
rlmNode('osx || macos-catalina') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
sh "./build-ios.sh --configuration=${configuration}"
sh "./build-ios.sh --configuration=${configuration} ${bashExtraArgs}"
}
stash includes: 'wrappers/build/**', name: 'ios-wrappers'
}
Expand All @@ -50,7 +60,7 @@ stage('Build wrappers') {
rlmNode('osx || macos-catalina') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
sh "REALM_CMAKE_CONFIGURATION=${configuration} ./build-macos.sh -GXcode"
sh "REALM_CMAKE_CONFIGURATION=${configuration} ./build-macos.sh -GXcode ${bashExtraArgs}"
}
stash includes: 'wrappers/build/**', name: 'macos-wrappers'
}
Expand All @@ -59,7 +69,7 @@ stage('Build wrappers') {
rlmNode('docker') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
buildWrappersInDocker('wrappers', 'centos.Dockerfile', "REALM_CMAKE_CONFIGURATION=${configuration} ./build.sh")
buildWrappersInDocker('wrappers', 'centos.Dockerfile', "REALM_CMAKE_CONFIGURATION=${configuration} ./build.sh ${bashExtraArgs}")
}
stash includes: 'wrappers/build/**', name: 'linux-wrappers'
}
Expand All @@ -72,7 +82,7 @@ stage('Build wrappers') {
rlmNode('docker') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
buildWrappersInDocker('wrappers_android', 'android.Dockerfile', "./build-android.sh --configuration=${configuration} --ARCH=${localAbi}")
buildWrappersInDocker('wrappers_android', 'android.Dockerfile', "./build-android.sh --configuration=${configuration} --ARCH=${localAbi} ${bashExtraArgs}")
}
stash includes: 'wrappers/build/**', name: "android-wrappers-${localAbi}"
}
Expand All @@ -85,7 +95,7 @@ stage('Build wrappers') {
rlmNode('windows') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
powershell ".\\build.ps1 Windows -Configuration ${configuration} -Platforms ${localPlatform}"
powershell ".\\build.ps1 Windows -Configuration ${configuration} -Platforms ${localPlatform} ${psExtraArgs}"
}
stash includes: 'wrappers/build/**', name: "windows-wrappers-${localPlatform}"
if (shouldPublishPackage()) {
Expand All @@ -101,7 +111,7 @@ stage('Build wrappers') {
rlmNode('windows') {
unstash 'dotnet-wrappers-source'
dir('wrappers') {
powershell ".\\build.ps1 WindowsStore -Configuration ${configuration} -Platforms ${localPlatform}"
powershell ".\\build.ps1 WindowsStore -Configuration ${configuration} -Platforms ${localPlatform} ${psExtraArgs}"
}
stash includes: 'wrappers/build/**', name: "windowsuniversal-wrappers-${localPlatform}"
if (shouldPublishPackage()) {
Expand Down
2 changes: 0 additions & 2 deletions Realm/Realm/Handles/AppHandle.EmailPassword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal partial class AppHandle : RealmHandle
private static class EmailNativeMethods
{
#pragma warning disable IDE1006 // Naming Styles
#pragma warning disable SA1121 // Use built-in type alias

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "shared_app_email_register_user", CallingConvention = CallingConvention.Cdecl)]
public static extern void register_user(AppHandle app,
Expand Down Expand Up @@ -66,7 +65,6 @@ public static extern void call_reset_password_function(AppHandle app,
IntPtr tcs_ptr, out NativeException ex);

#pragma warning restore IDE1006 // Naming Styles
#pragma warning restore SA1121 // Use built-in type alias
}

public readonly EmailPasswordApi EmailPassword;
Expand Down
3 changes: 1 addition & 2 deletions Realm/Realm/Handles/AppHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -34,7 +35,6 @@ internal partial class AppHandle : RealmHandle
private static class NativeMethods
{
#pragma warning disable IDE1006 // Naming Styles
#pragma warning disable SA1121 // Use built-in type alias

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public unsafe delegate void LogMessageCallback(IntPtr managed_handler, byte* message_buf, IntPtr message_len, LogLevel logLevel);
Expand Down Expand Up @@ -139,7 +139,6 @@ public static extern void call_reset_password_function(AppHandle app,
}

#pragma warning restore IDE1006 // Naming Styles
#pragma warning restore SA1121 // Use built-in type alias
}

static unsafe AppHandle()
Expand Down
11 changes: 11 additions & 0 deletions wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

if(ANDROID)
# On Android LTO builds produce larger binaries, so turn them off
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)

if(CMAKE_INTERPROCEDURAL_OPTIMIZATION)
STRING(REGEX REPLACE "-O[0-9z]" "-O2" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
else()
STRING(REGEX REPLACE "-O[0-9z]" "-Oz" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
endif()
endif()

option(REALM_DOTNET_BUILD_CORE_FROM_SOURCE "Build Realm Core from source, as opposed to downloading prebuilt binaries" ON)
if(REALM_DOTNET_BUILD_CORE_FROM_SOURCE)
set(REALM_BUILD_LIB_ONLY ON)
Expand Down
12 changes: 9 additions & 3 deletions wrappers/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ param(

[string]$Toolchain = 'c:\\src\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake',

[Switch]$Incremental
[Switch]$Incremental,

[Switch]$EnableLTO
)

Push-Location $PSScriptRoot
Expand All @@ -50,9 +52,13 @@ if ($Target -eq 'WindowsStore') {
$cmakeArgs += "-DCMAKE_SYSTEM_VERSION='8.1'", "-DCMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION='8.1'"
}

if ($EnableLTO) {
$cmakeArgs += "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON"
}

function triplet([string]$target, [string]$platform) {
$arch = $platform.ToLower()
if ($arch -eq 'win32') {
if ($arch -eq 'win32') {
$arch = 'x86'
}

Expand All @@ -73,7 +79,7 @@ foreach ($platform in $Platforms) {
if (-Not $Incremental) {
& $cmake $PSScriptRoot $cmakeArgs -DCMAKE_GENERATOR_PLATFORM="$platform" -DVCPKG_TARGET_TRIPLET="$(triplet -target $Target -platform $platform)"
}

& $cmake --build . --target install --config $Configuration
Pop-Location
}
5 changes: 5 additions & 0 deletions wrappers/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ set_target_properties(realm-wrappers PROPERTIES
MACOSX_FRAMEWORK_SHORT_VERSION_STRING "10.0.0"
MACOSX_FRAMEWORK_BUNDLE_VERSION "10.0.0"
)

if (ANDROID)
target_link_options(realm-wrappers PRIVATE LINKER:-gc-sections)
endif()

target_link_libraries(realm-wrappers Realm::ObjectStore Realm::QueryParser)

if(CMAKE_GENERATOR_PLATFORM)
Expand Down

0 comments on commit 8fa868c

Please sign in to comment.