2023-07-13 13:02:38 +03:00
#!/bin/bash
2023-09-27 22:34:46 +08:00
set -e
2023-07-13 13:02:38 +03:00
OS = mac
ARCH = " ${ 1 :- ` uname -a | rev | cut -d' ' -f1 | rev` } "
2023-10-05 00:06:23 +08:00
COMPOSE_ARCH = $ARCH
2024-04-19 21:17:22 +01:00
GHC_VERSION = 9.6.3
2025-02-09 15:06:05 +04:00
DATABASE_BACKEND = " ${ 2 :- sqlite } "
2023-08-25 04:56:37 +08:00
2023-07-13 13:02:38 +03:00
if [ " $ARCH " = = "arm64" ] ; then
ARCH = aarch64
2023-10-05 00:06:23 +08:00
else
COMPOSE_ARCH = x64
2023-07-13 13:02:38 +03:00
fi
2023-08-25 04:56:37 +08:00
2023-07-13 13:02:38 +03:00
LIB_EXT = dylib
LIB = libHSsimplex-chat-*-inplace-ghc*.$LIB_EXT
GHC_LIBS_DIR = $( ghc --print-libdir)
BUILD_DIR = dist-newstyle/build/$ARCH -*/ghc-*/simplex-chat-*
2024-01-31 21:47:10 +07:00
exports = ( $( sed 's/foreign export ccall "chat_migrate_init_key"//' src/Simplex/Chat/Mobile.hs | sed 's/foreign export ccall "chat_reopen_store"//' | grep "foreign export ccall" | cut -d '"' -f2) )
for elem in " ${ exports [@] } " ; do count = $( grep -R " $elem $" libsimplex.dll.def | wc -l) ; if [ $count -ne 1 ] ; then echo Wrong exports in libsimplex.dll.def. Add \" $elem \" to that file; exit 1; fi ; done
for elem in " ${ exports [@] } " ; do count = $( grep -R " \" $elem \" " flake.nix | wc -l) ; if [ $count -ne 2 ] ; then echo Wrong exports in flake.nix. Add \" $elem \" in two places of the file; exit 1; fi ; done
2023-07-13 13:02:38 +03:00
rm -rf $BUILD_DIR
2025-02-09 15:06:05 +04:00
if [ [ " $DATABASE_BACKEND " = = "postgres" ] ] ; then
echo "Building with postgres backend..."
2025-03-16 19:30:31 +00:00
cabal build -f client_postgres lib:simplex-chat lib:simplex-chat --ghc-options= " -optl-Wl,-rpath,@loader_path -optl-Wl,-L $GHC_LIBS_DIR / $ARCH -osx-ghc- $GHC_VERSION -optl-lHSrts_thr-ghc $GHC_VERSION -optl-lffi " --constraint 'simplexmq +client_library' --constraint 'simplex-chat +client_library'
2025-02-09 15:06:05 +04:00
else
echo "Building with sqlite backend..."
2025-03-16 19:30:31 +00:00
cabal build lib:simplex-chat lib:simplex-chat --ghc-options= " -optl-Wl,-rpath,@loader_path -optl-Wl,-L $GHC_LIBS_DIR / $ARCH -osx-ghc- $GHC_VERSION -optl-lHSrts_thr-ghc $GHC_VERSION -optl-lffi " --constraint 'simplexmq +client_library' --constraint 'simplex-chat +client_library'
2025-02-09 15:06:05 +04:00
fi
2023-07-13 13:02:38 +03:00
cd $BUILD_DIR /build
2023-09-27 22:34:46 +08:00
mkdir deps 2> /dev/null || true
2023-07-13 13:02:38 +03:00
# It's not included by default for some reason. Compiled lib tries to find system one but it's not always available
2023-08-25 04:56:37 +08:00
#cp $GHC_LIBS_DIR/libffi.dylib ./deps
(
BUILD = $PWD
cp /tmp/libffi-3.4.4/*-apple-darwin*/.libs/libffi.dylib $BUILD /deps || \
( \
cd /tmp && \
2024-01-17 15:23:43 +00:00
curl --tlsv1.2 "https://gitlab.haskell.org/ghc/libffi-tarballs/-/raw/libffi-3.4.4/libffi-3.4.4.tar.gz?inline=false" -o libffi.tar.gz && \
2023-08-25 04:56:37 +08:00
tar -xzvf libffi.tar.gz && \
cd "libffi-3.4.4" && \
./configure && \
make && \
cp *-apple-darwin*/.libs/libffi.dylib $BUILD /deps \
)
)
2023-07-13 13:02:38 +03:00
DYLIBS = ` otool -L $LIB | grep @rpath | tail -n +2 | cut -d' ' -f 1 | cut -d'/' -f2`
RPATHS = ` otool -l $LIB | grep "path " | cut -d' ' -f11`
PROCESSED_LIBS = ( )
function copy_deps( ) {
local LIB = $1
if [ [ " ${ PROCESSED_LIBS [*] } " = ~ " $LIB " ] ] ; then
return 0
fi
PROCESSED_LIBS += $LIB
local DYLIBS = ` otool -L $LIB | grep @rpath | tail -n +2 | cut -d' ' -f 1 | cut -d'/' -f2`
local NON_FINAL_RPATHS = ` otool -l $LIB | grep "path " | cut -d' ' -f11`
local RPATHS = ` otool -l $LIB | grep "path " | cut -d' ' -f11 | sed " s|@loader_path/..| $GHC_LIBS_DIR | " `
cp $LIB ./deps
if [ [ " $NON_FINAL_RPATHS " = = *"@loader_path/.." * ] ] ; then
2023-09-27 22:34:46 +08:00
# Need to point the lib to @loader_path instead
2023-07-13 13:02:38 +03:00
install_name_tool -add_rpath @loader_path ./deps/` basename $LIB `
fi
#echo LIB $LIB
#echo DYLIBS ${DYLIBS[@]}
#echo RPATHS ${RPATHS[@]}
for DYLIB in $DYLIBS ; do
for RPATH in $RPATHS ; do
if [ -f " $RPATH / $DYLIB " ] ; then
#echo DEP IS "$RPATH/$DYLIB"
if [ ! -f " deps/ $DYLIB " ] ; then
cp " $RPATH / $DYLIB " ./deps
fi
copy_deps " $RPATH / $DYLIB "
fi
done
done
}
copy_deps $LIB
2023-08-30 18:27:36 +03:00
# Special case
cp $( ghc --print-libdir) /$ARCH -osx-ghc-$GHC_VERSION /libHSghc-boot-th-$GHC_VERSION -ghc$GHC_VERSION .dylib deps
2023-07-13 13:02:38 +03:00
rm deps/` basename $LIB `
cd -
rm -rf apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS -$ARCH /
rm -rf apps/multiplatform/desktop/build/cmake
mkdir -p apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS -$ARCH /
2023-10-05 00:06:23 +08:00
cp -r $BUILD_DIR /build/deps/* apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS -$ARCH /
2023-07-13 13:02:38 +03:00
cp $BUILD_DIR /build/libHSsimplex-chat-*-inplace-ghc*.$LIB_EXT apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS -$ARCH /
2023-09-27 22:34:46 +08:00
cd apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS -$ARCH /
2025-02-09 15:06:05 +04:00
LIBCRYPTO_PATH = $( otool -l libHSsmplxmq-*.$LIB_EXT | grep libcrypto | cut -d' ' -f11)
install_name_tool -change $LIBCRYPTO_PATH @rpath/libcrypto.3.0.$LIB_EXT libHSsmplxmq-*.$LIB_EXT
2024-11-22 18:38:49 +00:00
cp $LIBCRYPTO_PATH libcrypto.3.0.$LIB_EXT
chmod 755 libcrypto.3.0.$LIB_EXT
install_name_tool -id " libcrypto.3.0. $LIB_EXT " libcrypto.3.0.$LIB_EXT
2023-10-05 00:06:23 +08:00
install_name_tool -id " libffi.8. $LIB_EXT " libffi.$LIB_EXT
2023-09-27 22:34:46 +08:00
LIBCRYPTO_PATH = $( otool -l $LIB | grep libcrypto | cut -d' ' -f11)
if [ -n " $LIBCRYPTO_PATH " ] ; then
2024-11-22 18:38:49 +00:00
install_name_tool -change $LIBCRYPTO_PATH @rpath/libcrypto.3.0.$LIB_EXT $LIB
2023-09-27 22:34:46 +08:00
fi
2025-02-09 15:06:05 +04:00
# We could change libpq and libHSpstgrsql for postgres (?), remove sqlite condition for exit below.
# Unnecessary for now as app with postgres backend is not for distribution.
if [ [ " $DATABASE_BACKEND " = = "sqlite" ] ] ; then
LIBCRYPTO_PATH = $( otool -l libHSdrct-*.$LIB_EXT | grep libcrypto | cut -d' ' -f11)
if [ -n " $LIBCRYPTO_PATH " ] ; then
install_name_tool -change $LIBCRYPTO_PATH @rpath/libcrypto.3.0.$LIB_EXT libHSdrct-*.$LIB_EXT
fi
2023-09-27 22:34:46 +08:00
2025-02-09 15:06:05 +04:00
LIBCRYPTO_PATH = $( otool -l libHSsqlcphr-*.$LIB_EXT | grep libcrypto | cut -d' ' -f11)
if [ -n " $LIBCRYPTO_PATH " ] ; then
install_name_tool -change $LIBCRYPTO_PATH @rpath/libcrypto.3.0.$LIB_EXT libHSsqlcphr-*.$LIB_EXT
fi
2023-11-02 03:11:04 +08:00
fi
2023-09-27 22:34:46 +08:00
for lib in $( find . -type f -name " *. $LIB_EXT " ) ; do
RPATHS = ` otool -l $lib | grep -E "path /Users/|path /usr/local|path /opt/" | cut -d' ' -f11`
for RPATH in $RPATHS ; do
install_name_tool -delete_rpath $RPATH $lib
done
done
2023-09-28 18:39:43 +08:00
LOCAL_DIRS = ` for lib in $( find . -type f -name " *. $LIB_EXT " ) ; do otool -l $lib | grep -E "/Users|/opt/|/usr/local" && echo $lib || true; done `
2023-09-27 22:34:46 +08:00
if [ -n " $LOCAL_DIRS " ] ; then
echo These libs still point to local directories:
echo $LOCAL_DIRS
2025-02-09 15:06:05 +04:00
if [ [ " $DATABASE_BACKEND " = = "sqlite" ] ] ; then
exit 1
fi
2023-09-27 22:34:46 +08:00
fi
cd -
2023-09-22 01:03:47 +08:00
scripts/desktop/prepare-vlc-mac.sh
2023-10-05 00:06:23 +08:00
links_dir = apps/multiplatform/build/links
mkdir -p $links_dir
cd $links_dir
rm macos-$COMPOSE_ARCH 2>/dev/null | true
ln -sf ../../common/src/commonMain/cpp/desktop/libs/$OS -$ARCH / macos-$COMPOSE_ARCH