Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WasmApp.Native.targets « build « wasm « mono « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 317597e45dbf1f5e8ab86088a57997a6759c35eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<Project>
  <!-- not really meant to be used w/o WasmApp.targets -->

  <UsingTask TaskName="ManagedToNativeGenerator" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />
  <UsingTask TaskName="Microsoft.WebAssembly.Build.Tasks.EmccCompile" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />

  <PropertyGroup>
    <_WasmBuildNativeCoreDependsOn>
      _PrepareForWasmBuildNative;
      _GenerateDriverGenC;
      _GenerateManagedToNative;
      _WasmCompileNativeFiles;
      _WasmLinkDotNet;
      _CompleteWasmBuildNative
    </_WasmBuildNativeCoreDependsOn>

    <_BeforeWasmBuildAppDependsOn>
      $(_BeforeWasmBuildAppDependsOn);
      _SetupEmscripten;
      _SetWasmBuildNativeDefaults
    </_BeforeWasmBuildAppDependsOn>

    <_ExeExt Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">.exe</_ExeExt>
    <WasmUseEMSDK_PATH Condition="'$(WasmUseEMSDK_PATH)' == '' and '$(EMSDK_PATH)' != '' and Exists('$(MSBuildThisFileDirectory)WasmApp.InTree.targets')">true</WasmUseEMSDK_PATH>
  </PropertyGroup>

  <ItemGroup>
    <UpToDateCheckInput Include="@(NativeFileReference)" />
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' == 'Debug' and '@(_MonoComponent->Count())' == 0">
    <_MonoComponent Include="hot_reload;debugger" />
  </ItemGroup>

  <Import Project="$(MSBuildThisFileDirectory)EmSdkRepo.Defaults.props" Condition="'$(WasmUseEMSDK_PATH)' == 'true'" />

  <Target Name="_SetupEmscripten">
    <PropertyGroup>
      <_EMSDKMissingPaths Condition="'$(_EMSDKMissingPaths)' == '' and ('$(EmscriptenSdkToolsPath)' == '' or !Exists('$(EmscriptenSdkToolsPath)'))">%24(EmscriptenSdkToolsPath)=$(EmscriptenSdkToolsPath) </_EMSDKMissingPaths>
      <_EMSDKMissingPaths Condition="'$(_EMSDKMissingPaths)' == '' and ('$(EmscriptenNodeToolsPath)' == '' or !Exists('$(EmscriptenNodeToolsPath)'))">%24(EmscriptenNodeToolsPath)=$(EmscriptenNodeToolsPath) </_EMSDKMissingPaths>
      <_EMSDKMissingPaths Condition="'$(_EMSDKMissingPaths)' == '' and ('$(EmscriptenUpstreamBinPath)' == '' or !Exists('$(EmscriptenUpstreamBinPath)'))">%24(EmscriptenUpstreamBinPath)=$(EmscriptenUpstreamBinPath) </_EMSDKMissingPaths>
    </PropertyGroup>

    <PropertyGroup>
      <_EMSDKMissingErrorMessage Condition="'$(EMSDK_PATH)' == '' and '$(EmscriptenSdkToolsPath)' == ''">Could not find emscripten sdk. Either set %24(EMSDK_PATH), or use workloads to get the sdk.</_EMSDKMissingErrorMessage>

      <_EMSDKMissingErrorMessage Condition="'$(_EMSDKMissingErrorMessage)' == '' and '$(_UsingEMSDK_PATH)' != 'true' and '$(_EMSDKMissingPaths)' != ''">Emscripten from the workload is missing some paths: $(_EMSDKMissingPaths).</_EMSDKMissingErrorMessage>
      <_EMSDKMissingErrorMessage Condition="'$(_EMSDKMissingErrorMessage)' == '' and '$(_UsingEMSDK_PATH)' == 'true' and !Exists($(EMSDK_PATH))">Could not find Emscripten sdk at %24(EMSDK_PATH)=$(EMSDK_PATH) .</_EMSDKMissingErrorMessage>
      <_EMSDKMissingErrorMessage Condition="'$(_EMSDKMissingErrorMessage)' == '' and '$(_UsingEMSDK_PATH)' == 'true' and '$(_EMSDKMissingPaths)' != ''">Specified Emscripten sdk at %24(EMSDK_PATH)=$(EMSDK_PATH) is missing some paths: $(_EMSDKMissingPaths).</_EMSDKMissingErrorMessage>

      <_IsEMSDKMissing Condition="'$(_EMSDKMissingErrorMessage)' != ''">true</_IsEMSDKMissing>
    </PropertyGroup>

    <PropertyGroup>
      <EmscriptenSdkToolsPath    Condition="'$(EmscriptenSdkToolsPath)' != ''"   >$([MSBuild]::NormalizeDirectory($(EmscriptenSdkToolsPath)))</EmscriptenSdkToolsPath>
      <EmscriptenNodeToolsPath   Condition="'$(EmscriptenNodeToolsPath)' != ''"  >$([MSBuild]::NormalizeDirectory($(EmscriptenNodeToolsPath)))</EmscriptenNodeToolsPath>
      <EmscriptenUpstreamBinPath Condition="'$(EmscriptenUpstreamBinPath)' != ''">$([MSBuild]::NormalizeDirectory($(EmscriptenUpstreamBinPath)))</EmscriptenUpstreamBinPath>
    </PropertyGroup>

    <!-- Environment variables required for running emsdk commands like `emcc` -->
    <ItemGroup Condition="'$(EmscriptenSdkToolsPath)' != ''">
      <EmscriptenEnvVars Include="DOTNET_EMSCRIPTEN_LLVM_ROOT=$(EmscriptenSdkToolsPath)bin" />
      <EmscriptenEnvVars Include="DOTNET_EMSCRIPTEN_BINARYEN_ROOT=$(EmscriptenSdkToolsPath)" />
      <EmscriptenEnvVars Include="DOTNET_EMSCRIPTEN_NODE_JS=$([MSBuild]::NormalizePath($(EmscriptenNodeToolsPath), 'bin', 'node$(_ExeExt)'))" />
    </ItemGroup>

    <ItemGroup>
      <_EmscriptenPrependPATHTrimmed Include="$([MSBuild]::ValueOrDefault('%(EmscriptenPrependPATH.Identity)\', '').TrimEnd('\/'))" />
      <EmscriptenPrependPATH Remove="@(EmscriptenPrependPATH)" />
      <EmscriptenPrependPATH Include="@(_EmscriptenPrependPATHTrimmed)" />
    </ItemGroup>

    <PropertyGroup>
      <!-- semicolon is a msbuild property separator. It is also the path separator on windows.
           So, we need to escape it here, so the paths don't get split up when converting
           to string[] for passing to Exec task -->
      <_PathSeparator Condition="'$(OS)' == 'Windows_NT'">%3B</_PathSeparator>
      <_PathSeparator Condition="'$(OS)' != 'Windows_NT'">:</_PathSeparator>

      <_EmscriptenPrependPATHProperty>@(EmscriptenPrependPATH -> '%(Identity)', '$(_PathSeparator)')</_EmscriptenPrependPATHProperty>
    </PropertyGroup>

    <ItemGroup>
      <EmscriptenEnvVars Include="PATH=$(_EmscriptenPrependPATHProperty)$(_PathSeparator)$([MSBuild]::Escape($(PATH)))" />

      <!--
        Python defaults to the system charset, and thus expects the files it's reading to
        match that. But that might not always be true. Eg. system charset=gbk, failing to read
        utf-8 files

        See https://github.com/dotnet/runtime/issues/53367 for the motivating issue
      -->
      <EmscriptenEnvVars Include="PYTHONUTF8=1" />
    </ItemGroup>
  </Target>

  <Target Name="_SetWasmBuildNativeDefaults">
    <!-- if already set, maybe by a user projects, then a missing emsdk is an error -->
    <Error Condition="'$(WasmBuildNative)' == 'true' and '$(_IsEMSDKMissing)' == 'true'"
           Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for building native files." />

    <Error Condition="'$(RunAOTCompilation)' == 'true' and '$(_IsEMSDKMissing)' == 'true'"
           Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for AOT'ing assemblies." />

    <!-- When Building -->
    <PropertyGroup Condition="'$(WasmBuildingForNestedPublish)' != 'true'">
      <!-- build AOT, only if explicitly requested -->
      <WasmBuildNative Condition="'$(RunAOTCompilation)' == 'true' and '$(RunAOTCompilationAfterBuild)' == 'true'">true</WasmBuildNative>

      <WasmBuildNative Condition="'$(WasmBuildNative)' == '' and @(NativeFileReference->Count()) > 0" >true</WasmBuildNative>
      <!-- need to re-link dotnet.js when targeting ES6 -->
      <WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(WasmEnableES6)' == 'true'" >true</WasmBuildNative>

      <WasmBuildNative Condition="'$(WasmBuildNative)' == ''">false</WasmBuildNative>
    </PropertyGroup>

    <!-- When Publishing -->
    <PropertyGroup Condition="'$(WasmBuildingForNestedPublish)' == 'true'">
      <!-- AOT==true overrides WasmBuildNative -->
      <WasmBuildNative Condition="'$(RunAOTCompilation)' == 'true'">true</WasmBuildNative>
      <WasmBuildNative Condition="'$(WasmBuildNative)' == '' and @(NativeFileReference->Count()) > 0" >true</WasmBuildNative>
      <!-- need to re-link dotnet.js when targeting ES6 -->
      <WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(WasmEnableES6)' == 'true'" >true</WasmBuildNative>

      <!-- not aot, not trimmed app, no reason to relink -->
      <WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(PublishTrimmed)' != 'true'">false</WasmBuildNative>

      <!-- default to relinking in Release config -->
      <WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(Configuration)' == 'Release'">true</WasmBuildNative>

      <WasmBuildNative Condition="'$(WasmBuildNative)' == ''">false</WasmBuildNative>
    </PropertyGroup>

    <!-- If we want to default to true, and sdk is missing, then just warn, and set it to false -->
    <Warning Condition="'$(WasmBuildNative)' == 'true' and '$(_IsEMSDKMissing)' == 'true'"
             Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for building native files." />

    <PropertyGroup>
      <WasmBuildNative Condition="'$(WasmBuildNative)' == 'true' and '$(_IsEMSDKMissing)' == 'true'">false</WasmBuildNative>
    </PropertyGroup>
  </Target>

  <Target Name="_WasmBuildNativeCore" DependsOnTargets="$(_WasmBuildNativeCoreDependsOn)"  Condition="'$(WasmBuildNative)' == 'true'" />

  <Target Name="_PrepareForWasmBuildNative">
    <Error Condition="'$(_IsEMSDKMissing)' == 'true'"
           Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for building native files." />

    <PropertyGroup>
      <_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm'))</_MonoAotCrossCompilerPath>
      <_EmccDefaultFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-default.rsp'))</_EmccDefaultFlagsRsp>
      <_EmccDefaultLinkFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-link.rsp'))</_EmccDefaultLinkFlagsRsp>
      <WasmNativeStrip Condition="'$(WasmNativeStrip)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">false</WasmNativeStrip>
      <WasmNativeStrip Condition="'$(WasmNativeStrip)' == ''">true</WasmNativeStrip>
      <WasmNativeDebugSymbols Condition="'$(WasmNativeDebugSymbols)' == ''">true</WasmNativeDebugSymbols>
      <WasmLinkIcalls Condition="'$(WasmLinkIcalls)' == ''">$(WasmBuildNative)</WasmLinkIcalls>
      <!-- Temporarily `false`, till sdk gets a fix for supporting the new file -->
      <WasmEmitSymbolMap Condition="'$(WasmEmitSymbolMap)' == ''">false</WasmEmitSymbolMap>

      <_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h</_WasmICallTablePath>
      <_WasmRuntimeICallTablePath>$(_WasmIntermediateOutputPath)runtime-icall-table.h</_WasmRuntimeICallTablePath>
      <_WasmPInvokeTablePath>$(_WasmIntermediateOutputPath)pinvoke-table.h</_WasmPInvokeTablePath>
      <_WasmInterpToNativeTablePath>$(_WasmIntermediateOutputPath)wasm_m2n_invoke.g.h</_WasmInterpToNativeTablePath>
      <_WasmPInvokeHPath>$(_WasmRuntimePackIncludeDir)wasm\pinvoke.h</_WasmPInvokeHPath>
      <_DriverGenCPath>$(_WasmIntermediateOutputPath)driver-gen.c</_DriverGenCPath>
      <DisableParallelAot Condition="'$(DisableParallelAot)' == ''">false</DisableParallelAot>
      <DisableParallelEmccCompile Condition="'$(DisableParallelEmccCompile)' == ''">$(DisableParallelAot)</DisableParallelEmccCompile>

      <_DriverGenCNeeded Condition="'$(_DriverGenCNeeded)' == '' and '$(_WasmShouldAOT)' == 'true'">true</_DriverGenCNeeded>

      <_EmccAssertionLevelDefault Condition="'$(_EmccAssertionLevelDefault)' == ''">0</_EmccAssertionLevelDefault>
      <_EmccOptimizationFlagDefault Condition="'$(_WasmDevel)' == 'true'">-O0 -s ASSERTIONS=$(_EmccAssertionLevelDefault)</_EmccOptimizationFlagDefault>
      <_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">-O1</_EmccOptimizationFlagDefault>
      <_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == ''">-Oz</_EmccOptimizationFlagDefault>

      <EmccCompileOptimizationFlag Condition="'$(EmccCompileOptimizationFlag)' == ''">$(_EmccOptimizationFlagDefault)</EmccCompileOptimizationFlag>
      <EmccLinkOptimizationFlag    Condition="'$(EmccLinkOptimizationFlag)' == '' and '$(Configuration)' == 'Release'">-O2</EmccLinkOptimizationFlag>
      <EmccLinkOptimizationFlag    Condition="'$(EmccLinkOptimizationFlag)' == ''"   >$(EmccCompileOptimizationFlag)</EmccLinkOptimizationFlag>

      <_EmccCompileRsp>$(_WasmIntermediateOutputPath)emcc-compile.rsp</_EmccCompileRsp>
      <_EmccCompileOutputMessageImportance Condition="'$(EmccVerbose)' == 'true'">Normal</_EmccCompileOutputMessageImportance>
      <_EmccCompileOutputMessageImportance Condition="'$(EmccVerbose)' != 'true'">Low</_EmccCompileOutputMessageImportance>

      <_EmccCompileBitcodeRsp>$(_WasmIntermediateOutputPath)emcc-compile-bc.rsp</_EmccCompileBitcodeRsp>
      <_EmccLinkRsp>$(_WasmIntermediateOutputPath)emcc-link.rsp</_EmccLinkRsp>

      <EmccInitialHeapSize Condition="'$(EmccInitialHeapSize)' == ''">$(EmccTotalMemory)</EmccInitialHeapSize>
      <EmccInitialHeapSize Condition="'$(EmccInitialHeapSize)' == ''">536870912</EmccInitialHeapSize>
    </PropertyGroup>

    <ItemGroup>
      <_WasmLinkDependencies Remove="@(_WasmLinkDependencies)" />

      <_EmccCommonFlags Include="$(_DefaultEmccFlags)" />
      <_EmccCommonFlags Include="$(EmccFlags)" />
      <_EmccCommonFlags Include="-s EXPORT_ES6=1"                   Condition="'$(WasmEnableES6)' == 'true'" />
      <_EmccCommonFlags Include="-g"                                Condition="'$(WasmNativeStrip)' == 'false'" />
      <_EmccCommonFlags Include="-v"                                Condition="'$(EmccVerbose)' != 'false'" />
      <_EmccCommonFlags Include="-s DISABLE_EXCEPTION_CATCHING=0"   Condition="'$(WasmExceptionHandling)' == 'false'" />
      <_EmccCommonFlags Include="-fwasm-exceptions"                 Condition="'$(WasmExceptionHandling)' == 'true'" />

      <_EmccIncludePaths Include="$(_WasmIntermediateOutputPath.TrimEnd('\/'))" />
      <_EmccIncludePaths Include="$(_WasmRuntimePackIncludeDir)mono-2.0" />
      <_EmccIncludePaths Include="$(_WasmRuntimePackIncludeDir)wasm" />

      <!-- Adding optimization flag at the top, so it gets precedence -->
      <_EmccCFlags Include="$(EmccCompileOptimizationFlag)" />
      <_EmccCFlags Include="@(_EmccCommonFlags)" />

      <_EmccCFlags Include="-DDISABLE_PERFTRACING_LISTEN_PORTS=1" />
      <_EmccCFlags Include="-DENABLE_AOT=1"                    Condition="'$(_WasmShouldAOT)' == 'true'" />
      <_EmccCFlags Include="-DDRIVER_GEN=1"                    Condition="'$(_WasmShouldAOT)' == 'true'" />
      <_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1"       Condition="'$(InvariantGlobalization)' == 'true'" />
      <_EmccCFlags Include="-DLINK_ICALLS=1"                   Condition="'$(WasmLinkIcalls)' == 'true'" />
      <_EmccCFlags Include="-DENABLE_AOT_PROFILER=1"           Condition="'$(WasmProfilers)' == 'aot'" />
      <_EmccCFlags Include="-DCORE_BINDINGS" />
      <_EmccCFlags Include="-DGEN_PINVOKE=1" />
      <_EmccCFlags Include="-emit-llvm" />

      <_EmccCFlags Include="&quot;-I%(_EmccIncludePaths.Identity)&quot;" />
      <_EmccCFlags Include="-g" Condition="'$(WasmNativeDebugSymbols)' == 'true'" />

      <!-- Adding optimization flag at the top, so it gets precedence -->
      <_EmccLDFlags Include="$(EmccLinkOptimizationFlag)" />
      <_EmccLDFlags Include="@(_EmccCommonFlags)" />
      <_EmccLDFlags Include="-Wl,--allow-undefined" />
      <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" />

      <!-- ILLinker should have removed unused imports, so error for Publish -->
      <_EmccLDSFlags Include="-s ERROR_ON_UNDEFINED_SYMBOLS=0" Condition="'$(WasmBuildingForNestedPublish)' != 'true'" />

      <_DriverCDependencies Include="$(_WasmPInvokeHPath);$(_WasmICallTablePath)" />
      <_DriverCDependencies Include="$(_DriverGenCPath)" Condition="'$(_DriverGenCNeeded)' == 'true'" />

      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)pinvoke.c"
                               Dependencies="$(_WasmPInvokeHPath);$(_WasmPInvokeTablePath)" />
      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)driver.c"
                               Dependencies="@(_DriverCDependencies)" />
      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)corebindings.c" />

      <_WasmRuntimePackSrcFile ObjectFile="$(_WasmIntermediateOutputPath)%(FileName).o" />

      <!-- See src\mono\wasm\runtime\modularize-dotnet.md -->
      <JSFileType Include="extpre.js"  Kind="extern-pre-js" />
      <JSFileType Include="iffe.js"    Kind="extern-pre-js" />
      <JSFileType Include="pre.js"     Kind="pre-js" />
      <JSFileType Include="lib.js"     Kind="js-library" />
      <JSFileType Include="post.js"    Kind="post-js" />
      <JSFileType Include="extpost.js" Kind="extern-post-js" />

      <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\*.%(JSFileType.Identity)"     Kind="%(JSFileType.Kind)" />
      <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\cjs\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" Condition="'$(WasmEnableES6)' != 'true'" />
      <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\es6\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" Condition="'$(WasmEnableES6)' == 'true'" />

      <_WasmNativeFileForLinking Include="@(NativeFileReference)" />

      <EmscriptenEnvVars Include="EMSDK_PYTHON=$(EmscriptenPythonToolsPath)python.exe" Condition="'$(OS)' == 'Windows_NT'" />
      <EmscriptenEnvVars Include="PYTHONPATH=$(EmscriptenPythonToolsPath)" Condition="'$(OS)' == 'Windows_NT'" />
      <EmscriptenEnvVars Include="PYTHONHOME=" Condition="'$(OS)' == 'Windows_NT'" />
      <EmscriptenEnvVars Include="EM_CACHE=$(WasmCachePath)" Condition="'$(WasmCachePath)' != ''" />
    </ItemGroup>

    <Error Text="Could not find NativeFileReference %(NativeFileReference.Identity)" Condition="'%(NativeFileReference.Identity)' != '' and !Exists(%(NativeFileReference.Identity))" />
  </Target>

  <Target Name="_GenerateManagedToNative">
    <ItemGroup>
      <_WasmPInvokeModules Include="%(_WasmNativeFileForLinking.FileName)" Condition="'%(_WasmNativeFileForLinking.ScanForPInvokes)' != 'false'" />

      <_WasmPInvokeModules Include="libSystem.Native" />
      <_WasmPInvokeModules Include="libSystem.IO.Compression.Native" />
      <_WasmPInvokeModules Include="libSystem.Globalization.Native" />
      <_WasmPInvokeModules Include="libSystem.Security.Cryptography.Native.Browser" />
    </ItemGroup>
    
    <Error Condition="'$(_MonoAotCrossCompilerPath)' == '' or !Exists('$(_MonoAotCrossCompilerPath)')"
           Text="Could not find AOT cross compiler at %24(_MonoAotCrossCompilerPath)=$(_MonoAotCrossCompilerPath)" />

    <Exec Command='"$(_MonoAotCrossCompilerPath)" --print-icall-table > "$(_WasmRuntimeICallTablePath)"' />
    <ItemGroup>
      <FileWrites Include="$(_WasmRuntimeICallTablePath)" />
    </ItemGroup>

    <MakeDir Directories="$(_WasmIntermediateOutputPath)" Condition="!Exists('$(_WasmIntermediateOutputPath)')"  />

    <ManagedToNativeGenerator
      Assemblies="@(_WasmAssembliesInternal)"
      RuntimeIcallTableFile="$(_WasmRuntimeICallTablePath)"
      IcallOutputPath="$(_WasmICallTablePath)"
      PInvokeModules="@(_WasmPInvokeModules)"
      PInvokeOutputPath="$(_WasmPInvokeTablePath)"
      InterpToNativeOutputPath="$(_WasmInterpToNativeTablePath)">
      <Output TaskParameter="FileWrites" ItemName="FileWrites" />
    </ManagedToNativeGenerator>
  </Target>

  <Target Name="_WasmSelectRuntimeComponentsForLinking" Condition="'$(WasmNativeWorkload)' == 'true'" DependsOnTargets="_MonoSelectRuntimeComponents" />

  <Target Name="_WasmCompileNativeFiles" DependsOnTargets="_CheckEmccIsExpectedVersion">
    <PropertyGroup>
      <_EmBuilder Condition="$([MSBuild]::IsOSPlatform('WINDOWS'))">embuilder.bat</_EmBuilder>
      <_EmBuilder Condition="!$([MSBuild]::IsOSPlatform('WINDOWS'))">embuilder.py</_EmBuilder>
    </PropertyGroup>

    <ItemGroup>
      <_EmccCFlags Include="$(EmccExtraCFlags)" />
    </ItemGroup>

    <WriteLinesToFile Lines="@(_EmccCFlags)" File="$(_EmccCompileRsp)" Overwrite="true" WriteOnlyWhenDifferent="true" />
    <ItemGroup>
      <FileWrites Include="$(_EmccCompileRsp)" />
    </ItemGroup>

    <!-- warm up the cache -->
    <Exec Command="$(_EmBuilder) build MINIMAL" EnvironmentVariables="@(EmscriptenEnvVars)" StandardOutputImportance="Low" StandardErrorImportance="Low" />

    <Message Text="Compiling native assets with emcc with $(EmccCompileOptimizationFlag). This may take a while ..." Importance="High" />
    <ItemGroup>
      <_WasmSourceFileToCompile Remove="@(_WasmSourceFileToCompile)" />
      <_WasmSourceFileToCompile Include="@(_WasmRuntimePackSrcFile)" Dependencies="%(_WasmRuntimePackSrcFile.Dependencies);$(_EmccDefaultFlagsRsp);$(_EmccCompileRsp)" />
    </ItemGroup>
    <EmccCompile
          SourceFiles="@(_WasmSourceFileToCompile)"
          Arguments='"@$(_EmccDefaultFlagsRsp)" "@$(_EmccCompileRsp)"'
          EnvironmentVariables="@(EmscriptenEnvVars)"
          DisableParallelCompile="$(DisableParallelEmccCompile)"
          OutputMessageImportance="$(_EmccCompileOutputMessageImportance)">
      <Output TaskParameter="OutputFiles" ItemName="FileWrites" />
    </EmccCompile>
  </Target>

  <Target Name="_WasmCompileAssemblyBitCodeFilesForAOT"
          Inputs="@(_BitcodeFile);$(_EmccDefaultFlagsRsp);$(_EmccCompileBitcodeRsp)"
          Outputs="@(_BitcodeFile->'%(ObjectFile)')"
          Condition="'$(_WasmShouldAOT)' == 'true' and @(_BitcodeFile->Count()) > 0"
          DependsOnTargets="_CheckEmccIsExpectedVersion;_WasmWriteRspForCompilingBitcode"
          Returns="@(FileWrites)">

    <ItemGroup>
      <_BitCodeFile Dependencies="%(_BitCodeFile.Dependencies);$(_EmccDefaultFlagsRsp);$(_EmccCompileBitcodeRsp)" />
    </ItemGroup>

    <Message Text="Compiling assembly bitcode files with $(EmccLinkOptimizationFlag) ..." Importance="High" Condition="@(_BitCodeFile->Count()) > 0" />
    <EmccCompile
          SourceFiles="@(_BitCodeFile)"
          Arguments="&quot;@$(_EmccDefaultFlagsRsp)&quot; &quot;@$(_EmccCompileBitcodeRsp)&quot;"
          EnvironmentVariables="@(EmscriptenEnvVars)"
          DisableParallelCompile="$(DisableParallelEmccCompile)"
          OutputMessageImportance="$(_EmccCompileOutputMessageImportance)">
      <Output TaskParameter="OutputFiles" ItemName="FileWrites" />
    </EmccCompile>
  </Target>

  <Target Name="_WasmWriteRspForCompilingBitcode">
    <ItemGroup>
      <_BitcodeLDFlags Include="@(_EmccLDFlags)" />
      <_BitcodeLDFlags Include="$(EmccExtraBitcodeLDFlags)" />
    </ItemGroup>
    <WriteLinesToFile Lines="@(_BitcodeLDFlags)" File="$(_EmccCompileBitcodeRsp)" Overwrite="true" WriteOnlyWhenDifferent="true" />
    <ItemGroup>
      <FileWrites Include="$(_EmccCompileBitcodeRsp)" />
    </ItemGroup>
  </Target>

  <Target Name="_WasmWriteRspFilesForLinking">
	<PropertyGroup>
      <_WasmEHLib Condition="'$(WasmExceptionHandling)' == 'true'">libmono-wasm-eh-wasm.a</_WasmEHLib>
      <_WasmEHLib Condition="'$(WasmExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>
      <_WasmEHLibToExclude Condition="'$(WasmExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>
      <_WasmEHLibToExclude Condition="'$(WasmExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>
	</PropertyGroup>
    <ItemGroup>
      <!-- order matters -->
      <_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" />
      <_WasmNativeFileForLinking Include="%(_WasmSourceFileToCompile.ObjectFile)" />

      <_WasmNativeFileForLinking
          Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)*.a"
          Exclude="@(_MonoRuntimeComponentDontLink->'$(MicrosoftNetCoreAppRuntimePackRidNativeDir)%(Identity)')" />
      <_WasmNativeFileForLinking Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLib)" />
      <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLibToExclude)" />

      <_WasmExtraJSFile Include="@(Content)" Condition="'%(Content.Extension)' == '.js'" />

      <_EmccLinkStepArgs Include="@(_EmccLDFlags)" />
      <_EmccLinkStepArgs Include="@(_EmccLDSFlags)" />
      <_EmccLinkStepArgs Include="--emit-symbol-map" Condition="'$(WasmEmitSymbolMap)' == 'true'" />

      <_EmccLinkStepArgs Include="--%(_WasmExtraJSFile.Kind) &quot;%(_WasmExtraJSFile.Identity)&quot;" Condition="'%(_WasmExtraJSFile.Kind)' != ''" />
      <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" />

      <_EmccLinkStepArgs Include="&quot;%(_WasmNativeFileForLinking.Identity)&quot;" />
      <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" />

      <_EmccLinkStepArgs Include="-o &quot;$(_WasmIntermediateOutputPath)dotnet.js&quot;" />
      <_WasmLinkDependencies Include="$(_EmccLinkRsp)" />

      <_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" />
    </ItemGroup>

    <WriteLinesToFile Lines="@(_EmccLinkStepArgs)" File="$(_EmccLinkRsp)" Overwrite="true" WriteOnlyWhenDifferent="true" />
    <ItemGroup>
      <FileWrites Include="$(_EmccLinkRsp)" />
    </ItemGroup>
  </Target>

  <Target Name="_WasmLinkDotNet"
          Inputs="@(_WasmLinkDependencies);$(_EmccDefaultFlagsRsp);$(_EmccDefaultLinkFlagsRsp);$(_EmccLinkRsp)"
          Outputs="$(_WasmIntermediateOutputPath)dotnet.js;$(_WasmIntermediateOutputPath)dotnet.wasm"
          DependsOnTargets="_CheckEmccIsExpectedVersion;_WasmSelectRuntimeComponentsForLinking;_WasmCompileAssemblyBitCodeFilesForAOT;_WasmWriteRspFilesForLinking"
          Returns="@(FileWrites)" >

    <Message Text="Linking with emcc with $(EmccLinkOptimizationFlag). This may take a while ..." Importance="High" />
    <Message Text="Running emcc with @(_EmccLinkStepArgs->'%(Identity)', ' ')" Importance="Low" />
    <Exec Command='emcc "@$(_EmccDefaultFlagsRsp)" "@$(_EmccDefaultLinkFlagsRsp)" "@$(_EmccLinkRsp)"' EnvironmentVariables="@(EmscriptenEnvVars)" />
    <ItemGroup>
      <FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.wasm" />
      <FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.js" />
      <FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.js.symbols" Condition="'$(WasmEmitSymbolMap)' == 'true'" />
    </ItemGroup>

    <Message Text="Stripping symbols from dotnet.wasm ..." Importance="High" Condition="'$(WasmNativeStrip)' == 'true'" />
    <Exec Command='wasm-opt$(_ExeExt) --enable-exception-handling --strip-dwarf "$(_WasmIntermediateOutputPath)dotnet.wasm" -o "$(_WasmIntermediateOutputPath)dotnet.wasm"'
          Condition="'$(WasmNativeStrip)' == 'true'"
          IgnoreStandardErrorWarningFormat="true"
          EnvironmentVariables="@(EmscriptenEnvVars)" />
  </Target>

  <Target Name="_CompleteWasmBuildNative">
    <ItemGroup>
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.wasm" />
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.js" />
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.worker.js" Condition="Exists('$(_WasmIntermediateOutputPath)dotnet.worker.js')" />
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.js.symbols" Condition="'$(WasmEmitSymbolMap)' == 'true' and Exists('$(_WasmIntermediateOutputPath)dotnet.js.symbols')" />
    </ItemGroup>
  </Target>

  <Target Name="_GenerateDriverGenC" Condition="'$(_WasmShouldAOT)' != 'true'">
  </Target>

  <Target Name="_CheckEmccIsExpectedVersion">
    <Error Condition="'$(_IsEMSDKMissing)' == 'true'"
           Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for building native files." />

    <ReadEmccProps JsonFilePath="$(_WasmRuntimePackSrcDir)emcc-props.json">
      <Output TaskParameter="EmccProperties" ItemName="_EmccPropItems" />
    </ReadEmccProps>

    <CreateProperty Value="%(_EmccPropItems.Value)">
      <Output TaskParameter="Value" PropertyName="%(_EmccPropItems.Identity)" />
    </CreateProperty>

    <Error Condition="'$(RuntimeEmccVersionRaw)' == ''"
           Text="%24(RuntimeEmccVersionRaw) is not set. '$(_EmccPropsPath)' should have set that."/>

    <PropertyGroup>
      <_EmccVersionCommand>emcc --version</_EmccVersionCommand>
    </PropertyGroup>

    <Exec Command="$(_EmccVersionCommand)" WorkingDirectory="$(_WasmIntermediateOutputPath)" EnvironmentVariables="@(EmscriptenEnvVars)" ConsoleToMsBuild="true" StandardOutputImportance="Low" IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" ItemName="_VersionLines" />
      <Output TaskParameter="ExitCode" PropertyName="_EmccVersionExitCode" />
    </Exec>

    <!-- If `emcc -version` failed, then run it again, so we can surface the output as *Errors*. This allows the errors to show up correctly,
         versus trying to use the output lines with the Error task -->
    <Exec Condition="$(_EmccVersionExitCode) != '0'"
          Command="$(_EmccVersionCommand)"
          WorkingDirectory="$(_WasmIntermediateOutputPath)"
          EnvironmentVariables="@(EmscriptenEnvVars)"
          CustomErrorRegularExpression=".*"
          />

    <!-- we want to get the first line from the output, which has the version.
         Rest of the lines are the license -->
    <ItemGroup>
      <_ReversedVersionLines Include="@(_VersionLines->Reverse())" />
    </ItemGroup>
    <PropertyGroup>
      <ActualEmccVersionRaw>%(_ReversedVersionLines.Identity)</ActualEmccVersionRaw>
      <_VersionMismatchMessage>Emscripten version mismatch. The runtime pack in $(MicrosoftNetCoreAppRuntimePackDir) expects '$(RuntimeEmccVersionRaw)', but emcc being used has version '$(ActualEmccVersionRaw)'. This might cause build failures.</_VersionMismatchMessage>
    </PropertyGroup>

    <Warning Condition="'$(RuntimeEmccVersionRaw)' != '$(ActualEmccVersionRaw)' and '$(_WasmStrictVersionMatch)' != 'true'"
             Text="$(_VersionMismatchMessage)" />
    <Error Condition="'$(RuntimeEmccVersionRaw)' != '$(ActualEmccVersionRaw)' and '$(_WasmStrictVersionMatch)' == 'true'"
           Text="$(_VersionMismatchMessage)" />
  </Target>

  <!--
      *******************************
      ************ AOT **************
      *******************************
  -->

  <Target Name="_WasmAotCompileApp" Condition="'$(_WasmShouldAOT)' == 'true'">
    <PropertyGroup>
      <!-- FIXME: do it once -->
      <_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm'))</_MonoAotCrossCompilerPath>
    </PropertyGroup>

    <Error Condition="'@(_WasmAssembliesInternal)' == ''" Text="Item _WasmAssembliesInternal is empty" />
    <Error Condition="'$(_IsEMSDKMissing)' == 'true'"
           Text="$(_EMSDKMissingErrorMessage) Emscripten SDK is required for AOT'ing assemblies." />
    <Error Condition="'$(_MonoAotCrossCompilerPath)' == '' or !Exists('$(_MonoAotCrossCompilerPath)')"
           Text="Could not find AOT cross compiler at %24(_MonoAotCrossCompilerPath)=$(_MonoAotCrossCompilerPath)" />

    <ItemGroup>
      <MonoAOTCompilerDefaultAotArguments Include="no-opt" />
      <MonoAOTCompilerDefaultAotArguments Include="static" />
      <MonoAOTCompilerDefaultAotArguments Include="direct-icalls" />
      <MonoAOTCompilerDefaultAotArguments Include="deterministic" />
      <MonoAOTCompilerDefaultProcessArguments Include="--wasm-exceptions" Condition="'$(WasmExceptionHandling)' == 'true'" />
      <AotProfilePath Include="$(WasmAotProfilePath)"/>
    </ItemGroup>
    <ItemGroup>
      <_AotInputAssemblies Include="@(_WasmAssembliesInternal)">
        <AotArguments>@(MonoAOTCompilerDefaultAotArguments, ';')</AotArguments>
        <ProcessArguments>@(MonoAOTCompilerDefaultProcessArguments, ';')</ProcessArguments>
      </_AotInputAssemblies>

      <_WasmAssembliesInternal Remove="@(_WasmAssembliesInternal)" />

      <_WasmAOTSearchPaths Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)" />
      <_WasmAOTSearchPaths Include="$(MicrosoftNetCoreAppRuntimePackRidDir)\lib\net7.0" />
    </ItemGroup>

    <ItemGroup>
      <!-- Skip known-huge assemblies specified in csproj -->
      <_AotInputAssemblies Condition="'%(Filename)' != '' and '@(_AotInputAssemblies->Metadata(`Filename`))' != '' and '@(_AOT_InternalForceInterpretAssemblies->Metadata(`Filename`))' != ''">
        <AOT_InternalForceToInterpret>true</AOT_InternalForceToInterpret>
      </_AotInputAssemblies>
    </ItemGroup>

    <PropertyGroup>
      <!--<AOTMode Condition="'$(AOTMode)' == '' and '$(AOTProfilePath)' != ''">LLVMOnlyInterp</AOTMode>-->
      <AOTMode Condition="'$(AOTMode)' == ''">LLVMOnlyInterp</AOTMode>
      <_AOTCompilerCacheFile>$(_WasmIntermediateOutputPath)aot_compiler_cache.json</_AOTCompilerCacheFile>
    </PropertyGroup>

    <Message Text="AOT'ing @(_AotInputAssemblies->Count()) assemblies" Importance="High" />

    <!-- Dedup -->
    <PropertyGroup Condition="'$(WasmDedup)' == 'true'">
      <_WasmDedupAssembly>$(_WasmIntermediateOutputPath)\aot-instances.dll</_WasmDedupAssembly>
    </PropertyGroup>
    <WriteLinesToFile Condition="'$(WasmDedup)' == 'true'" File="$(_WasmIntermediateOutputPath)/aot-instances.cs" Overwrite="true" Lines="" WriteOnlyWhenDifferent="true" />
    <Csc
      Condition="'$(WasmDedup)' == 'true'"
      Sources="$(_WasmIntermediateOutputPath)\aot-instances.cs"
      OutputAssembly="$(_WasmDedupAssembly)"
      TargetType="library"
      Deterministic="true"
      References="@(ReferencePath)"
      ToolExe="$(CscToolExe)"
      ToolPath="$(CscToolPath)" />
    <ItemGroup Condition="'$(WasmDedup)' == 'true'">
      <_AotInputAssemblies Include="$(_WasmDedupAssembly)">
        <AotArguments>@(MonoAOTCompilerDefaultAotArguments, ';')</AotArguments>
        <ProcessArguments>@(MonoAOTCompilerDefaultProcessArguments, ';')</ProcessArguments>
      </_AotInputAssemblies>
    </ItemGroup>

    <MonoAOTCompiler
      CompilerBinaryPath="$(_MonoAotCrossCompilerPath)"
      OutputDir="$(_WasmIntermediateOutputPath)"
      Mode="$(AOTMode)"
      OutputType="AsmOnly"
      Assemblies="@(_AotInputAssemblies)"
      AdditionalAssemblySearchPaths="@(_WasmAOTSearchPaths)"
      UseAotDataFile="false"
      AOTProfilePath="$(AOTProfilePath)"
      AotModulesTablePath="$(_DriverGenCPath)"
      UseLLVM="true"
      DisableParallelAot="$(DisableParallelAot)"
      DedupAssembly="$(_WasmDedupAssembly)"
      CacheFilePath="$(_AOTCompilerCacheFile)"
      LLVMDebug="dwarfdebug"
      LLVMPath="$(EmscriptenUpstreamBinPath)"
      IntermediateOutputPath="$(_WasmIntermediateOutputPath)"
      AotProfilePath="@(AotProfilePath)">

      <Output TaskParameter="CompiledAssemblies" ItemName="_WasmAssembliesInternal" />
      <Output TaskParameter="FileWrites" ItemName="FileWrites" />
    </MonoAOTCompiler>

    <ItemGroup>
      <_BitcodeFile Include="%(_WasmAssembliesInternal.LlvmBitcodeFile)" />
      <_BitcodeFile ObjectFile="$(_WasmIntermediateOutputPath)%(FileName).o" />

      <!-- Add the skipped assemblies -->
      <_WasmAssembliesInternal Include="@(_AotInputAssemblies->WithMetadataValue('AOT_InternalForceToInterpret', 'true'))" />
    </ItemGroup>
  </Target>

  <!-- '$(ArchiveTests)' != 'true' is to skip on CI for now -->
  <Target Name="_WasmStripAOTAssemblies" Condition="'$(_WasmShouldAOT)' == 'true' and '$(WasmStripAOTAssemblies)' == 'true' and '$(AOTMode)' != 'LLVMOnlyInterp' and '$(ArchiveTests)' != 'true'">
    <PropertyGroup>
      <_WasmStrippedAssembliesPath>$([MSBuild]::NormalizeDirectory($(_WasmIntermediateOutputPath), 'stripped-assemblies'))</_WasmStrippedAssembliesPath>
    </PropertyGroup>

    <ItemGroup>
      <_AOTedAssemblies Include="@(_WasmAssembliesInternal)" />
      <_WasmStrippedAssemblies
              Include="@(_AOTedAssemblies)"
              OriginalPath="%(_WasmAssembliesInternal.Identity)" />
    </ItemGroup>

    <!-- Run mono-cil-strip on the assemblies -->
    <!-- TODO: replace this with a linker task -->
    <MakeDir Directories="$(_WasmStrippedAssembliesPath)" />
    <Exec Command='mono-cil-strip -q "%(_WasmStrippedAssemblies.OriginalPath)" "%(_WasmStrippedAssemblies.Identity)"' />

    <ItemGroup>
      <_WasmAssembliesInternal Remove="@(_WasmAssembliesInternal)" />
      <_WasmAssembliesInternal Include="@(_WasmStrippedAssemblies)" />
    </ItemGroup>
  </Target>

  <UsingTask TaskName="ReadEmccProps" AssemblyFile="$(MonoTargetsTasksAssemblyPath)"
             TaskFactory="JsonToItemsTaskFactory.JsonToItemsTaskFactory">
    <ParameterGroup>
      <EmccProperties ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
    </ParameterGroup>
  </UsingTask>
</Project>