Skip to content

Instantly share code, notes, and snippets.

@maddie
Last active January 24, 2023 14:47
Show Gist options
  • Save maddie/65a27be2c73ab698497c8c92606e4920 to your computer and use it in GitHub Desktop.
Save maddie/65a27be2c73ab698497c8c92606e4920 to your computer and use it in GitHub Desktop.
Build Javet on macOS
#!/bin/bash -e
# base directory for everything (output, depot_tools, source code, etc.)
BASEDIR=~/javet-build
###############################################
## DO NOT EDIT BELOW THIS LINE UNLESS NEEDED ##
###############################################
GITHUB_WORKFLOW=$(curl -sL https://raw.githubusercontent.com/caoccao/Javet/596200a7958541a8e1d2845c95ef12953be6b649/.github/workflows/linux_build_artifact.yml | grep -Eo "\w*JAVET_.*:.*")
# Javet GitHub tag/branch
JAVET_VERSION=$(echo "${GITHUB_WORKFLOW}" | grep JAVET_VERSION | sed -e "s/.*: //g")
# Target V8 engine version to build, please check Javet release notes
V8_VERSION=$(echo "${GITHUB_WORKFLOW}" | grep JAVET_V8_VERSION | sed -e "s/.*: //g")
# Target Node.js version to build, please check Javet release notes
NODE_VERSION=v$(echo "${GITHUB_WORKFLOW}" | grep JAVET_NODE_VERSION | sed -e "s/.*: //g")
if [ -z $JAVET_VERSION ] || [ -z $V8_VERSION ] || [ -z $NODE_VERSION ]; then
echo "failed to fetch latest Javet version info, please check network."
exit 0
fi
echo "building Javet v${JAVET_VERSION} with V8 v${V8_VERSION} and Node.js ${NODE_VERSION}"
# define valid ccache symlinks path if you want to use ccache, else leave empty
CCACHE_PATH=$(brew --prefix)/$(ls -al $(which ccache) | cut -d '/' -f 6-8)/libexec
# JAVA_HOME to Java 1.8
JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
if [ -z ${JAVA_HOME} ]; then
echo "Java 1.8 not found, aborting"
exit 0
fi
# Target architechture to build, same as host
TARGET_ARCH=$(uname -m) # x64 or arm64
case ${TARGET_ARCH} in
"x86_64")
TARGET_ARCH="x64"
;;
"arm64")
;;
*)
echo "Unknown architechture: ${TARGET_ARCH}"
exit 0
;;
esac
# threads for compilation
J=$(sysctl -n hw.ncpu)
[ ! -d ${BASEDIR} ] && mkdir -p ${BASEDIR}
export JAVA_HOME
# Google depot_tools for fetching V8 source code
DEPOT_TOOLS_PATH=${BASEDIR}/depot_tools
# add depot_tools to PATH for convenient access
export PATH=${DEPOT_TOOLS_PATH}:${PATH}
# set ccache if usable
if [ -d "${CCACHE_PATH}" ]; then
export PATH=${CCACHE_PATH}:${PATH}
export USE_CCACHE=1
export CCACHE_CPP2=yes
export CCACHE_SLOPPINESS=time_macros
echo "ccache is enabled"
else
echo "ccache is not enabled"
fi
# check for git, used for cloning source code
if [ ! -x "$(which git)" ]; then
brew install git
fi
# check for ccache, used for compilation
if [ ! -x "$(which ccache)" ]; then
brew install ccache
fi
# check for cmake, used for compilation
if [ ! -x "$(which cmake)" ]; then
brew install cmake
fi
# check for ninja, used for compilation
if [ ! -x "$(which ninja)" ]; then
brew install ninja
fi
# check for gradle, used for compilation
if [ ! -x "$(which gradle)" ]; then
brew install gradle
fi
# clone depot_tools if not exist at defined path
# check for depot_tools validity
if [ ! -x ${DEPOT_TOOLS_PATH}/gclient ] || [ ! -x ${DEPOT_TOOLS_PATH}/fetch ] || [ ! -x ${DEPOT_TOOLS_PATH}/gn ]; then
if [ -d ${DEPOT_TOOLS_PATH} ]; then
echo "depot_tools directory exists, removing"
rm -rf ${DEPOT_TOOLS_PATH}
fi
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git ${DEPOT_TOOLS_PATH}
fi
V8_DIR="${BASEDIR}/v8"
NODE_DIR="${BASEDIR}/node"
JAVET_DIR="${BASEDIR}/Javet"
# checkout V8 source code, re-fetch if needed
if [ ! -d $V8_DIR/v8 ]; then
rm -rf $V8_DIR
mkdir -p $V8_DIR
cd $V8_DIR
fetch v8
fi
# start building V8
cd $V8_DIR/v8
rm -rf out.gn out
git reset --hard
git checkout -q 10.5.218.8
git fetch --all
gclient sync
git checkout -q $V8_VERSION
mkdir -p out.gn/$TARGET_ARCH.release
cat > out.gn/$TARGET_ARCH.release/args.gn << EOF
is_debug = false
target_cpu = "$TARGET_ARCH"
v8_monolithic = true
v8_use_external_startup_data = false
is_component_build = false
v8_enable_i18n_support= false
v8_enable_pointer_compression = false
v8_static_library = true
symbol_level = 0
use_custom_libcxx = false
v8_enable_sandbox = false
dcheck_always_on = false
EOF
# set cc_wrapper if ccache is used
if [ $USE_CCACHE = "1" ]; then
echo 'cc_wrapper = "ccache"' >> out.gn/$TARGET_ARCH.release/args.gn
fi
gn gen out.gn/$TARGET_ARCH.release
ninja -C out.gn/$TARGET_ARCH.release v8_monolith
# checkout Node.js source code
if [ ! -d $NODE_DIR ]; then
rm -rf $NODE_DIR
mkdir -p $NODE_DIR
cd $NODE_DIR
git clone https://github.com/nodejs/node .
fi
# start building Node.js
cd $NODE_DIR
rm -rf out
git reset --hard
git fetch --all
git checkout -q $NODE_VERSION
./configure --enable-static --without-intl
make -j$J
# checkout Javet source code
if [ ! -d ${JAVET_DIR} ]; then
rm -rf ${JAVET_DIR}
mkdir -p ${JAVET_DIR}
cd ${JAVET_DIR}
git clone https://github.com/caoccao/Javet.git .
fi
# start building Javet
cd $JAVET_DIR
git reset --hard
git fetch --all
git checkout -q $JAVET_VERSION
cd cpp
rm -rf build
sh build-macos.sh -DV8_DIR=$V8_DIR/v8
sh build-macos.sh -DNODE_DIR=$NODE_DIR
cd ..
gradle build test
zip ${JAVET_DIR}/src/main/resources/libjavet-macos-$(uname -m)-v.${JAVET_VERSION}.dylibs.zip ${JAVET_DIR}/src/main/resources/libjavet-node-macos-$(uname -m).v.${JAVET_VERSION}.dylib ${JAVET_DIR}/src/main/resources/libjavet-v8-macos-$(uname -m).v.${JAVET_VERSION}.dylib
echo "Result libraries are located at $JAVET_DIR/src/main/resources"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment