Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono][tasks] Extract EmitWasmBundle into generic EmitBundle task and enable bundling in mono self-contained library #84191

Merged
merged 115 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
1d4e246
[tasks] Move EmitWasmBundle out of wasm specific task
mdh1418 Mar 31, 2023
2955cdb
[tasks] Make EmitBundle more generic
mdh1418 Mar 31, 2023
2d25efc
[tasks] Generate EmitBundle task and replace invocations of old task
mdh1418 Mar 31, 2023
d8d97e8
Extend preloading assemblies with mono_assembly_open
mdh1418 Mar 31, 2023
eb5e396
[LibraryBuilder] Integrate bundling
mdh1418 Mar 31, 2023
a34c284
Refactor LibraryBuilder targets to isolate Bundling
mdh1418 Apr 3, 2023
c1924b2
[mono] Add mono_get_bundled_assembly in preparation for bundled runti…
mdh1418 Apr 3, 2023
9084234
[mono][librarymode] Bundle runtimeconfig.bin
mdh1418 Apr 3, 2023
382a93d
Address feedback
mdh1418 Apr 5, 2023
d4d4193
Consolidate runtimeconfig functions
mdh1418 Apr 6, 2023
e70e61a
[LibraryBuilder] Enable bundling by default
mdh1418 Apr 6, 2023
ecf5ffc
Merge branch 'main' into mono_library_mode_bundling
lewing Apr 12, 2023
fda389a
Merge remote-tracking branch 'upstream/main' into HEAD
mdh1418 Apr 12, 2023
791c743
Handle pdb bundling
mdh1418 Apr 11, 2023
d98c0a5
Default EmitBundle callback function to mono_add_bundled_resource
mdh1418 Apr 11, 2023
6fc54b1
Combine bundled resource types into generic bundled data
mdh1418 Apr 12, 2023
d7c09d0
Add api to free memory dynamically allocated for the bundled resource…
mdh1418 Apr 12, 2023
d1b5f02
Prepend enum and entries with mono
mdh1418 Apr 12, 2023
c0c443c
Move bundling api into separate source and header
mdh1418 Apr 12, 2023
7683ed1
Fix source file name
mdh1418 Apr 18, 2023
b1f3240
Prefix struct with Mono
mdh1418 Apr 18, 2023
ddf6691
Integrate new bundling api into mono debug
mdh1418 Apr 18, 2023
130aff3
Fix types in autoinit
mdh1418 Apr 18, 2023
b8177d0
[mono] Base bundling apis around preallocation of MonoBundled*Resourc…
mdh1418 Apr 22, 2023
60aa110
[mono] Clean up bundled assembly and satellite assembly search
mdh1418 Apr 22, 2023
ba241b5
[mono] Incorporate new bundling APIs into bundled resource retrievals
mdh1418 Apr 22, 2023
cfa60e5
[EmitBundle][task] Add templates for bundled source files
mdh1418 Apr 21, 2023
6fe9912
[EmitBundle] Enable consolidation of bundled resource symbols
mdh1418 Apr 21, 2023
077c020
[EmitBundle] Generate bundled source file to preallocate MonoBundled*…
mdh1418 Apr 21, 2023
1e7ad1c
[EmitBundle] Output symbol to resource type map for populating functi…
mdh1418 Apr 21, 2023
c5eb985
[LibraryBuilder] Generate source file to add preallocated MonoBundled…
mdh1418 Apr 22, 2023
d28ee4b
[LibraryBuilder] Incorporate EmitBundle templates into targets flow
mdh1418 Apr 22, 2023
a796bfb
[EmitBundle] Move bundle registration back into EmitBundle
mdh1418 Apr 24, 2023
364a119
Update invocations of EmitBundle task
mdh1418 Apr 24, 2023
7e48dc6
Merge remote-tracking branch 'upstream/main' into mono_library_mode_b…
mdh1418 Apr 24, 2023
46bd211
Address feedback
mdh1418 May 3, 2023
ddade81
[mono] Prefer C99 compliant types for partially exposed APIs
mdh1418 May 3, 2023
7c0081f
Address various fixups
mdh1418 May 4, 2023
39b5107
Combine preallocation registration and rename to templates
mdh1418 May 4, 2023
69af44c
Prefer symbol data over symfile
mdh1418 May 4, 2023
c3401df
Differentiate resource name and symbol
mdh1418 May 4, 2023
239c649
Update invocations of EmitBundle
mdh1418 May 4, 2023
ae088f2
Add inlined resource getters
mdh1418 May 4, 2023
233025a
Add unique id field for each MonoBundledResource
mdh1418 May 5, 2023
7f4258a
Include culture information for satellite resources
mdh1418 May 5, 2023
a632a73
Generate preallocation and registration source file iff BundleFile pr…
mdh1418 May 5, 2023
429fe75
Generate output itemgroup detailing resource data
mdh1418 May 5, 2023
5aeaef4
Leverage new bundling api from old bundling api and deprecate
mdh1418 May 7, 2023
ccd783b
[mono][debug] Migrate symbol data registration to new bundling api
mdh1418 May 10, 2023
3cf95fc
Remove symbol data from MonoBundleSatelliteAssemblyResource
mdh1418 May 9, 2023
5d265db
Directly determine resource type
mdh1418 May 9, 2023
3206aab
Update bundled header and switch to non internal types
mdh1418 May 10, 2023
a8c5d88
Update Autoinitialization logic to leverage bundled runtimeconfig sym…
mdh1418 May 10, 2023
fbe022e
Integrate header into bundled source
mdh1418 May 10, 2023
3390e90
Add functions to free allocated MonoBundledResources
mdh1418 May 11, 2023
e16e467
Fixup Leverage new bundling api from old bundling api and deprecate
mdh1418 May 11, 2023
684b667
Fixup Address various fixups
mdh1418 May 11, 2023
50fd4ba
Remove unnecessary includes
mdh1418 May 11, 2023
236539a
Fix trailing commas in structs and extra lines in generated files
mdh1418 May 11, 2023
df96a6d
Move specific bundled resource getters into source file
mdh1418 May 11, 2023
befa5d6
Various cleanups
mdh1418 May 11, 2023
c3bb50a
Cleanup logic surrounding EmitBundle task invocations
mdh1418 May 11, 2023
a060748
Rename output metadata to DataLenSymbol and DataLenSymbolValue
mdh1418 May 11, 2023
aff9c60
Simplify bundled resource hashtable insertion
mdh1418 May 12, 2023
c1e9ec1
Cleanup variable instantiation
mdh1418 May 13, 2023
aeaf879
Fixup mono-debug
mdh1418 May 13, 2023
a6b9245
Cleanup header
mdh1418 May 13, 2023
6c7d33d
fixup wasi
mdh1418 May 13, 2023
dfba3f4
fixup template format
mdh1418 May 13, 2023
e8aa970
Add flexibility to auto initialize runtime without bundled runtimeconfig
mdh1418 May 13, 2023
d191d59
Various fixups
mdh1418 May 13, 2023
afaf91a
Use g_free for memory allocated through glib apis
mdh1418 May 15, 2023
3eff6b9
Move condition end tag into LibraryBuilder to avoid empty line and cl…
mdh1418 May 15, 2023
7d3bebd
Add bundled resource getters to access data values without MonoBundle…
mdh1418 May 15, 2023
187855f
Emit one object file for single resource data source file mode
mdh1418 May 16, 2023
7cf6edd
Rename resource size symbol
mdh1418 May 16, 2023
8a78222
Use public friendly API in file linking towards runtime
mdh1418 May 16, 2023
f5ec428
Cleanup types populated through bundled resource getters
mdh1418 May 16, 2023
f309569
Move extern block completely to LibraryBuilder side
mdh1418 May 16, 2023
5001622
Update EmitBundle task invocations to use BundleRegistrationFunctionName
mdh1418 May 16, 2023
0b3b30d
Refactor EmitBundle to account for timezone duplicate resource contents
mdh1418 May 17, 2023
1803ede
Merge remote-tracking branch 'upstream/main' into mono_library_mode_b…
mdh1418 May 17, 2023
2af674d
wip
mdh1418 May 19, 2023
4541530
wip
mdh1418 May 23, 2023
204cae7
WIP
mdh1418 May 24, 2023
a77c39c
[mono] Alias assemblies with known extension in bundled resources has…
mdh1418 May 25, 2023
ec2570d
[tasks] Calculate destination files within EmitBundle task itself
mdh1418 May 25, 2023
4ba29ad
Update invocations of EmitBundle to grab DestinationFiles from output
mdh1418 May 25, 2023
9e5e42f
Guard webcil specific logic
mdh1418 May 25, 2023
d0fafb6
Include OutputDirectory path into output destinationFiles
mdh1418 May 25, 2023
5dad711
Fix debugger tests and memory leak
mdh1418 May 25, 2023
51637a3
Various fixups and Documentation
mdh1418 May 25, 2023
0e33442
Try adding resources with known assembly extensions as dll
mdh1418 May 25, 2023
37b1c82
Revert to custom hash and equal without WEBCIL guard
mdh1418 May 26, 2023
88f90d4
Consolidate file and bundledResource variables
mdh1418 May 30, 2023
e4c6ef6
Add culture metadata to SatelliteAssembly resources
mdh1418 May 30, 2023
dbe7c21
Truncate Encoding for readability
mdh1418 May 30, 2023
8705c97
Enable custom hash for bundling resources on wasm
mdh1418 May 30, 2023
4071c10
Leverage new bundling api for wasm wasi drivers
mdh1418 Jun 1, 2023
a573583
Add helper to dynamically add bundled assembly and satellite_assembly…
mdh1418 Jun 1, 2023
7d1a6bd
Limit when bundled resources can be added and freed
mdh1418 Jun 1, 2023
58eeb95
Restrict MonoBundledResource getters
mdh1418 Jun 1, 2023
4e568b1
Restrict exposure of underlying MonoBundledAssemblyResource modification
mdh1418 Jun 1, 2023
b755fc7
WIP
mdh1418 Jun 2, 2023
e34aff7
Free function chaining.
lateralusX Jun 2, 2023
854c5ec
Fix crash when output parameters are NULL.
lateralusX Jun 2, 2023
4379c37
Fix WASM/WASI build errors.
lateralusX Jun 2, 2023
59ba150
Align bundled resources registration struct
mdh1418 Jun 5, 2023
102c47d
Merge remote-tracking branch 'upstream/main' into mono_library_mode_b…
mdh1418 Jun 5, 2023
560491a
[task] Check PE metadata to determine resource type
mdh1418 Jun 6, 2023
5e41b81
Cleanup Satellite Assembly bundling
mdh1418 Jun 6, 2023
18afeda
Address Feedback
mdh1418 Jun 8, 2023
c2c69ab
Split MonoBundledAssemblyResource getter
mdh1418 Jun 9, 2023
45d2f8b
Addressing more feedback
mdh1418 Jun 13, 2023
b377ff4
Validate OutputDirectory
mdh1418 Jun 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<LibraryBuilderDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'LibraryBuilder', 'Debug', '$(NetCoreAppToolCurrent)'))</LibraryBuilderDir>
<MonoAOTCompilerDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'MonoAOTCompiler', 'Debug', '$(NetCoreAppToolCurrent)'))</MonoAOTCompilerDir>
<MonoTargetsTasksDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'MonoTargetsTasks', 'Debug', '$(NetCoreAppToolCurrent)'))</MonoTargetsTasksDir>
<EmitBundleDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'EmitBundle', 'Debug', '$(NetCoreAppToolCurrent)'))</EmitBundleDir>
<TestExclusionListTasksDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'TestExclusionListTasks', 'Debug', '$(NetCoreAppToolCurrent)'))</TestExclusionListTasksDir>
<InstallerTasksAssemblyPath Condition="'$(MSBuildRuntimeType)' == 'Core'">$([MSBuild]::NormalizePath('$(ArtifactsBinDir)', 'installer.tasks', 'Debug', '$(NetCoreAppToolCurrent)', 'installer.tasks.dll'))</InstallerTasksAssemblyPath>
<InstallerTasksAssemblyPath Condition="'$(MSBuildRuntimeType)' != 'Core'">$([MSBuild]::NormalizePath('$(ArtifactsBinDir)', 'installer.tasks', 'Debug', '$(NetFrameworkToolCurrent)', 'installer.tasks.dll'))</InstallerTasksAssemblyPath>
Expand All @@ -166,6 +167,7 @@
<LibraryBuilderTasksAssemblyPath>$([MSBuild]::NormalizePath('$(LibraryBuilderDir)', 'LibraryBuilder.dll'))</LibraryBuilderTasksAssemblyPath>
<MonoAOTCompilerTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoAOTCompilerDir)', 'MonoAOTCompiler.dll'))</MonoAOTCompilerTasksAssemblyPath>
<MonoTargetsTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoTargetsTasksDir)', 'MonoTargetsTasks.dll'))</MonoTargetsTasksAssemblyPath>
<EmitBundleTasksAssemblyPath>$([MSBuild]::NormalizePath('$(EmitBundleDir)', 'EmitBundle.dll'))</EmitBundleTasksAssemblyPath>
<TestExclusionListTasksAssemblyPath>$([MSBuild]::NormalizePath('$(TestExclusionListTasksDir)', 'TestExclusionListTasks.dll'))</TestExclusionListTasksAssemblyPath>
<CoreCLRToolPath>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'coreclr', '$(TargetOS).$(TargetArchitecture).$(RuntimeConfiguration)'))</CoreCLRToolPath>
<ILAsmToolPath Condition="'$(DotNetBuildFromSource)' == 'true' or '$(BuildArchitecture)' == 's390x' or '$(BuildArchitecture)' == 'ppc64le'">$(CoreCLRToolPath)</ILAsmToolPath>
Expand Down
53 changes: 53 additions & 0 deletions src/mono/mono/mini/aot-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,59 @@ mono_aot_register_module (gpointer *aot_info)
mono_aot_unlock ();
}

