From ae1b611e316da9c0de29c6dc5a2c8ab1e97f9a91 Mon Sep 17 00:00:00 2001
From: "Jay Freeman (saurik)" <saurik@saurik.com>
Date: Wed, 2 Dec 2015 20:22:05 -0800
Subject: [PATCH] Add libuv to the build (static linking for Apple).

---
 .gitignore         |  1 +
 .gitmodules        |  3 ++
 Makefile.am        |  2 +-
 Makefile.in        | 10 +++---
 apple-configure.sh | 65 +++++++++++++++++++-------------------
 apple-lib.sh       | 66 +++++++++++++++++++++++++++++++++++++++
 apple-libffi.sh    | 54 +-------------------------------
 apple-libuv.sh     | 22 +++++++++++++
 apple.mk           | 19 +++++++++--
 config.h.in        |  3 ++
 configure          | 78 ++++++++++++++++++++++++++++++++++++++++++++++
 configure.ac       |  4 +++
 libuv              |  1 +
 13 files changed, 236 insertions(+), 92 deletions(-)
 create mode 100755 apple-lib.sh
 create mode 100755 apple-libuv.sh
 create mode 160000 libuv

diff --git a/.gitignore b/.gitignore
index 8433dae..637968d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -39,6 +39,7 @@ readline-6.2
 readline-6.2.tar.gz
 readline.*
 libffi.*
+libuv.*
 build.*
 Cycript.ios
 Cycript.osx
diff --git a/.gitmodules b/.gitmodules
index e1df5ea..5d5f583 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -5,3 +5,6 @@
 	path = readline
 	url = git://git.savannah.gnu.org/readline.git
 	ignore = dirty
+[submodule "libuv"]
+	path = libuv
+	url = https://github.com/joyent/libuv.git
diff --git a/Makefile.am b/Makefile.am
index e67adb7..a4d79e3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,7 +41,7 @@ lib_LTLIBRARIES =
 
 lib_LTLIBRARIES += libcycript.la
 libcycript_la_LDFLAGS = $(CY_LDFLAGS)
-libcycript_la_LIBADD = $(LTLIBFFI) $(LTLIBGCC) -ldl
+libcycript_la_LIBADD = $(LTLIBUV) $(LTLIBFFI) $(LTLIBGCC) -ldl
 
 libcycript_la_SOURCES = ConvertUTF.c Decode.cpp Driver.cpp Highlight.cpp Library.cpp Network.cpp Output.cpp Replace.cpp Syntax.cpp
 libcycript_la_SOURCES += Parser.cpp Scanner.cpp
diff --git a/Makefile.in b/Makefile.in
index 04b082e..cafdcf7 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -177,8 +177,9 @@ am__DEPENDENCIES_1 =
 @CY_JAVA_TRUE@am__DEPENDENCIES_3 = $(am__DEPENDENCIES_1)
 @CY_OBJECTIVEC_TRUE@am__DEPENDENCIES_4 = $(am__DEPENDENCIES_1)
 libcycript_la_DEPENDENCIES = $(am__DEPENDENCIES_1) \
