From 6324ed9417b2da86733d31a1a699be946606ff50 Mon Sep 17 00:00:00 2001
From: Marco A L Barbosa <malbarbo@gmail.com>
Date: Thu, 8 Jun 2017 17:05:30 -0300
Subject: [PATCH] Add support for emscripten tests

---
 ci/docker/asmjs-unknown-emscripten/Dockerfile | 20 +++++++++
 .../wasm32-unknown-emscripten/Dockerfile      | 20 +++++++++
 ci/emscripten-entry.sh                        | 19 +++++++++
 ci/emscripten.sh                              | 36 ++++++++++++++++
 ci/run.sh                                     |  4 ++
 libc-test/build.rs                            | 42 ++++++++++++++++---
 6 files changed, 136 insertions(+), 5 deletions(-)
 create mode 100644 ci/docker/asmjs-unknown-emscripten/Dockerfile
 create mode 100644 ci/docker/wasm32-unknown-emscripten/Dockerfile
 create mode 100755 ci/emscripten-entry.sh
 create mode 100644 ci/emscripten.sh

diff --git a/ci/docker/asmjs-unknown-emscripten/Dockerfile b/ci/docker/asmjs-unknown-emscripten/Dockerfile
new file mode 100644
index 00000000..7de85251
--- /dev/null
+++ b/ci/docker/asmjs-unknown-emscripten/Dockerfile
@@ -0,0 +1,20 @@
+FROM ubuntu:16.04
+
+RUN apt-get update && \
+    apt-get install -y --no-install-recommends \
+    ca-certificates \
+    curl \
+    gcc \
+    git \
+    libc6-dev \
+    python \
+    xz-utils
+
+COPY emscripten.sh /
+RUN bash /emscripten.sh
+
+ENV PATH=$PATH:/rust/bin
+
+COPY emscripten-entry.sh /
+ENTRYPOINT ["/emscripten-entry.sh"]
+
diff --git a/ci/docker/wasm32-unknown-emscripten/Dockerfile b/ci/docker/wasm32-unknown-emscripten/Dockerfile
new file mode 100644
index 00000000..7de85251
--- /dev/null
+++ b/ci/docker/wasm32-unknown-emscripten/Dockerfile
@@ -0,0 +1,20 @@
+FROM ubuntu:16.04
+
+RUN apt-get update && \
+    apt-get install -y --no-install-recommends \
+    ca-certificates \
+    curl \
+    gcc \
+    git \
+    libc6-dev \
+    python \
+    xz-utils
+
+COPY emscripten.sh /
+RUN bash /emscripten.sh
+
+ENV PATH=$PATH:/rust/bin
+
+COPY emscripten-entry.sh /
+ENTRYPOINT ["/emscripten-entry.sh"]
+
diff --git a/ci/emscripten-entry.sh b/ci/emscripten-entry.sh
new file mode 100755
index 00000000..acaebfe8
--- /dev/null
+++ b/ci/emscripten-entry.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+# Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+# file at the top-level directory of this distribution and at
+# http://rust-lang.org/COPYRIGHT.
+#
+# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+# option. This file may not be copied, modified, or distributed
+# except according to those terms.
+
+set -ex
+
+source /emsdk-portable/emsdk_env.sh &> /dev/null
+
+# emsdk-portable provides a node binary, but we need version 8 to run wasm
+export PATH="/node-v8.0.0-linux-x64/bin:$PATH"
+
+exec "$@"
diff --git a/ci/emscripten.sh b/ci/emscripten.sh
new file mode 100644
index 00000000..1a20643b
--- /dev/null
+++ b/ci/emscripten.sh
@@ -0,0 +1,36 @@
+# Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+# file at the top-level directory of this distribution and at
+# http://rust-lang.org/COPYRIGHT.
+#
+# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+# option. This file may not be copied, modified, or distributed
+# except according to those terms.
+
+set -ex
+
+cd /
+curl -L https://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz | \
+    tar -xz
+
+cd /emsdk-portable
+./emsdk update
+./emsdk install sdk-1.37.13-64bit
+./emsdk activate sdk-1.37.13-64bit
+
+# Compile and cache libc
+source ./emsdk_env.sh
+echo "main(){}" > a.c
+HOME=/emsdk-portable/ emcc a.c
+HOME=/emsdk-portable/ emcc -s BINARYEN=1 a.c
+rm -f a.*
+
+# Make emsdk usable by any user
+cp /root/.emscripten /emsdk-portable
+chmod a+rxw -R /emsdk-portable
+
+# node 8 is required to run wasm
+cd /
+curl -L https://nodejs.org/dist/v8.0.0/node-v8.0.0-linux-x64.tar.xz | \
+    tar -xJ
diff --git a/ci/run.sh b/ci/run.sh
index 3ddc7b39..08bb8cc9 100755
--- a/ci/run.sh
+++ b/ci/run.sh
@@ -168,6 +168,10 @@ case "$TARGET" in
     qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/libc-test
     ;;
 
+  *-emscripten)
+    cd $CARGO_TARGET_DIR/$TARGET/debug/deps/ && node ../libc-test.js
+  ;;
+
   *-rumprun-netbsd)
     rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/libc-test
     qemu-system-x86_64 -nographic -vga none -m 64 \
