Skip to content

Commit

Permalink
Include optional Rids if present (#4)
Browse files Browse the repository at this point in the history
We need to be able to optionally build native libraries for android and ios (and other platforms).
So lets add support for that. The issue is we might have builds which will produce multiple rids on the same job.

Now if your build produces artifacts/android-arm64 and artifacts/osx they both will be automatically archived.
The packaging system will try to download all known and optional rids and packages those that exist into the final nuget.

Add support for the README file

When testing the OpenAL library I was seeing this

Found version-specific or distribution-specific runtime identifier(s): windows-x64. Affected libraries: MonoGame.Library.OpenAL. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details.
It seems using things like windows-x64 is not allowed. It should be win-x64 or just win.

Fix the rids to match the spec https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.NETCore.Platforms/readme.md
  • Loading branch information
dellis1972 authored Sep 5, 2024
1 parent 5fefae8 commit e1aac69
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 23 deletions.
6 changes: 4 additions & 2 deletions MonoGame.Library.BuildScripts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cake.FileHelpers" Version="6.1.3" />
<PackageReference Include="Cake.Frosting" Version="3.1.0" />
<PackageReference Include="Cake.FileHelpers" Version="7.0.0" />
<PackageReference Include="Cake.Frosting" Version="4.0.0" />
<PackageReference Include="NuGet.Packaging" Version="6.10.1" />
<PackageReference Include="System.Formats.Asn1" Version="8.0.1" />
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions Resources/MonoGame.Library.X.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
<Description>This package contains binaries for {X} library built for usage with MonoGame.</Description>
<PackageIcon>Icon.png</PackageIcon>
<IncludeBuildOutput>false</IncludeBuildOutput>
<PackageLicenseFile>{LicenceName}</PackageLicenseFile>
<PackageLicenseFile>{LicenseName}</PackageLicenseFile>
<PackageReadMeFile>{ReadMeName}</PackageReadMeFile>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>

<ItemGroup>
<None Include="{LicencePath}" Pack="true" PackagePath="{LicencePackagePath}" />
<None Include="{LicensePath}" Pack="true" PackagePath="" />
<None Include="{ReadMePath}" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
Expand Down
37 changes: 34 additions & 3 deletions Tasks/PublishLibraryTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace BuildScripts;

Expand All @@ -10,14 +11,45 @@ public sealed class PublishLibraryTask : AsyncFrostingTask<BuildContext>

public override async Task RunAsync(BuildContext context)
{
var knownRids = new string[] {
"win-x64",
"win-x86",
"win-arm64",
"linux-x64",
"linux-arm",
"linux-arm64",
"osx",
"osx-x64",
"osx-arm64",
"ios-arm64",
"iossimulator-x64",
"iossimulator-arm64",
"android-arm64",
"android-arm",
"android-x86",
"android-x64"
};
var availableRids = new List<string> ();

foreach (var knownRid in knownRids) {
if (context.DirectoryExists ($"{context.ArtifactsDir}/{knownRid}")) {
availableRids.Add (knownRid);
}
}

foreach (var r in availableRids)
await context.BuildSystem().GitHubActions.Commands.UploadArtifact(DirectoryPath.FromString($"{context.ArtifactsDir}/{r}"), $"artifacts-{r}");

if (availableRids.Any ())
return;

var rid = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
rid = "windows";
rid = "win";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
rid = "osx";
else
rid = "linux";

if (!(RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && context.IsUniversalBinary))
{
rid += RuntimeInformation.ProcessArchitecture switch
Expand All @@ -26,7 +58,6 @@ public override async Task RunAsync(BuildContext context)
_ => "-x64",
};
}

await context.BuildSystem().GitHubActions.Commands.UploadArtifact(DirectoryPath.FromString(context.ArtifactsDir), $"artifacts-{rid}");
}
}
68 changes: 52 additions & 16 deletions Tasks/PublishPackageTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,30 @@ public override async Task RunAsync(BuildContext context)
var requiredRids = context.IsUniversalBinary ?
new string[]
{
"windows-x64",
"win-x64",
"linux-x64",
"osx"
"osx",
}
: new string[] {
"windows-x64",
"win-x64",
"linux-x64",
"osx-x64",
"osx-arm64"
};
var optionalRids = new string[] {
"ios-arm64",
"iossimulator-x64",
"iossimulator-arm64",
"android-arm64",
"android-arm",
"android-x86",
"android-x64",
"linux-arm",
"linux-arm64",
};

