Skip to content

Commit

Permalink
Updated all tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
AristurtleDev committed Apr 28, 2024
1 parent 79ffc90 commit d6067bb
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 56 deletions.
42 changes: 26 additions & 16 deletions build/BuildLinuxTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,40 @@ namespace BuildScripts;
[TaskName("Build Linux")]
[IsDependentOn(typeof(PrepTask))]
[IsDependeeOf(typeof(BuildToolTask))]
public sealed class BuildLinuxTask : FrostingTask<BuildContext>
public sealed class BuildLinuxTask : BuildTaskBase
{
public override bool ShouldRun(BuildContext context) => context.IsRunningOnLinux();

public override void Run(BuildContext context)
{
// Patch vcpkg files for linux build
context.StartProcess("patch", "./buildscripts/vcpkg/ports/ffmpeg/portfile.cmake ./patches/ffmpeg-portfile.patch");
context.StartProcess("patch", "./buildscripts/vcpkg/triplets/x64-linux.cmake ./patches/x64-linux-cmake.patch");
// 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));

// Bootstrap vcpkg
context.StartProcess("buildscripts/vcpkg/bootstrap-vcpkg.sh");
// Generate common build directory path
var buildDirectory = $"{absoluteArtifactDir}/linux-x86_64";

// Perform x64-linux build
context.StartProcess("buildscripts/vcpkg/vcpkg", "install ffmpeg[mp3lame,vorbis]:x64-linux");
// Create the build settings used by each library build
var buildSettings = new BuildSettings
{
ShellCommand = "sh",
PrefixFlag = buildDirectory,
PkgConfigPath = $"{buildDirectory}/lib/pkgconfig",
HostFlag = "x86_64-linux-gnu",
CFlags = $"-w -I{buildDirectory}/include",
CPPFlags = $"-I{buildDirectory}/include",
LDFlags = $"-L{buildDirectory}/lib --static"
};

// Copy build to artifacts
context.CopyFile("buildscripts/vcpkg/installed/x64-linux/tools/ffmpeg/ffmpeg", $"{context.ArtifactsDir}/ffmpeg");
}
// Get the configuration flags that will be used for the FFMpeg build
var ffmpegConfigureFlags = GetFFMpegConfigureFlags(context, "linux-x86_64");

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-linux.cmake ./patches/x64-linux-cmake.patch");
// Build each library in correct order
BuildOgg(context, buildSettings);
BuildVorbis(context, buildSettings);
BuildLame(context, buildSettings);
BuildFFMpeg(context, buildSettings, ffmpegConfigureFlags);

// Move the built binary from the build directory to the artifact directory
context.MoveFile($"{buildDirectory}/bin/ffmpeg", $"{absoluteArtifactDir}/ffmpeg");
}
}
98 changes: 74 additions & 24 deletions build/BuildMacOSTask.cs
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");
}
}
}
127 changes: 127 additions & 0 deletions build/BuildTaskBase.cs
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);
}
}
52 changes: 36 additions & 16 deletions build/BuildWIndowsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,50 @@ namespace BuildScripts;
[TaskName("Build Windows")]
[IsDependentOn(typeof(PrepTask))]
[IsDependeeOf(typeof(BuildToolTask))]
public sealed class BuildWindowsTask : FrostingTask<BuildContext>
public sealed class BuildWindowsTask : BuildTaskBase
{
public override bool ShouldRun(BuildContext context) => context.IsRunningOnWindows();

public override void Run(BuildContext context)
{
// Patch vcpkg files for windows build
context.StartProcess("patch", "./buildscripts/vcpkg/ports/ffmpeg/portfile.cmake ./patches/ffmpeg-portfile.patch");
context.StartProcess("patch", "./buildscripts/vcpkg/triplets/x64-windows-static.cmake ./patches/x64-windows-static-cmake.patch");
// 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)).ToString();

// Bootstrap vcpkg
context.StartProcess("buildscripts/vcpkg/bootstrap-vcpkg.bat");
// Since we are using mingw to build, paths need to be in unix format
// e.g. C:\Users\MonoGame\Desktop\ => /c/Users/MonoGame/Desktop
absoluteArtifactDir = absoluteArtifactDir.Replace("\\", "/");
absoluteArtifactDir = $"/{absoluteArtifactDir[0]}{absoluteArtifactDir[2..]}";

// Perform x64-windows build
context.StartProcess("buildscripts/vcpkg/vcpkg.exe", "install ffmpeg[mp3lame,vorbis]:x64-windows-static");
// Generate common build directory path
var buildDirectory = $"{absoluteArtifactDir}/windows-x86_64";
context.CreateDirectory(buildDirectory);

// Copy build to artifacts
context.CopyFile("buildscripts/vcpkg/installed/x64-windows-static/tools/ffmpeg/ffmpeg.exe", $"{context.ArtifactsDir}/ffmpeg.exe");
}
// Create the build settings used by each library build
var buildSettings = new BuildSettings
{
ShellCommand = @"C:\msys64\usr\bin\bash",
PrefixFlag = buildDirectory,
HostFlag = "x86_64-w64-mingw32",
PkgConfigPath = $"{buildDirectory}/lib/pkgconfig",
Path = "/usr/bin:/mingw64/bin",
CCFlags = "x86_64-w64-mingw32-gcc",
CFlags = $"-w -I{buildDirectory}/include",
CPPFlags = $"-I{buildDirectory}/include",
LDFlags = $"-L{buildDirectory}/lib --static"
};

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-windows-static.cmake ./patches/x64-windows-static-cmake.patch");
// Get the configuration flags that will be used for the FFMpeg build
var ffmpegConfigureFlags = GetFFMpegConfigureFlags(context, "windows-x86_64");

// Build each library in correct order
BuildOgg(context, buildSettings);
BuildVorbis(context, buildSettings);
BuildLame(context, buildSettings);
BuildFFMpeg(context, buildSettings, ffmpegConfigureFlags);

// Move the built binary from the build directory to the artifact directory
// Note: For some reason, unlike the linux and mac builds, the windows build will not copy the binary using the
// unix like path created. It fails to find it. Instead, a relative path is used here for windows.
context.CopyFile($"{context.ArtifactsDir}/windows-x86_64/bin/ffmpeg.exe", $"{context.ArtifactsDir}/ffmpeg.exe");
}
}

0 comments on commit d6067bb

Please sign in to comment.