Skip to content

Commit

Permalink
Add test for source-built NativeAOT. (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Aug 20, 2024
1 parent 487a333 commit f93664f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
if [[ ${{ matrix.dotnet_version }} == 9.* ]]; then
dnf install 'dnf-command(copr)' -y
dnf copr enable @dotnet-sig/dotnet-preview -y
dnf install -y dotnet-sdk-aot-${{ matrix.dotnet_version }}
fi
dnf install -y dotnet-sdk-${{ matrix.dotnet_version }}
if [[ ! ${{ matrix.dotnet_version }} == *6* ]]; then
Expand Down
38 changes: 38 additions & 0 deletions nativeaot-sb/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Security.Cryptography;
using System.Text;
using System.IO.Compression;

byte[] bytes = Encoding.UTF8.GetBytes("some bytes");

#if NO_DEPS

// No dependencies.

#elif DEP_CRYPTO

using (SHA256 mySHA256 = SHA256.Create())
{
mySHA256.ComputeHash(bytes);
}

#elif DEP_ZLIB

using (var stream = new ZLibStream(new MemoryStream(), CompressionMode.Compress, leaveOpen: false))
{
stream.Write(bytes);
}

#elif DEP_BROTLI

using (var stream = new BrotliStream(new MemoryStream(), CompressionMode.Compress, leaveOpen: false))
{
stream.Write(bytes);
}

#else

#error "A preprocessor macro must be set to control the app dependencies."

#endif

Console.WriteLine("Success");
11 changes: 11 additions & 0 deletions nativeaot-sb/console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(TestTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>console</AssemblyName>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions nativeaot-sb/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "nativeaot-sb",
"enabled": true,
"requiresSdk": true,
"version": "9.0",
"versionSpecific": false,
"type": "bash",
"cleanup": true,
"skipWhen": [
"runtime=mono" // nativeaot is not available with mono
]
}
40 changes: 40 additions & 0 deletions nativeaot-sb/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

set -euo pipefail
IFS=$'\n\t'
set -x

RID_ARG="/p:RuntimeIdentifier=$(../runtime-id --sdk)"

# Depending on the code used by the program, different native libraries are needed.
# We compile different programs to test if those native libraries are found.
# Some native libraries (like libunwind and rapidjson) are always needed. Consequently, each program check those.

for DEPENDENCY in NO_DEPS DEP_CRYPTO DEP_ZLIB DEP_BROTLI
do
echo "Testing NativeAOT with $DEPENDENCY"

rm -rf bin obj

# Publish the app.
if ! dotnet publish $RID_ARG /p:DefineConstants=$DEPENDENCY /p:PublishAot=true; then
echo "FAIL: failed to publish application using NativeAot."
exit 1
fi

# Verify the published output contains a single file.
PUBLISHED_FILE_COUNT=$(ls ./bin/*/net*/*/publish | grep -v console.dbg | wc -l)
if [ "$PUBLISHED_FILE_COUNT" != "1" ]; then
echo "FAIL: published NativeAot app contains more than 1 file."
exit 1
fi

# Verify the application runs.
APP_OUTPUT="$(./bin/*/net*/*/publish/console 2>&1)"
if [ "$APP_OUTPUT" != "Success" ]; then
echo "FAIL: NativeAot console application did not have expected output."
exit 1
fi
done

echo "PASS: source-built NativeAot apps build and run."

0 comments on commit f93664f

Please sign in to comment.