-	$(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \
-	$(am__DEPENDENCIES_3) $(am__DEPENDENCIES_4)
+	$(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \
+	$(am__DEPENDENCIES_2) $(am__DEPENDENCIES_3) \
+	$(am__DEPENDENCIES_4)
 am__libcycript_la_SOURCES_DIST = ConvertUTF.c Decode.cpp Driver.cpp \
 	Highlight.cpp Library.cpp Network.cpp Output.cpp Replace.cpp \
 	Syntax.cpp Parser.cpp Scanner.cpp Complete.cpp \
@@ -442,6 +443,7 @@ LTLIBGCC = @LTLIBGCC@
 LTLIBOBJS = @LTLIBOBJS@
 LTLIBREADLINE = @LTLIBREADLINE@
 LTLIBTERMCAP = @LTLIBTERMCAP@
+LTLIBUV = @LTLIBUV@
 LTOBJECTIVEC = @LTOBJECTIVEC@
 LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@
 MAINT = @MAINT@
@@ -551,8 +553,8 @@ AM_LDFLAGS = -fvisibility=hidden
 CY_LDFLAGS = -no-undefined -avoid-version -export-dynamic
 lib_LTLIBRARIES = libcycript.la
 libcycript_la_LDFLAGS = $(CY_LDFLAGS)
-libcycript_la_LIBADD = $(LTLIBFFI) $(LTLIBGCC) -ldl $(am__append_3) \
-	$(am__append_9) $(am__append_12)
+libcycript_la_LIBADD = $(LTLIBUV) $(LTLIBFFI) $(LTLIBGCC) -ldl \
+	$(am__append_3) $(am__append_9) $(am__append_12)
 libcycript_la_SOURCES = ConvertUTF.c Decode.cpp Driver.cpp \
 	Highlight.cpp Library.cpp Network.cpp Output.cpp Replace.cpp \
 	Syntax.cpp Parser.cpp Scanner.cpp $(am__append_1) \
diff --git a/apple-configure.sh b/apple-configure.sh
index 7e1c50d..68625e5 100755
--- a/apple-configure.sh
+++ b/apple-configure.sh
@@ -23,11 +23,6 @@ set -e
 
 cd "${0%%/*}"
 
-if [[ ! -e readline.osx/libreadline.a ]]; then
-    ./apple-readline.sh; fi
-if [[ ! -e libffi.a ]]; then
-    ./libffi.sh; fi
-
 if ! which aclocal; then
     touch aclocal.m4; fi
 if ! which autoconf; then
@@ -38,6 +33,7 @@ if ! which autoheader; then
     touch config.h.in; fi
 
 flags=("$@")
+ccf=(-g0 -O3)
 
 function path() {
     xcodebuild -sdk "$1" -version Path
@@ -49,51 +45,56 @@ mac=$(path macosx)
 function configure() {
     local dir=$1
     local sdk=$2
-    local flg=$3
-    shift 3
+    local arc=$3
+    local min=$4
+    local ffi=$5
+    local cpf=$6
+    local ldf=$7
+    local obc=$8
+    shift 8
+
+    set -- "$@" --enable-static --with-pic
 
     cc=$(xcrun --sdk "${sdk}" -f clang)
     cxx=$(xcrun --sdk "${sdk}" -f clang++)
+
+    flg="-arch ${arc} ${min}"
     flg+=" -isysroot $(path "${sdk}")"
 
     rm -rf build."${dir}"
     mkdir build."${dir}"
     cd build."${dir}"
 
-    CC="${cc} ${flg}" CXX="${cxx} ${flg}" OBJCXX="${cxx} ${flg}" \
-        ../configure --enable-maintainer-mode "${flags[@]}" --prefix="/usr" "$@"
+    if "${ffi}"; then
+        cpf+=" -I../libffi.${arch}/include"
+        ldf+=" -L../libffi.${arch}/.libs"
+    fi
 
-    cd ..
-}
+    cpf+=" -I../libuv/include"
+    ldf+=" -L../libuv.${arch}/.libs"
 
-function build() {
-    local dir=$1
-    local sdk=$2
-    local flg=$3
-    shift 3
+    ../configure --enable-maintainer-mode "${flags[@]}" --prefix="/usr" "$@" \
+        CC="${cc} ${flg}" CXX="${cxx} ${flg}" OBJCXX="${cxx} ${flg}" \
+        CFLAGS="${ccf[*]}" CXXFLAGS="${ccf[*]}" OBJCXXFLAGS="${ccf[*]} ${obc}" \
+        CPPFLAGS="${cpf}" LDFLAGS="${ldf}"
 
-    configure "${dir}" "${sdk}" "${flg}" "$@" --enable-static --with-pic
+    cd ..
 }
 
-gof=(-g0 -O3)
-
 for arch in i386 x86_64; do
-    build "osx-${arch}" "${mac}" "-arch ${arch} -mmacosx-version-min=10.6" \
-        CFLAGS="${gof[*]}" CXXFLAGS="${gof[*]}" OBJCXXFLAGS="${gof[*]}" \
-        CPPFLAGS="-I../readline.osx" LDFLAGS="-L../readline.osx"
+    configure "osx-${arch}" "${mac}" "${arch}" "-mmacosx-version-min=10.6" \
+        false "-I../readline.osx" "-L../readline.osx" ""
 done
 
 for arch in i386 x86_64; do
-    build "sim-${arch}" iphonesimulator "-arch ${arch} -mios-simulator-version-min=4.0" \
-        CFLAGS="${gof[*]}" CXXFLAGS="${gof[*]}" OBJCXXFLAGS="${gof[*]} -fobjc-abi-version=2 -fobjc-legacy-dispatch" \
-        CPPFLAGS="-I../libffi.${arch}/include" \
-        LDFLAGS="-L.." \
-    --disable-console
+    configure "sim-${arch}" iphonesimulator "${arch}" "-mios-simulator-version-min=4.0" \
+        true "" "" "-fobjc-abi-version=2 -fobjc-legacy-dispatch" \
+        --disable-console
 done
 
 for arch in armv6 armv7 armv7s arm64; do
-    cpf="-I../libffi.${arch}/include"
-    ldf="-L.."
+    cpf=""
+    ldf=""
 
     flg=()
     if [[ ${arch} != armv6 ]]; then
@@ -114,7 +115,7 @@ for arch in armv6 armv7 armv7s arm64; do
         #cpf+=" -mthumb"
     fi
 
-    build "ios-${arch}" iphoneos "-arch ${arch} -miphoneos-version-min=${min}" --host=arm-apple-darwin10 \
-        CFLAGS="${gof[*]}" CXXFLAGS="${gof[*]}" OBJCXXFLAGS="${gof[*]}" \
-        CPPFLAGS="${cpf}" LDFLAGS="${ldf}" LFLAGS="--ecs --meta-ecs" "${flg[@]}"
+    configure "ios-${arch}" iphoneos "${arch}" "-miphoneos-version-min=${min}" \
+        true "${cpf[*]}" "${ldf[*]}" "" \
+        --host=arm-apple-darwin10 LFLAGS="--ecs --meta-ecs" "${flg[@]}"
 done
diff --git a/apple-lib.sh b/apple-lib.sh
new file mode 100755
index 0000000..c3b43c8
--- /dev/null
+++ b/apple-lib.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+
+# Cycript - Optimizing JavaScript Compiler/Runtime
+# Copyright (C) 2009-2015  Jay Freeman (saurik)
+
+# GNU Affero General Public License, Version 3 {{{
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# }}}
+
+set -e
+
+lib=$1
+shift
+extra=("$@")
+
+archs=()
+function arch() {
+    local arch=$1
+    local host=$2
+    local sdk=$3
+    local os=$4
+    local min=$5
+    shift 5
+
+    rm -rf "lib${lib}.${arch}"
+    if ! isysroot=$(xcodebuild -sdk "${sdk}" -version Path); then
+        return
+    fi
+
+    archs+=("${arch}")
+    mkdir "lib${lib}.${arch}"
+
+    flags=("${extra[@]}")
+    flags+=(-isysroot "${isysroot}")
+    flags+=(-m${os}-version-min="${min}")
+    flags+=(-O3 -g3)
+
+    if [[ ${arch} == armv* && ${arch} != armv6 ]]; then
+        flags+=(-mthumb)
+    fi
+
+    cd "lib${lib}.${arch}"
+    CC="clang -arch ${arch}" CXX="clang++ -arch ${arch}" CFLAGS="${flags[*]}" CPPFLAGS="${flags[*]} $*" ../lib"${lib}"/configure --host="${host}" --enable-static --disable-shared
+    make -j5
+    cd ..
+}
+
+arch armv6 arm-apple-darwin10 iphoneos iphoneos 2.0 -mllvm -arm-reserve-r9
+arch armv7 arm-apple-darwin10 iphoneos iphoneos 2.0
+arch armv7s arm-apple-darwin10 iphoneos iphoneos 2.0
+arch arm64 aarch64-apple-darwin11 iphoneos iphoneos 2.0
+
+arch i386 i386-apple-darwin10 iphonesimulator ios-simulator 4.0
+arch x86_64 x86_64-apple-darwin11 iphonesimulator ios-simulator 4.0
diff --git a/apple-libffi.sh b/apple-libffi.sh
index c20b5ba..296d12d 100755
--- a/apple-libffi.sh
+++ b/apple-libffi.sh
@@ -19,56 +19,4 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # }}}
 
-set -e
-
-archs=()
-function arch() {
-    local arch=$1
-    local host=$2
-    local sdk=$3
-    local os=$4
-    local min=$5
-    shift 5
-
-    rm -rf "libffi.${arch}"
-    if ! isysroot=$(xcodebuild -sdk "${sdk}" -version Path); then
-        return
-    fi
-
-    archs+=("${arch}")
-    mkdir "libffi.${arch}"
-
-    flags=()
-    flags+=(-isysroot "${isysroot}")
-    flags+=(-m${os}-version-min="${min}")
-    flags+=(-DPAGE_MAX_SIZE=16384 -DPAGE_MAX_SHIFT=14)
-    flags+=(-fno-stack-protector)
-    flags+=(-O3 -g3)
-
-    if [[ ${arch} == arm* && ${arch} != arm64 ]]; then
-        flags+=(-mthumb)
-    fi
-
-    cd "libffi.${arch}"
-    CC="clang -arch ${arch}" CXX="clang++ -arch ${arch}" CFLAGS="${flags[*]}" CPPFLAGS="${flags[*]} $*" ../libffi/configure --host="${host}"
-    make
-    cd ..
-}
-
-arch armv6 arm-apple-darwin10 iphoneos iphoneos 2.0 -mllvm -arm-reserve-r9
-arch armv7 arm-apple-darwin10 iphoneos iphoneos 2.0
-arch armv7s arm-apple-darwin10 iphoneos iphoneos 2.0
-arch arm64 aarch64-apple-darwin11 iphoneos iphoneos 2.0
-
-arch i386 i386-apple-darwin10 iphonesimulator ios-simulator 4.0
-arch x86_64 x86_64-apple-darwin11 iphonesimulator ios-simulator 4.0
-
-libffi=()
-for arch in "${archs[@]}"; do
-    a=libffi."${arch}"/.libs/libffi.a
-    # sectionForAddress(...) address not in any section file '...' for architecture i386
-    ar m "${a}" src/prep_cif.o
-    libffi+=("${a}")
-done
-
-lipo -create -output libffi.a "${libffi[@]}"
+exec ./apple-lib.sh ffi -DPAGE_MAX_SIZE=16384 -DPAGE_MAX_SHIFT=14 -fno-stack-protector
diff --git a/apple-libuv.sh b/apple-libuv.sh
new file mode 100755
index 0000000..2129503
--- /dev/null
+++ b/apple-libuv.sh
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+
+# Cycript - Optimizing JavaScript Compiler/Runtime
+# Copyright (C) 2009-2015  Jay Freeman (saurik)
+
+# GNU Affero General Public License, Version 3 {{{
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# }}}
+
+exec ./apple-lib.sh uv
diff --git a/apple.mk b/apple.mk
index 1c3e625..e10a805 100644
--- a/apple.mk
+++ b/apple.mk
@@ -76,9 +76,15 @@ deb: $(deb)
 
 clean := 
 
+library := libffi libuv
+
 # make stubbornly refuses to believe that these @'s are bugs
 # http://osdir.com/ml/help-make-gnu/2012-04/msg00008.html
 
+define build_lar
+Cycript.lib/$(1).a: $(1).$(2)/.libs/$(1).a
+endef
+
 define build_any
 .PHONY: build-$(1)-$(2)
 build-$(1)-$(2):
@@ -88,6 +94,11 @@ build.$(1)-$(2)/.libs/libcycript.a: build-$(1)-$(2)
 clean-$(1)-$(2):
 	$$(MAKE) -C build.$(1)-$(2) clean
 clean += clean-$(1)-$(2)
+ifneq ($(1),sim)
+$(foreach lib,$(library),
+$(call build_lar,$(lib),$(2))
+)
+endif
 endef
 
 define build_lib
@@ -133,6 +144,10 @@ $(foreach arch,armv6 arm64,$(eval $(call build_arm,$(arch))))
 clean: $(clean)
 	rm -rf cycript Cycript.lib libcycript*.o
 
+$(patsubst %,Cycript.lib/%.a,$(library)):
+	@mkdir -p $(dir $@)
+	$(lipo) -create -output $@ $^
+
 Cycript.lib/libcycript.dylib: build.osx-i386/.libs/libcycript.dylib build.osx-x86_64/.libs/libcycript.dylib build.ios-armv6/.libs/libcycript.dylib build.ios-arm64/.libs/libcycript.dylib
 	@mkdir -p $(dir $@)
 	$(lipo) -create -output $@ $^
@@ -157,9 +172,9 @@ Cycript.lib/libcycript-sim.dylib: build.sim-i386/.libs/libcycript.dylib build.si
 	$(lipo) -create -output $@ $^
 	codesign -s $(codesign) $@
 
-libcycript-%.o: build.%/.libs/libcycript.a xcode.map
+libcycript-%.o: build.%/.libs/libcycript.a $(patsubst %,Cycript.lib/%.a,$(library)) xcode.map
 	@mkdir -p $(dir $@)
-	ld -r -arch $$($(lipo) -detailed_info $< | sed -e '/^Non-fat file: / ! d; s/.*: //') -o $@ -all_load -exported_symbols_list xcode.map -x $< libffi.a
+	ld -r -arch $$($(lipo) -detailed_info $< | sed -e '/^Non-fat file: / ! d; s/.*: //') -o $@ -all_load -exported_symbols_list xcode.map -x $(filter %.a,$^)
 
 libcycript-ios.o: libcycript-ios-armv6.o libcycript-ios-armv7.o libcycript-ios-armv7s.o libcycript-ios-arm64.o libcycript-sim-i386.o libcycript-sim-x86_64.o
 	$(lipo) -create -output $@ $^
diff --git a/config.h.in b/config.h.in
index 3f01fbd..71f2f5c 100644
--- a/config.h.in
+++ b/config.h.in
@@ -67,6 +67,9 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
+/* Define to 1 if you have the <uv.h> header file. */
+#undef HAVE_UV_H
+
 /* Define to the sub-directory where libtool stores uninstalled libraries. */
 #undef LT_OBJDIR
 
diff --git a/configure b/configure
index b537939..ca12353 100755
--- a/configure
+++ b/configure
@@ -655,6 +655,7 @@ CY_JAVA
 LTLIBFFI
 LIBFFI_LIBS
 LIBFFI_CFLAGS
+LTLIBUV
 CY_EXECUTE_FALSE
 CY_EXECUTE_TRUE
 CY_ATTACH_FALSE
@@ -20901,6 +20902,83 @@ fi
 case $CY_EXECUTE in #(
   1) :
 
+
+    cy_save=$LIBS
+    LIBS=
+    for ac_header in uv.h
+do :
+  ac_fn_cxx_check_header_mongrel "$LINENO" "uv.h" "ac_cv_header_uv_h" "$ac_includes_default"
+if test "x$ac_cv_header_uv_h" = xyes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_UV_H 1
+_ACEOF
+
+        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing uv_loop_init" >&5
+$as_echo_n "checking for library containing uv_loop_init... " >&6; }
+if ${ac_cv_search_uv_loop_init+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_func_search_save_LIBS=$LIBS
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char uv_loop_init ();
+int
+main ()
+{
+return uv_loop_init ();
+  ;
+  return 0;
+}
+_ACEOF
+for ac_lib in '' uv; do
+  if test -z "$ac_lib"; then
+    ac_res="none required"
+  else
+    ac_res=-l$ac_lib
+    LIBS="-l$ac_lib  $ac_func_search_save_LIBS"
+  fi
+  if ac_fn_cxx_try_link "$LINENO"; then :
+  ac_cv_search_uv_loop_init=$ac_res
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext
+  if ${ac_cv_search_uv_loop_init+:} false; then :
+  break
+fi
+done
+if ${ac_cv_search_uv_loop_init+:} false; then :
+
+else
+  ac_cv_search_uv_loop_init=no
+fi
+rm conftest.$ac_ext
+LIBS=$ac_func_search_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_uv_loop_init" >&5
+$as_echo "$ac_cv_search_uv_loop_init" >&6; }
+ac_res=$ac_cv_search_uv_loop_init
+if test "$ac_res" != no; then :
+  test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
+
+fi
+
+
+fi
+
+done
+
+    LTLIBUV=$LIBS
+    LIBS=$cy_save
+
+
+
     for ac_header in ffi.h ffi/ffi.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
diff --git a/configure.ac b/configure.ac
index b482870..88e3230 100644
--- a/configure.ac
+++ b/configure.ac
@@ -196,6 +196,10 @@ AC_DEFUN([CY_CHECK_PKG_CONFIG_LIBFFI], [
 AM_CONDITIONAL([CY_EXECUTE], [test "x$CY_EXECUTE" = x1])
 
 AS_CASE([$CY_EXECUTE], [1], [
+    CY_LT_LIB([LTLIBUV], [AC_CHECK_HEADERS([uv.h], [
+        AC_SEARCH_LIBS([uv_loop_init], [uv])
+    ])])
+
     AC_CHECK_HEADERS([ffi.h ffi/ffi.h], [break])
 
     CY_LT_LIB([LTLIBFFI], AS_IF([test "x$ac_cv_header_ffi_h" = xno && test "x$ac_cv_header_ffi_ffi_h" = xno], [
diff --git a/libuv b/libuv
new file mode 160000
index 0000000..52ae456
--- /dev/null
+++ b/libuv
@@ -0,0 +1 @@
+Subproject commit 52ae456b0d666f6f4dbb7f52675f4f131855bd22
-- 
2.45.2