static GHashTable *bundled_assemblies = NULL;

static int bundles_added = 0;

void
mono_add_bundled_assembly (const char *name, const unsigned char *data, unsigned int size)
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
{
if (!bundled_assemblies)
bundled_assemblies = g_hash_table_new (g_str_hash, g_str_equal);

MonoBundledAssembly *bundled_assembly = g_new0 (MonoBundledAssembly, 1);
bundled_assembly->name = g_strdup (name);
g_assert (bundled_assembly->name);
bundled_assembly->data = data;
bundled_assembly->size = size;
g_hash_table_insert (bundled_assemblies, (gpointer) name, bundled_assembly);
bundles_added++;
}

void
mono_get_bundled_assembly (const char *name, const unsigned char **out_data, unsigned int *out_size)
{
*out_data = NULL;
*out_size = 0;
MonoBundledAssembly *bundled_assembly;

if (g_hash_table_lookup_extended (bundled_assemblies, name, NULL, (gpointer *)&bundled_assembly)) {
*out_data = bundled_assembly->data;
*out_size = bundled_assembly->size;
}
}

static void
populate_bundle (gpointer key, gpointer value, gpointer user_data)
{
MonoBundledAssembly *bundled_assembly = (MonoBundledAssembly *)value;
MonoBundledAssembly **bundle = (MonoBundledAssembly **)user_data;
g_assert (bundles_added > 0);
bundle [bundles_added-- - 1] = bundled_assembly;
}

void
mono_register_bundle (void)
{
if (!bundled_assemblies)
return;

MonoBundledAssembly **bundle = g_new0 (MonoBundledAssembly*, bundles_added + 1);
g_hash_table_foreach (bundled_assemblies, populate_bundle, bundle);

mono_register_bundled_assemblies ((const MonoBundledAssembly **)bundle);
}

void
mono_aot_init (void)
{
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono/mini/aot-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ typedef struct MonoAotFileInfo
/* Number of symbols in the MonoAotFileInfo structure */
#define MONO_AOT_FILE_INFO_NUM_SYMBOLS (((G_STRUCT_OFFSET (MonoAotFileInfo, MONO_AOT_FILE_INFO_LAST_SYMBOL) - G_STRUCT_OFFSET (MonoAotFileInfo, MONO_AOT_FILE_INFO_FIRST_SYMBOL)) / sizeof (gpointer)) + 1)

void mono_add_bundled_assembly (const char *name, const unsigned char *data, unsigned int size);
void mono_get_bundled_assembly (const char *name, const unsigned char **out_data, unsigned int *out_size);
void mono_register_bundle (void);
void mono_aot_init (void);
gpointer mono_aot_get_method (MonoMethod *method, MonoError *error);
gpointer mono_aot_get_method_from_token (MonoImage *image, guint32 token, MonoError *error);
Expand Down
3 changes: 2 additions & 1 deletion src/mono/msbuild/android/build/AndroidBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@
<Target Name="_AndroidGenerateRuntimeConfig"
Inputs="$(_AndroidRuntimeConfigFilePath)"
Outputs="$(_ParsedRuntimeConfigFilePath)"
Condition="Exists('$(_AndroidRuntimeConfigFilePath)')">
Condition="Exists('$(_AndroidRuntimeConfigFilePath)')"
BeforeTargets="_GenerateBundle">
<ItemGroup>
<_RuntimeConfigReservedProperties Include="RUNTIME_IDENTIFIER"/>
<_RuntimeConfigReservedProperties Include="APP_CONTEXT_BASE_DIRECTORY"/>
Expand Down
3 changes: 2 additions & 1 deletion src/mono/msbuild/apple/build/AppleBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@
<Target Name="_AppleGenerateRuntimeConfig"
Inputs="$(_AppleRuntimeConfigFilePath)"
Outputs="$(_ParsedRuntimeConfigFilePath)"
Condition="Exists('$(_AppleRuntimeConfigFilePath)')">
Condition="Exists('$(_AppleRuntimeConfigFilePath)')"
BeforeTargets="_GenerateBundle">
<ItemGroup>
<_RuntimeConfigReservedProperties Include="RUNTIME_IDENTIFIER"/>
<_RuntimeConfigReservedProperties Include="APP_CONTEXT_BASE_DIRECTORY"/>
Expand Down
39 changes: 38 additions & 1 deletion src/mono/msbuild/common/LibraryBuilder.targets
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<Project>
<UsingTask TaskName="EmitBundleSourceFiles"
AssemblyFile="$(EmitBundleTasksAssemblyPath)" />
<UsingTask TaskName="LibraryBuilderTask"
AssemblyFile="$(LibraryBuilderTasksAssemblyPath)" />

<Target Name="_BuildNativeLibrary"
Condition="'$(_IsLibraryMode)' == 'true' and '$(RunAOTCompilation)' == 'true'">
Condition="'$(_IsLibraryMode)' == 'true' and '$(RunAOTCompilation)' == 'true'"
DependsOnTargets="_GenerateBundle">

<PropertyGroup>
<_IsSharedLibrary>false</_IsSharedLibrary>
Expand All @@ -14,12 +17,15 @@

<ItemGroup>
<_ExtraLibrarySources Include="$(_AotModuleTablePath)" />
<_ExtraLibrarySources Include="$(_AssembliesToBundleSourceFile)" />
<_ExtraLibrarySources Include="%(_AssembliesToBundleWithHashes.DestinationFile)" />
<_ExtraLinkerArgs Include="@(_CommonLinkerArgs)" />
</ItemGroup>

<LibraryBuilderTask
Assemblies="@(_AssembliesToBundleInternal)"
AssembliesLocation="$(AssembliesLocation)"
BundlesAssemblies="$(BundlesAssemblies)"
ExtraLinkerArguments="@(_ExtraLinkerArgs)"
ExtraSources="@(_ExtraLibrarySources)"
IsSharedLibrary="$(_IsSharedLibrary)"
Expand All @@ -35,4 +41,35 @@
</LibraryBuilderTask>
</Target>

<Target Name="_GenerateBundle"
Condition="'$(BundlesAssemblies)' == 'true'">
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved

<PropertyGroup>
<_AssembliesToBundleSourceFile>$(BundleDir)bundled_assemblies.c</_AssembliesToBundleSourceFile>
</PropertyGroup>

<ItemGroup>
<_AssembliesToBundle Remove="@(_AssembliesToBundle)" />
<_AssembliesToBundle Include="@(_AssembliesToBundleInternal)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure we will end up in scenario where user want to select what assets to bundle into the library and what to keep on disk, so then we would need to have metadata on items list that we filter on and pass to bundle task, or the bundle task evaluate the metadata. We have similar concept on other platforms, where each assembly that we run through the toolchain can have metadata set by user. Maybe we can leave it out in this first PR and do it follow up PR.

We already know of one scenario where this might be needed, on maccatalyst you can create on framework that includes several different target platforms, so you end up with libraries targeting x64/arm64 in the same framework, but since System.Private.CoreLib is tied close to the target platform, you would like to bundle that assembly into the library, but keep other assemblies on disk so they can be shared between platforms.

<_AssembliesToBundle Include="$(_ParsedRuntimeConfigFilePath)" />
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<GetFileHash Files="@(_AssembliesToBundle)">
<Output TaskParameter="Items" ItemName="_AssembliesToBundleWithHashes" />
</GetFileHash>

<ItemGroup>
<_AssembliesToBundleWithHashes Update="@(_AssembliesToBundleWithHashes)">
<DestinationFile>$(BundleDir)%(_AssembliesToBundleWithHashes.Filename)%(_AssembliesToBundleWithHashes.Extension).$([System.String]::Copy(%(_AssembliesToBundleWithHashes.FileHash)).Substring(0, 8)).c</DestinationFile>
</_AssembliesToBundleWithHashes>
</ItemGroup>

<EmitBundleSourceFiles
FilesToBundle="@(_AssembliesToBundleWithHashes)"
BundleName="assemblies"
BundleFile="$(_AssembliesToBundleSourceFile)"
RegistrationCallbackFunctionName="mono_add_bundled_assembly"
/>
</Target>

</Project>
6 changes: 3 additions & 3 deletions src/mono/wasi/build/WasiApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@
<!--EnvironmentVariables="@(EmscriptenEnvVars)" />-->
<!--</Target>-->

<UsingTask TaskName="Microsoft.WebAssembly.Build.Tasks.EmitWasmBundleObjectFiles" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />
<UsingTask TaskName="EmitBundleObjectFiles" AssemblyFile="$(EmitBundleTasksAssemblyPath)" />
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
<Target Name="_GenerateAssemblyObjectFiles" Returns="@(_WasiObjectFilesForBundle)">
<!-- Get the file hashes of everything in @(_WasmBundleFiles), then pass it all to EmitWasmBundleObjectFiles. This
<!-- Get the file hashes of everything in @(_WasmBundleFiles), then pass it all to EmitBundleObjectFiles. This
will emit corresponding .o files for anything we don't already have on disk. -->
<PropertyGroup>
<_WasmAssembliesBundleObjectFile>$(_WasmIntermediateOutputPath)wasi_bundled_assemblies.o</_WasmAssembliesBundleObjectFile>
Expand All @@ -357,7 +357,7 @@
</WasmBundleAssembliesWithHashes>
</ItemGroup>
<!-- TODO make this incremental compilation -->
<EmitWasmBundleObjectFiles
<EmitBundleObjectFiles
FilesToBundle="@(WasmBundleAssembliesWithHashes)"
ClangExecutable="$(WasiClang)"
BundleName="assemblies"
Expand Down
8 changes: 4 additions & 4 deletions src/mono/wasi/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
extern void mono_wasm_register_timezones_bundle();
extern void mono_register_timezones_bundle();
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
#ifdef BUNDLED_ASSEMBLIES
extern void mono_wasm_register_assemblies_bundle();
extern void mono_register_assemblies_bundle();
#endif

extern const char* dotnet_wasi_getentrypointassemblyname();
Expand Down Expand Up @@ -432,9 +432,9 @@ mono_wasm_load_runtime (const char *unused, int debug_level)

mini_parse_debug_option ("top-runtime-invoke-unhandled");

mono_wasm_register_timezones_bundle();
mono_register_timezones_bundle();
#ifdef BUNDLED_ASSEMBLIES
mono_wasm_register_assemblies_bundle();
mono_register_assemblies_bundle();
#endif
mono_dl_fallback_register (wasm_dl_load, wasm_dl_symbol, NULL, NULL);
mono_wasm_install_get_native_to_interp_tramp (get_native_to_interp);
Expand Down
6 changes: 3 additions & 3 deletions src/mono/wasi/wasi.proj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</ManagedToNativeGenerator>
</Target>

<UsingTask TaskName="Microsoft.WebAssembly.Build.Tasks.EmitWasmBundleObjectFiles" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />
<UsingTask TaskName="EmitBundleObjectFiles" AssemblyFile="$(EmitBundleTasksAssemblyPath)" />
<Target Name="GenerateTimezonesArchive" Returns="@(_WasmArchivedTimezones)">
<PropertyGroup>
<_WasmTimezonesPath>$([MSBuild]::NormalizePath('$(PkgSystem_Runtime_TimeZoneData)', 'contentFiles', 'any', 'any', 'data'))</_WasmTimezonesPath>
Expand All @@ -86,14 +86,14 @@
</_WasmBundleTimezonesWithHashes>
</ItemGroup>
<!-- TODO make this incremental compilation -->
<EmitWasmBundleObjectFiles
<EmitBundleObjectFiles
FilesToBundle="@(_WasmBundleTimezonesWithHashes)"
ClangExecutable="$(WasiClang)"
BundleName="timezones"
BundleFile="$(_WasmTimezonesBundleObjectFile)"
RegistrationCallbackFunctionName="mono_wasm_add_bundled_file"
>
</EmitWasmBundleObjectFiles>
</EmitBundleObjectFiles>
<ItemGroup>
<WasmBundleTimezonesObjects Include="$([MSBuild]::MakeRelative($(WasiObjDir), %(_WasmBundleTimezonesWithHashes.DestinationFile)).Replace('\','/'))" />
<WasmBundleTimezonesObjects Include="$([MSBuild]::MakeRelative($(WasiObjDir), $(_WasmTimezonesBundleObjectFile)).Replace('\','/'))" />
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int monoeg_g_setenv(const char *variable, const char *value, int overwrite);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);

