Skip to content

Commit

Permalink
refactor(bin): generate 'pkgconfig' files
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Oct 12, 2023
1 parent 38ce5aa commit 2f89e1c
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 8 deletions.
4 changes: 4 additions & 0 deletions bin/build-runtime-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ function main () {
fi
fi

if [[ "$host" = "Linux" ]] && [[ "$platform" = "desktop" ]]; then
"$root/bin/generate-socket-runtime-pkg-config.sh"
fi

if [[ "$platform" == "android" ]]; then
# This is a sanity check to confirm that the static_library is > 8 bytes
# If an empty ${objects[@]} is provided to ar, it will still spit out a header without an error code.
Expand Down
2 changes: 1 addition & 1 deletion bin/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function quiet () {
if [ -n "$VERBOSE" ]; then
echo "$command" "$@"
"$command" "$@"
else
else
"$command" "$@" > /dev/null 2>&1
fi

Expand Down
167 changes: 167 additions & 0 deletions bin/generate-socket-runtime-pkg-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#!/usr/bin/env bash
# vim: set syntax=bash:

declare root="$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" && pwd)"

source "$root/bin/mush.sh"
source "$root/bin/functions.sh"
source "$root/bin/android-functions.sh"

declare platform="desktop"
declare host="$(host_os)"
declare arch="$(host_arch)"

if (( TARGET_OS_IPHONE )); then
arch="arm64"
platform="iPhoneOS"
elif (( TARGET_IPHONE_SIMULATOR )); then
arch="x86_64"
platform="iPhoneSimulator"
elif (( TARGET_OS_ANDROID )); then
arch="aarch64"
platform="android"
elif (( TARGET_ANDROID_EMULATOR )); then
arch="x86_64"
platform="android"
fi

while (( $# > 0 )); do
declare arg="$1"; shift
if [[ "$arg" = "--arch" ]]; then
arch="$1"; shift; continue
fi

if [[ "$arg" = "--force" ]] || [[ "$arg" = "-f" ]]; then
pass_force="$arg"
force=1; continue
fi

if [[ "$arg" = "--platform" ]]; then
if [[ "$1" = "ios" ]] || [[ "$1" = "iPhoneOS" ]] || [[ "$1" = "iphoneos" ]]; then
arch="arm64"
platform="iPhoneOS";
export TARGET_OS_IPHONE=1
elif [[ "$1" = "ios-simulator" ]] || [[ "$1" = "iPhoneSimulator" ]] || [[ "$1" = "iphonesimulator" ]]; then
[[ -z "$arch" ]] && arch="x86_64"
platform="iPhoneSimulator";
export TARGET_IPHONE_SIMULATOR=1
elif [[ "$1" = "android" ]] || [[ "$1" = "android-emulator" ]]; then
platform="android";
export TARGET_OS_ANDROID=1
else
platform="$1";
fi
shift
continue
fi

# Don't rebuild if header mtimes are newer than .o files - Be sure to manually delete affected assets as required
if [[ "$arg" == "--ignore-header-mtimes" ]]; then
ignore_header_mtimes=1; continue
fi

args+=("$arg")
done

declare input="$root/socket-runtime.pc.in"
declare output="$root/build/$arch-$platform/pkgconfig/socket-runtime.pc"

mkdir -p "$(dirname "$output")"

declare version="$(cat "$root/VERSION.txt")"
declare lib_directory="$root/build/$arch-$platform/lib"
declare include_directory="$root/build/$arch-$platform/include"

declare ldflags=()
declare dependencies=()
declare cflags=(
"-0s"
"-std=c++2a"
"-fvisibility=hidden"
)

if [ "$platform" == "iPhoneOS" ]; then
platform="ios"
elif [ "$platform" == "iPhoneSimulator" ]; then
platform="ios-simulator"
fi

if [ "$host" == "Linux" ]; then
if [ "$platform" == "desktop" ]; then
if [[ "$(basename "$CXX")" =~ clang ]]; then
cflags+=("-stdlib=libstdc++")
cflags+=("-Wno-unused-command-line-argument")
fi
cflags+=("-fPIC")
ldflags+=("-ldl")
dependencies+=("gtk+-3.0" "webkit2gtk-4.1")
fi
elif [ "$host" == "Win32" ]; then
if [ "$platform" == "desktop" ]; then
if [[ "$(basename "$CXX")" =~ clang ]]; then
cflags+=("-Wno-unused-command-line-argument")
cflags+=("-stdlib=libstdc++")
fi

# https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170
# Because we can't pass /MT[d] directly, we have to manually set the flags
cflags+=(
"-D_MT"
"-D_DLL"
"-DWIN32"
"-DWIN32_LEAN_AND_MEAN"
"-Xlinker" "/NODEFAULTLIB:libcmt"
"-Wno-nonportable-include-path"
)
fi
elif [ "$host" == "Darwin" ]; then
if [ "$platform" == "desktop" ]; then
cflags+=("-ObjC++")
cflags+=("-fPIC")
fi
fi

if [ "$platform" == "android" ]; then
android_fte > /dev/null 2>&1
cflags+=("-DANDROID -pthreads -fexceptions -fPIC -frtti -fsigned-char -D_FILE_OFFSET_BITS=64 -Wno-invalid-command-line-argument -Wno-unused-command-line-argument")
cflags+=("-I$(dirname $NDK_BUILD)/sources/cxx-stl/llvm-libc++/include")
cflags+=("-I$(dirname $NDK_BUILD)/sources/cxx-stl/llvm-libc++abi/include")
fi

if [ "$platform" == "ios" ] || [ "$platform" == "ios-simulator" ]; then
if [ "$host" != "Darwin" ]; then
echo "error: Cannot generate pkgconfig file for iPhoneOS or iPhoneSimulator on '$host'" >&2
exit 1
fi

if [ "$platform" == "ios" ]; then
ios_sdk_path="$(xcrun -sdk iphoneos -show-sdk-path)"
cflags+=("-arch arm64")
cflags+=("-target arm64-apple-ios")
cflags+=("-Wno-unguarded-availability-new")
cflags+=("-miphoneos-version-min=$IPHONEOS_VERSION_MIN")
elif [ "$platform" == "ios-simulator" ]; then
ios_sdk_path="$(xcrun -sdk iphonesimulator -show-sdk-path)"
cflags+=("-arch $arch")
cflags+=("-mios-simulator-version-min=$IPHONEOS_VERSION_MIN")
fi
cflags+=("-iframeworkwithsysroot /System/Library/Frameworks")
cflags+=("-isysroot $ios_sdk_path/")
cflags+=("-F $ios_sdk_path/System/Library/Frameworks/")
cflags+=("-fembed-bitcode")
fi

export CFLAGS="${cflags[@]}"
export LDFLAGS="${ldflags[@]}"
export DEPENDENCIES="${dependencies[@]}"
export VERSION="$version"
export LIB_DIRECTORY="$lib_directory"
export INCLUDE_DIRECTORY="$include_directory"

rm -f "$output"

if ! cat "$input" | mush > "$output"; then
exit $?
fi

echo "Wrote pkgconfig file to '$output'"
14 changes: 10 additions & 4 deletions bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ function _build_cli {
local test_headers=()
if [[ -z "$ignore_header_mtimes" ]]; then
test_headers+=("$(find "$src"/cli/*.hh 2>/dev/null)")
test_headers+=("$(ls "$src"/*.hh)")
fi
test_headers+=("$src"/../VERSION.txt)
local newest_mtime=0
Expand Down Expand Up @@ -487,10 +486,10 @@ function _prebuild_ios_simulator_main () {
function _prepare {
echo "# preparing directories..."
local arch="$(host_arch)"
rm -rf "$SOCKET_HOME"/{lib$d,src,bin,include,objects,api}
rm -rf "$SOCKET_HOME"/{lib$d,src,bin,include,objects,api,pkgconfig}
rm -rf "$SOCKET_HOME"/{lib$d,objects}/"$arch-desktop"

mkdir -p "$SOCKET_HOME"/{lib$d,src,bin,include,objects,api}
mkdir -p "$SOCKET_HOME"/{lib$d,src,bin,include,objects,api,pkgconfig}
mkdir -p "$SOCKET_HOME"/{lib$d,objects}/"$arch-desktop"

if [[ "$host" = "Darwin" ]]; then
Expand Down Expand Up @@ -572,6 +571,13 @@ function _install {
exit 1
fi

if [ "$host" == "Linux" ]; then
echo "# copying pkgconfig to $SOCKET_HOME/pkgconfig"
rm -rf "$SOCKET_HOME/pkgconfig"
mkdir -p "$SOCKET_HOME/pkgconfig"
cp -rfp "$BUILD_DIR/$arch-$platform/pkgconfig"/* "$SOCKET_HOME/pkgconfig"
fi

if [ "$platform" == "desktop" ]; then
echo "# copying js api to $SOCKET_HOME/api"
mkdir -p "$SOCKET_HOME/api"
Expand Down Expand Up @@ -897,7 +903,6 @@ function onsignal () {
exit "$status"
}

_check_compiler_features
_prepare
cd "$BUILD_DIR" || exit 1

Expand Down Expand Up @@ -966,6 +971,7 @@ if [[ "$host" == "Win32" ]]; then
wait $_compile_libuv_pid
fi

_check_compiler_features
_build_runtime_library
_build_cli & pids+=($!)

Expand Down
4 changes: 1 addition & 3 deletions bin/ldflags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ if [[ "$host" = "Darwin" ]]; then
ldflags+=("-ldl")
elif [[ "$host" = "Linux" ]]; then
ldflags+=("-ldl")
if [ -z "$BUILDING_SSC_CLI" ]; then
ldflags+=($(pkg-config --libs gtk+-3.0 webkit2gtk-4.1))
fi
ldflags+=($(pkg-config --libs gtk+-3.0 webkit2gtk-4.1))
elif [[ "$host" = "Win32" ]]; then
if [[ -n "$DEBUG" ]]; then
# https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170
Expand Down
Loading

0 comments on commit 2f89e1c

Please sign in to comment.