From 117f44219a0114558ffeb61d1a1a714a7672683b Mon Sep 17 00:00:00 2001 From: jonas echterhoff Date: Thu, 3 Nov 2011 15:55:11 +0100 Subject: [PATCH] NaCl build scripts from Elijah --- nacl/README | 35 ++++++ nacl/common.sh | 206 +++++++++++++++++++++++++++++++ nacl/config-nacl-runtime.cache | 19 +++ nacl/config-nacl-runtime64.cache | 19 +++ nacl/nacl-common.sh | 19 +++ nacl/nacl-install-all.sh | 24 ++++ nacl/nacl-mono-config-cache | 16 +++ nacl/nacl-mono.sh | 75 +++++++++++ nacl/nacl-runtime-mono.sh | 103 ++++++++++++++++ nacl/nacl64-mono-config-cache | 16 +++ nacl/nacl64-mono.sh | 73 +++++++++++ nacl/normal-mono.sh | 51 ++++++++ nacl/test/hw.cs | 60 +++++++++ nacl/test/my.c | 163 ++++++++++++++++++++++++ nacl/test/nacl | 164 ++++++++++++++++++++++++ 15 files changed, 1043 insertions(+) create mode 100644 nacl/README create mode 100644 nacl/common.sh create mode 100644 nacl/config-nacl-runtime.cache create mode 100644 nacl/config-nacl-runtime64.cache create mode 100644 nacl/nacl-common.sh create mode 100755 nacl/nacl-install-all.sh create mode 100644 nacl/nacl-mono-config-cache create mode 100755 nacl/nacl-mono.sh create mode 100755 nacl/nacl-runtime-mono.sh create mode 100644 nacl/nacl64-mono-config-cache create mode 100755 nacl/nacl64-mono.sh create mode 100755 nacl/normal-mono.sh create mode 100644 nacl/test/hw.cs create mode 100644 nacl/test/my.c create mode 100755 nacl/test/nacl diff --git a/nacl/README b/nacl/README new file mode 100644 index 00000000000..fad7e9179f9 --- /dev/null +++ b/nacl/README @@ -0,0 +1,35 @@ +Quick guide +=========== + +Prerequistites +-------------- +1. Native Client SDK (see http://code.google.com/chrome/nativeclient) +2. Mono with NaCl support (you have it if you're reading this file) + + +Directory conventions used in this document +(your directories will differ...) + /path/to/naclsdk Native Client SDK directory + /path/to/mono Mono for NaCl + + +Instructions +------------ +1. If this is a clean checkout, you may need to do this from /path/to/mono: + ./autogen.sh && make distclean + +2. Setting your environment: + export NACL_SDK_ROOT=/path/to/naclsdk + export NACL_NEWLIB=1 + +3. Run ./nacl-install-all.sh to install: + *) 32-bit NaCl Mono AOT compiler (nacl-mono) + *) 64-bit NaCl Mono AOT compiler (nacl64-mono) + *) 32-bit NaCl Mono runtime (+JIT compiler) + *) 64-bit NaCl Mono runtime (+JIT compiler) + *) Non-nacl Mono installation (needed for mscorlib.dll, etc) + +Simple Test (requires sel_ldr to run) + cd /path/to/mono/nacl/test + ./nacl [normal,aot,regression] (defaults to nacl,jit,simple test) + diff --git a/nacl/common.sh b/nacl/common.sh new file mode 100644 index 00000000000..2adc3a90e98 --- /dev/null +++ b/nacl/common.sh @@ -0,0 +1,206 @@ +# Copyright (c) 2011 The Native Client Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that be +# found in the LICENSE file. +# + +set -o nounset +set -o errexit + +# scripts that source this file must be run from within packages tree +readonly SAVE_PWD=$(pwd) + +# Pick platform directory for compiler. +readonly OS_NAME=$(uname -s) +if [ $OS_NAME = "Darwin" ]; then + readonly OS_SUBDIR="mac" + readonly OS_SUBDIR_SHORT="mac" +elif [ $OS_NAME = "Linux" ]; then + readonly OS_SUBDIR="linux" + readonly OS_SUBDIR_SHORT="linux" +else + readonly OS_SUBDIR="windows" + readonly OS_SUBDIR_SHORT="win" +fi + +readonly MACHINE=$(uname -m) +if [ $MACHINE = "x86_64" ]; then + readonly TARGET_BITSIZE=${TARGET_BITSIZE:-"64"} + readonly HOST_BITSIZE=${HOST_BITSIZE:-"64"} +else + # uname -m reports i686 on Linux and i386 on Mac + readonly TARGET_BITSIZE=${TARGET_BITSIZE:-"32"} + readonly HOST_BITSIZE=${HOST_BITSIZE:-"32"} +fi + +if [ $TARGET_BITSIZE == "64" ]; then + readonly TARGET_BIT_PREFIX="64" + readonly CROSS_ID=x86_64 +else + readonly TARGET_BIT_PREFIX="" + readonly CROSS_ID=i686 +fi +# we might want to override the detected host platform (e.g. on OSX 10.6) +if [ $HOST_BITSIZE == "64" ]; then + readonly HOST_BIT_PREFIX="64" +else + readonly HOST_BIT_PREFIX="" +fi + +export NACL_CROSS_PREFIX=${CROSS_ID}-nacl +export NACL_CROSS_PREFIX_DASH=${NACL_CROSS_PREFIX}- + +readonly NACL_NEWLIB=${NACL_NEWLIB:-"0"} + +if [ $NACL_NEWLIB = "1" ]; then + readonly NACL_SDK_BASE=${NACL_SDK_ROOT}/toolchain/${OS_SUBDIR_SHORT}_x86_newlib +else + readonly NACL_SDK_BASE=${NACL_SDK_ROOT}/toolchain/${OS_SUBDIR_SHORT}_x86 +fi + +readonly NACL_BIN_PATH=${NACL_SDK_BASE}/bin +export NACLCC=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}gcc +export NACLCXX=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}g++ +export NACLAR=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}ar +export NACLRANLIB=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}ranlib +export NACLLD=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}ld +export NACLAS=${NACL_BIN_PATH}/${NACL_CROSS_PREFIX_DASH}as + +# NACL_SDK_GCC_SPECS_PATH is where nacl-gcc 'specs' file will be installed +readonly NACL_SDK_GCC_SPECS_PATH=${NACL_SDK_BASE}/lib/gcc/x86_64-nacl/4.4.3 + +# NACL_SDK_USR is where the headers, libraries, etc. will be installed +readonly NACL_SDK_USR=${NACL_SDK_BASE}/${NACL_CROSS_PREFIX}/usr +readonly NACL_SDK_USR_INCLUDE=${NACL_SDK_USR}/include +readonly NACL_SDK_USR_LIB=${NACL_SDK_USR}/lib + + +###################################################################### +# Helper functions +###################################################################### + +Banner() { + echo "######################################################################" + echo $* + echo "######################################################################" +} + + +VerifyPath() { + # make sure path isn't all slashes (possibly from an unset variable) + local PATH=$1 + local TRIM=${PATH##/} + if [ ${#TRIM} -ne 0 ]; then + return 0 + else + return 1 + fi +} + + +ChangeDir() { + local NAME=$1 + if VerifyPath ${NAME}; then + cd ${NAME} + else + echo "ChangeDir called with bad path." + exit -1 + fi +} + + +Remove() { + local NAME=$1 + if VerifyPath ${NAME}; then + rm -rf ${NAME} + else + echo "Remove called with bad path." + exit -1 + fi +} + + +MakeDir() { + local NAME=$1 + if VerifyPath ${NAME}; then + mkdir -p ${NAME} + else + echo "MakeDir called with bad path." + exit -1 + fi +} + + +PatchSpecFile() { + # fix up spaces so gcc sees entire path + local SED_SAFE_SPACES_USR_INCLUDE=${NACL_SDK_USR_INCLUDE/ /\ /} + local SED_SAFE_SPACES_USR_LIB=${NACL_SDK_USR_LIB/ /\ /} + # have nacl-gcc dump specs file & add include & lib search paths + ${NACL_SDK_BASE}/bin/x86_64-nacl-gcc -dumpspecs |\ + sed "/*cpp:/{ + N + s|$| -I${SED_SAFE_SPACES_USR_INCLUDE}| + }" |\ + sed "/*link_libgcc:/{ + N + s|$| -L${SED_SAFE_SPACES_USR_LIB}| + }" >${NACL_SDK_GCC_SPECS_PATH}/specs +} + + +DefaultConfigureStep() { + Banner "Configuring ${PACKAGE_NAME}" + # export the nacl tools + export CC=${NACLCC} + export CXX=${NACLCXX} + export AR=${NACLAR} + export RANLIB=${NACLRANLIB} + export PKG_CONFIG_PATH=${NACL_SDK_USR_LIB}/pkgconfig + export PKG_CONFIG_LIBDIR=${NACL_SDK_USR_LIB} + export PATH=${NACL_BIN_PATH}:${PATH}; + ChangeDir ${NACL_PACKAGES_REPOSITORY}/${PACKAGE_NAME} + Remove ${PACKAGE_NAME}-build + MakeDir ${PACKAGE_NAME}-build + cd ${PACKAGE_NAME}-build + ../configure \ + --host=nacl \ + --disable-shared \ + --prefix=${NACL_SDK_USR} \ + --exec-prefix=${NACL_SDK_USR} \ + --libdir=${NACL_SDK_USR_LIB} \ + --oldincludedir=${NACL_SDK_USR_INCLUDE} \ + --with-http=off \ + --with-html=off \ + --with-ftp=off \ + --with-x=no +} + + +DefaultBuildStep() { + # assumes pwd has makefile + make clean + make -j4 +} + + +DefaultInstallStep() { + # assumes pwd has makefile + make install +} + + +DefaultCleanUpStep() { + PatchSpecFile + ChangeDir ${SAVE_PWD} +} + + +DefaultPackageInstall() { + DefaultPreInstallStep + DefaultDownloadStep + DefaultExtractStep + DefaultPatchStep + DefaultConfigureStep + DefaultBuildStep + DefaultInstallStep + DefaultCleanUpStep +} diff --git a/nacl/config-nacl-runtime.cache b/nacl/config-nacl-runtime.cache new file mode 100644 index 00000000000..4bd26c474f8 --- /dev/null +++ b/nacl/config-nacl-runtime.cache @@ -0,0 +1,19 @@ +# This file is a shell script that caches the results of configure +# tests run on this system so they can be shared between configure +# scripts and configure runs, see configure's option --config-cache. +# It is not useful on other systems. If it contains results you don't +# want to keep, you may remove or edit it. +# +# config.status only pays attention to the cache file if you give it +# the --recheck option to rerun configure. +# +# `ac_cv_env_foo' variables (set or unset) will be overridden when +# loading this file, other *unset* `ac_cv_foo' will be assigned the +# following values. + +ac_cv_func_mmap=${ac_cv_func_mmap=no} +ac_cv_var_timezone=${ac_cv_var_timezone=yes} +ac_cv_host=${ac_cv_host=i686-pc-nacl} +ac_cv_target=${ac_cv_target=i686-pc-nacl} +ac_cv_func_backtrace_symbols=${ac_cv_func_backtrace_symbols=no} +mono_cv_uscore=${mono_cv_uscore=no} diff --git a/nacl/config-nacl-runtime64.cache b/nacl/config-nacl-runtime64.cache new file mode 100644 index 00000000000..b952fef84c6 --- /dev/null +++ b/nacl/config-nacl-runtime64.cache @@ -0,0 +1,19 @@ +# This file is a shell script that caches the results of configure +# tests run on this system so they can be shared between configure +# scripts and configure runs, see configure's option --config-cache. +# It is not useful on other systems. If it contains results you don't +# want to keep, you may remove or edit it. +# +# config.status only pays attention to the cache file if you give it +# the --recheck option to rerun configure. +# +# `ac_cv_env_foo' variables (set or unset) will be overridden when +# loading this file, other *unset* `ac_cv_foo' will be assigned the +# following values. + +ac_cv_func_mmap=${ac_cv_func_mmap=no} +ac_cv_var_timezone=${ac_cv_var_timezone=yes} +ac_cv_host=${ac_cv_host=x86_64-pc-nacl} +ac_cv_target=${ac_cv_target=x86_64-pc-nacl} +ac_cv_func_backtrace_symbols=${ac_cv_func_backtrace_symbols=no} +mono_cv_uscore=${mono_cv_uscore=no} diff --git a/nacl/nacl-common.sh b/nacl/nacl-common.sh new file mode 100644 index 00000000000..94dde8ec324 --- /dev/null +++ b/nacl/nacl-common.sh @@ -0,0 +1,19 @@ + +CopyNormalMonoLibs() { + NORMAL_MSCORLIB_DLL=$MONO_TRUNK_NACL/normal-mono/lib/mono/4.0/mscorlib.dll + if [ ! -f ${NORMAL_MSCORLIB_DLL} ] + then + Banner "Normal mscorlib.dll not found, building normal mono" + cd ${MONO_TRUNK_NACL} + ./normal-mono.sh + fi + if [ ! -f ${NORMAL_MSCORLIB_DLL} ] + then + Banner "Normal mscorlib.dll not found after normal mono build, exiting..." + exit -1 + fi + Banner "Copying normal-mono libs to install dir" + mkdir -p ${INSTALL_PATH}/lib/mono + cp -R ${MONO_TRUNK_NACL}/normal-mono/lib/mono/* ${INSTALL_PATH}/lib/mono/ +} + diff --git a/nacl/nacl-install-all.sh b/nacl/nacl-install-all.sh new file mode 100755 index 00000000000..d32f53216cb --- /dev/null +++ b/nacl/nacl-install-all.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +if [[ ! ${NACL_SDK_ROOT} || ! ${NACL_SDK_ROOT-_} ]]; then + echo "Set NACL_SDK_ROOT to the location of your Native Client SDK" + exit 1 +fi + + +if [[ ${TARGET_BITSIZE} && ${TARGET_BITSIZE-_} ]]; then + echo "Unset TARGET_BITSIZE before running this script" + exit 1 +fi + +# 32-bit NaCl AOT cross-compiler +./nacl-mono.sh +# 64-bit NaCl AOT cross-compiler +./nacl64-mono.sh +# 32-bit NaCl Mono runtime (+JIT compiler) +TARGET_BITSIZE=32 ./nacl-runtime-mono.sh +# 64-bit NaCl Mono runtime (+JIT compiler) +TARGET_BITSIZE=64 ./nacl-runtime-mono.sh + diff --git a/nacl/nacl-mono-config-cache b/nacl/nacl-mono-config-cache new file mode 100644 index 00000000000..830854bb661 --- /dev/null +++ b/nacl/nacl-mono-config-cache @@ -0,0 +1,16 @@ +# This file is a shell script that caches the results of configure +# tests run on this system so they can be shared between configure +# scripts and configure runs, see configure's option --config-cache. +# It is not useful on other systems. If it contains results you don't +# want to keep, you may remove or edit it. +# +# config.status only pays attention to the cache file if you give it +# the --recheck option to rerun configure. +# +# `ac_cv_env_foo' variables (set or unset) will be overridden when +# loading this file, other *unset* `ac_cv_foo' will be assigned the +# following values. + +mono_cv_uscore=${mono_cv_uscore=no} +ac_cv_target=${ac_cv_target=i686-pc-nacl} + diff --git a/nacl/nacl-mono.sh b/nacl/nacl-mono.sh new file mode 100755 index 00000000000..f34468cfb24 --- /dev/null +++ b/nacl/nacl-mono.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +# nacl-mono.sh +# +# usage: nacl-mono.sh +# +# this script builds a compiler for 32-bit NaCl code +# (installed in ./compiler folder) +# + +TARGET_BITSIZE=32 + +source common.sh +source nacl-common.sh + +readonly MONO_TRUNK_NACL=$(pwd) + +readonly PACKAGE_NAME=nacl-mono-build + +readonly INSTALL_PATH=${NACL_SDK_USR} + + + +CustomConfigureStep() { + Banner "Configuring ${PACKAGE_NAME}" + set +e + cd ${PACKAGE_NAME} + make distclean + cd ${MONO_TRUNK_NACL} + set -e + Remove ${PACKAGE_NAME} + MakeDir ${PACKAGE_NAME} + cd ${PACKAGE_NAME} + cp ../nacl-mono-config-cache ../nacl-mono-config-cache.temp + if [ $HOST_BITSIZE = "64" ]; then + ../../configure \ + CC='cc -m32' CXX='g++ -m32' \ + --host=i386-pc-linux \ + --build=amd64-pc-linux \ + --target=nacl \ + --prefix=${INSTALL_PATH} \ + --exec-prefix=${INSTALL_PATH} \ + --with-tls=pthread \ + --enable-nacl-codegen \ + --disable-mono-debugger \ + --disable-mcs-build \ + --with-sigaltstack=no \ + --with-sgen=no \ + --cache-file=../nacl-mono-config-cache.temp + else + ../../configure \ + --target=nacl \ + --prefix=${INSTALL_PATH} \ + --exec-prefix=${INSTALL_PATH} \ + --with-tls=pthread \ + --enable-nacl-codegen \ + --disable-mono-debugger \ + --disable-mcs-build \ + --with-sigaltstack=no \ + --with-sgen=no \ + --cache-file=../nacl-mono-config-cache.temp + fi + + + rm ../nacl-mono-config-cache.temp +} + +CustomPackageInstall() { + CustomConfigureStep + DefaultBuildStep + DefaultInstallStep +} + +CustomPackageInstall +exit 0 diff --git a/nacl/nacl-runtime-mono.sh b/nacl/nacl-runtime-mono.sh new file mode 100755 index 00000000000..f7c52b95e9f --- /dev/null +++ b/nacl/nacl-runtime-mono.sh @@ -0,0 +1,103 @@ +#!/bin/bash +# Copyright (c) 2009 The Native Client Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that be +# found in the LICENSE file. +# + +# nacl-runtime-mono.sh +# +# usage: nacl-runtime-mono.sh +# +# this script builds mono runtime for Native Client +# + +readonly MONO_TRUNK_NACL=$(pwd) + +source common.sh +source nacl-common.sh + +readonly PACKAGE_NAME=runtime${TARGET_BIT_PREFIX}-build +readonly INSTALL_PATH=${NACL_SDK_USR} + + +CustomConfigureStep() { + Banner "Configuring ${PACKAGE_NAME}" + # export the nacl tools + set +e + if [ -f ${PACKAGE_NAME}/Makefile ] + then + cd ${PACKAGE_NAME} + fi + make distclean + cd ${MONO_TRUNK_NACL} + set -e + if [ $NACL_NEWLIB = "1" ]; then + NACL_NEWLIB_DEFINE=-DNACL_NEWLIB + CONFIG_OPTS="--host=nacl${TARGET_BIT_PREFIX} --disable-shared --cache-file=../config-nacl-runtime${TARGET_BIT_PREFIX}.cache.temp" + else + NACL_NEWLIB_DEFINE= + CONFIG_OPTS="--enable-shared --disable-parallel-mark" + if [ $TARGET_BITSIZE == '32' ]; then + CONFIG_OPTS="--host=i386-pc-linux --build=i386-pc-linux --target=i386-pc-linux ${CONFIG_OPTS}" + else + CONFIG_OPTS="--host=x86_64-pc-linux --build=x86_64-pc-linux --target=x86_64-pc-linux ${CONFIG_OPTS}" + fi + # UGLY hack to allow dynamic linking + sed -i -e s/elf_i386/elf_nacl/ -e s/elf_x86_64/elf64_nacl/ ../configure + sed -i -e s/elf_i386/elf_nacl/ -e s/elf_x86_64/elf64_nacl/ ../libgc/configure + sed -i -e s/elf_i386/elf_nacl/ -e s/elf_x86_64/elf64_nacl/ ../eglib/configure + fi + cp config-nacl-runtime${TARGET_BIT_PREFIX}.cache config-nacl-runtime${TARGET_BIT_PREFIX}.cache.temp + Remove ${PACKAGE_NAME} + MakeDir ${PACKAGE_NAME} + cd ${PACKAGE_NAME} + CC=${NACLCC} CXX=${NACLCXX} AR=${NACLAR} RANLIB=${NACLRANLIB} PKG_CONFIG_PATH=${NACL_SDK_USR_LIB}/pkgconfig LD="${NACLLD}" \ + PKG_CONFIG_LIBDIR=${NACL_SDK_USR_LIB} PATH=${NACL_BIN_PATH}:${PATH} LIBS="-lnacl_dyncode -lc -lg -lnosys -lnacl" \ + CFLAGS="-g -O2 -D_POSIX_PATH_MAX=256 -DPATH_MAX=256 ${NACL_NEWLIB_DEFINE}" ../../configure \ + ${CONFIG_OPTS} \ + --exec-prefix=${INSTALL_PATH} \ + --libdir=${INSTALL_PATH}/lib \ + --prefix=${INSTALL_PATH} \ + --oldincludedir=${INSTALL_PATH}/include \ + --disable-mcs-build \ + --with-glib=embedded \ + --with-tls=pthread \ + --enable-threads=posix \ + --without-sigaltstack \ + --without-mmap \ + --with-gc=included \ + --enable-nacl-gc \ + --with-sgen=no \ + --enable-nls=no \ + --enable-nacl-codegen + echo "// --- Native Client runtime below" >> config.h + echo "#define pthread_cleanup_push(x, y)" >> config.h + echo "#define pthread_cleanup_pop(x)" >> config.h + echo "#undef HAVE_EPOLL" >> config.h + echo "#undef HAVE_WORKING_SIGALTSTACK" >> config.h + echo "extern long int timezone;" >> config.h + echo "extern int daylight;" >> config.h + echo "#define sem_trywait(x) sem_wait(x)" >> config.h + echo "#define sem_timedwait(x,y) sem_wait(x)" >> config.h +if [ $NACL_NEWLIB = "1" ]; then + echo "#define getdtablesize() (32768)" >> config.h +fi + echo "// --- Native Client runtime below" >> eglib/src/eglib-config.h + echo "#undef G_BREAKPOINT" >> eglib/src/eglib-config.h + echo "#define G_BREAKPOINT() G_STMT_START { __asm__ (\"hlt\"); } G_STMT_END" >> eglib/src/eglib-config.h + rm ../config-nacl-runtime${TARGET_BIT_PREFIX}.cache.temp +} + +CustomInstallStep() { + make install + CopyNormalMonoLibs +} + +CustomPackageInstall() { + CustomConfigureStep + DefaultBuildStep + CustomInstallStep +} + +CustomPackageInstall +exit 0 diff --git a/nacl/nacl64-mono-config-cache b/nacl/nacl64-mono-config-cache new file mode 100644 index 00000000000..6e2d0423801 --- /dev/null +++ b/nacl/nacl64-mono-config-cache @@ -0,0 +1,16 @@ +# This file is a shell script that caches the results of configure +# tests run on this system so they can be shared between configure +# scripts and configure runs, see configure's option --config-cache. +# It is not useful on other systems. If it contains results you don't +# want to keep, you may remove or edit it. +# +# config.status only pays attention to the cache file if you give it +# the --recheck option to rerun configure. +# +# `ac_cv_env_foo' variables (set or unset) will be overridden when +# loading this file, other *unset* `ac_cv_foo' will be assigned the +# following values. + +mono_cv_uscore=${mono_cv_uscore=no} +ac_cv_target=${ac_cv_target=x86_64-pc-nacl} + diff --git a/nacl/nacl64-mono.sh b/nacl/nacl64-mono.sh new file mode 100755 index 00000000000..3f033561296 --- /dev/null +++ b/nacl/nacl64-mono.sh @@ -0,0 +1,73 @@ +#!/bin/bash + +# nacl64-mono.sh +# +# usage: nacl64-mono.sh +# +# this script builds a compiler for 64-bit NaCl code +# (installed in ./compiler folder) +# + +TARGET_BITSIZE=64 + +source common.sh +source nacl-common.sh + +readonly MONO_TRUNK_NACL=$(pwd) + +readonly PACKAGE_NAME=nacl64-mono-build + +readonly INSTALL_PATH=${NACL_SDK_USR} + +CustomConfigureStep() { + Banner "Configuring ${PACKAGE_NAME}" + set +e + cd ${PACKAGE_NAME} + make distclean + cd ${MONO_TRUNK_NACL} + set -e + Remove ${PACKAGE_NAME} + MakeDir ${PACKAGE_NAME} + cd ${PACKAGE_NAME} + cp ../nacl64-mono-config-cache ../nacl64-mono-config-cache.temp + if [ $HOST_BITSIZE = "64" ]; then + ../../configure \ + CC='cc -m32' CXX='g++ -m32' \ + --host=i386-pc-linux \ + --build=amd64-pc-linux \ + --target=nacl64 \ + --prefix=${INSTALL_PATH} \ + --exec-prefix=${INSTALL_PATH} \ + --with-tls=pthread \ + --enable-nacl-codegen \ + --disable-mono-debugger \ + --disable-mcs-build \ + --with-sigaltstack=no \ + --with-sgen=no \ + --cache-file=../nacl64-mono-config-cache.temp + else + ../../configure \ + --target=nacl64 \ + --prefix=${INSTALL_PATH} \ + --exec-prefix=${INSTALL_PATH} \ + --with-tls=pthread \ + --enable-nacl-codegen \ + --disable-mono-debugger \ + --disable-mcs-build \ + --with-sigaltstack=no \ + --with-sgen=no \ + --cache-file=../nacl64-mono-config-cache.temp + fi + + + rm ../nacl64-mono-config-cache.temp +} + +CustomPackageInstall() { + CustomConfigureStep + DefaultBuildStep + DefaultInstallStep +} + +CustomPackageInstall +exit 0 diff --git a/nacl/normal-mono.sh b/nacl/normal-mono.sh new file mode 100755 index 00000000000..aca073ff978 --- /dev/null +++ b/nacl/normal-mono.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# Copyright (c) 2009 The Native Client Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that be +# found in the LICENSE file. +# + +# normal-mono.sh +# +# usage: normal-mono.sh +# +# this script builds normal x86 mono +# (installed in ./normal folder) +# + +readonly MONO_TRUNK_NACL=$(pwd) + +readonly PACKAGE_NAME=mono-normal-build + +source common.sh + + +CustomConfigureStep() { + Banner "Configuring ${PACKAGE_NAME}" + set +e + if [ -f ${PACKAGE_NAME}/Makefile ] + then + cd ${PACKAGE_NAME} + make distclean + fi + cd ${MONO_TRUNK_NACL} + set -e + Remove ${PACKAGE_NAME} + MakeDir ${PACKAGE_NAME} + cd ${PACKAGE_NAME} + ../../configure \ + --prefix=${MONO_TRUNK_NACL}/normal-mono \ + --disable-parallel-mark \ + --enable-nls=no \ + --with-tls=pthread \ + --with-sgen=no +} + +CustomPackageInstall() { + CustomConfigureStep + DefaultBuildStep + DefaultInstallStep +} + + +CustomPackageInstall +exit 0 diff --git a/nacl/test/hw.cs b/nacl/test/hw.cs new file mode 100644 index 00000000000..f82c33e5470 --- /dev/null +++ b/nacl/test/hw.cs @@ -0,0 +1,60 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Collections; +using System.Text; +using System.Threading; + +namespace Test { + + public class c_code { + + [MethodImplAttribute (MethodImplOptions.InternalCall)] + public extern static void my_c_func(int x, string s, double d); + [MethodImplAttribute (MethodImplOptions.InternalCall)] + public extern static void my_c_pass(int x); + } + + public class HelloWorld + { + static public void Main () + { + } + + static public void Foobar (int x, string s) + { + // first line is a simple test + // 1. call back into c code 2. use mscorlib Math.Sqrt() + c_code.my_c_func(x, s, Math.Sqrt(3.1415 * 3.1415)); + + // second part of this test: + // attempt a try/catch, generate exception w/ throw + try { + c_code.my_c_pass(0); + // attempt an invalid cast + throw new InvalidCastException(); + c_code.my_c_pass(1); + } + catch (InvalidCastException e) { + c_code.my_c_pass(2); + } + c_code.my_c_pass(3); + + // third part of this test: + // attempt an invalid cast again, this time generating + // exception instead of using explicit throw. + try { + c_code.my_c_pass(0); + StringBuilder reference1 = new StringBuilder(); + object reference2 = reference1; + // attempt invalid cast + int reference3 = (int)reference2; + c_code.my_c_pass(4); + } + catch (InvalidCastException e) { + c_code.my_c_pass(5); + } + c_code.my_c_pass(3); + } + } +} diff --git a/nacl/test/my.c b/nacl/test/my.c new file mode 100644 index 00000000000..be10d7a9915 --- /dev/null +++ b/nacl/test/my.c @@ -0,0 +1,163 @@ +#include +#include +#include +#if defined(__GLIBC__) +#define __USE_GNU +#include +#include +#endif + +#include +#include +#include +#include + +extern void* mono_aot_module_mscorlib_info; + +extern void* mono_aot_module_hw_info; + +extern void mono_set_corlib_data(void *data, size_t size); +extern void mono_aot_register_module(void *aot_info); +extern void mono_aot_init(void); +extern void mono_jit_set_aot_only(int aot_only); +extern MonoDomain * mini_init (const char *filename, const char *runtime_version); + +#if !defined(TRUE) +#define TRUE 1 +#endif +#if !defined(FALSE) +#define FALSE 0 +#endif + +void my_c_func(int arg, const char *str, double d) { + /* str from c# is immutable */ + printf("*** my_c_func(%d, '%s', %1.4f) received\n", arg, str, (float)d); +} + +void my_c_pass(int x) { + char *msg = "undefined"; + switch(x) { + case 0: msg = "about to throw an exception..."; break; + case 1: msg = "thrown invalid cast exception was not caught!"; break; + case 2: msg = "thrown invalid cast exception was safely caught!"; break; + case 3: msg = "...leaving exeception test."; break; + case 4: msg = "generated invalid cast exception was not caught!"; break; + case 5: msg = "generated invalid cast exception was safely caught!"; break; + } + printf("*** my_c_pass(%d): %s\n", x, msg); +} + +void try_mono() { + MonoDomain *domain; + MonoAssembly *ma; + MonoImage *mi; + MonoClass *mc; + MonoMethodDesc *mmd; + MonoMethod *mm; + MonoObject *mo; + FILE *mscorlib; + char *corlib_data = NULL; + void *args [2]; + static int x = 123000; + args [0] = &x; + args [1] = "hello world"; + +#if defined(__native_client__) + mscorlib = fopen("mscorlib.dll", "r"); + if (NULL != mscorlib) { + size_t size; + struct stat st; + if (0 == stat("mscorlib.dll", &st)) { + size = st.st_size; + printf("reading mscorlib.dll, size %ld\n", size); + corlib_data = malloc(size); + if (corlib_data != NULL) { + while (fread(corlib_data, 1, size, mscorlib) != 0) ; + if (!ferror(mscorlib)) { + mono_set_corlib_data(corlib_data, size); + } else { + perror("error reading mscorlib.dll"); + free(corlib_data); + corlib_data = NULL; + } + } else { + perror("Could not allocate memory"); + } + } else { + perror("stat error"); + } + fclose(mscorlib); + } +#endif + +#ifdef AOT_VERSION + printf("address of mono_aot_module_mscorlib_info: %p\n", mono_aot_module_mscorlib_info); + printf("address of mono_aot_module_hw_info: %p\n", mono_aot_module_hw_info); + + // mono_jit_set_aot_only(TRUE) should be enabled now. + // if not enabled, I suspect we're still jitting... + mono_jit_set_aot_only(TRUE); + + mono_aot_register_module(mono_aot_module_mscorlib_info); + mono_aot_register_module(mono_aot_module_hw_info); +#endif + + mono_debug_init(MONO_DEBUG_FORMAT_MONO); + domain = mono_jit_init("hw.exe", "v2.0.50727"); + + printf("mono domain: %p\n", domain); + + ma = mono_domain_assembly_open(domain, "hw.exe"); + printf("mono assembly: %p\n", ma); + + mi = mono_assembly_get_image(ma); + printf("mono image: %p\n", mi); + + mc = mono_class_from_name(mi, "Test", "HelloWorld"); + printf("mono class: %p\n", mc); + + mmd = mono_method_desc_new("Test.HelloWorld:Foobar(int,string)", TRUE); + printf("mono desc method: %p\n", mmd); + + mm = mono_method_desc_search_in_image(mmd, mi); + printf("mono method: %p\n", mm); + + // add c functions for mono test code to invoke + mono_add_internal_call("Test.c_code::my_c_func", (void *) my_c_func); + mono_add_internal_call("Test.c_code::my_c_pass", (void *) my_c_pass); + + mo = mono_runtime_invoke(mm, NULL, args, NULL); + + printf("mono object: %p\n", mo); + if (NULL != corlib_data) free(corlib_data); +} + +#if defined(__GLIBC__) +int dl_printer(struct dl_phdr_info *info, size_t size, void *data) { + int j; + printf("DL name=%s (%d segments)\n", info->dlpi_name, + info->dlpi_phnum); + for (j = 0; j < info->dlpi_phnum; j++) { + if (info->dlpi_phdr[j].p_type == PT_LOAD) + printf("\t\t header %2d: address=%10p\n", j, + (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr)); + } + return 0; +} +#endif + +int main() { + int i; + setvbuf(stdout, NULL, _IONBF, 0); + printf("address of main(): %p\n", main); + printf("address of stack : %p\n", &i); +#if defined(__GLIBC__) + mallopt(M_TRIM_THRESHOLD, -1); + dl_iterate_phdr(dl_printer, NULL); +#endif + printf("\nProgram a.out output:\n"); + printf("==========================\n"); + try_mono(); + printf("==========================\n\n"); + return 0; +} diff --git a/nacl/test/nacl b/nacl/test/nacl new file mode 100755 index 00000000000..f53569cb8d3 --- /dev/null +++ b/nacl/test/nacl @@ -0,0 +1,164 @@ +#!/bin/bash + +set -o nounset +set -o errexit +set -o verbose + +source ../common.sh + +MONO_RUNTIME_BASE=${NACL_SDK_USR} +MONO_BASE=${NACL_SDK_USR} +CC=$NACLCC +AS=$NACLAS +MODE=nacl +COMPILE_AOT=0 +AOT_OBJS= +CC_DEFINES= +MONO_SNAPSHOT=mono-normal-build +RELOCATE_RODATA="-Wl,--section-start=.rodata=0x10020000" + +if [ $# -gt 0 ]; then + while [ -n "$*" ] + do + if [ $1 == "normal" ]; then + MONO_RUNTIME_BASE=../normal-mono + MONO_BASE=../normal-mono + CC=gcc + AS=as + MODE=normal + elif [ $1 == "aot" ]; then + COMPILE_AOT=1 + CC_DEFINES=-DAOT_VERSION + #RELOCATE_RODATA= + elif [ $1 == "regression" ]; then + MODE=regression + else + echo "Unrecognized option '$1'" + exit -1 + fi + shift + done +fi + +MONO="${MONO_BASE}/bin/mono" +readonly MONO_NORMAL_BASE=../normal-mono +readonly NCVAL=ncval_x86_${TARGET_BITSIZE} + +readonly IRT=${NACL_BIN_PATH}/../runtime/irt_core_x86_${TARGET_BITSIZE}.nexe + +if [ $NACL_NEWLIB = "1" ]; then + readonly SEL_LDR_ARGS= + readonly LD_FLAGS="-static" + if [ $COMPILE_AOT = "1" ]; then + MONO="${MONO_BASE}/bin/nacl${TARGET_BIT_PREFIX}-mono" + fi +else + readonly SEL_LDR_ARGS="${NACL_BIN_PATH}/../x86_64-nacl/lib${TARGET_BITSIZE}/runnable-ld.so --library-path ${NACL_BIN_PATH}/../x86_64-nacl/lib${TARGET_BITSIZE}:${NACL_SDK_USR_LIB}" + readonly LD_FLAGS="-ldl -lrt" + RELOCATE_RODATA= +fi + +# add nacl-gcc to path (from NaCl SDK) +export PATH=${NACL_BIN_PATH}:$PATH + +# add normal mono/gmcs to path +export PATH=${MONO_NORMAL_BASE}/bin:$PATH + +# echo version of nacl-gcc +$CC --version + +echo $PATH +SEL_LDR=sel_ldr_x86_${TARGET_BITSIZE} +which ${SEL_LDR} + +# echo version of gmcs +which gmcs +${MONO_NORMAL_BASE}/bin/gmcs --version + +# echo version of nacl-mono +if [ $COMPILE_AOT = "1" -o $MODE = "normal" ]; then + ${MONO} --version +fi + +# add MONO_PATH so mono can crank on local directory +#export MONO_PATH=$(pwd) +#echo ${MONO_PATH} + +#----- +# enable the appropriate set of AOT options below. + +readonly AOT_OPTIONS=full,static,nodebug,ntrampolines=4096 +#----- + +# make a temp copy of mscorlib.dll in this directory +cp ${MONO_NORMAL_BASE}/lib/mono/4.0/mscorlib.dll . + +# compile c-sharp file with gmcs +../normal-mono/bin/gmcs -lib:. -warn:0 hw.cs + +# convert .exe to .exe.o assembly files +# convert mscorlib to a .dll.o file +if [ $COMPILE_AOT = "1" ]; then + MONO_PATH=. ${MONO} --aot=${AOT_OPTIONS} mscorlib.dll + MONO_PATH=. ${MONO} --aot=${AOT_OPTIONS} hw.exe + AOT_OBJS="hw.exe.o mscorlib.dll.o" +fi + +# compile c and assembly into a.out, all statically linked +# different options for normal and nacl-mono +if [ $MODE = "normal" ]; then + $CC -g -static my.c ${CC_DEFINES} ${AOT_OBJS} -o hw-test -lmono-2.0 -lpthread -lm -ldl -lrt -I${MONO_RUNTIME_BASE}/include -I${MONO_RUNTIME_BASE}/include/mono-2.0 -L${MONO_RUNTIME_BASE}/lib +elif [ $MODE = "nacl" ]; then + $CC my.c ${CC_DEFINES} ${AOT_OBJS} -finstrument-for-thread-suspension -o hw-test.nexe -lmono-2.0 -lpthread -lm -lnacl_dyncode -lg -lnosys -I${MONO_RUNTIME_BASE}/include -I${MONO_RUNTIME_BASE}/include/mono-2.0 -L${MONO_RUNTIME_BASE}/lib ${RELOCATE_RODATA} ${LD_FLAGS} +fi + +readonly fsatests="basic.exe basic-float.exe basic-long.exe basic-calls.exe objects.exe arrays.exe basic-math.exe exceptions.exe devirtualization.exe basic-simd.exe gc-test.exe generics.exe iltests.exe nacl.exe" +if [ $MODE = "regression" ]; then + #rm -rf fsa-tmp + mkdir -p fsa-tmp + DIR=$(pwd) + cd ../${MONO_SNAPSHOT}/mono/mini/ + make $fsatests generics-variant-types.dll TestDriver.dll + cp $fsatests generics-variant-types.dll TestDriver.dll $DIR/fsa-tmp + cd - + + CLASS=${MONO_NORMAL_BASE}/lib/mono/4.0 + cp $CLASS/System.Core.dll $CLASS/System.dll $CLASS/Mono.Posix.dll $CLASS/System.Configuration.dll $CLASS/System.Security.dll $CLASS/System.Xml.dll $CLASS/Mono.Security.dll $CLASS/Mono.Simd.dll fsa-tmp + cp mscorlib.dll fsa-tmp + + AOT_OBJS="" + if [ $COMPILE_AOT = "1" ]; then + for t in $fsatests; do + MONO_PATH=fsa-tmp ${MONO} --aot=${AOT_OPTIONS} fsa-tmp/$t + AOT_OBJS="${AOT_OBJS} fsa-tmp/$t.o" + done + for d in fsa-tmp/*.dll; do + MONO_PATH=fsa-tmp ${MONO} --aot=${AOT_OPTIONS} $d + AOT_OBJS="${AOT_OBJS} $d.o" + done + fi + + $CC -o fsa-tmp/fsacheck.nexe -finstrument-for-thread-suspension -g ../../mono/mini/fsacheck.c ${CC_DEFINES} ${AOT_OBJS} -lmono-2.0 -lpthread -lm -lnacl_dyncode -lg -lnosys -L${MONO_RUNTIME_BASE}/lib -I${MONO_RUNTIME_BASE}/include/mono-2.0 -I${MONO_RUNTIME_BASE}/include ${RELOCATE_RODATA} ${LD_FLAGS} +fi + +if [ $MODE = "regression" ]; then + cd fsa-tmp + ${NCVAL} --local_cpuid fsacheck.nexe 2> validator_out || echo "fsacheck.nexe invalid: continuing anyway" + ${NACL_CROSS_PREFIX_DASH}objdump -d fsacheck.nexe > fsacheck.disasm + for t in $fsatests; do + ${SEL_LDR} -S -a -B ${IRT} ${SEL_LDR_ARGS} ./fsacheck.nexe $t || true + done +else + export MONO_PATH=$(pwd) + # run generated test(select one or more below) + if [ $MODE = "normal" ]; then + ./hw-test + else + ${NCVAL} --local_cpuid hw-test.nexe 2> validator_out || echo "hw-test.nexe invalid: continuing anyway" + ${NACL_CROSS_PREFIX_DASH}objdump -d hw-test.nexe > hw-test.disasm + ${SEL_LDR} -S -a -B ${IRT} ${SEL_LDR_ARGS} ./hw-test.nexe + fi +fi + +exit 0 + -- GitLab