extern void mono_wasm_register_timezones_bundle();
extern void mono_register_timezones_bundle();
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
extern void mono_wasm_set_entrypoint_breakpoint (const char* assembly_name, int method_token);
static void mono_wasm_init_finalizer_thread (void);

Expand Down Expand Up @@ -512,7 +512,7 @@ mono_wasm_load_runtime (const char *unused, int debug_level)

mini_parse_debug_option ("top-runtime-invoke-unhandled");

mono_wasm_register_timezones_bundle();
mono_register_timezones_bundle();
mono_dl_fallback_register (wasm_dl_load, wasm_dl_symbol, NULL, NULL);
mono_wasm_install_get_native_to_interp_tramp (get_native_to_interp);

Expand Down
8 changes: 4 additions & 4 deletions src/mono/wasm/wasm.proj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</ManagedToNativeGenerator>
</Target>

<UsingTask TaskName="Microsoft.WebAssembly.Build.Tasks.EmitWasmBundleSourceFiles" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />
<UsingTask TaskName="EmitBundleSourceFiles" AssemblyFile="$(EmitBundleTasksAssemblyPath)" />
<Target Name="GenerateTimezonesArchive" Returns="@(_WasmArchivedTimezones)">
<PropertyGroup>
<_WasmTimezonesPath>$([MSBuild]::NormalizePath('$(PkgSystem_Runtime_TimeZoneData)', 'contentFiles', 'any', 'any', 'data'))</_WasmTimezonesPath>
Expand All @@ -104,13 +104,13 @@
</_WasmBundleTimezonesWithHashes>
</ItemGroup>
<!-- TODO make this incremental compilation -->
<EmitWasmBundleSourceFiles
<EmitBundleSourceFiles
FilesToBundle="@(_WasmBundleTimezonesWithHashes)"
BundleName="timezones"
BundleFile="$(WasmObjDir)\wasm-bundled-timezones.c"
RegistrationCallbackFunctionName="mono_wasm_add_bundled_file"
>
</EmitWasmBundleSourceFiles>
/>

