From 881330e49ec8a029663a5b03bb1702355464cc58 Mon Sep 17 00:00:00 2001
From: "Frank A. Krueger"
Date: Thu, 8 Mar 2018 13:30:06 -0800
Subject: [PATCH] Add wasm build
---
Ooui.Wasm/Dockerfile | 66 ++++++++++++++++++++++++++++++++++
Ooui.Wasm/Makefile | 18 ++++++++++
Ooui.Wasm/System.Core.cs | 16 +++++++++
Ooui.Wasm/hello.cs | 77 ++++++++++++++++++++++++++++++++++++++++
Ooui.Wasm/index.html | 44 +++++++++++++++++++++++
5 files changed, 221 insertions(+)
create mode 100644 Ooui.Wasm/Dockerfile
create mode 100644 Ooui.Wasm/Makefile
create mode 100644 Ooui.Wasm/System.Core.cs
create mode 100644 Ooui.Wasm/hello.cs
create mode 100644 Ooui.Wasm/index.html
diff --git a/Ooui.Wasm/Dockerfile b/Ooui.Wasm/Dockerfile
new file mode 100644
index 0000000..bf88989
--- /dev/null
+++ b/Ooui.Wasm/Dockerfile
@@ -0,0 +1,66 @@
+FROM alpine:3.7
+
+RUN apk update \
+ && apk upgrade \
+ && apk add --no-cache g++ musl-dev make cmake git subversion python
+
+RUN mkdir /mono-wasm \
+ && cd /mono-wasm \
+ && git clone --depth 1 https://github.com/lrz/mono-wasm.git build
+
+# RUN cd /mono-wasm \
+# && svn co --quiet http://llvm.org/svn/llvm-project/llvm/trunk llvm \
+# && cd llvm/tools \
+# && svn co --quiet http://llvm.org/svn/llvm-project/cfe/trunk clang \
+# && svn co --quiet http://llvm.org/svn/llvm-project/lld/trunk lld
+
+RUN cd /mono-wasm \
+ && wget http://releases.llvm.org/5.0.1/llvm-5.0.1.src.tar.xz \
+ && unxz llvm-5.0.1.src.tar.xz \
+ && tar xf llvm-5.0.1.src.tar \
+ && rm llvm-5.0.1.src.tar \
+ && mv llvm-5.0.1.src llvm \
+ && cd llvm/tools \
+ && wget http://releases.llvm.org/5.0.1/cfe-5.0.1.src.tar.xz \
+ && unxz cfe-5.0.1.src.tar.xz \
+ && tar xf cfe-5.0.1.src.tar \
+ && rm cfe-5.0.1.src.tar \
+ && mv cfe-5.0.1.src clang \
+ && wget http://releases.llvm.org/5.0.1/lld-5.0.1.src.tar.xz \
+ && unxz lld-5.0.1.src.tar.xz \
+ && tar xf lld-5.0.1.src.tar \
+ && rm lld-5.0.1.src.tar \
+ && mv lld-5.0.1.src lld
+
+RUN mkdir /mono-wasm/llvm-build \
+ && cd /mono-wasm/llvm-build \
+ && cmake -G "Unix Makefiles" -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DCMAKE_BUILD_TYPE=Release ../llvm \
+ && make -j7
+
+RUN cd /mono-wasm \
+ && git clone --depth 1 https://github.com/mono/llvm.git llvm-mono
+
+ENV CXXFLAGS="-fpermissive"
+
+RUN mkdir /mono-wasm/llvm-mono-build \
+ && cd /mono-wasm/llvm-mono-build \
+ && cmake -G "Unix Makefiles" -DCMAKE_OSX_ARCHITECTURES="i386;x86_64" -DLLVM_TARGETS_TO_BUILD=X86 ../llvm-mono \
+ && cp -R ../llvm-mono/include/* include/ \
+ && sed -i "s/#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)/#if 0/" /usr/include/stdio.h \
+ && sed -i "s/#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)/#if 0/" /usr/include/sys/stat.h \
+ # && sed -i "s/namespace llvm/#undef fopen\n#undef fopen64\n#undef fseeko\n#undef fseeko64\n#undef fstat\n#undef fstat64\n#undef ftello\n#undef ftello64\n#undef lstat\n#undef lstat64\n#undef stat\n#undef stat64\n#undef tmpfile\n#undef tmpfile64\n\nnamespace llvm/" /mono-wasm/llvm-mono-build/include/llvm/Target/TargetLibraryInfo.h
+ && make -j7
+
+ENV CXXFLAGS=""
+
+RUN cd /mono-wasm \
+ && git clone --depth 1 https://github.com/lrz/mono-wasm-mono.git mono-compiler
+
+RUN apk add --no-cache bash autoconf automake libtool linux-headers \
+ && cd /mono-wasm/mono-compiler \
+ && ./autogen.sh --with-cross-offsets=offsets-wasm32.h CFLAGS="-DCOMPILE_WASM32 -DMONO_CROSS_COMPILE" CXXFLAGS="-DCOMPILE_WASM32 -DMONO_CROSS_COMPILE" --disable-boehm --with-sigaltstack=no --enable-llvm --enable-llvm-runtime --with-llvm=../llvm-mono-build --disable-btls --with-runtime_preset=testing_aot_full
+
+# RUN cd /mono-wasm/mono-compiler/eglib \
+# && make -j7 \
+# && cd ../mono \
+# && make -j7
diff --git a/Ooui.Wasm/Makefile b/Ooui.Wasm/Makefile
new file mode 100644
index 0000000..cd11c33
--- /dev/null
+++ b/Ooui.Wasm/Makefile
@@ -0,0 +1,18 @@
+OOUI_SRCS=$(wildcard ../Ooui/*.cs)
+
+all: hello.exe output/index.wasm output/index.html
+
+hello.exe: hello.cs Ooui.dll Makefile
+ mcs -nostdlib -noconfig -r:mono-wasm-macos/dist/lib/mscorlib.dll -r:Ooui.dll hello.cs -out:hello.exe
+
+Ooui.dll: $(OOUI_SRCS) System.Core.cs Makefile
+ mcs -nostdlib -noconfig -r:mono-wasm-macos/dist/lib/mscorlib.dll -define:NO_PROCESS,NO_SERVER,NO_XML System.Core.cs $(OOUI_SRCS) -out:Ooui.dll
+
+output/index.wasm: hello.exe Makefile
+ mono-wasm-macos/dist/bin/mono-wasm -i hello.exe -o output
+
+output/index.html: index.html
+ cp index.html output
+
+clean:
+ rm -rf build output hello.exe
diff --git a/Ooui.Wasm/System.Core.cs b/Ooui.Wasm/System.Core.cs
new file mode 100644
index 0000000..f8d5d11
--- /dev/null
+++ b/Ooui.Wasm/System.Core.cs
@@ -0,0 +1,16 @@
+using System;
+
+namespace System.ComponentModel
+{
+ public interface INotifyPropertyChanged
+ {
+
+ }
+
+ public class PropertyChangedEventArgs : EventArgs
+ {
+
+ }
+
+ public delegate void PropertyChangedEventHandler (object sender, PropertyChangedEventArgs e);
+}
\ No newline at end of file
diff --git a/Ooui.Wasm/hello.cs b/Ooui.Wasm/hello.cs
new file mode 100644
index 0000000..b802c97
--- /dev/null
+++ b/Ooui.Wasm/hello.cs
@@ -0,0 +1,77 @@
+using Mono.WebAssembly;
+using System;
+
+class Hello
+{
+ static int Factorial(int n)
+ {
+ if (n == 0) {
+ return 1;
+ }
+ return n * Factorial(n - 1);
+ }
+
+ // This function is called from the browser by JavaScript.
+ // Here we calculate the factorial of the given number then use the
+ // Mono.WebAssembly API to retrieve the element from the DOM and set its
+ // innerText property to the factorial result.
+ static void FactorialInElement(int n, string element_id)
+ {
+ Console.WriteLine(
+ "Calculating factorial of {0} into DOM element {1}",
+ n, element_id);
+
+ int f = Factorial(n);
+
+ var elem = HtmlPage.Document.GetElementById(element_id);
+ elem.InnerText = f.ToString();
+ }
+
+ static void PrintHtmlElements(HtmlElement elem, int level)
+ {
+ string str = "";
+ for (int i = 0; i < level; i++) {
+ str += " ";
+ }
+
+ str += $"<{elem.TagName}";
+
+ foreach (var name in elem.AttributeNames) {
+ var value = elem.GetAttribute(name);
+ str += $" {name}='{value}'";
+ }
+
+ str += ">";
+
+ Console.WriteLine(str);
+
+ var list = elem.Children;
+ for (int i = 0; i < list.Count; i++) {
+ var child = list[i];
+ PrintHtmlElements(child, level + 1);
+ }
+ }
+
+ static int Main(string[] args)
+ {
+ Ooui.Div odiv = new Ooui.Div ();
+ int f = Factorial(6);
+ HtmlPage.Window.Alert($"Hello world! factorial(6) -> {odiv}");
+
+ var bi = HtmlPage.BrowserInformation;
+ Console.WriteLine($"BrowserInformation: Name {bi.Name} BrowserVersion {bi.BrowserVersion} UserAgent {bi.UserAgent} Platform {bi.Platform} CookiesEnabled {bi.CookiesEnabled} ProductName {bi.ProductName}");
+
+ var d = HtmlPage.Document;
+ Console.WriteLine($"Document Location: {d.Location}");
+
+ PrintHtmlElements(d.DocumentElement, 0);
+
+ var p = d.CreateElement("p");
+ p.InnerText = "This text was added at runtime.";
+ d.Body.AppendChild(p);
+
+ if (args.Length > 0) FactorialInElement(0, ""); // this is a hack so that the linker does not remove the FactorialInElement() method
+
+ return f;
+ }
+}
diff --git a/Ooui.Wasm/index.html b/Ooui.Wasm/index.html
new file mode 100644
index 0000000..121112d
--- /dev/null
+++ b/Ooui.Wasm/index.html
@@ -0,0 +1,44 @@
+
+
+
+
+ WebAssembly Example
+
+
+
+
+
+
+
+
+
+
+