[Wasm] Use Path.Combine in Packager (#18332)
[mono-project.git] / sdks / wasm / packager.cs
blobc1595f5ec254842326dde97c38ba9fcf7dba4988
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 = "WebAssembly.Net.Http";
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,
356 All
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;
375 int Run (string[] args) {
376 var add_binding = true;
377 var root_assemblies = new List<string> ();
378 enable_debug = false;
379 string builddir = null;
380 string sdkdir = null;
381 string emscripten_sdkdir = null;
382 var aot_assemblies = "";
383 out_prefix = Environment.CurrentDirectory;
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;
422 var opts = new WasmOptions () {
423 AddBinding = true,
424 Debug = false,
425 DebugRuntime = false,
426 Linker = false,
427 ILStrip = true,
428 LinkerVerbose = false,
429 EnableZLib = false,
430 EnableFS = false,
431 NativeStrip = true,
432 Simd = false,
433 EnableDynamicRuntime = false
436 var p = new OptionSet () {
437 { "nobinding", s => opts.AddBinding = false },
438 { "out=", s => out_prefix = s },
439 { "appdir=", s => out_prefix = s },
440 { "builddir=", s => builddir = s },
441 { "mono-sdkdir=", s => sdkdir = s },
442 { "emscripten-sdkdir=", s => emscripten_sdkdir = s },
443 { "netcore-sdkdir=", s => netcore_sdkdir = s },
444 { "prefix=", s => app_prefix = s },
445 { "deploy=", s => deploy_prefix = s },
446 { "vfs=", s => vfs_prefix = s },
447 { "aot", s => ee_mode = ExecMode.Aot },
448 { "aot-interp", s => ee_mode = ExecMode.AotInterp },
449 { "template=", s => runtimeTemplate = s },
450 { "asset=", s => assets.Add(s) },
451 { "search-path=", s => root_search_paths.Add(s) },
452 { "profile=", s => profilers.Add (s) },
453 { "copy=", s => copyTypeParm = s },
454 { "aot-assemblies=", s => aot_assemblies = s },
455 { "aot-profile=", s => aot_profile = s },
456 { "link-mode=", s => linkModeParm = s },
457 { "link-descriptor=", s => linkDescriptor = s },
458 { "pinvoke-libs=", s => pinvoke_libs = s },
459 { "native-lib=", s => native_libs.Add (s) },
460 { "preload-file=", s => preload_files.Add (s) },
461 { "embed-file=", s => embed_files.Add (s) },
462 { "framework=", s => framework = s },
463 { "help", s => print_usage = true },
466 AddFlag (p, new BoolFlag ("debug", "enable c# debugging", opts.Debug, b => opts.Debug = b));
467 AddFlag (p, new BoolFlag ("debugrt", "enable debug runtime", opts.DebugRuntime, b => opts.DebugRuntime = b));
468 AddFlag (p, new BoolFlag ("linker", "enable the linker", opts.Linker, b => opts.Linker = b));
469 AddFlag (p, new BoolFlag ("binding", "enable the binding engine", opts.AddBinding, b => opts.AddBinding = b));
470 AddFlag (p, new BoolFlag ("link-icalls", "link away unused icalls", opts.LinkIcalls, b => opts.LinkIcalls = b));
471 AddFlag (p, new BoolFlag ("il-strip", "strip IL code from assemblies in AOT mode", opts.ILStrip, b => opts.ILStrip = b));
472 AddFlag (p, new BoolFlag ("linker-verbose", "set verbose option on linker", opts.LinkerVerbose, b => opts.LinkerVerbose = b));
473 AddFlag (p, new BoolFlag ("zlib", "enable the use of zlib for System.IO.Compression support", opts.EnableZLib, b => opts.EnableZLib = b));
474 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));
475 AddFlag (p, new BoolFlag ("threads", "enable threads", opts.EnableThreads, b => opts.EnableThreads = b));
476 AddFlag (p, new BoolFlag ("dynamic-runtime", "enable dynamic runtime (support for Emscripten's dlopen)", opts.EnableDynamicRuntime, b => opts.EnableDynamicRuntime = b));
477 AddFlag (p, new BoolFlag ("native-strip", "strip final executable", opts.NativeStrip, b => opts.NativeStrip = b));
478 AddFlag (p, new BoolFlag ("simd", "enable SIMD support", opts.Simd, b => opts.Simd = b));
480 var new_args = p.Parse (args).ToArray ();
481 foreach (var a in new_args) {
482 root_assemblies.Add (a);
485 if (print_usage) {
486 Usage ();
487 return 0;
490 if (!Enum.TryParse(copyTypeParm, true, out copyType)) {
491 Console.WriteLine("Invalid copy value");
492 Usage ();
493 return 1;
496 if (!Enum.TryParse(linkModeParm, true, out linkMode)) {
497 Console.WriteLine("Invalid link-mode value");
498 Usage ();
499 return 1;
502 enable_debug = opts.Debug;
503 enable_linker = opts.Linker;
504 add_binding = opts.AddBinding;
505 use_release_runtime = !opts.DebugRuntime;
506 il_strip = opts.ILStrip;
507 linker_verbose = opts.LinkerVerbose;
508 gen_pinvoke = pinvoke_libs != "";
509 enable_zlib = opts.EnableZLib;
510 enable_fs = opts.EnableFS;
511 enable_threads = opts.EnableThreads;
512 enable_dynamic_runtime = opts.EnableDynamicRuntime;
513 enable_simd = opts.Simd;
515 if (ee_mode == ExecMode.Aot || ee_mode == ExecMode.AotInterp)
516 enable_aot = true;
518 if (enable_aot || opts.Linker)
519 enable_linker = true;
520 if (opts.LinkIcalls)
521 link_icalls = true;
522 if (!enable_linker || !enable_aot)
523 enable_dedup = false;
524 if (enable_aot || link_icalls || gen_pinvoke || profilers.Count > 0 || native_libs.Count > 0 || preload_files.Count > 0 || embed_files.Count > 0)
525 build_wasm = true;
526 if (!enable_aot && link_icalls)
527 enable_lto = true;
528 if (ee_mode != ExecMode.Aot)
529 // Can't strip out IL code in mixed mode, since the interpreter might execute some methods even if they have AOTed code available
530 il_strip = false;
532 if (aot_assemblies != "") {
533 if (ee_mode != ExecMode.AotInterp) {
534 Console.Error.WriteLine ("The --aot-assemblies= argument requires --aot-interp.");
535 return 1;
538 if (link_icalls && !enable_linker) {
539 Console.Error.WriteLine ("The --link-icalls option requires the --linker option.");
540 return 1;
542 if (framework != "") {
543 if (framework.StartsWith ("netcoreapp")) {
544 is_netcore = true;
545 if (netcore_sdkdir == "") {
546 Console.Error.WriteLine ("The --netcore-sdkdir= argument is required.");
547 return 1;
549 if (!Directory.Exists (netcore_sdkdir)) {
550 Console.Error.WriteLine ($"The directory '{netcore_sdkdir}' doesn't exist.");
551 return 1;
553 } else {
554 Console.Error.WriteLine ("The only valid value for --framework is 'netcoreapp...'");
555 return 1;
559 if (aot_profile != null && !File.Exists (aot_profile)) {
560 Console.Error.WriteLine ($"AOT profile file '{aot_profile}' not found.");
561 return 1;
564 var tool_prefix = Path.GetDirectoryName (typeof (Driver).Assembly.Location);
566 //are we working from the tree?
567 if (sdkdir != null) {
568 framework_prefix = Path.Combine (tool_prefix, "framework"); //all framework assemblies are currently side built to packager.exe
569 } else if (Directory.Exists (Path.Combine (tool_prefix, "../out/wasm-bcl/wasm"))) {
570 framework_prefix = Path.Combine (tool_prefix, "framework"); //all framework assemblies are currently side built to packager.exe
571 sdkdir = Path.Combine (tool_prefix, "../out");
572 } else {
573 framework_prefix = Path.Combine (tool_prefix, "framework");
574 sdkdir = tool_prefix;
576 string bcl_root = Path.Combine (sdkdir, "wasm-bcl");
577 var bcl_prefix = Path.Combine (bcl_root, "wasm");
578 bcl_tools_prefix = Path.Combine (bcl_root, "wasm_tools");
579 bcl_facades_prefix = Path.Combine (bcl_prefix, "Facades");
580 bcl_prefixes = new List<string> ();
581 if (is_netcore) {
582 /* corelib */
583 bcl_prefixes.Add (Path.Combine (bcl_root, "netcore"));
584 /* .net runtime */
585 bcl_prefixes.Add (netcore_sdkdir);
586 } else {
587 bcl_prefixes.Add (bcl_prefix);
590 foreach (var ra in root_assemblies) {
591 AssemblyKind kind;
592 var resolved = Resolve (ra, out kind);
593 Import (resolved, kind);
595 if (add_binding) {
596 var bindings = ResolveFramework (BINDINGS_ASM_NAME + ".dll");
597 Import (bindings, AssemblyKind.Framework);
598 var http = ResolveFramework (HTTP_ASM_NAME + ".dll");
599 Import (http, AssemblyKind.Framework);
600 var websockets = ResolveFramework (WEBSOCKETS_ASM_NAME + ".dll");
601 Import (websockets, AssemblyKind.Framework);
604 if (enable_aot) {
605 var to_aot = new Dictionary<string, bool> ();
606 if (is_netcore)
607 to_aot ["System.Private.CoreLib"] = true;
608 else
609 to_aot ["mscorlib"] = true;
610 if (aot_assemblies != "") {
611 foreach (var s in aot_assemblies.Split (','))
612 to_aot [s] = true;
614 foreach (var ass in assemblies) {
615 if (aot_assemblies == "" || to_aot.ContainsKey (ass.name)) {
616 ass.aot = true;
617 to_aot.Remove (ass.name);
620 if (to_aot.Count > 0) {
621 Console.Error.WriteLine ("Unknown assembly name '" + to_aot.Keys.ToArray ()[0] + "' in --aot-assemblies option.");
622 return 1;
626 if (builddir != null) {
627 emit_ninja = true;
628 if (!Directory.Exists (builddir))
629 Directory.CreateDirectory (builddir);
632 if (!emit_ninja) {
633 if (!Directory.Exists (out_prefix))
634 Directory.CreateDirectory (out_prefix);
635 var bcl_dir = Path.Combine (out_prefix, deploy_prefix);
636 if (Directory.Exists (bcl_dir))
637 Directory.Delete (bcl_dir, true);
638 Directory.CreateDirectory (bcl_dir);
639 foreach (var f in file_list) {
640 CopyFile(f, Path.Combine (bcl_dir, Path.GetFileName (f)), copyType);
644 if (deploy_prefix.EndsWith ("/"))
645 deploy_prefix = deploy_prefix.Substring (0, deploy_prefix.Length - 1);
646 if (vfs_prefix.EndsWith ("/"))
647 vfs_prefix = vfs_prefix.Substring (0, vfs_prefix.Length - 1);
649 // the linker does not consider these core by default
650 var wasm_core_assemblies = new Dictionary<string, bool> ();
651 if (add_binding) {
652 wasm_core_assemblies [BINDINGS_ASM_NAME] = true;
653 wasm_core_assemblies [HTTP_ASM_NAME] = true;
654 wasm_core_assemblies [WEBSOCKETS_ASM_NAME] = true;
656 // wasm core bindings module
657 var wasm_core_bindings = string.Empty;
658 if (add_binding) {
659 wasm_core_bindings = BINDINGS_MODULE;
661 // wasm core bindings support file
662 var wasm_core_support = string.Empty;
663 var wasm_core_support_library = string.Empty;
664 if (add_binding) {
665 wasm_core_support = BINDINGS_MODULE_SUPPORT;
666 wasm_core_support_library = $"--js-library {BINDINGS_MODULE_SUPPORT}";
668 var runtime_js = Path.Combine (emit_ninja ? builddir : out_prefix, "runtime.js");
669 if (emit_ninja) {
670 File.Delete (runtime_js);
671 File.Copy (runtimeTemplate, runtime_js);
672 } else {
673 if (File.Exists(runtime_js) && (File.Exists(runtimeTemplate))) {
674 CopyFile (runtimeTemplate, runtime_js, CopyType.IfNewer, $"runtime template <{runtimeTemplate}> ");
675 } else {
676 if (File.Exists(runtimeTemplate))
677 CopyFile (runtimeTemplate, runtime_js, CopyType.IfNewer, $"runtime template <{runtimeTemplate}> ");
678 else {
679 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};";
680 File.Delete (runtime_js);
681 File.WriteAllText (runtime_js, runtime_gen);
686 AssemblyData dedup_asm = null;
688 if (enable_dedup) {
689 dedup_asm = new AssemblyData () { name = "aot-dummy",
690 filename = "aot-dummy.dll",
691 bc_path = "$builddir/aot-dummy.dll.bc",
692 o_path = "$builddir/aot-dummy.dll.o",
693 app_path = "$appdir/$deploy_prefix/aot-dummy.dll",
694 linkout_path = "$builddir/linker-out/aot-dummy.dll",
695 aot = true
697 assemblies.Add (dedup_asm);
698 file_list.Add ("aot-dummy.dll");
701 var file_list_str = string.Join (",", file_list.Select (f => $"\"{Path.GetFileName (f)}\"").Distinct());
702 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);
703 config += "}\n";
704 var config_js = Path.Combine (emit_ninja ? builddir : out_prefix, "mono-config.js");
705 File.Delete (config_js);
706 File.WriteAllText (config_js, config);
708 string wasm_runtime_dir;
709 if (is_netcore)
710 wasm_runtime_dir = Path.Combine (tool_prefix, "builds", use_release_runtime ? "netcore-release" : "netcore-debug");
711 else if (enable_threads)
712 wasm_runtime_dir = Path.Combine (tool_prefix, "builds", use_release_runtime ? "threads-release" : "threads-debug");
713 else if (enable_dynamic_runtime)
714 wasm_runtime_dir = Path.Combine (tool_prefix, "builds", use_release_runtime ? "dynamic-release" : "dynamic-debug");
715 else
716 wasm_runtime_dir = Path.Combine (tool_prefix, "builds", use_release_runtime ? "release" : "debug");
717 if (!emit_ninja) {
718 var interp_files = new List<string> { "mono.js", "mono.wasm" };
719 if (enable_threads) {
720 interp_files.Add ("mono.worker.js");
722 foreach (var fname in interp_files) {
723 File.Delete (Path.Combine (out_prefix, fname));
724 File.Copy (
725 Path.Combine (wasm_runtime_dir, fname),
726 Path.Combine (out_prefix, fname));
729 foreach(var asset in assets) {
730 CopyFile (asset,
731 Path.Combine (out_prefix, Path.GetFileName (asset)), copyType, "Asset: ");
735 if (!emit_ninja)
736 return 0;
738 var filenames = new Dictionary<string, string> ();
739 foreach (var a in assemblies) {
740 var assembly = a.src_path;
741 if (assembly == null)
742 continue;
743 string filename = Path.GetFileName (assembly);
744 if (filenames.ContainsKey (filename)) {
745 Console.WriteLine ("Duplicate input assembly: " + assembly + " " + filenames [filename]);
746 return 1;
748 filenames [filename] = assembly;
751 if (build_wasm) {
752 if (sdkdir == null) {
753 Console.WriteLine ("The --mono-sdkdir argument is required.");
754 return 1;
756 if (emscripten_sdkdir == null) {
757 Console.WriteLine ("The --emscripten-sdkdir argument is required.");
758 return 1;
760 GenDriver (builddir, profilers, ee_mode, link_icalls);
763 string runtime_dir = is_netcore ? "$mono_sdkdir/wasm-runtime-netcore-release" : "$mono_sdkdir/wasm-runtime-release";
764 string runtime_libdir = $"{runtime_dir}/lib";
766 string runtime_libs = "";
767 if (ee_mode == ExecMode.Interp || ee_mode == ExecMode.AotInterp || link_icalls) {
768 runtime_libs += $"$runtime_libdir/libmono-ee-interp.a $runtime_libdir/libmono-ilgen.a ";
769 // We need to link the icall table because the interpreter uses it to lookup icalls even if the aot-ed icall wrappers are available
770 if (!link_icalls)
771 runtime_libs += $"$runtime_libdir/libmono-icall-table.a ";
773 runtime_libs += $"$runtime_libdir/libmonosgen-2.0.a ";
774 if (is_netcore)
775 runtime_libs += $"$runtime_libdir/System.Native.bc";
776 else
777 runtime_libs += $"$runtime_libdir/libmono-native.a";
779 string aot_args = "llvm-path=$emscripten_sdkdir/upstream/bin,";
780 string profiler_libs = "";
781 string profiler_aot_args = "";
782 foreach (var profiler in profilers) {
783 profiler_libs += $"$runtime_libdir/libmono-profiler-{profiler}-static.a ";
784 if (profiler_aot_args != "")
785 profiler_aot_args += " ";
786 profiler_aot_args += $"--profile={profiler}";
788 string extra_link_libs = "";
789 foreach (var lib in native_libs)
790 extra_link_libs += lib + " ";
791 if (aot_profile != null) {
792 CopyFile (aot_profile, Path.Combine (builddir, Path.GetFileName (aot_profile)), CopyType.IfNewer, "");
793 aot_args += $"profile={aot_profile},profile-only,";
795 if (ee_mode == ExecMode.AotInterp)
796 aot_args += "interp,";
797 if (build_wasm)
798 enable_zlib = true;
800 wasm_runtime_dir = Path.GetFullPath (wasm_runtime_dir);
801 sdkdir = Path.GetFullPath (sdkdir);
802 out_prefix = Path.GetFullPath (out_prefix);
804 string driver_deps = "";
805 if (link_icalls)
806 driver_deps += " $builddir/icall-table.h";
807 if (gen_pinvoke)
808 driver_deps += " $builddir/pinvoke-table.h";
809 string emcc_flags = "";
810 if (enable_lto)
811 emcc_flags += "--llvm-lto 1 ";
812 if (enable_zlib)
813 emcc_flags += "-s USE_ZLIB=1 ";
814 if (enable_fs)
815 emcc_flags += "-s FORCE_FILESYSTEM=1 ";
816 foreach (var pf in preload_files)
817 emcc_flags += "--preload-file " + pf + " ";
818 foreach (var f in embed_files)
819 emcc_flags += "--embed-file " + f + " ";
820 string emcc_link_flags = "";
821 if (enable_debug)
822 emcc_link_flags += "-O0 ";
823 string strip_cmd = "";
824 if (opts.NativeStrip)
825 strip_cmd = " && $wasm_strip $out_wasm";
826 if (enable_simd) {
827 aot_args += "mattr=simd,";
828 emcc_flags = "-s SIMD=1 ";
831 var ninja = File.CreateText (Path.Combine (builddir, "build.ninja"));
833 // Defines
834 ninja.WriteLine ($"mono_sdkdir = {sdkdir}");
835 ninja.WriteLine ($"emscripten_sdkdir = {emscripten_sdkdir}");
836 ninja.WriteLine ($"tool_prefix = {tool_prefix}");
837 ninja.WriteLine ($"appdir = {out_prefix}");
838 ninja.WriteLine ($"builddir = .");
839 ninja.WriteLine ($"wasm_runtime_dir = {wasm_runtime_dir}");
840 ninja.WriteLine ($"runtime_libdir = {runtime_libdir}");
841 ninja.WriteLine ($"deploy_prefix = {deploy_prefix}");
842 ninja.WriteLine ($"bcl_dir = {bcl_prefix}");
843 ninja.WriteLine ($"bcl_facades_dir = {bcl_facades_prefix}");
844 ninja.WriteLine ($"tools_dir = {bcl_tools_prefix}");
845 ninja.WriteLine ($"emsdk_env = $builddir/emsdk_env.sh");
846 if (add_binding) {
847 ninja.WriteLine ($"wasm_core_bindings = $builddir/{BINDINGS_MODULE}");
848 ninja.WriteLine ($"wasm_core_support = {wasm_core_support}");
849 ninja.WriteLine ($"wasm_core_support_library = {wasm_core_support_library}");
850 } else {
851 ninja.WriteLine ("wasm_core_bindings =");
852 ninja.WriteLine ("wasm_core_support =");
853 ninja.WriteLine ("wasm_core_support_library =");
855 if (is_netcore)
856 ninja.WriteLine ("cross = $mono_sdkdir/wasm-cross-netcore-release/bin/wasm32-unknown-none-mono-sgen");
857 else
858 ninja.WriteLine ("cross = $mono_sdkdir/wasm-cross-release/bin/wasm32-unknown-none-mono-sgen");
859 ninja.WriteLine ("emcc = source $emsdk_env && emcc");
860 ninja.WriteLine ("wasm_strip = $emscripten_sdkdir/upstream/bin/wasm-strip");
861 // -s ASSERTIONS=2 is very slow
862 ninja.WriteLine ($"emcc_flags = -Oz -g {emcc_flags}-s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=1 -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\']\"");
863 ninja.WriteLine ($"aot_base_args = llvmonly,asmonly,no-opt,static,direct-icalls,deterministic,{aot_args}");
865 // Rules
866 ninja.WriteLine ("rule aot");
867 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");
868 ninja.WriteLine (" description = [AOT] $src_file -> $outfile");
869 ninja.WriteLine ("rule aot-instances");
870 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");
871 ninja.WriteLine (" description = [AOT-INSTANCES] $outfile");
872 ninja.WriteLine ("rule mkdir");
873 ninja.WriteLine (" command = mkdir -p $out");
874 ninja.WriteLine ("rule cp");
875 ninja.WriteLine (" command = cp $in $out");
876 // Copy $in to $out only if it changed
877 ninja.WriteLine ("rule cpifdiff");
878 ninja.WriteLine (" command = if cmp -s $in $out ; then : ; else cp $in $out ; fi");
879 ninja.WriteLine (" restat = true");
880 ninja.WriteLine (" description = [CPIFDIFF] $in -> $out");
881 ninja.WriteLine ("rule create-emsdk-env");
882 ninja.WriteLine (" command = $emscripten_sdkdir/emsdk construct_env $out");
883 ninja.WriteLine ("rule emcc");
884 ninja.WriteLine (" command = bash -c '$emcc $emcc_flags $flags -c -o $out $in'");
885 ninja.WriteLine (" description = [EMCC] $in -> $out");
886 ninja.WriteLine ("rule emcc-link");
887 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}");
888 ninja.WriteLine (" description = [EMCC-LINK] $in -> $out_js");
889 ninja.WriteLine ("rule linker");
890 ninja.WriteLine (" command = mono $tools_dir/monolinker.exe -out $builddir/linker-out -l none --deterministic --explicit-reflection --disable-opt unreachablebodies --exclude-feature com --exclude-feature remoting --exclude-feature etw $linker_args || exit 1; for f in $out; do if test ! -f $$f; then echo > empty.cs; csc /deterministic /nologo /out:$$f /target:library empty.cs; else touch $$f; fi; done");
891 ninja.WriteLine (" description = [IL-LINK]");
892 ninja.WriteLine ("rule aot-dummy");
893 ninja.WriteLine (" command = echo > aot-dummy.cs; csc /deterministic /out:$out /target:library aot-dummy.cs");
894 ninja.WriteLine ("rule gen-runtime-icall-table");
895 ninja.WriteLine (" command = $cross --print-icall-table > $out");
896 ninja.WriteLine ("rule gen-icall-table");
897 ninja.WriteLine (" command = mono $tools_dir/wasm-tuner.exe --gen-icall-table $runtime_table $in > $out");
898 ninja.WriteLine ("rule gen-pinvoke-table");
899 ninja.WriteLine (" command = mono $tools_dir/wasm-tuner.exe --gen-pinvoke-table $pinvoke_libs $in > $out");
900 ninja.WriteLine ("rule ilstrip");
901 ninja.WriteLine (" command = cp $in $out; mono $tools_dir/mono-cil-strip.exe $out");
902 ninja.WriteLine (" description = [IL-STRIP]");
904 // Targets
905 ninja.WriteLine ("build $appdir: mkdir");
906 ninja.WriteLine ("build $appdir/$deploy_prefix: mkdir");
907 ninja.WriteLine ("build $appdir/runtime.js: cpifdiff $builddir/runtime.js");
908 ninja.WriteLine ("build $appdir/mono-config.js: cpifdiff $builddir/mono-config.js");
909 if (build_wasm) {
910 var source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "driver.c"));
911 ninja.WriteLine ($"build $builddir/driver.c: cpifdiff {source_file}");
912 ninja.WriteLine ($"build $builddir/driver-gen.c: cpifdiff $builddir/driver-gen.c.in");
914 var pinvoke_file_name = is_netcore ? "pinvoke-tables-default-netcore.h" : "pinvoke-tables-default.h";
915 var pinvoke_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", pinvoke_file_name));
916 ninja.WriteLine ($"build $builddir/{pinvoke_file_name}: cpifdiff {pinvoke_file}");
917 driver_deps += $" $builddir/{pinvoke_file_name}";
919 var driver_cflags = enable_aot ? "-DENABLE_AOT=1" : "";
921 if (add_binding) {
922 var bindings_source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "corebindings.c"));
923 ninja.WriteLine ($"build $builddir/corebindings.c: cpifdiff {bindings_source_file}");
925 ninja.WriteLine ($"build $builddir/corebindings.o: emcc $builddir/corebindings.c | $emsdk_env");
926 ninja.WriteLine ($" flags = -I{runtime_dir}/include/mono-2.0");
927 driver_cflags += " -DCORE_BINDINGS ";
929 if (gen_pinvoke)
930 driver_cflags += " -DGEN_PINVOKE ";
931 if (is_netcore)
932 driver_cflags += " -DENABLE_NETCORE ";
934 ninja.WriteLine ("build $emsdk_env: create-emsdk-env");
935 ninja.WriteLine ($"build $builddir/driver.o: emcc $builddir/driver.c | $emsdk_env $builddir/driver-gen.c {driver_deps}");
936 ninja.WriteLine ($" flags = {driver_cflags} -DDRIVER_GEN=1 -I{runtime_dir}/include/mono-2.0");
938 if (enable_zlib) {
939 var zlib_source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "zlib-helper.c"));
940 ninja.WriteLine ($"build $builddir/zlib-helper.c: cpifdiff {zlib_source_file}");
942 ninja.WriteLine ($"build $builddir/zlib-helper.o: emcc $builddir/zlib-helper.c | $emsdk_env");
943 ninja.WriteLine ($" flags = -s USE_ZLIB=1 -I{runtime_dir}/include/mono-2.0");
945 } else {
946 ninja.WriteLine ("build $appdir/mono.js: cpifdiff $wasm_runtime_dir/mono.js");
947 ninja.WriteLine ("build $appdir/mono.wasm: cpifdiff $wasm_runtime_dir/mono.wasm");
948 if (enable_threads) {
949 ninja.WriteLine ("build $appdir/mono.worker.js: cpifdiff $wasm_runtime_dir/mono.worker.js");
952 if (enable_aot)
953 ninja.WriteLine ("build $builddir/aot-in: mkdir");
955 var source_file = Path.GetFullPath (Path.Combine (tool_prefix, "src", "linker-subs.xml"));
956 ninja.WriteLine ($"build $builddir/linker-subs.xml: cpifdiff {source_file}");
958 var ofiles = "";
959 var bc_files = "";
960 string linker_infiles = "";
961 string linker_ofiles = "";
962 string dedup_infiles = "";
963 if (enable_linker) {
964 string path = Path.Combine (builddir, "linker-in");
965 if (!Directory.Exists (path))
966 Directory.CreateDirectory (path);
968 string aot_in_path = enable_linker ? "$builddir/linker-out" : "$builddir";
969 foreach (var a in assemblies) {
970 var assembly = a.src_path;
971 if (assembly == null)
972 continue;
973 string filename = Path.GetFileName (assembly);
974 var filename_noext = Path.GetFileNameWithoutExtension (filename);
975 string filename_pdb = Path.ChangeExtension (filename, "pdb");
976 var source_file_path = Path.GetFullPath (assembly);
977 var source_file_path_pdb = Path.ChangeExtension (source_file_path, "pdb");
978 string infile = "";
979 string infile_pdb = "";
980 bool emit_pdb = assemblies_with_dbg_info.Contains (source_file_path_pdb);
981 if (enable_linker) {
982 a.linkin_path = $"$builddir/linker-in/{filename}";
983 a.linkout_path = $"$builddir/linker-out/{filename}";
984 linker_infiles += $"{a.linkin_path} ";
985 linker_ofiles += $" {a.linkout_path}";
986 ninja.WriteLine ($"build {a.linkin_path}: cp {source_file_path}");
987 a.aotin_path = a.linkout_path;
988 infile = $"{a.aotin_path}";
989 } else {
990 infile = $"$builddir/{filename}";
991 ninja.WriteLine ($"build $builddir/{filename}: cpifdiff {source_file_path}");
992 a.linkout_path = infile;
993 if (emit_pdb) {
994 ninja.WriteLine ($"build $builddir/{filename_pdb}: cpifdiff {source_file_path_pdb}");
995 infile_pdb = $"$builddir/{filename_pdb}";
999 a.final_path = infile;
1000 if (il_strip) {
1001 ninja.WriteLine ($"build $builddir/ilstrip-out/{filename} : ilstrip {infile}");
1002 a.final_path = $"$builddir/ilstrip-out/{filename}";
1005 ninja.WriteLine ($"build $appdir/$deploy_prefix/{filename}: cpifdiff {a.final_path}");
1006 if (emit_pdb && infile_pdb != "")
1007 ninja.WriteLine ($"build $appdir/$deploy_prefix/{filename_pdb}: cpifdiff {infile_pdb}");
1008 if (a.aot) {
1009 a.bc_path = $"$builddir/{filename}.bc";
1010 a.o_path = $"$builddir/{filename}.o";
1011 a.aot_depfile_path = $"$builddir/linker-out/{filename}.depfile";
1013 if (filename == "mscorlib.dll") {
1014 // mscorlib has no dependencies so we can skip the aot step if the input didn't change
1015 // The other assemblies depend on their references
1016 infile = "$builddir/aot-in/mscorlib.dll";
1017 a.aotin_path = infile;
1018 ninja.WriteLine ($"build {a.aotin_path}: cpifdiff {a.linkout_path}");
1020 ninja.WriteLine ($"build {a.bc_path}.tmp: aot {infile}");
1021 ninja.WriteLine ($" src_file={infile}");
1022 ninja.WriteLine ($" outfile={a.bc_path}.tmp");
1023 ninja.WriteLine ($" mono_path=$builddir/aot-in:{aot_in_path}");
1024 ninja.WriteLine ($" depfile={a.aot_depfile_path}");
1025 if (enable_dedup)
1026 ninja.WriteLine ($" aot_args=dedup-skip");
1028 ninja.WriteLine ($"build {a.bc_path}: cpifdiff {a.bc_path}.tmp");
1029 ninja.WriteLine ($"build {a.o_path}: emcc {a.bc_path} | $emsdk_env");
1031 ofiles += " " + $"{a.o_path}";
1032 bc_files += " " + $"{a.bc_path}";
1033 dedup_infiles += $" {a.aotin_path}";
1036 if (enable_dedup) {
1038 * Run the aot compiler in dedup mode:
1039 * mono --aot=<args>,dedup-include=aot-dummy.dll <assemblies> aot-dummy.dll
1040 * This will process all assemblies and emit all instances into the aot image of aot-dummy.dll
1042 var a = dedup_asm;
1044 * The dedup process will read in the .dedup files created when running with dedup-skip, so add all the
1045 * .bc files as dependencies.
1047 ninja.WriteLine ($"build {a.bc_path}.tmp: aot-instances | {bc_files} {a.linkout_path}");
1048 ninja.WriteLine ($" dedup_image={a.filename}");
1049 ninja.WriteLine ($" src_files={dedup_infiles} {a.linkout_path}");
1050 ninja.WriteLine ($" outfile={a.bc_path}.tmp");
1051 ninja.WriteLine ($" mono_path=$builddir/aot-in:{aot_in_path}");
1052 ninja.WriteLine ($"build {a.app_path}: cpifdiff {a.linkout_path}");
1053 ninja.WriteLine ($"build {a.linkout_path}: aot-dummy");
1054 // The dedup image might not have changed
1055 ninja.WriteLine ($"build {a.bc_path}: cpifdiff {a.bc_path}.tmp");
1056 ninja.WriteLine ($"build {a.o_path}: emcc {a.bc_path} | $emsdk_env");
1057 ofiles += $" {a.o_path}";
1059 if (link_icalls) {
1060 string icall_assemblies = "";
1061 foreach (var a in assemblies) {
1062 if (a.name == "mscorlib" || a.name == "System")
1063 icall_assemblies += $"{a.linkout_path} ";
1065 ninja.WriteLine ("build $builddir/icall-table.json: gen-runtime-icall-table");
1066 ninja.WriteLine ($"build $builddir/icall-table.h: gen-icall-table {icall_assemblies}");
1067 ninja.WriteLine ($" runtime_table=$builddir/icall-table.json");
1069 if (gen_pinvoke) {
1070 string pinvoke_assemblies = "";
1071 foreach (var a in assemblies)
1072 pinvoke_assemblies += $"{a.linkout_path} ";
1073 ninja.WriteLine ($"build $builddir/pinvoke-table.h: gen-pinvoke-table {pinvoke_assemblies}");
1074 ninja.WriteLine ($" pinvoke_libs=System.Native,{pinvoke_libs}");
1076 if (build_wasm) {
1077 string zlibhelper = enable_zlib ? "$builddir/zlib-helper.o" : "";
1078 ninja.WriteLine ($"build $appdir/mono.js $appdir/mono.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");
1079 ninja.WriteLine (" out_js=$appdir/mono.js");
1080 ninja.WriteLine (" out_wasm=$appdir/mono.wasm");
1082 if (enable_linker) {
1083 switch (linkMode) {
1084 case LinkMode.SdkOnly:
1085 coremode = "link";
1086 usermode = "copy";
1087 break;
1088 case LinkMode.All:
1089 coremode = "link";
1090 usermode = "link";
1091 break;
1092 default:
1093 coremode = "link";
1094 usermode = "link";
1095 break;
1098 string linker_args = "";
1099 //linker_args += "--substitutions linker-subs.xml ";
1100 linker_infiles += "| linker-subs.xml";
1101 if (!string.IsNullOrEmpty (linkDescriptor)) {
1102 linker_args += $"-x {linkDescriptor} ";
1103 foreach (var assembly in root_assemblies) {
1104 string filename = Path.GetFileName (assembly);
1105 linker_args += $"-p {usermode} {filename} -r linker-in/{filename} ";
1107 } else {
1108 foreach (var assembly in root_assemblies) {
1109 string filename = Path.GetFileName (assembly);
1110 linker_args += $"-a linker-in/{filename} ";
1114 // the linker does not consider these core by default
1115 foreach (var assembly in wasm_core_assemblies.Keys) {
1116 linker_args += $"-p {coremode} {assembly} ";
1118 if (linker_verbose) {
1119 linker_args += "--verbose ";
1121 linker_args += $"-d linker-in -d $bcl_dir -d $bcl_facades_dir -c {coremode} -u {usermode} ";
1122 foreach (var assembly in wasm_core_assemblies.Keys) {
1123 linker_args += $"-r {assembly} ";
1126 ninja.WriteLine ("build $builddir/linker-out: mkdir");
1127 ninja.WriteLine ($"build {linker_ofiles}: linker {linker_infiles}");
1128 ninja.WriteLine ($" linker_args={linker_args}");
1130 if (il_strip)
1131 ninja.WriteLine ("build $builddir/ilstrip-out: mkdir");
1133 foreach(var asset in assets) {
1134 var filename = Path.GetFileName (asset);
1135 var abs_path = Path.GetFullPath (asset);
1136 ninja.WriteLine ($"build $appdir/{filename}: cpifdiff {abs_path}");
1139 ninja.Close ();
1141 return 0;
1144 static void CopyFile(string sourceFileName, string destFileName, CopyType copyType, string typeFile = "")
1146 Console.WriteLine($"{typeFile}cp: {copyType} - {sourceFileName} -> {destFileName}");
1147 switch (copyType)
1149 case CopyType.Always:
1150 File.Copy(sourceFileName, destFileName, true);
1151 break;
1152 case CopyType.IfNewer:
1153 if (!File.Exists(destFileName))
1155 File.Copy(sourceFileName, destFileName);
1157 else
1159 var srcInfo = new FileInfo (sourceFileName);
1160 var dstInfo = new FileInfo (destFileName);
1162 if (srcInfo.LastWriteTime.Ticks > dstInfo.LastWriteTime.Ticks || srcInfo.Length > dstInfo.Length)
1163 File.Copy(sourceFileName, destFileName, true);
1164 else
1165 Console.WriteLine($" skipping: {sourceFileName}");
1167 break;
1168 default:
1169 File.Copy(sourceFileName, destFileName);
1170 break;