Skip to content

Commit

Permalink
Build universal binary on macOS (#7)
Browse files Browse the repository at this point in the history
* Try build universal binary on macOS

* Update to latest build script and remove prebuilt arm64 binary

* Update buildscript

* Properly use the correct makefile

* Patching the source to work with xcode compiler

* Fix copying output

* Wrong line replace...

* More simple makefile edit
  • Loading branch information
ThomasFOG authored Jan 22, 2024
1 parent 2c00bd5 commit 05f0a00
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
submodules: recursive
- name: Run CAKE
run: dotnet run --project ./build/Build.csproj
run: dotnet run --project ./build/Build.csproj --universalBinary=true
env:
ACTIONS_RUNTIME_TOKEN: ${{ env.ACTIONS_RUNTIME_TOKEN }}
ACTIONS_RUNTIME_URL: "${{ env.ACTIONS_RUNTIME_URL }}"
Expand All @@ -46,7 +46,7 @@ jobs:
with:
submodules: recursive
- name: Run CAKE
run: dotnet run --project ./build/Build.csproj -- --target=Package --libraryname=FreeImage --licensepath=freeimage/license-fi.txt
run: dotnet run --project ./build/Build.csproj -- --target=Package --universalBinary=true --libraryname=FreeImage --licensepath=freeimage/license-fi.txt
env:
ACTIONS_RUNTIME_TOKEN: ${{ env.ACTIONS_RUNTIME_TOKEN }}
ACTIONS_RUNTIME_URL: "${{ env.ACTIONS_RUNTIME_URL }}"
Expand Down
22 changes: 10 additions & 12 deletions Build.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Build", "build\Build.csproj", "{59B3FA06-A6EA-40E1-81F5-5E8D0FD49BD3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Build", "build\Build.csproj", "{59B3FA06-A6EA-40E1-81F5-5E8D0FD49BD3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoGame.Library.BuildScripts", "buildscripts\MonoGame.Library.BuildScripts.csproj", "{EFB3110F-3A99-494B-A982-90D574529FA9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1EA7981F-FAAA-4181-8AC6-DB159054775B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1EA7981F-FAAA-4181-8AC6-DB159054775B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EA7981F-FAAA-4181-8AC6-DB159054775B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EA7981F-FAAA-4181-8AC6-DB159054775B}.Release|Any CPU.Build.0 = Release|Any CPU
{59B3FA06-A6EA-40E1-81F5-5E8D0FD49BD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59B3FA06-A6EA-40E1-81F5-5E8D0FD49BD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59B3FA06-A6EA-40E1-81F5-5E8D0FD49BD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59B3FA06-A6EA-40E1-81F5-5E8D0FD49BD3}.Release|Any CPU.Build.0 = Release|Any CPU
{D72EAD34-EE70-47CD-BF8A-F2FA8AA11FE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D72EAD34-EE70-47CD-BF8A-F2FA8AA11FE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D72EAD34-EE70-47CD-BF8A-F2FA8AA11FE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D72EAD34-EE70-47CD-BF8A-F2FA8AA11FE5}.Release|Any CPU.Build.0 = Release|Any CPU
{EFB3110F-3A99-494B-A982-90D574529FA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFB3110F-3A99-494B-A982-90D574529FA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFB3110F-3A99-494B-A982-90D574529FA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFB3110F-3A99-494B-A982-90D574529FA9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
73 changes: 56 additions & 17 deletions build/BuildMacOSTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,65 @@ public sealed class BuildMacOSTask : FrostingTask<BuildContext>

public override void Run(BuildContext context)
{
var cflags = "-Wno-implicit-function-declaration";
switch (RuntimeInformation.ProcessArchitecture)
// we need to patch the source to be compatible with the xcode 64bit compiler

// ZLib
context.ReplaceTextInFiles(
"freeimage/Source/ZLib/gzlib.c",
"/* Local functions */",
"#ifdef __APPLE__\r\n #define _LARGEFILE64_SOURCE /* See feature_test_macros(7) */\r\n #include <sys/types.h>\r\n #include <unistd.h>\r\n#endif");
context.ReplaceTextInFiles(
"freeimage/Source/ZLib/gzguts.h",
"#ifdef _LARGEFILE64_SOURCE",
"#ifdef __APPLE__\r\n #include <unistd.h>\r\n#endif\r\n\r\n#ifdef _LARGEFILE64_SOURCE");
// LibJXR
context.ReplaceTextInFiles(
"freeimage/Source/LibJXR/image/decode/segdec.c",
"#include \"strcodec.h\"",
"#ifdef __APPLE__\r\n #include <libkern/OSByteOrder.h>\r\n #define _byteswap_ulong(x) _OSSwapInt32\r\n#endif\r\n\r\n#include \"strcodec.h\"");
context.ReplaceTextInFiles(
"freeimage/Source/LibJXR/jxrgluelib/JXRGlueJxr.c",
"#include <limits.h>",
"#ifdef __APPLE__\r\n #include <wchar.h>\r\n#endif\r\n\r\n#include <limits.h>");

// we need to modify the makefile to produce a dynamic library

var makeFilePattern = "freeimage/Makefile.osx";
// disable neon instructions in case we're building for arm64
context.ReplaceTextInFiles(
makeFilePattern,
"COMPILERFLAGS = -Os -fexceptions -fvisibility=hidden -DNO_LCMS -D__ANSI__",
"COMPILERFLAGS = -Os -fexceptions -fvisibility=hidden -DNO_LCMS -D__ANSI__ -DDISABLE_PERF_MEASUREMENT -DPNG_ARM_NEON_OPT=0");
// rename output to libFreeImage.dylib
context.ReplaceTextInFiles(
makeFilePattern,
"SHAREDLIB = lib$(TARGET)-$(VER_MAJOR).$(VER_MINOR).dylib",
"SHAREDLIB = lib$(TARGET).dylib");
// build only dynamic library
context.ReplaceTextInFiles(
makeFilePattern,
"FreeImage: $(STATICLIB)",
"FreeImage: $(SHAREDLIB)");
context.ReplaceTextInFiles(
makeFilePattern,
"cp *.a Dist",
"cp *.dylib Dist");

// generate x86_64 and arm64 at once
if (context.IsUniversalBinary)
{
case Architecture.Arm:
case Architecture.Arm64:
cflags += " -O3 -fPIC -fexceptions -fvisibility=hidden -DPNG_ARM_NEON_OPT=0";
break;
context.ReplaceTextInFiles(
makeFilePattern,
"I386",
"ARM64");
context.ReplaceTextInFiles(
makeFilePattern,
"i386",
"arm64");
}

var makeFilePattern = "freeimage/Makefile.*";
context.ReplaceRegexInFiles(makeFilePattern, @"SHAREDLIB.+\=.+", "SHAREDLIB = lib$(TARGET).dylib");
context.ReplaceTextInFiles(makeFilePattern, "cp *.so Dist/", "cp *.dylib Dist/");
context.ReplaceTextInFiles(makeFilePattern, "$(CC) -s -shared -Wl,-soname,$(VERLIBNAME) $(LDFLAGS) -o $@ $(MODULES) $(LIBRARIES)", "$(CXX) -dynamiclib -install_name $(LIBNAME) -current_version $(VER_MAJOR).$(VER_MINOR) -compatibility_version $(VER_MAJOR) $(LDFLAGS) -o $@ $(MODULES)");

var buildWorkingDir = "freeimage/";
var env = new Dictionary<string, string>
{
{ "CFLAGS", cflags }
};
context.StartProcess("make", new ProcessSettings { WorkingDirectory = buildWorkingDir, Arguments = "-f Makefile.gnu", EnvironmentVariables = env });
context.CopyFile(@"freeimage/Dist/libfreeimage.dylib", $"{context.ArtifactsDir}/libfreeimage.dylib");
context.StartProcess("make", new ProcessSettings { WorkingDirectory = buildWorkingDir, Arguments = "-f Makefile.osx"});
context.CopyFile(@"freeimage/Dist/libFreeImage.dylib", $"{context.ArtifactsDir}/libfreeimage.dylib");
}
}
Binary file removed runtimes/osx-arm64/native/libfreeimage.dylib
Binary file not shown.

0 comments on commit 05f0a00

Please sign in to comment.