Skip to content

Commit 79e41d0

Browse files
committed
Flag libraries and submodules for WASM compilation
1 parent 70a531c commit 79e41d0

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

‎crates/bindings-csharp/Runtime/build/SpacetimeDB.Runtime.targets‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@
3535
<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
3636
</PropertyGroup>
3737

38+
<!-- The SDK builds RID-agnostic project references, such as libraries mounted with
39+
[Namespace], without the module's wasi-wasm RID. Their generated code must still
40+
target the WASM Runtime that the module links. -->
41+
<PropertyGroup Condition="'$(_SpacetimeWasmLibrary)' == 'true'">
42+
<DefineConstants>$(DefineConstants);SPACETIMEDB_WASM_AOT</DefineConstants>
43+
</PropertyGroup>
44+
45+
<Target Name="_SpacetimeFlowWasmToProjectReferences"
46+
BeforeTargets="AssignProjectConfiguration"
47+
Condition="'$(_UseNativeAotLlvm)' == 'true' And $(TargetFramework.StartsWith('net10.'))">
48+
<ItemGroup>
49+
<ProjectReference Condition="'%(ProjectReference.ReferenceOutputAssembly)' != 'false'">
50+
<AdditionalProperties>%(ProjectReference.AdditionalProperties);_SpacetimeWasmLibrary=true</AdditionalProperties>
51+
</ProjectReference>
52+
</ItemGroup>
53+
</Target>
54+
3855
<Import
3956
Project="$(PkgMicrosoft_DotNet_ILCompiler_LLVM)\build\Microsoft.DotNet.ILCompiler.LLVM.targets"
4057
Condition="'$(_UseNativeAotLlvm)' == 'true' and '$(ILCompilerTargetsPath)' == '' and '$(PkgMicrosoft_DotNet_ILCompiler_LLVM)' != '' and Exists('$(PkgMicrosoft_DotNet_ILCompiler_LLVM)\build\Microsoft.DotNet.ILCompiler.LLVM.targets')" />

‎tools/ci/commands/test-csharp-build/src/main.rs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn main() -> Result<()> {
2121
.with_context(|| format!("Build checks failed for {template}"))?;
2222
}
2323
check_test_references(work, &version).context("Test-project reference checks failed")?;
24+
check_library_reference(work, &cli, &version).context("Library project reference check failed")?;
2425
check_net8(work, &cli).context(".NET 8 WASM build check failed")?;
2526
Ok(())
2627
}
@@ -326,6 +327,84 @@ public class ModuleReferenceTests
326327
Ok(())
327328
}
328329

330+
fn check_library_reference(work: &Path, cli: &Path, version: &str) -> Result<()> {
331+
// The SDK builds a plain library reference without the module's wasi-wasm RID.
332+
// It must still compile its generated code for the WASM Runtime the module links.
333+
println!("Checking a WASM module with a library project reference");
334+
let module = work.join("library/module");
335+
let library = module.join("libraries/AuthLib");
336+
fs::create_dir_all(&library)?;
337+
let mut project = Element::parse(File::open(work.join("basic-cs/module/StdbModule.csproj"))?)?;
338+
let references = Element::parse(
339+
r#"<ItemGroup>
340+
<ProjectReference Include="libraries/AuthLib/AuthLib.csproj" />
341+
<Compile Remove="libraries/**/*.cs" />
342+
</ItemGroup>"#
343+
.as_bytes(),
344+
)?;
345+
project.children.push(XMLNode::Element(references));
346+
project.write(File::create(module.join("StdbModule.csproj"))?)?;
347+
fs::copy(work.join("basic-cs/global.json"), work.join("library/global.json"))?;
348+
fs::write(
349+
module.join("Lib.cs"),
350+
r#"using SpacetimeDB;
351+
352+
[assembly: Namespace(typeof(AuthLib.Marker), Accessor = "MyAuth", Name = "auth_data")]
353+
354+
public static partial class Module
355+
{
356+
[Reducer]
357+
public static void AddAuthUser(ReducerContext ctx, uint id) => AuthLib.Functions.Add(ctx.As.MyAuth, id);
358+
}
359+
"#,
360+
)?;
361+
fs::write(
362+
library.join("AuthLib.csproj"),
363+
format!(
364+
r#"<Project Sdk="Microsoft.NET.Sdk">
365+
<PropertyGroup>
366+
<TargetFramework>net10.0</TargetFramework>
367+
<ImplicitUsings>enable</ImplicitUsings>
368+
<Nullable>enable</Nullable>
369+
</PropertyGroup>
370+
<ItemGroup>
371+
<PackageReference Include="SpacetimeDB.Runtime" Version="{version}" />
372+
</ItemGroup>
373+
</Project>
374+
"#
375+
),
376+
)?;
377+
fs::write(
378+
library.join("Lib.cs"),
379+
r#"using SpacetimeDB;
380+
381+
// This check only builds the library as part of the WASM module.
382+
#if !SPACETIMEDB_WASM_AOT
383+
#error The library was compiled for the managed Runtime, but the module links the WASM Runtime.
384+
#endif
385+
386+
namespace AuthLib;
387+
388+
public class Marker { }
389+
390+
[Table(Accessor = "User", Name = "auth_users", Public = true)]
391+
public partial struct User
392+
{
393+
[PrimaryKey]
394+
public uint Id;
395+
}
396+
397+
public static partial class Functions
398+
{
399+
[Reducer]
400+
public static void Add(ReducerContext ctx, uint id) => ctx.Db.User.Insert(new User { Id = id });
401+
}
402+
"#,
403+
)?;
404+
run(work, cmd!(cli, "build", "--module-path", &module))?;
405+
Ok(())
406+
}
407+
329408
fn check_net8(work: &Path, cli: &Path) -> Result<()> {
330409
// Exercise the .NET 8 SDK's WASI import order, not just our final property values.
331410
let module = work.join("net8/module");

0 commit comments

Comments
 (0)