// Download built artifacts
var downloadedRids = new List<string>();
if (context.BuildSystem().IsRunningOnGitHubActions)
{
foreach (var rid in requiredRids)
Expand All @@ -49,27 +61,51 @@ public override async Task RunAsync(BuildContext context)

context.CreateDirectory(directoryPath);
await context.BuildSystem().GitHubActions.Commands.DownloadArtifact($"artifacts-{rid}", directoryPath);
downloadedRids.Add (rid);
}
foreach (var rid in optionalRids)
{
var directoryPath = $"runtimes/{rid}/native";
if (context.DirectoryExists(directoryPath))
continue;

context.CreateDirectory(directoryPath);
try {
await context.BuildSystem().GitHubActions.Commands.DownloadArtifact($"artifacts-{rid}", directoryPath);
downloadedRids.Add (rid);
} catch {
// Rid not available, remove the directory.
context.DeleteDirectory (directoryPath,new DeleteDirectorySettings () { Recursive = true, Force = true});
}
}
}

// Generate Project
var projectData = await ReadEmbeddedResourceAsync("MonoGame.Library.X.txt");
projectData = projectData.Replace("{X}", context.PackContext.LibraryName);
projectData = projectData.Replace("{LicencePath}", context.PackContext.LicensePath);
var description = $"This package contains native libraries for {context.PackContext.LibraryName} built for usage with MonoGame.";

var readMeName = "README.md";
var readMePath = $"{readMeName}";

var licensePath = context.PackContext.LicensePath;
var licenseName = "LICENSE";

if (context.PackContext.LicensePath.EndsWith(".txt"))
projectData = projectData.Replace("{LicenceName}", "LICENSE.txt").Replace("{LicencePackagePath}", "LICENSE.txt");
else if (context.PackContext.LicensePath.EndsWith(".md"))
projectData = projectData.Replace("{LicenceName}", "LICENSE.md").Replace("{LicencePackagePath}", "LICENSE.md");
else
// https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5030#issue
projectData = projectData.Replace("{LicenceName}", "LICENSE").Replace("{LicencePackagePath}", "");
if (licensePath.EndsWith(".txt")) licenseName += ".txt";
else if (licensePath.EndsWith(".md")) licenseName += ".md";

var librariesToInclude = from rid in requiredRids from filePath in Directory.GetFiles($"runtimes/{rid}/native")
var librariesToInclude = from rid in downloadedRids from filePath in Directory.GetFiles($"runtimes/{rid}/native")
select $"<Content Include=\"{filePath}\"><PackagePath>runtimes/{rid}/native</PackagePath></Content>";
projectData = projectData.Replace("{LibrariesToInclude}", string.Join(Environment.NewLine, librariesToInclude));

// Generate Project
var projectData = await ReadEmbeddedResourceAsync("MonoGame.Library.X.txt");
projectData = projectData.Replace("{X}", context.PackContext.LibraryName)
.Replace("{Description}", description)
.Replace("{LicensePath}", licensePath)
.Replace("{ReadMePath}", readMeName)
.Replace("{LicenseName}", licenseName)
.Replace("{ReadMeName}", readMeName)
.Replace("{LibrariesToInclude}", string.Join(Environment.NewLine, librariesToInclude));

await File.WriteAllTextAsync($"MonoGame.Library.{context.PackContext.LibraryName}.csproj", projectData);
await File.WriteAllTextAsync(readMePath, description);
await SaveEmbeddedResourceAsync("Icon.png", "Icon.png");

// Build
Expand Down

0 comments on commit e1aac69

Please sign in to comment.