diff --git a/libc-test/build.rs b/libc-test/build.rs
index 619b8d34..0c01a951 100644
--- a/libc-test/build.rs
+++ b/libc-test/build.rs
@@ -13,7 +13,8 @@ fn main() {
     let linux = target.contains("unknown-linux");
     let android = target.contains("android");
     let apple = target.contains("apple");
-    let musl = target.contains("musl");
+    let emscripten = target.contains("asm");
+    let musl = target.contains("musl") || emscripten;
     let uclibc = target.contains("uclibc");
     let freebsd = target.contains("freebsd");
     let dragonfly = target.contains("dragonfly");
@@ -25,7 +26,7 @@ fn main() {
     let mut cfg = ctest::TestGenerator::new();
 
     // Pull in extra goodies
-    if linux || android {
+    if linux || android || emscripten {
         cfg.define("_GNU_SOURCE", None);
     } else if netbsd {
         cfg.define("_NETBSD_SOURCE", Some("1"));
@@ -165,7 +166,7 @@ fn main() {
         }
     }
 
-    if linux {
+    if linux || emscripten {
         cfg.header("mqueue.h");
         cfg.header("ucontext.h");
         cfg.header("sys/signalfd.h");
@@ -183,7 +184,7 @@ fn main() {
         cfg.header("shadow.h");
     }
 
-    if linux || android {
+    if linux || android || emscripten {
         cfg.header("malloc.h");
         cfg.header("net/ethernet.h");
         cfg.header("netpacket/packet.h");
@@ -237,7 +238,7 @@ fn main() {
         cfg.header("sys/ioctl_compat.h");
     }
 
-    if linux || freebsd || dragonfly || netbsd || apple {
+    if linux || emscripten || freebsd || dragonfly || netbsd || apple {
         if !uclibc {
             cfg.header("aio.h");
         }
@@ -356,6 +357,9 @@ fn main() {
             "FILE_ATTRIBUTE_INTEGRITY_STREAM" |
             "ERROR_NOTHING_TO_TERMINATE" if mingw => true,
 
+            // not defined
+            "IUTF8" | "ENOATTR" | "EXTA" | "EXTB" if emscripten => true,
+
             "SIG_IGN" => true, // sighandler_t weirdness
 
             // types on musl are defined a little differently
@@ -428,6 +432,34 @@ fn main() {
             "prlimit" | "prlimit64" |        // non-int in 2nd arg
             "strerror_r" if linux => true,   // actually xpg-something-or-other
 
+            // not defined or fails to link
+            "aio_cancel" | "aio_error" | "aio_fsync" | "aio_read" | "aio_read_write" | "aio_return"|
+            "aio_suspend" | "aio_write" | "clock_nanosleep" | "clone" | "daemon" | "endspent" |
+            "epoll_create" | "epoll_create1" | "epoll_ctl" | "epoll_pwait" | "epoll_wait" |
+            "eventfd" | "faccessat" | "fallocate" | "fgetxattr" | "flistxattr" | "fork" |
+            "forkpty" | "fremovexattr" | "fsetxattr" | "ftok" | "futimes" | "getdtablesize" |
+            "getgrgid" | "getgrnam" | "getgroups" | "getpgid" | "getpgrp" | "getpwnam_r" |
+            "getpwuid_r" | "getspent" | "getspnam" | "getxattr" | "initgroups" | "lgetxattr" |
+            "listxattr" | "llistxattr" | "lremovexattr" | "lsetxattr" | "lutimes" | "mount" |
+            "mq_close" | "mq_getattr" | "mq_open" | "mq_receive" | "mq_send" | "mq_setattr" |
+            "mq_unlink" | "msgctl" | "msgget" | "msgrcv" | "msgsnd" | "pclose" | "popen" |
+            "ppoll" | "prctl" | "prlimit" | "prlimit64" | "process_vm_readv" |
+            "process_vm_writev" | "pthread_atfork" | "pthread_attr_getguardsize" |
+            "pthread_kill" | "pthread_mutexattr_getpshared" | "pthread_mutex_timedlock" |
+            "pthread_sigmask" | "ptrace" | "quotactl" | "readahead" | "reboot" | "removexattr" |
+            "sched_getaffinity" | "sched_getparam" | "sched_get_priority_max" |
+            "sched_get_priority_min" | "sched_getscheduler" | "sched_rr_get_interval" |
+            "sched_setaffinity" | "sched_setparam" | "sched_setscheduler" | "sem_close" |
+            "semctl" | "semget" | "semop" | "sem_open" | "sem_timedwait" | "sem_unlink" |
+            "sendfile" | "setfsgid" | "setfsuid" | "setgroups" | "sethostname" | "setns" |
+            "setpgid" | "setpgrp" | "setspent" | "settimeofday" | "setxattr" | "shmat" |
+            "shmctl" | "shmdt" | "shmget" | "sigaltstack" | "signalfd" | "sigsuspend" |
+            "sigtimedwait" | "sigwait" | "sigwaitinfo" | "splice" | "sync_file_range" |
+            "syscall" | "sysinfo" | "tee" | "umount" | "umount2" | "unshare" | "vmsplice"
+                if emscripten => true,
+
+            // n if n.starts_with("epoll") && emscripten => true,
+
             // int vs uint. Sorry musl, your prototype declarations are "correct" in the sense that
             // they match the interface defined by Linux verbatim, but they conflict with other
             // send*/recv* syscalls
-- 
GitLab