<ItemGroup>
<_WasmBundleTimezonesSources Include="$([MSBuild]::MakeRelative($(WasmObjDir), %(_WasmBundleTimezonesWithHashes.DestinationFile)).Replace('\','/'))" />
<_WasmBundleTimezonesSources Include="$([MSBuild]::MakeRelative($(WasmObjDir), $(_WasmTimezonesBundleSourceFile)).Replace('\','/'))" />
Expand Down
21 changes: 21 additions & 0 deletions src/tasks/EmitBundleTask/EmitBundle.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFrameworkForNETCoreTasks)</TargetFramework>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Common\Utils.cs" />

<PackageReference Include="Microsoft.Build.Tasks.Core" Version="$(MicrosoftBuildTasksCoreVersion)" />
</ItemGroup>

<Target Name="GetFilesToPackage" Returns="@(FilesToPackage)">
<ItemGroup>
<FilesToPackage Include="$(OutputPath)$(MSBuildProjectName)*"
TargetPath="tasks\$(TargetFrameworkForNETCoreTasks)" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
using System.Threading.Tasks;
using Microsoft.Build.Framework;

namespace Microsoft.WebAssembly.Build.Tasks;

public abstract class EmitWasmBundleBase : Microsoft.Build.Utilities.Task, ICancelableTask
public abstract class EmitBundleBase : Microsoft.Build.Utilities.Task, ICancelableTask
lateralusX marked this conversation as resolved.
Show resolved Hide resolved
{
private CancellationTokenSource BuildTaskCancelled { get; } = new();

Expand Down Expand Up @@ -65,7 +63,7 @@ public override bool Execute()
return (registeredName, symbolName);
}).ToList();

Log.LogMessage(MessageImportance.Low, "Bundling {numFiles} files for {bundleName}", files.Count, BundleName);
Log.LogMessage(MessageImportance.Low, "Bundling {0} files for {1}", files.Count, BundleName);

if (remainingDestinationFilesToBundle.Length > 0)
{
Expand Down Expand Up @@ -109,7 +107,7 @@ public override bool Execute()
return Emit(BundleFile, (inputStream) =>
{
using var outputUtf8Writer = new StreamWriter(inputStream, Utf8NoBom);
GenerateRegisteredBundledObjects($"mono_wasm_register_{BundleName}_bundle", RegistrationCallbackFunctionName, files, outputUtf8Writer);
GenerateRegisteredBundledObjects($"mono_register_{BundleName}_bundle", RegistrationCallbackFunctionName, files, outputUtf8Writer);
}) && !Log.HasLoggedErrors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
using System.IO;
using Microsoft.Build.Framework;

namespace Microsoft.WebAssembly.Build.Tasks;

public class EmitWasmBundleObjectFiles : EmitWasmBundleBase
public class EmitBundleObjectFiles : EmitBundleBase
{
[Required]
public string ClangExecutable { get; set; } = default!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using System.IO;
using Microsoft.Build.Framework;

namespace Microsoft.WebAssembly.Build.Tasks;

// It would be ideal that this Task would always produce object files as EmitWasmBundleObjectFiles does.
// EmitWasmBundleObjectFiles could do it with clang by streaming code directly to clang input stream.
// It would be ideal that this Task would always produce object files as EmitBundleObjectFiles does.
// EmitBundleObjectFiles could do it with clang by streaming code directly to clang input stream.
// For emcc it's not possible, so we need to write the code to disk first and then compile it in MSBuild.
public class EmitWasmBundleSourceFiles : EmitWasmBundleBase
public class EmitBundleSourceFiles : EmitBundleBase
{
public override bool Emit(string destinationFile, Action<Stream> inputProvider)
{
Expand Down
12 changes: 11 additions & 1 deletion src/tasks/LibraryBuilder/LibraryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public bool IsSharedLibrary
/// </summary>
public string? AssembliesLocation { get; set; }

/// <summary>
/// Determines whether or not assemblies are bundled into the library
/// </summary>
public bool BundlesAssemblies { get; set; }

public bool StripDebugSymbols { get; set; }

/// <summary>
Expand Down Expand Up @@ -179,7 +184,7 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot

if (symbolsAdded > 0)
{
exportedAssemblies.Add(Path.GetFileNameWithoutExtension(compiledAssembly.Path));
exportedAssemblies.Add(Path.GetFileName(compiledAssembly.Path));
}
}
}
Expand Down Expand Up @@ -309,6 +314,11 @@ private string GenerateExtraDefinitions()
extraDefinitions.AppendLine("add_definitions(-DUSES_AOT_DATA=1)");
}

if (BundlesAssemblies)
{
extraDefinitions.AppendLine("add_definitions(-DBUNDLED_ASSEMBLIES=1)");
}

return extraDefinitions.ToString();
}

Expand Down