Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build emonk statically with spidermonkey 1.8.5 #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
*.beam
*.swp
main.js
couchjs
*.o
*.so
c_src/js-1.8.5
c_src/nsprpub/
c_src/system/
.DS_Store
priv
ebin
test/*.beam
libs
c_src/*.tar.gz
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ TEST_SUPPORT = \
%.beam: %.erl
erlc -o test/ $<

all:
all: compile

compile:
echo "==> Build emonk & dependencies"
./rebar compile

verbose:
./rebar compile verbose=1

check: all $(TEST_SUPPORT)
prove test/*.t

Expand Down
6 changes: 3 additions & 3 deletions c_src/to_erl.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ to_erl_string(ErlNifEnv* env, JSContext* cx, jsval val, ERL_NIF_TERM* term)
size_t len;

str = JS_ValueToString(cx, val);
data = JS_GetStringBytesZ(cx, str);
data = JS_EncodeString(cx, str);
if(data == NULL) return ERROR;
len = strlen(data);

Expand Down Expand Up @@ -106,7 +106,7 @@ to_erl_object(ErlNifEnv* env, JSContext* cx, JSObject* obj, ERL_NIF_TERM* term)
length = 0;
while(JS_NextProperty(cx, iter, &idp))
{
if(idp == JSVAL_VOID) break;
if(idp == JSID_VOID) break;
length += 1;
}

Expand All @@ -119,7 +119,7 @@ to_erl_object(ErlNifEnv* env, JSContext* cx, JSObject* obj, ERL_NIF_TERM* term)
index = 0;
while(JS_NextProperty(cx, iter, &idp))
{
if(idp == JSVAL_VOID)
if(idp == JSID_VOID)
{
list = enif_make_list_from_array(env, array, length);
*term = enif_make_tuple1(env, list);
Expand Down
15 changes: 8 additions & 7 deletions c_src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static JSClass global_class = {
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
Expand Down Expand Up @@ -109,7 +109,7 @@ static JSClass jserl_class = {
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
Expand All @@ -118,12 +118,13 @@ static JSClass jserl_class = {
};

static JSBool
jserl_send(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)
jserl_send(JSContext* cx, uintN argc, jsval* vp)
{
vm_ptr vm = (vm_ptr) JS_GetContextPrivate(cx);
ErlNifEnv* env;
job_ptr job;
ENTERM mesg;
jsval* argv = JS_ARGV(cx, vp);

if(argc < 0)
{
Expand Down Expand Up @@ -154,7 +155,7 @@ jserl_send(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)

assert(job->type == job_response && "Invalid message response.");

*rval = to_js(job->env, cx, job->args);
JS_SET_RVAL(cx, vp, to_js(job->env, cx, job->args));
job_destroy(job);

return JS_TRUE;
Expand Down Expand Up @@ -257,8 +258,8 @@ vm_run(void* arg)
flags |= JSOPTION_COMPILE_N_GO;
flags |= JSOPTION_XML;
JS_SetOptions(cx, JS_GetOptions(cx) | flags);
gl = JS_NewObject(cx, &global_class, NULL, NULL);

gl = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
if(gl == NULL)
{
fprintf(stderr, "Failed to create global object.\n");
Expand Down Expand Up @@ -430,7 +431,7 @@ vm_call(JSContext* cx, JSObject* gl, job_ptr job)
jsval rval;
jsid idp;
int argc;

// Get the function object.

func = to_js(job->env, cx, job->name);
Expand Down
Binary file modified rebar
Binary file not shown.
13 changes: 9 additions & 4 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
{so_name, "emonk.so"}.

{port_envs, [
%% Link the spidermonkey library
{".*", "CFLAGS", "$CFLAGS -g -Wall"},
{".*", "LDFLAGS", "$LDFLAGS -lmozjs"},
{"DRV_CFLAGS", "$DRV_CFLAGS -fPIC -Wall -I libs/js/include -DXP_UNIX"},

{"DRV_LDFLAGS", "$DRV_LDFLAGS $ERL_LDFLAGS libs/js/lib/libjs_static.a libs/nsprpub/lib/libnspr4.a"},

%% Make sure to link -lstdc++ on linux or solaris
{"(linux|solaris)", "LDFLAGS", "$LDFLAGS -lstdc++"},
{"darwin", "LDFLAGS", "$LDFLAGS -lstdc++"},
{"(linux|freebsd)", "LDFLAGS", "$LDFLAGS -lstdc++ -lpthread"},

%% OS X Leopard flags for 64-bit
{"darwin9.*-64$", "CXXFLAGS", "-m64"},
Expand All @@ -17,3 +18,7 @@
{"darwin10.*-32$", "CXXFLAGS", "-m32"},
{"darwin10.*-32$", "LDFLAGS", "-arch i386"}
]}.

{pre_hooks, [{compile, "./support/build_libs.sh"}]}.
{post_hooks, [{clean, "./support/build_libs.sh clean"}]}.

248 changes: 248 additions & 0 deletions support/build_libs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
#!/bin/sh


BUILD_LIBS_DIR=$(cd ${0%/*} && pwd)

export CORE_TOP=${BUILD_LIBS_DIR%/*}
cd $CORE_TOP

CORE_TOP=`pwd`

CURLBIN=`which curl`
if ! test -n "CURLBIN"; then
display_error "Error: curl is required. Add it to 'PATH'"
exit 1
fi

GUNZIP=`which gunzip`
UNZIP=`which unzip`
TAR=`which tar`
GNUMAKE=`which gmake`

PATCHES=$CORE_TOP/support/patches
STATICLIBS=$CORE_TOP/libs
DISTDIR=$STATICLIBS/dists

# nspr sources
NSPR_VER=4.8.8
NSPR_DISTNAME=nspr-$NSPR_VER.tar.gz
NSPR_SITE=http://dl.refuge.io

# spidermonkey js sources
JS_VER=185-1.0.0
JS_REALVER=1.8.5
JS_DISTNAME=js$JS_VER.tar.gz
JS_SITE=http://dl.refuge.io
JSDIR=$STATICLIBS/js-$JS_REALVER
JS_LIBDIR=$STATICLIBS/js/lib
JS_INCDIR=$STATICLIBS/js/include



[ "$MACHINE" ] || MACHINE=`(uname -m) 2>/dev/null` || MACHINE="unknown"
[ "$RELEASE" ] || RELEASE=`(uname -r) 2>/dev/null` || RELEASE="unknown"
[ "$SYSTEM" ] || SYSTEM=`(uname -s) 2>/dev/null` || SYSTEM="unknown"
[ "$BUILD" ] || VERSION=`(uname -v) 2>/dev/null` || VERSION="unknown"


CFLAGS="-g -O2 -Wall"
LDFLAGS="-lstdc++"
ARCH=
ISA64=
GNUMAKE=make
CC=gcc
CXX=g++
case "$SYSTEM" in
Linux)
ARCH=`arch 2>/dev/null`
;;
FreeBSD|OpenBSD|NetBSD)
ARCH=`(uname -p) 2>/dev/null`
GNUMAKE=gmake
;;
Darwin)
ARCH=`(uname -p) 2>/dev/null`
ISA64=`(sysctl -n hw.optional.x86_64) 2>/dev/null`
;;
*)
ARCH="unknown"
;;
esac


# TODO: add mirror & signature validation support
fetch()
{
TARGET=$DISTDIR/$1
if ! test -f $TARGET; then
echo "==> Fetch $1 to $TARGET"
$CURLBIN --progress-bar -L $2/$1 -o $TARGET
fi
}


clean_nspr()
{
rm -rf $STATICLIBS/nspr*
rm -f $DISTDIR/$NSPR_DISTNAME
}


build_nspr()
{
NSPR_CONFIGURE_ENV=""
case "$SYSTEM" in
Linux)
ARCH=`arch 2>/dev/null`
if [ "$ARCH" = "x86_64" ]; then
NSPR_CONFIGURE_ENV="--enable-64bit"
fi
CFLAGS="$CFLAGS -lpthread"
;;
FreeBSD|OpenBSD|NetBSD)
ARCH=`(uname -p) 2>/dev/null`
if [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "amd64" ]; then
NSPR_CONFIGURE_ENV+=--enable-64bit
fi
;;
Darwin)
if [ "$ISA64" = "1" ]; then
NSPR_CONFIGURE_ENV+=--enable-64bit
fi
;;
esac

fetch $NSPR_DISTNAME $NSPR_SITE

echo "==> build nspr"
cd $STATICLIBS
$GUNZIP -c $DISTDIR/$NSPR_DISTNAME | $TAR xf -

cd $STATICLIBS/nspr-$NSPR_VER/mozilla/nsprpub
./configure --disable-debug --enable-optimize \
--prefix=$STATICLIBS/nsprpub $NSPR_CONFIGURE_ENV

$GNUMAKE all
$GNUMAKE install
}

clean_js()
{
rm -rf $STATICLIBS/js*
rm -f $DISTDIR/$JS_DISTNAME
}

build_js()
{

fetch $JS_DISTNAME $JS_SITE

mkdir -p $JS_LIBDIR
mkdir -p $JS_INCDIR

cd $STATICLIBS
$GUNZIP -c $DISTDIR/$JS_DISTNAME | $TAR -xf -

echo "==> build js"
cd $JSDIR/js/src
patch -p0 -i $PATCHES/js/patch-jsprf_cpp
patch -p0 -i $PATCHES/js/patch-configure

env CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" \
CPPFLAGS="-DXP_UNIX -DJS_C_STRINGS_ARE_UTF8" \
./configure --prefix=$STATICLIBS/js \
--disable-debug \
--enable-optimize \
--enable-static \
--disable-shared-js \
--disable-tests \
--with-system-nspr \
--with-nspr-prefix=$STATICLIBS/nsprpub && \
$GNUMAKE all

mkdir -p $JS_INCDIR/js
cp $JSDIR/js/src/*.h $JS_INCDIR
cp $JSDIR/js/src/*.tbl $JS_INCDIR
cp $JSDIR/js/src/libjs_static.a $JS_LIBDIR
}


do_setup()
{
echo "==> couch_core static libs (compile)"
mkdir -p $DISTDIR
}

do_builddeps()
{
if [ ! -f $STATICLIBS/nsprpub/lib/libnspr4.a ]; then
clean_nspr
build_nspr
fi

if [ ! -f $STATICLIBS/js/lib/libjs_static.a ]; then
clean_js
build_js
fi
}


clean()
{
clean_nspr
clean_js
}



usage()
{
cat << EOF
Usage: $basename [command] [OPTIONS]

The $basename command compile Mozilla Spidermonkey fro emonk.

Commands:

all: build couch_core static libs
clean: clean static libs
-?: display usage

Report bugs at <https://github.com/refuge/couch_core>.
EOF
}



if [ "x$1" = "x" ]; then
do_setup
do_builddeps
exit 0
fi

case "$1" in
all)
shift 1
do_setup
do_builddeps
;;
clean)
shift 1
clean
;;
help|--help|-h|-?)
usage
exit 0
;;
*)
echo $basename: ERROR Unknown command $arg 1>&2
echo 1>&2
usage 1>&2
echo "### $basename: Exitting." 1>&2
exit 1;
;;
esac


exit 0

Loading