-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79ffc90
commit d6067bb
Showing
4 changed files
with
263 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,92 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace BuildScripts; | ||
|
||
[TaskName("Build macOS")] | ||
[IsDependentOn(typeof(PrepTask))] | ||
[IsDependeeOf(typeof(BuildToolTask))] | ||
public sealed class BuildMacOSTask : FrostingTask<BuildContext> | ||
public sealed class BuildMacOSTask : BuildTaskBase | ||
{ | ||
public override bool ShouldRun(BuildContext context) => context.IsRunningOnMacOs(); | ||
|
||
public override void Run(BuildContext context) | ||
{ | ||
// Patch vcpkg files for mac build | ||
context.StartProcess("patch", "./buildscripts/vcpkg/ports/ffmpeg/portfile.cmake ./patches/ffmpeg-portfile.patch"); | ||
context.StartProcess("patch", "./buildscripts/vcpkg/triplets/x64-osx.cmake ./patches/x64-osx-cmake.patch"); | ||
context.StartProcess("patch", "./buildscripts/vcpkg/triplets/arm64-osx.cmake ./patches/arm64-osx-cmake.patch"); | ||
// Determine which mac architecture(s) to build for. | ||
var buildx8664 = context.IsUniversalBinary || RuntimeInformation.ProcessArchitecture is not Architecture.Arm or Architecture.Arm64; | ||
var buildArm64 = context.IsUniversalBinary || RuntimeInformation.ProcessArchitecture is Architecture.Arm or Architecture.Arm64; | ||
|
||
// Absolute path to the artifact directory is needed for flags since they don't allow relative path | ||
var absoluteArtifactDir = context.MakeAbsolute(new DirectoryPath(context.ArtifactsDir)); | ||
|
||
// Generate common build directory paths for each architectures build artifacts | ||
var x866BuildDirectory = $"{absoluteArtifactDir}/osx-x86_64"; | ||
var arm64BuildDirectory = $"{absoluteArtifactDir}/osx-arm64"; | ||
|
||
// Bootstrap vcpkg | ||
context.StartProcess("buildscripts/vcpkg/bootstrap-vcpkg.sh"); | ||
if (buildx8664) | ||
{ | ||
// Create the build settings used by each library build | ||
var buildSettings = new BuildSettings() | ||
{ | ||
ShellCommand = "zsh", | ||
PrefixFlag = x866BuildDirectory, | ||
PkgConfigPath = $"{x866BuildDirectory}/lib/pkgconfig", | ||
HostFlag = "x86_64-apple-darwin", | ||
CFlags = $"-w -arch x86_64 -I{x866BuildDirectory}/include", | ||
CPPFlags = $"-arch x86_64 -I{x866BuildDirectory}/include", | ||
CXXFlags = "-arch x86_84", | ||
LDFlags = $"-arch x86_64 -L{x866BuildDirectory}/lib" | ||
}; | ||
|
||
// Perform x64-osx build | ||
context.StartProcess("buildscripts/vcpkg/vcpkg", "install ffmpeg[mp3lame,vorbis]:x64-osx"); | ||
// Get the configuration flags that will be used for the FFMpeg build | ||
var x8664FFMpegConfigureFlags = GetFFMpegConfigureFlags(context, "osx-x86_64"); | ||
|
||
// Perform arm64-osx build | ||
context.StartProcess("buildscripts/vcpkg/vcpkg", "install ffmpeg[mp3lame,vorbis]:arm64-osx"); | ||
// Build each library in correct order | ||
BuildOgg(context, buildSettings); | ||
BuildVorbis(context, buildSettings); | ||
BuildLame(context, buildSettings); | ||
BuildFFMpeg(context, buildSettings, x8664FFMpegConfigureFlags); | ||
} | ||
|
||
// Use lipo to combine into universal binary and output in the artifacts directory | ||
string x64 = "buildscripts/vcpkg/installed/x64-osx/tools/ffmpeg/ffmpeg"; | ||
string arm64 = "buildscripts/vcpkg/installed/arm64-osx/tools/ffmpeg/ffmpeg"; | ||
context.StartProcess("lipo", new ProcessSettings() | ||
if (buildArm64) | ||
{ | ||
Arguments = $"-create {x64} {arm64} -output {context.ArtifactsDir}/ffmpeg" | ||
}); | ||
} | ||
// Create the build settings used by each library build | ||
var buildSettings = new BuildSettings() | ||
{ | ||
ShellCommand = "zsh", | ||
PrefixFlag = arm64BuildDirectory, | ||
PkgConfigPath = $"{arm64BuildDirectory}/lib/pkgconfig", | ||
HostFlag = "aarch64-apple-darwin", | ||
CFlags = $"-w -arch arm64 -I{arm64BuildDirectory}/include", | ||
CPPFlags = $"-arch arm64 -I{arm64BuildDirectory}/include", | ||
CXXFlags = "-arch arm64", | ||
LDFlags = $"-arch arm64 -L{arm64BuildDirectory}/lib" | ||
}; | ||
|
||
public override void Finally(BuildContext context) | ||
{ | ||
// Ensure we revert the patched files so when running/testing locally they are put back in original state | ||
context.StartProcess("patch", "-R ./buildscripts/vcpkg/ports/ffmpeg/portfile.cmake ./patches/ffmpeg-portfile.patch"); | ||
context.StartProcess("patch", "-R ./buildscripts/vcpkg/triplets/x64-osx.cmake ./patches/x64-osx-cmake.patch"); | ||
context.StartProcess("patch", "-R ./buildscripts/vcpkg/triplets/arm64-osx.cmake ./patches/arm64-osx-cmake.patch"); | ||
// Get the configuration flags that will be used for the FFMpeg build | ||
var arm64FFMpegConfigureFlags = GetFFMpegConfigureFlags(context, "osx-arm64"); | ||
|
||
BuildOgg(context, buildSettings); | ||
BuildVorbis(context, buildSettings); | ||
BuildLame(context, buildSettings); | ||
BuildFFMpeg(context, buildSettings, arm64FFMpegConfigureFlags); | ||
} | ||
|
||
// Move the build binary from the build directory to the artifact directory. | ||
// If this is a universal build, we'll need to combine both binaries using lipo and output the result of that. | ||
if (buildx8664 && buildArm64) | ||
{ | ||
context.StartProcess("lipo", new ProcessSettings() | ||
{ | ||
Arguments = $"-create {x866BuildDirectory}/bin/ffmpeg {arm64BuildDirectory}/bin/ffmpeg -output {absoluteArtifactDir}/ffmpeg" | ||
}); | ||
} | ||
else if (buildx8664) | ||
{ | ||
context.CopyFile($"{x866BuildDirectory}/bin/ffmpeg", $"{absoluteArtifactDir}/ffmpeg"); | ||
} | ||
else | ||
{ | ||
context.CopyFile($"{arm64BuildDirectory}/bin/ffmpeg", $"{absoluteArtifactDir}/ffmpeg"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace BuildScripts; | ||
|
||
public abstract class BuildTaskBase : FrostingTask<BuildContext> | ||
{ | ||
protected static void BuildOgg(BuildContext context, BuildSettings buildSettings) | ||
{ | ||
var processSettings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = "./ogg", | ||
EnvironmentVariables = buildSettings.GetEnvironmentVariables() | ||
}; | ||
|
||
var commandExecutor = new CommandExecutionHelper(context, processSettings, buildSettings); | ||
|
||
// Ensure clean start if we're running locally and testing over and over | ||
commandExecutor.ExecuteCommand("make distclean"); | ||
|
||
// Run autogen.sh to create configuration files | ||
commandExecutor.ExecuteCommand("./autogen.sh"); | ||
|
||
// Run configure to build make file | ||
commandExecutor.ExecuteCommand($"./configure --prefix=\"{buildSettings.PrefixFlag}\" --host=\"{buildSettings.HostFlag}\" --disable-shared"); | ||
|
||
// Run make | ||
commandExecutor.ExecuteCommand($"make -j{Environment.ProcessorCount}"); | ||
|
||
// Run make install | ||
commandExecutor.ExecuteCommand("make install"); | ||
} | ||
|
||
protected static void BuildVorbis(BuildContext context, BuildSettings buildSettings) | ||
{ | ||
var processSettings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = "./vorbis", | ||
EnvironmentVariables = buildSettings.GetEnvironmentVariables() | ||
}; | ||
|
||
var commandExecutor = new CommandExecutionHelper(context, processSettings, buildSettings); | ||
|
||
// Ensure clean start if we're running locally and testing over and over | ||
commandExecutor.ExecuteCommand("make distclean"); | ||
|
||
// Run autogen.sh to create configuration files | ||
commandExecutor.ExecuteCommand("./autogen.sh"); | ||
|
||
// Run configure to build make file | ||
commandExecutor.ExecuteCommand($"./configure --prefix=\"{buildSettings.PrefixFlag}\" --host=\"{buildSettings.HostFlag}\" --disable-examples --disable-docs --disable-shared"); | ||
|
||
// Run make | ||
commandExecutor.ExecuteCommand($"make -j{Environment.ProcessorCount}"); | ||
|
||
// Run make install | ||
commandExecutor.ExecuteCommand("make install"); | ||
} | ||
|
||
protected static void BuildLame(BuildContext context, BuildSettings buildSettings) | ||
{ | ||
var processSettings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = "./lame", | ||
EnvironmentVariables = buildSettings.GetEnvironmentVariables() | ||
}; | ||
|
||
var commandExecutor = new CommandExecutionHelper(context, processSettings, buildSettings); | ||
|
||
// Ensure clean start if we're running locally and testing over and over | ||
commandExecutor.ExecuteCommand("make distclean"); | ||
|
||
// Run configure to build make file | ||
commandExecutor.ExecuteCommand($"./configure --prefix='{buildSettings.PrefixFlag}' --host=\"{buildSettings.HostFlag}\" --disable-frontend --disable-decoder --disable-shared"); | ||
|
||
// Run make | ||
commandExecutor.ExecuteCommand($"make -j{Environment.ProcessorCount}"); | ||
|
||
// Run make install | ||
commandExecutor.ExecuteCommand("make install"); | ||
} | ||
|
||
protected static void BuildFFMpeg(BuildContext context, BuildSettings buildSettings, string configureFlags) | ||
{ | ||
var processSettings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = "./ffmpeg", | ||
EnvironmentVariables = buildSettings.GetEnvironmentVariables() | ||
}; | ||
|
||
var commandExecutor = new CommandExecutionHelper(context, processSettings, buildSettings); | ||
|
||
// Ensure clean start if we're running locally and testing over and over | ||
commandExecutor.ExecuteCommand("make distclean"); | ||
|
||
// Run configure to build make file | ||
commandExecutor.ExecuteCommand($"./configure --prefix=\"{buildSettings.PrefixFlag}\" {configureFlags}"); | ||
|
||
// Run make | ||
commandExecutor.ExecuteCommand($"make -j{Environment.ProcessorCount}"); | ||
|
||
// Run make install | ||
commandExecutor.ExecuteCommand("make install"); | ||
} | ||
|
||
protected static string GetFullPathToArtifactDirectory(BuildContext context) | ||
{ | ||
string fullPath = System.IO.Path.GetFullPath(context.ArtifactsDir); | ||
|
||
if (context.IsRunningOnWindows()) | ||
{ | ||
// Windows uses mingw for compilation and expects paths to be in unix format | ||
// e.g. C:\Users\MonoGame\Desktop\ => /c/Users/MonoGame/Desktop | ||
fullPath = fullPath.Replace("\\", "/"); | ||
fullPath = $"/{fullPath[0]}{fullPath[2..]}"; | ||
} | ||
|
||
return fullPath; | ||
} | ||
|
||
protected static string GetFFMpegConfigureFlags(BuildContext context, string rid) | ||
{ | ||
var ignoreCommentsAndNewLines = (string line) => !line.StartsWith('#') && !line.StartsWith(' '); | ||
var configureFlags = context.FileReadLines("ffmpeg.config").Where(ignoreCommentsAndNewLines); | ||
var osConfigureFlags = context.FileReadLines($"ffmpeg.{rid}.config").Where(ignoreCommentsAndNewLines); | ||
return string.Join(' ', configureFlags) + " " + string.Join(' ', osConfigureFlags); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters