[wasm][sdk][nuget] Bump version. (#19483)
[mono-project.git] / sdks / wasm / packager.cs
blob78ba71df87302e3fe8dcbf2bbc4a592f4764c1d7
1 using System;
2 using System.Linq;
3 using System.IO;
4 using System.Collections.Generic;
5 using Mono.Cecil;
6 using Mono.Options;
7 using Mono.Cecil.Cil;
9 //
10 // Google V8 style options:
11 // - bool: --foo/--no-foo
14 enum FlagType {
15 BoolFlag,
18 // 'Option' is already used by Mono.Options
19 class Flag {
20 public Flag (string name, string desc, FlagType type) {
21 Name = name;
22 FlagType = type;
23 Description = desc;
26 public string Name {
27 get; set;
30 public FlagType FlagType {
31 get; set;
34 public string Description {
35 get; set;
39 class BoolFlag : Flag {
40 public BoolFlag (string name, string description, bool def_value, Action<bool> action) : base (name, description, FlagType.BoolFlag) {
41 Setter = action;
42 DefaultValue = def_value;
45 public Action<bool> Setter {
46 get; set;
49 public bool DefaultValue {
50 get; set;
54 class Driver {
55 static bool enable_debug, enable_linker;
56 static string app_prefix, framework_prefix, bcl_tools_prefix, bcl_facades_prefix, out_prefix;
57 static List<string> bcl_prefixes;
58 static HashSet<string> asm_map = new HashSet<string> ();
59 static List<string> file_list = new List<string> ();
60 static HashSet<string> assemblies_with_dbg_info = new HashSet<string> ();
61 static List<string> root_search_paths = new List<string>();
63 const string BINDINGS_ASM_NAME = "WebAssembly.Bindings";
64 const string BINDINGS_RUNTIME_CLASS_NAME = "WebAssembly.Runtime";
65 const string HTTP_ASM_NAME = "System.Net.Http.WebAssemblyHttpHandler";
66 const string WEBSOCKETS_ASM_NAME = "WebAssembly.Net.WebSockets";
67 const string BINDINGS_MODULE = "corebindings.o";
68 const string BINDINGS_MODULE_SUPPORT = "$tool_prefix/src/binding_support.js";
70 class AssemblyData {
71 // Assembly name
72 public string name;
73 // Base filename
74 public string filename;
75 // Path outside build tree
76 public string src_path;
77 // Path of .bc file
78 public string bc_path;
79 // Path of the wasm object file
80 public string o_path;
81 // Path in appdir
82 public string app_path;
83 // Path of the AOT depfile
84 public string aot_depfile_path;
85 // Linker input path
86 public string linkin_path;
87 // Linker output path
88 public string linkout_path;
89 // AOT input path
90 public string aotin_path;
91 // Final output path after IL strip
92 public string final_path;
93 // Whenever to AOT this assembly
94 public bool aot;
97 static List<AssemblyData> assemblies = new List<AssemblyData> ();
99 enum AssemblyKind {
100 User,
101 Framework,
102 Bcl,
103 None,
106 void AddFlag (OptionSet options, Flag flag) {
107 if (flag is BoolFlag) {
108 options.Add (flag.Name, s => (flag as BoolFlag).Setter (true));
109 options.Add ("no-" + flag.Name, s => (flag as BoolFlag).Setter (false));
111 option_list.Add (flag);
114 static List<Flag> option_list = new List<Flag> ();
116 static void Usage () {
117 Console.WriteLine ("Usage: packager.exe <options> <assemblies>");
118 Console.WriteLine ("Valid options:");
119 Console.WriteLine ("\t--help Show this help message");
120 Console.WriteLine ("\t--debugrt Use the debug runtime (default release) - this has nothing to do with C# debugging");
121 Console.WriteLine ("\t--aot Enable AOT mode");
122 Console.WriteLine ("\t--aot-interp Enable AOT+INTERP mode");
123 Console.WriteLine ("\t--prefix=x Set the input assembly prefix to 'x' (default to the current directory)");
124 Console.WriteLine ("\t--out=x Set the output directory to 'x' (default to the current directory)");
125 Console.WriteLine ("\t--mono-sdkdir=x Set the mono sdk directory to 'x'");
126 Console.WriteLine ("\t--deploy=x Set the deploy prefix to 'x' (default to 'managed')");
127 Console.WriteLine ("\t--vfs=x Set the VFS prefix to 'x' (default to 'managed')");
128 Console.WriteLine ("\t--template=x Set the template name to 'x' (default to 'runtime.js')");
129 Console.WriteLine ("\t--asset=x Add specified asset 'x' to list of assets to be copied");
130 Console.WriteLine ("\t--search-path=x Add specified path 'x' to list of paths used to resolve assemblies");
131 Console.WriteLine ("\t--copy=always|ifnewer Set the type of copy to perform.");
132 Console.WriteLine ("\t\t 'always' overwrites the file if it exists.");
133 Console.WriteLine ("\t\t 'ifnewer' copies or overwrites the file if modified or size is different.");
134 Console.WriteLine ("\t--profile=x Enable the 'x' mono profiler.");
135 Console.WriteLine ("\t--aot-assemblies=x List of assemblies to AOT in AOT+INTERP mode.");
136 Console.WriteLine ("\t--aot-profile=x Use 'x' as the AOT profile.");
137 Console.WriteLine ("\t--link-mode=sdkonly|all Set the link type used for AOT. (EXPERIMENTAL)");
138 Console.WriteLine ("\t\t 'sdkonly' only link the Core libraries.");
139 Console.WriteLine ("\t\t 'all' link Core and User assemblies. (default)");
140 Console.WriteLine ("\t--pinvoke-libs=x DllImport libraries used.");
141 Console.WriteLine ("\t--native-lib=x Link the native library 'x' into the final executable.");
142 Console.WriteLine ("\t--preload-file=x Preloads the file or directory 'x' into the virtual filesystem.");
143 Console.WriteLine ("\t--embed-file=x Embeds the file or directory 'x' into the virtual filesystem.");
145 Console.WriteLine ("foo.dll Include foo.dll as one of the root assemblies");
146 Console.WriteLine ();
148 Console.WriteLine ("Additional options (--option/--no-option):");
149 foreach (var flag in option_list) {
150 if (flag is BoolFlag) {
151 Console.WriteLine (" --" + flag.Name + " (" + flag.Description + ")");
152 Console.WriteLine (" type: bool default: " + ((flag as BoolFlag).DefaultValue ? "true" : "false"));
157 static void Debug (string s) {
158 Console.WriteLine (s);
161 static string FindFrameworkAssembly (string asm) {
162 return asm;
165 static bool Try (string prefix, string name, out string out_res) {
166 out_res = null;
168 string res = (Path.Combine (prefix, name));
169 if (File.Exists (res)) {
170 out_res = Path.GetFullPath (res);
171 return true;
173 return false;
176 static string ResolveWithExtension (string prefix, string name) {
177 string res = null;
179 if (Try (prefix, name, out res))
180 return res;
181 if (Try (prefix, name + ".dll", out res))
182 return res;
183 if (Try (prefix, name + ".exe", out res))
184 return res;
185 return null;
188 static string ResolveUser (string asm_name) {
189 return ResolveWithExtension (app_prefix, asm_name);
192 static string ResolveFramework (string asm_name) {
193 return ResolveWithExtension (framework_prefix, asm_name);
196 static string ResolveBcl (string asm_name) {
197 foreach (var prefix in bcl_prefixes) {
198 string res = ResolveWithExtension (prefix, asm_name);
199 if (res != null)
200 return res;
202 return null;
205 static string ResolveBclFacade (string asm_name) {
206 return ResolveWithExtension (bcl_facades_prefix, asm_name);
209 static string Resolve (string asm_name, out AssemblyKind kind) {
210 kind = AssemblyKind.User;
211 var asm = ResolveUser (asm_name);
212 if (asm != null)
213 return asm;
215 kind = AssemblyKind.Framework;
216 asm = ResolveFramework (asm_name);
217 if (asm != null)
218 return asm;
220 kind = AssemblyKind.Bcl;
221 asm = ResolveBcl (asm_name);
222 if (asm == null)
223 asm = ResolveBclFacade (asm_name);
224 if (asm != null)
225 return asm;
227 kind = AssemblyKind.None;
228 throw new Exception ($"Could not resolve {asm_name}");
231 static bool is_sdk_assembly (string filename) {
232 foreach (var prefix in bcl_prefixes)
233 if (filename.StartsWith (prefix))
234 return true;
235 return false;
238 static void Import (string ra, AssemblyKind kind) {
239 if (!asm_map.Add (Path.GetFullPath (ra)))
240 return;
241 ReaderParameters rp = new ReaderParameters();
242 bool add_pdb = enable_debug && File.Exists (Path.ChangeExtension (ra, "pdb"));
243 if (add_pdb) {
244 rp.ReadSymbols = true;
245 // Facades do not have symbols
246 rp.ThrowIfSymbolsAreNotMatching = false;
247 rp.SymbolReaderProvider = new DefaultSymbolReaderProvider(false);
250 var resolver = new DefaultAssemblyResolver();
251 root_search_paths.ForEach(resolver.AddSearchDirectory);
252 foreach (var prefix in bcl_prefixes)
253 resolver.AddSearchDirectory (prefix);
254 resolver.AddSearchDirectory(bcl_facades_prefix);
255 resolver.AddSearchDirectory(framework_prefix);
256 rp.AssemblyResolver = resolver;
258 rp.InMemory = true;
259 var image = ModuleDefinition.ReadModule (ra, rp);
260 file_list.Add (ra);
261 //Debug ($"Processing {ra} debug {add_pdb}");
263 var data = new AssemblyData () { name = image.Assembly.Name.Name, src_path = ra };
264 assemblies.Add (data);
266 if (add_pdb && (kind == AssemblyKind.User || kind == AssemblyKind.Framework)) {
267 var pdb_path = Path.ChangeExtension (Path.GetFullPath (ra), "pdb");
268 file_list.Add (pdb_path);
269 assemblies_with_dbg_info.Add (pdb_path);
272 var parent_kind = kind;
274 foreach (var ar in image.AssemblyReferences) {
275 // Resolve using root search paths first
276 AssemblyDefinition resolved = null;
277 try {
278 resolved = image.AssemblyResolver.Resolve(ar, rp);
279 } catch {
282 if (resolved == null && is_sdk_assembly (ra))
283 // FIXME: netcore assemblies have missing references
284 continue;
286 if (resolved != null) {
287 Import (resolved.MainModule.FileName, parent_kind);
288 } else {
289 var resolve = Resolve (ar.Name, out kind);
290 Import(resolve, kind);
295 void GenDriver (string builddir, List<string> profilers, ExecMode ee_mode, bool link_icalls) {
296 var symbols = new List<string> ();
297 foreach (var adata in assemblies) {
298 if (adata.aot)
299 symbols.Add (String.Format ("mono_aot_module_{0}_info", adata.name.Replace ('.', '_').Replace ('-', '_')));
302 var w = File.CreateText (Path.Combine (builddir, "driver-gen.c.in"));
304 foreach (var symbol in symbols) {
305 w.WriteLine ($"extern void *{symbol};");
308 w.WriteLine ("static void register_aot_modules ()");
309 w.WriteLine ("{");
310 foreach (var symbol in symbols)
311 w.WriteLine ($"\tmono_aot_register_module ({symbol});");
312 w.WriteLine ("}");
314 foreach (var profiler in profilers) {
315 w.WriteLine ($"void mono_profiler_init_{profiler} (const char *desc);");
316 w.WriteLine ("EMSCRIPTEN_KEEPALIVE void mono_wasm_load_profiler_" + profiler + " (const char *desc) { mono_profiler_init_" + profiler + " (desc); }");
319 switch (ee_mode) {
320 case ExecMode.AotInterp:
321 w.WriteLine ("#define EE_MODE_LLVMONLY_INTERP 1");
322 break;
323 case ExecMode.Aot:
324 w.WriteLine ("#define EE_MODE_LLVMONLY 1");
325 break;
326 default:
327 break;
330 if (link_icalls)
331 w.WriteLine ("#define LINK_ICALLS 1");
333 w.Close ();
336 public static int Main (string[] args) {
337 return new Driver ().Run (args);
340 enum CopyType
342 Default,
343 Always,
344 IfNewer
347 enum ExecMode {
348 Interp = 1,
349 Aot = 2,
350 AotInterp = 3
353 enum LinkMode
355 SdkOnly,
359 class WasmOptions {
360 public bool Debug;
361 public bool DebugRuntime;
362 public bool AddBinding;
363 public bool Linker;
364 public bool LinkIcalls;
365 public bool ILStrip;
366 public bool LinkerVerbose;
367 public bool EnableZLib;
368 public bool EnableFS;
369 public bool EnableThreads;
370 public bool NativeStrip;
371 public bool Simd;
372 public bool EnableDynamicRuntime;
373 public bool LinkerExcludeDeserialization;
376 int Run (string[] args) {
377 var add_binding = true;
378 var root_assemblies = new List<string> ();
379 enable_debug = false;
380 string builddir = null;
381 string sdkdir = null;
382 string emscripten_sdkdir = null;
383 var aot_assemblies = "";
384 app_prefix = Environment.CurrentDirectory;
385 var deploy_prefix = "managed";
386 var vfs_prefix = "managed";
387 var use_release_runtime = true;
388 var enable_aot = false;
389 var enable_dedup = true;
390 var print_usage = false;
391 var emit_ninja = false;
392 bool build_wasm = false;
393 bool enable_lto = false;
394 bool link_icalls = false;
395 bool gen_pinvoke = false;
396 bool enable_zlib = false;
397 bool enable_fs = false;
398 bool enable_threads = false;
399 bool enable_dynamic_runtime = false;
400 bool is_netcore = false;
401 bool enable_simd = false;
402 var il_strip = false;
403 var linker_verbose = false;
404 var runtimeTemplate = "runtime.js";
405 var assets = new List<string> ();
406 var profilers = new List<string> ();
407 var native_libs = new List<string> ();
408 var preload_files = new List<string> ();
409 var embed_files = new List<string> ();
410 var pinvoke_libs = "";
411 var copyTypeParm = "default";
412 var copyType = CopyType.Default;
413 var ee_mode = ExecMode.Interp;
414 var linkModeParm = "all";
415 var linkMode = LinkMode.All;
416 var linkDescriptor = "";
417 var framework = "";
418 var netcore_sdkdir = "";
419 string coremode, usermode;
420 string aot_profile = null;
421 string wasm_runtime_path = null;
423 var opts = new WasmOptions () {
424 AddBinding = true,
425 Debug = false,
426 DebugRuntime = false,
427 Linker = false,
428 ILStrip = true,
429 LinkerVerbose = false,
430 EnableZLib = false,
431 EnableFS = false,
432 NativeStrip = true,
433 Simd = false,
434 EnableDynamicRuntime = false,
435 LinkerExcludeDeserialization = true
438 var p = new OptionSet () {
439 { "nobinding", s => opts.AddBinding = false },
440 { "out=", s => out_prefix = s },
441 { "appdir=", s => out_prefix = s },
442 { "builddir=", s => builddir = s },
443 { "mono-sdkdir=", s => sdkdir = s },
444 { "emscripten-sdkdir=", s => emscripten_sdkdir = s },
445 { "netcore-sdkdir=", s => netcore_sdkdir = s },
446 { "prefix=", s => app_prefix = s },
447 { "wasm-runtime-path=", s => wasm_runtime_path = s },
448 { "deploy=", s => deploy_prefix = s },
449 { "vfs=", s => vfs_prefix = s },
450 { "aot", s => ee_mode = ExecMode.Aot },
451 { "aot-interp", s => ee_mode = ExecMode.AotInterp },
452 { "template=", s => runtimeTemplate = s },
453 { "asset=", s => assets.Add(s) },
454 { "search-path=", s => root_search_paths.Add(s) },
455 { "profile=", s => profilers.Add (s) },
456 { "copy=", s => copyTypeParm = s },
457 { "aot-assemblies=", s => aot_assemblies = s },
458 { "aot-profile=", s => aot_profile = s },
459 { "link-mode=", s => linkModeParm = s },
460 { "link-descriptor=", s => linkDescriptor = s },
461 { "pinvoke-libs=", s => pinvoke_libs = s },
462 { "native-lib=", s => native_libs.Add (s) },
463 { "preload-file=", s => preload_files.Add (s) },
464 { "embed-file=", s => embed_files.Add (s) },
465 { "framework=", s => framework = s },
466 { "help", s => print_usage = true },
469 AddFlag (p, new BoolFlag ("debug", "enable c# debugging", opts.Debug, b => opts.Debug = b));
470 AddFlag (p, new BoolFlag ("debugrt", "enable debug runtime", opts.DebugRuntime, b => opts.DebugRuntime = b));
471 AddFlag (p, new BoolFlag ("linker", "enable the linker", opts.Linker, b => opts.Linker = b));
472 AddFlag (p, new BoolFlag ("binding", "enable the binding engine", opts.AddBinding, b => opts.AddBinding = b));
473 AddFlag (p, new BoolFlag ("link-icalls", "link away unused icalls", opts.LinkIcalls, b => opts.LinkIcalls = b));
474 AddFlag (p, new BoolFlag ("il-strip", "strip IL code from assemblies in AOT mode", opts.ILStrip, b => opts.ILStrip = b));
475 AddFlag (p, new BoolFlag ("linker-verbose", "set verbose option on linker", opts.LinkerVerbose, b => opts.LinkerVerbose = b));
476 AddFlag (p, new BoolFlag ("zlib", "enable the use of zlib for System.IO.Compression support", opts.EnableZLib, b => opts.EnableZLib = b));
477 AddFlag (p, new BoolFlag ("enable-fs", "enable filesystem support (through Emscripten's file_packager.py in a later phase)", opts.EnableFS, b => opts.EnableFS = b));
478 AddFlag (p, new BoolFlag ("threads", "enable threads", opts.EnableThreads, b => opts.EnableThreads = b));
479 AddFlag (p, new BoolFlag ("dynamic-runtime", "enable dynamic runtime (support for Emscripten's dlopen)", opts.EnableDynamicRuntime, b => opts.EnableDynamicRuntime = b));
480 AddFlag (p, new BoolFlag ("native-strip", "strip final executable", opts.NativeStrip, b => opts.NativeStrip = b));
481 AddFlag (p, new BoolFlag ("simd", "enable SIMD support", opts.Simd, b => opts.Simd = b));
482 AddFlag (p, new BoolFlag ("linker-exclude-deserialization", "Link out .NET deserialization support", opts.LinkerExcludeDeserialization, b => opts.LinkerExcludeDeserialization = b));
484 var new_args = p.Parse (args).ToArray ();
485 foreach (var a in new_args) {
486 root_assemblies.Add (a);
489 if (print_usage) {
490 Usage ();
491 return 0;
494 if (!Enum.TryParse(copyTypeParm, true, out copyType)) {
495 Console.WriteLine("Invalid copy value");
496 Usage ();
497 return 1;
500 if (!Enum.TryParse(linkModeParm, true, out linkMode)) {
501 Console.WriteLine("Invalid link-mode value");
502 Usage ();
503 return 1;
506 if (out_prefix == null) {
507 Console.Error.WriteLine ("The --appdir= argument is required.");
508 return 1;
511 enable_debug = opts.Debug;
512 enable_linker = opts.Linker;
513 add_binding = opts.AddBinding;
514 use_release_runtime = !opts.DebugRuntime;
515 il_strip = opts.ILStrip;
516 linker_verbose = opts.LinkerVerbose;
517 gen_pinvoke = pinvoke_libs != "";
518 enable_zlib = opts.EnableZLib;
519 enable_fs = opts.EnableFS;
520 enable_threads = opts.EnableThreads;
521 enable_dynamic_runtime = opts.EnableDynamicRuntime;
522 enable_simd = opts.Simd;
524 if (ee_mode == ExecMode.Aot || ee_mode == ExecMode.AotInterp)
525 enable_aot = true;
527 if (enable_aot || opts.Linker)
528 enable_linker = true;
529 if (opts.LinkIcalls)
530 link_icalls = true;
531 if (!enable_linker || !enable_aot)
532 enable_dedup = false;
533 if (enable_aot || link_icalls || gen_pinvoke || profilers.Count > 0 || native_libs.Count > 0 || preload_files.Count > 0 || embed_files.Count > 0) {
534 build_wasm = true;
535 emit_ninja = true;
537 if (!enable_aot && link_icalls)
538 enable_lto = true;
539 if (ee_mode != ExecMode.Aot)
540 // Can't strip out IL code in mixed mode, since the interpreter might execute some methods even if they have AOTed code available
541 il_strip = false;
543 if (aot_assemblies != "") {
544 if (ee_mode != ExecMode.AotInterp) {
545 Console.Error.WriteLine ("The --aot-assemblies= argument requires --aot-interp.");
546 return 1;
549 if (link_icalls && !enable_linker) {
550 Console.Error.WriteLine ("The --link-icalls option requires the --linker option.");
551 return 1;
553 if (framework != "") {
554 if (framework.StartsWith ("netcoreapp")) {
555 is_netcore = true;
556 if (netcore_sdkdir == "") {
557 Console.Error.WriteLine ("The --netcore-sdkdir= argument is required.");
558 return 1;
560 if (!Directory.Exists (netcore_sdkdir)) {
561 Console.Error.WriteLine ($"The directory '{netcore_sdkdir}' doesn't exist.");
562 return 1;
564 } else {
565 Console.Error.WriteLine ("The only valid value for --framework is 'netcoreapp...'");
566 return 1;
570 if (aot_profile != null && !File.Exists (aot_profile)) {
571 Console.Error.WriteLine ($"AOT profile file '{aot_profile}' not found.");
572 return 1;
575 if (enable_simd && !is_netcore) {
576 Console.Error.WriteLine ("--simd is only supported with netcore.");
577 return 1;
580 var tool_prefix = Path.GetDirectoryName (typeof (Driver).Assembly.Location);
582 //are we working from the tree?
583 if (sdkdir != null) {
584 framework_prefix = Path.Combine (tool_prefix, "framework"); //all framework assemblies are currently side built to packager.exe
585 } else if (Directory.Exists (Path.Combine (tool_prefix, "../out/wasm-bcl/wasm"))) {
586 framework_prefix = Path.Combine (tool_prefix, "framework"); //all framework assemblies are currently side built to packager.exe
587 sdkdir = Path.Combine (tool_prefix, "../out");
588 } else {
589 framework_prefix = Path.Combine (tool_prefix, "framework");
590 sdkdir = tool_prefix;
592 string bcl_root = Path.Combine (sdkdir, "wasm-bcl");
593 var bcl_prefix = Path.Combine (bcl_root, "wasm");
594 bcl_tools_prefix = Path.Combine (bcl_root, "wasm_tools");
595 bcl_facades_prefix = Path.Combine (bcl_prefix, "Facades");
596 bcl_prefixes = new List<string> ();
597 if (is_netcore) {
598 /* corelib */
599 bcl_prefixes.Add (Path.Combine (bcl_root, "netcore"));
600 /* .net runtime */
601 bcl_prefixes.Add (netcore_sdkdir);
602 } else {
603 bcl_prefixes.Add (bcl_prefix);
606 foreach (var ra in root_assemblies) {
607 AssemblyKind kind;
608 var resolved = Resolve (ra, out kind);
609 Import (resolved, kind);
611 if (add_binding) {
612 var bindings = ResolveFramework (BINDINGS_ASM_NAME + ".dll");
613 Import (bindings, AssemblyKind.Framework);
614 var http = ResolveFramework (HTTP_ASM_NAME + ".dll");
615 Import (http, AssemblyKind.Framework);
616 var websockets = ResolveFramework (WEBSOCKETS_ASM_NAME + ".dll");
617 Import (websockets, AssemblyKind.Framework);
620 if (enable_aot) {
621 var to_aot = new Dictionary<string, bool> ();
622 if (is_netcore)
623 to_aot ["System.Private.CoreLib"] = true;
624 else
625 to_aot ["mscorlib"] = true;
626 if (aot_assemblies != "") {
627 foreach (var s in aot_assemblies.Split (','))
628 to_aot [s] = true;
630 foreach (var ass in assemblies) {
631 if (aot_assemblies == "" || to_aot.ContainsKey (ass.name)) {
632 ass.aot = true;
633 to_aot.Remove (ass.name);
636 if (to_aot.Count > 0) {
637 Console.Error.WriteLine ("Unknown assembly name '" + to_aot.Keys.ToArray ()[0] + "' in --aot-assemblies option.");
638 return 1;
642 if (builddir != null) {
643 emit_ninja = true;
644 if (!Directory.Exists (builddir))
645 Directory.CreateDirectory (builddir);
648 if (!emit_ninja) {
649 if (!Directory.Exists (out_prefix))
650 Directory.CreateDirectory (out_prefix);
651 var bcl_dir = Path.Combine (out_prefix, deploy_prefix);
652 if (Directory.Exists (bcl_dir))
653 Directory.Delete (bcl_dir, true);
654 Directory.CreateDirectory (bcl_dir);
655 foreach (var f in file_list) {
656 CopyFile(f, Path.Combine (bcl_dir, Path.GetFileName (f)), copyType);
660 if (deploy_prefix.EndsWith ("/"))
661 deploy_prefix = deploy_prefix.Substring (0, deploy_prefix.Length - 1);
662 if (vfs_prefix.EndsWith ("/"))
663 vfs_prefix = vfs_prefix.Substring (0, vfs_prefix.Length - 1);
665 // wasm core bindings module
666 var wasm_core_bindings = string.Empty;
667 if (add_binding) {
668 wasm_core_bindings = BINDINGS_MODULE;
670 // wasm core bindings support file
671 var wasm_core_support = string.Empty;
672 var wasm_core_support_library = string.Empty;
673 if (add_binding) {
674 wasm_core_support = BINDINGS_MODULE_SUPPORT;
675 wasm_core_support_library = $"--js-library {BINDINGS_MODULE_SUPPORT}";
677 var runtime_js = Path.Combine (emit_ninja ? builddir : out_prefix, "runtime.js");
678 if (emit_ninja) {
679 File.Delete (runtime_js);
680 File.Copy (runtimeTemplate, runtime_js);
681 } else {
682 if (File.Exists(runtime_js) && (File.Exists(runtimeTemplate))) {
683 CopyFile (runtimeTemplate, runtime_js, CopyType.IfNewer, $"runtime template <{runtimeTemplate}> ");
684 } else {
685 if (File.Exists(runtimeTemplate))
686 CopyFile (runtimeTemplate, runtime_js, CopyType.IfNewer, $"runtime template <{runtimeTemplate}> ");
687 else {
688 var runtime_gen = "\nvar Module = {\n\tonRuntimeInitialized: function () {\n\t\tMONO.mono_load_runtime_and_bcl (\n\t\tconfig.vfs_prefix,\n\t\tconfig.deploy_prefix,\n\t\tconfig.enable_debugging,\n\t\tconfig.file_list,\n\t\tfunction () {\n\t\t\tApp.init ();\n\t\t}\n\t)\n\t},\n};";
689 File.Delete (runtime_js);
690 File.WriteAllText (runtime_js, runtime_gen);
695 AssemblyData dedup_asm = null;
697 if (enable_dedup) {
698 dedup_asm = new AssemblyData () { name = "aot-instances",
699 filename = "aot-instances.dll",
700 bc_path = "$builddir/aot-instances.dll.bc",
701 o_path = "$builddir/aot-instances.dll.o",
702 app_path = "$appdir/$deploy_prefix/aot-instances.dll",
703 linkout_path = "$builddir/linker-out/aot-instances.dll",
704 aot = true
706 assemblies.Add (dedup_asm);
707 file_list.Add ("aot-instances.dll");
710 var file_list_str = string.Join (",", file_list.Select (f => $"\"{Path.GetFileName (f)}\"").Distinct());
711 var config = String.Format ("config = {{\n \tvfs_prefix: \"{0}\",\n \tdeploy_prefix: \"{1}\",\n \tenable_debugging: {2},\n \tfile_list: [ {3} ],\n", vfs_prefix, deploy_prefix, enable_debug ? "1" : "0", file_list_str);
712 config += "}\n";
713 var config_js = Path.Combine (emit_ninja ? builddir : out_prefix, "mono-config.js");
714 File.Delete (config_js);
715 File.WriteAllText (config_js, config);
718 if (wasm_runtime_path == null)
719 wasm_runtime_path = Path.Combine (tool_prefix, "builds");
721 string wasm_runtime_dir;
722 if (is_netcore)
723 wasm_runtime_dir = Path.Combine (wasm_runtime_path, use_release_runtime ? "netcore-release" : "netcore-debug");
724 else if (enable_threads)
725 wasm_runtime_dir = Path.Combine (wasm_runtime_path, use_release_runtime ? "threads-release" : "threads-debug");
726 else if (enable_dynamic_runtime)
727 wasm_runtime_dir = Path.Combine (wasm_runtime_path, use_release_runtime ? "dynamic-release" : "dynamic-debug");
728 else
729 wasm_runtime_dir = Path.Combine (wasm_runtime_path, use_release_runtime ? "release" : "debug");
730 if (!emit_ninja) {
731 var interp_files = new List<string> { "dotnet.js", "dotnet.wasm" };
732 if (enable_threads) {
733 interp_files.Add ("dotnet.worker.js");
735 foreach (var fname in interp_files) {
736 File.Delete (Path.Combine (out_prefix, fname));
737 File.Copy (
738 Path.Combine (wasm_runtime_dir, fname),
739 Path.Combine (out_prefix, fname));
742 foreach(var asset in assets) {
743 CopyFile (asset,
744 Path.Combine (out_prefix, Path.GetFileName (asset)), copyType, "Asset: ");
748 if (!emit_ninja)
749 return 0;
751 if (builddir == null) {
752 Console.Error.WriteLine ("The --builddir argument is required.");
753 return 1;
756 var filenames = new Dictionary<string, string> ();
757 foreach (var a in assemblies) {
758 var assembly = a.src_path;
759 if (assembly == null)
760 continue;
761 string filename = Path.GetFileName (assembly);
762 if (filenames.ContainsKey (filename)) {
763 Console.WriteLine ("Duplicate input assembly: " + assembly + " " + filenames [filename]);
764 return 1;
766 filenames [filename] = assembly;
769 if (build_wasm) {
770 if (sdkdir == null) {
771 Console.WriteLine ("The --mono-sdkdir argument is required.");
772 return 1;
774 if (emscripten_sdkdir == null) {
775 Console.WriteLine ("The --emscripten-sdkdir argument is required.");
776 return 1;
778 GenDriver (builddir, profilers, ee_mode, link_icalls);
781 string runtime_dir = is_netcore ? "$mono_sdkdir/wasm-runtime-netcore-release" : "$mono_sdkdir/wasm-runtime-release";
782 string runtime_libdir = $"{runtime_dir}/lib";
784 string runtime_libs = "";
785 if (ee_mode == ExecMode.Interp || ee_mode == ExecMode.AotInterp || link_icalls) {
786 runtime_libs += $"$runtime_libdir/libmono-ee-interp.a $runtime_libdir/libmono-ilgen.a ";
787 // We need to link the icall table because the interpreter uses it to lookup icalls even if the aot-ed icall wrappers are available
788 if (!link_icalls)
789 runtime_libs += $"$runtime_libdir/libmono-icall-table.a ";
791 runtime_libs += $"$runtime_libdir/libmonosgen-2.0.a ";
792 if (is_netcore)
793 runtime_libs += $"$runtime_libdir/System.Native.bc";
794 else
795 runtime_libs += $"$runtime_libdir/libmono-native.a";
797 string aot_args = "llvm-path=$emscripten_sdkdir/upstream/bin,";
798 string profiler_libs = "";
799 string profiler_aot_args = "";
800 foreach (var profiler in profilers) {
801 profiler_libs += $"$runtime_libdir/libmono-profiler-{profiler}-static.a ";
802 if (profiler_aot_args != "")
803 profiler_aot_args += " ";
804 profiler_aot_args += $"--profile={profiler}";
806 string extra_link_libs = "";
807 foreach (var lib in native_libs)
808 extra_link_libs += lib + " ";
809 if (aot_profile != null) {
810 CopyFile (aot_profile, Path.Combine (builddir, Path.GetFileName (aot_profile)), CopyType.IfNewer, "");
811 aot_args += $"profile={aot_profile},profile-only,";
813 if (ee_mode == ExecMode.AotInterp)
814 aot_args += "interp,";
815 if (build_wasm)
816 enable_zlib = true;
818 wasm_runtime_dir = Path.GetFullPath (wasm_runtime_dir);
819 sdkdir = Path.GetFullPath (sdkdir);
820 out_prefix = Path.GetFullPath (out_prefix);
822 string driver_deps = "";
823 if (link_icalls)
824 driver_deps += " $builddir/icall-table.h";
825 if (gen_pinvoke)
826 driver_deps += " $builddir/pinvoke-table.h";
827 string emcc_flags = "";
828 if (enable_lto)
829 emcc_flags += "--llvm-lto 1 ";
830 if (enable_zlib)
831 emcc_flags += "-s USE_ZLIB=1 ";
832 if (enable_fs)
833 emcc_flags += "-s FORCE_FILESYSTEM=1 ";
834 foreach (var pf in preload_files)
835 emcc_flags += "--preload-file " + pf + " ";
836 foreach (var f in embed_files)
837 emcc_flags += "--embed-file " + f + " ";
838 string emcc_link_flags = "";
839 if (enable_debug)
840 emcc_link_flags += "-O0 ";
841 string strip_cmd = "";
842 if (opts.NativeStrip)
843 strip_cmd = " && $wasm_strip $out_wasm";
844 if (enable_simd) {
845 aot_args += "mattr=simd,";
846 emcc_flags += "-s SIMD=1 ";
848 if (!use_release_runtime)
849 // -s ASSERTIONS=2 is very slow
850 emcc_flags += "-s ASSERTIONS=1 ";
852 var ninja = File.CreateText (Path.Combine (builddir, "build.ninja"));
854 // Defines
855 ninja.WriteLine ($"mono_sdkdir = {sdkdir}");
856 ninja.WriteLine ($"emscripten_sdkdir = {emscripten_sdkdir}");
857 ninja.WriteLine ($"tool_prefix = {tool_prefix}");
858 ninja.WriteLine ($"appdir = {out_prefix}");
859 ninja.WriteLine ($"builddir = .");
860 ninja.WriteLine ($"wasm_runtime_dir = {wasm_runtime_dir}");
861 ninja.WriteLine ($"runtime_libdir = {runtime_libdir}");
862 ninja.WriteLine ($"deploy_prefix = {deploy_prefix}");
863 ninja.WriteLine ($"bcl_dir = {bcl_prefix}");
864 ninja.WriteLine ($"bcl_facades_dir = {bcl_facades_prefix}");
865 ninja.WriteLine ($"framework_dir = {framework_prefix}");
866 ninja.WriteLine ($"tools_dir = {bcl_tools_prefix}");
867 ninja.WriteLine ($"emsdk_env = $builddir/emsdk_env.sh");
868 if (add_binding) {
869 ninja.WriteLine ($"wasm_core_bindings = $builddir/{BINDINGS_MODULE}");
870 ninja.WriteLine ($"wasm_core_support = {wasm_core_support}");
871 ninja.WriteLine ($"wasm_core_support_library = {wasm_core_support_library}");
872 } else {
873 ninja.WriteLine ("wasm_core_bindings =");
874 ninja.WriteLine ("wasm_core_support =");
875 ninja.WriteLine ("wasm_core_support_library =");
877 if (is_netcore)
878 ninja.WriteLine ("cross = $mono_sdkdir/wasm-cross-netcore-release/bin/wasm32-unknown-none-mono-sgen");
879 else
880 ninja.WriteLine ("cross = $mono_sdkdir/wasm-cross-release/bin/wasm32-unknown-none-mono-sgen");
881 ninja.WriteLine ("emcc = source $emsdk_env && emcc");
882 ninja.WriteLine ("wasm_strip = $emscripten_sdkdir/upstream/bin/wasm-strip");
883 ninja.WriteLine ($"emcc_flags = -Oz -g {emcc_flags}-s DISABLE_EXCEPTION_CATCHING=0 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s BINARYEN=1 -s TOTAL_MEMORY=134217728 -s ALIASING_FUNCTION_POINTERS=0 -s NO_EXIT_RUNTIME=1 -s ERROR_ON_UNDEFINED_SYMBOLS=1 -s \"EXTRA_EXPORTED_RUNTIME_METHODS=[\'ccall\', \'cwrap\', \'setValue\', \'getValue\', \'UTF8ToString\']\" -s \"EXPORTED_FUNCTIONS=[\'___cxa_is_pointer_type\', \'___cxa_can_catch\']\" -s \"DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[\'setThrew\', \'memset\']\"");
884 ninja.WriteLine ($"aot_base_args = llvmonly,asmonly,no-opt,static,direct-icalls,deterministic,{aot_args}");
886 // Rules
887 ninja.WriteLine ("rule aot");
888 ninja.WriteLine ($" command = MONO_PATH=$mono_path $cross --debug {profiler_aot_args} --aot=$aot_args,$aot_base_args,depfile=$depfile,llvm-outfile=$outfile $src_file");
889 ninja.WriteLine (" description = [AOT] $src_file -> $outfile");
890 ninja.WriteLine ("rule aot-instances");
891 ninja.WriteLine ($" command = MONO_PATH=$mono_path $cross --debug {profiler_aot_args} --aot=$aot_base_args,llvm-outfile=$outfile,dedup-include=$dedup_image $src_files");
892 ninja.WriteLine (" description = [AOT-INSTANCES] $outfile");
893 ninja.WriteLine ("rule mkdir");
894 ninja.WriteLine (" command = mkdir -p $out");
895 ninja.WriteLine ("rule cp");
896 ninja.WriteLine (" command = cp $in $out");
897 // Copy $in to $out only if it changed
898 ninja.WriteLine ("rule cpifdiff");
899 ninja.WriteLine (" command = if cmp -s $in $out ; then : ; else cp $in $out ; fi");
900 ninja.WriteLine (" restat = true");
901 ninja.WriteLine (" description = [CPIFDIFF] $in -> $out");
902 ninja.WriteLine ("rule create-emsdk-env");
903 ninja.WriteLine (" command = $emscripten_sdkdir/emsdk construct_env $out");
904 ninja.WriteLine ("rule emcc");
905 ninja.WriteLine (" command = bash -c '$emcc $emcc_flags $flags -c -o $out $in'");
906 ninja.WriteLine (" description = [EMCC] $in -> $out");
907 ninja.WriteLine ("rule emcc-link");
908 ninja.WriteLine ($" command = bash -c '$emcc $emcc_flags {emcc_link_flags} -o $out_js --js-library $tool_prefix/src/library_mono.js --js-library $tool_prefix/src/dotnet_support.js {wasm_core_support_library} $in' {strip_cmd}");
909 ninja.WriteLine (" description = [EMCC-LINK] $in -> $out_js");
910 ninja.WriteLine ("rule linker");
911 ninja.WriteLine (" command = mono $tools_dir/monolinker.exe -out $builddir/linker-out -l none --deterministic --disable-opt unreachablebodies --exclude-feature com --exclude-feature remoting --exclude-feature etw $linker_args || exit 1; mono $tools_dir/wasm-tuner.exe --gen-empty-assemblies $out");
912 ninja.WriteLine (" description = [IL-LINK]");
913 ninja.WriteLine ("rule aot-instances-dll");
914 ninja.WriteLine (" command = echo > aot-instances.cs; csc /deterministic /out:$out /target:library aot-instances.cs");
915 ninja.WriteLine ("rule gen-runtime-icall-table");
916 ninja.WriteLine (" command = $cross --print-icall-table > $out");
917 ninja.WriteLine ("rule gen-icall-table");
918 ninja.WriteLine (" command = mono $tools_dir/wasm-tuner.exe --gen-icall-table $runtime_table $in > $out");
919 ninja.WriteLine ("rule gen-pinvoke-table");
920 ninja.WriteLine (" command = mono $tools_dir/wasm-tuner.exe --gen-pinvoke-table $pinvoke_libs $in > $out");
921 ninja.WriteLine ("rule ilstrip");
922 ninja.WriteLine (" command = cp $in $out; mono $tools_dir/mono-cil-strip.exe -q $out");
923 ninja.WriteLine (" description = [IL-STRIP]");
925 // Targets
926 ninja.WriteLine ("build $appdir: mkdir");
927 ninja.WriteLine ("build $appdir/$deploy_prefix: mkdir");
928 ninja.WriteLine ("build $appdir/runtime.js: cpifdiff $builddir/runtime.js");
929 ninja.WriteLine ("build $appdir/mono-config.js: cpifdiff $builddir/mono-config.js");
930 if (build_wasm) {
931 var source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "driver.c"));
932 ninja.WriteLine ($"build $builddir/driver.c: cpifdiff {source_file}");
933 ninja.WriteLine ($"build $builddir/driver-gen.c: cpifdiff $builddir/driver-gen.c.in");
935 var pinvoke_file_name = is_netcore ? "pinvoke-tables-default-netcore.h" : "pinvoke-tables-default.h";
936 var pinvoke_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", pinvoke_file_name));
937 ninja.WriteLine ($"build $builddir/{pinvoke_file_name}: cpifdiff {pinvoke_file}");
938 driver_deps += $" $builddir/{pinvoke_file_name}";
940 var driver_cflags = enable_aot ? "-DENABLE_AOT=1" : "";
942 if (add_binding) {
943 var bindings_source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "corebindings.c"));
944 ninja.WriteLine ($"build $builddir/corebindings.c: cpifdiff {bindings_source_file}");
946 ninja.WriteLine ($"build $builddir/corebindings.o: emcc $builddir/corebindings.c | $emsdk_env");
947 ninja.WriteLine ($" flags = -I{runtime_dir}/include/mono-2.0");
948 driver_cflags += " -DCORE_BINDINGS ";
950 if (gen_pinvoke)
951 driver_cflags += " -DGEN_PINVOKE ";
952 if (is_netcore)
953 driver_cflags += " -DENABLE_NETCORE ";
955 ninja.WriteLine ("build $emsdk_env: create-emsdk-env");
956 ninja.WriteLine ($"build $builddir/driver.o: emcc $builddir/driver.c | $emsdk_env $builddir/driver-gen.c {driver_deps}");
957 ninja.WriteLine ($" flags = {driver_cflags} -DDRIVER_GEN=1 -I{runtime_dir}/include/mono-2.0");
959 if (enable_zlib) {
960 var zlib_source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "zlib-helper.c"));
961 ninja.WriteLine ($"build $builddir/zlib-helper.c: cpifdiff {zlib_source_file}");
963 ninja.WriteLine ($"build $builddir/zlib-helper.o: emcc $builddir/zlib-helper.c | $emsdk_env");
964 ninja.WriteLine ($" flags = -s USE_ZLIB=1 -I{runtime_dir}/include/mono-2.0");
966 } else {
967 ninja.WriteLine ("build $appdir/dotnet.js: cpifdiff $wasm_runtime_dir/dotnet.js");
968 ninja.WriteLine ("build $appdir/dotnet.wasm: cpifdiff $wasm_runtime_dir/dotnet.wasm");
969 if (enable_threads) {
970 ninja.WriteLine ("build $appdir/dotnet.worker.js: cpifdiff $wasm_runtime_dir/dotnet.worker.js");
973 if (enable_aot)
974 ninja.WriteLine ("build $builddir/aot-in: mkdir");
976 var source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "linker-subs.xml"));
977 ninja.WriteLine ($"build $builddir/linker-subs.xml: cpifdiff {source_file}");
979 var ofiles = "";
980 var bc_files = "";
981 string linker_infiles = "";
982 string linker_ofiles = "";
983 string dedup_infiles = "";
984 if (enable_linker) {
985 string path = Path.Combine (builddir, "linker-in");
986 if (!Directory.Exists (path))
987 Directory.CreateDirectory (path);
989 string aot_in_path = enable_linker ? "$builddir/linker-out" : "$builddir";
990 foreach (var a in assemblies) {
991 var assembly = a.src_path;
992 if (assembly == null)
993 continue;
994 string filename = Path.GetFileName (assembly);
995 var filename_noext = Path.GetFileNameWithoutExtension (filename);
996 string filename_pdb = Path.ChangeExtension (filename, "pdb");
997 var source_file_path = Path.GetFullPath (assembly);
998 var source_file_path_pdb = Path.ChangeExtension (source_file_path, "pdb");
999 string infile = "";
1000 string infile_pdb = "";
1001 bool emit_pdb = assemblies_with_dbg_info.Contains (source_file_path_pdb);
1002 if (enable_linker) {
1003 a.linkin_path = $"$builddir/linker-in/{filename}";
1004 a.linkout_path = $"$builddir/linker-out/{filename}";
1005 linker_infiles += $"{a.linkin_path} ";
1006 linker_ofiles += $" {a.linkout_path}";
1007 ninja.WriteLine ($"build {a.linkin_path}: cp {source_file_path}");
1008 a.aotin_path = a.linkout_path;
1009 infile = $"{a.aotin_path}";
1010 } else {
1011 infile = $"$builddir/{filename}";
1012 ninja.WriteLine ($"build $builddir/{filename}: cpifdiff {source_file_path}");
1013 a.linkout_path = infile;
1014 if (emit_pdb) {
1015 ninja.WriteLine ($"build $builddir/{filename_pdb}: cpifdiff {source_file_path_pdb}");
1016 infile_pdb = $"$builddir/{filename_pdb}";
1020 a.final_path = infile;
1021 if (il_strip) {
1022 ninja.WriteLine ($"build $builddir/ilstrip-out/{filename} : ilstrip {infile}");
1023 a.final_path = $"$builddir/ilstrip-out/{filename}";
1026 ninja.WriteLine ($"build $appdir/$deploy_prefix/{filename}: cpifdiff {a.final_path}");
1027 if (emit_pdb && infile_pdb != "")
1028 ninja.WriteLine ($"build $appdir/$deploy_prefix/{filename_pdb}: cpifdiff {infile_pdb}");
1029 if (a.aot) {
1030 a.bc_path = $"$builddir/{filename}.bc";
1031 a.o_path = $"$builddir/{filename}.o";
1032 a.aot_depfile_path = $"$builddir/linker-out/{filename}.depfile";
1034 if (filename == "mscorlib.dll") {
1035 // mscorlib has no dependencies so we can skip the aot step if the input didn't change
1036 // The other assemblies depend on their references
1037 infile = "$builddir/aot-in/mscorlib.dll";
1038 a.aotin_path = infile;
1039 ninja.WriteLine ($"build {a.aotin_path}: cpifdiff {a.linkout_path}");
1041 ninja.WriteLine ($"build {a.bc_path}.tmp: aot {infile}");
1042 ninja.WriteLine ($" src_file={infile}");
1043 ninja.WriteLine ($" outfile={a.bc_path}.tmp");
1044 ninja.WriteLine ($" mono_path=$builddir/aot-in:{aot_in_path}");
1045 ninja.WriteLine ($" depfile={a.aot_depfile_path}");
1046 if (enable_dedup)
1047 ninja.WriteLine ($" aot_args=dedup-skip");
1049 ninja.WriteLine ($"build {a.bc_path}: cpifdiff {a.bc_path}.tmp");
1050 ninja.WriteLine ($"build {a.o_path}: emcc {a.bc_path} | $emsdk_env");
1052 ofiles += " " + $"{a.o_path}";
1053 bc_files += " " + $"{a.bc_path}";
1054 dedup_infiles += $" {a.aotin_path}";
1057 if (enable_dedup) {
1059 * Run the aot compiler in dedup mode:
1060 * mono --aot=<args>,dedup-include=aot-instances.dll <assemblies> aot-instances.dll
1061 * This will process all assemblies and emit all instances into the aot image of aot-instances.dll
1063 var a = dedup_asm;
1065 * The dedup process will read in the .dedup files created when running with dedup-skip, so add all the
1066 * .bc files as dependencies.
1068 ninja.WriteLine ($"build {a.bc_path}.tmp: aot-instances | {bc_files} {a.linkout_path}");
1069 ninja.WriteLine ($" dedup_image={a.filename}");
1070 ninja.WriteLine ($" src_files={dedup_infiles} {a.linkout_path}");
1071 ninja.WriteLine ($" outfile={a.bc_path}.tmp");
1072 ninja.WriteLine ($" mono_path=$builddir/aot-in:{aot_in_path}");
1073 ninja.WriteLine ($"build {a.app_path}: cpifdiff {a.linkout_path}");
1074 ninja.WriteLine ($"build {a.linkout_path}: aot-instances-dll");
1075 // The dedup image might not have changed
1076 ninja.WriteLine ($"build {a.bc_path}: cpifdiff {a.bc_path}.tmp");
1077 ninja.WriteLine ($"build {a.o_path}: emcc {a.bc_path} | $emsdk_env");
1078 ofiles += $" {a.o_path}";
1080 if (link_icalls) {
1081 string icall_assemblies = "";
1082 foreach (var a in assemblies) {
1083 if (a.name == "mscorlib" || a.name == "System")
1084 icall_assemblies += $"{a.linkout_path} ";
1086 ninja.WriteLine ("build $builddir/icall-table.json: gen-runtime-icall-table");
1087 ninja.WriteLine ($"build $builddir/icall-table.h: gen-icall-table {icall_assemblies}");
1088 ninja.WriteLine ($" runtime_table=$builddir/icall-table.json");
1090 if (gen_pinvoke) {
1091 string pinvoke_assemblies = "";
1092 foreach (var a in assemblies)
1093 pinvoke_assemblies += $"{a.linkout_path} ";
1094 ninja.WriteLine ($"build $builddir/pinvoke-table.h: gen-pinvoke-table {pinvoke_assemblies}");
1095 ninja.WriteLine ($" pinvoke_libs=System.Native,{pinvoke_libs}");
1097 if (build_wasm) {
1098 string zlibhelper = enable_zlib ? "$builddir/zlib-helper.o" : "";
1099 ninja.WriteLine ($"build $appdir/dotnet.js $appdir/dotnet.wasm: emcc-link $builddir/driver.o {zlibhelper} {wasm_core_bindings} {ofiles} {profiler_libs} {extra_link_libs} {runtime_libs} | $tool_prefix/src/library_mono.js $tool_prefix/src/dotnet_support.js {wasm_core_support} $emsdk_env");
1100 ninja.WriteLine (" out_js=$appdir/dotnet.js");
1101 ninja.WriteLine (" out_wasm=$appdir/dotnet.wasm");
1103 if (enable_linker) {
1104 switch (linkMode) {
1105 case LinkMode.SdkOnly:
1106 coremode = "link";
1107 usermode = "copy";
1108 break;
1109 case LinkMode.All:
1110 coremode = "link";
1111 usermode = "link";
1112 break;
1113 default:
1114 coremode = "link";
1115 usermode = "link";
1116 break;
1119 string linker_args = "";
1120 if (enable_aot)
1121 // Only used by the AOT compiler
1122 linker_args += "--explicit-reflection ";
1123 linker_args += "--used-attrs-only true ";
1124 linker_args += "--substitutions linker-subs.xml ";
1125 linker_infiles += "| linker-subs.xml";
1126 if (opts.LinkerExcludeDeserialization)
1127 linker_args += "--exclude-feature deserialization ";
1128 if (!string.IsNullOrEmpty (linkDescriptor)) {
1129 linker_args += $"-x {linkDescriptor} ";
1130 foreach (var assembly in root_assemblies) {
1131 string filename = Path.GetFileName (assembly);
1132 linker_args += $"-p {usermode} {filename} -r linker-in/{filename} ";
1134 } else {
1135 foreach (var assembly in root_assemblies) {
1136 string filename = Path.GetFileName (assembly);
1137 linker_args += $"-a linker-in/{filename} ";
1141 if (linker_verbose) {
1142 linker_args += "--verbose ";
1144 linker_args += $"-d linker-in -d $bcl_dir -d $bcl_facades_dir -d $framework_dir -c {coremode} -u {usermode} ";
1146 ninja.WriteLine ("build $builddir/linker-out: mkdir");
1147 ninja.WriteLine ($"build {linker_ofiles}: linker {linker_infiles}");
1148 ninja.WriteLine ($" linker_args={linker_args}");
1150 if (il_strip)
1151 ninja.WriteLine ("build $builddir/ilstrip-out: mkdir");
1153 foreach(var asset in assets) {
1154 var filename = Path.GetFileName (asset);
1155 var abs_path = Path.GetFullPath (asset);
1156 ninja.WriteLine ($"build $appdir/{filename}: cpifdiff {abs_path}");
1159 ninja.Close ();
1161 return 0;
1164 static void CopyFile(string sourceFileName, string destFileName, CopyType copyType, string typeFile = "")
1166 Console.WriteLine($"{typeFile}cp: {copyType} - {sourceFileName} -> {destFileName}");
1167 switch (copyType)
1169 case CopyType.Always:
1170 File.Copy(sourceFileName, destFileName, true);
1171 break;
1172 case CopyType.IfNewer:
1173 if (!File.Exists(destFileName))
1175 File.Copy(sourceFileName, destFileName);
1177 else
1179 var srcInfo = new FileInfo (sourceFileName);
1180 var dstInfo = new FileInfo (destFileName);
1182 if (srcInfo.LastWriteTime.Ticks > dstInfo.LastWriteTime.Ticks || srcInfo.Length > dstInfo.Length)
1183 File.Copy(sourceFileName, destFileName, true);
1184 else
1185 Console.WriteLine($" skipping: {sourceFileName}");
1187 break;
1188 default:
1189 File.Copy(sourceFileName, destFileName);
1190 break;