Skip to content

Commit

Permalink
Update podspec and add cmake script
Browse files Browse the repository at this point in the history
  • Loading branch information
takameyer committed Jan 4, 2024
1 parent 380be44 commit c5a5e2a
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.personal-information.location</key>
<true/>
</dict>
</plist>
25 changes: 25 additions & 0 deletions packages/realm/RealmJS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ Pod::Spec.new do |s|
s.source = { :http => 'https://github.com/realm/realm-js/blob/main/CONTRIBUTING.md#how-to-debug-react-native-podspec' }

s.source_files = 'react-native/ios/RealmReact/*.mm'
# 'binding/ios/*.mm',
# #'binding/jsi/*.{hpp,cpp,h}',
# 'binding/*.{hpp}',
# 'bindgen/src/**/*.{h}'
# 'bindgen/vendor/**/*.{h,hpp}'

s.public_header_files = 'react-native/ios/RealmReact/*.h'

s.frameworks = uses_frameworks ? ['React'] : []
Expand All @@ -51,6 +57,7 @@ Pod::Spec.new do |s|
s.pod_target_xcconfig = {
# Setting up clang
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
#'GCC_CXX_LANGUAGE_STANDARD' => 'c++17',
'CLANG_CXX_LIBRARY' => 'libc++',
# Setting the current project version and versioning system to get a symbol for analytics
'CURRENT_PROJECT_VERSION' => s.version,
Expand All @@ -67,4 +74,22 @@ Pod::Spec.new do |s|
s.vendored_frameworks = 'react-native/ios/realm-js-ios.xcframework'

s.dependency 'React'

script_location = "#{__dir__}/scripts/build-ios-on-install.sh"

CMAKE_PATH = Pod::Executable::which!('cmake')
NODE_PATH = Pod::Executable::which!('node')

# Post install script
s.script_phase = {
:name => 'Generate Realm xcframework',
:execution_position => :before_compile,
:script => <<-EOS
export CMAKE_PATH=#{CMAKE_PATH}
export NODE_PATH=#{NODE_PATH}
/bin/sh -c \"#{script_location} -c $CONFIGURATION $PLATFORM_NAME\"
EOS
}
end

#:script => "bash -c \"echo \"#{__dir__}/scripts/build-ios-on-install.sh -c \"$CONFIGURATION\" \"$EFFECTIVE_PLATFORM_NAME\"\"\"",
13 changes: 12 additions & 1 deletion packages/realm/bindgen/jsi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
# This enables building for iOS on the end-users machine,
# where "react-native" is installed as a sibling to our package instead of being a dev-dependency of our package.

if(NOT DEFINED NODE_PATH)
set(NODE_PATH $ENV{NODE_PATH})
if(NOT NODE_PATH)
find_program(NODE_PATH node)
endif()
endif()

if(NOT DEFINED NODE_PATH)
message(FATAL_ERROR "Node.js not found")
endif()

execute_process(
COMMAND node --print "path.dirname(require.resolve('react-native/package.json'))"
COMMAND ${NODE_PATH} --print "path.dirname(require.resolve('react-native/package.json'))"
OUTPUT_VARIABLE REACT_NATIVE_ROOT_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY
Expand Down
139 changes: 139 additions & 0 deletions packages/realm/scripts/build-ios-on-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/bash

set -e
set -o pipefail

# Start in the root directory of the project.
cd "$(dirname "$0")/.."

# Need npx and node
NODE_DIRECTORY=$(dirname "$NODE_PATH")
export PATH="$NODE_DIRECTORY:$PATH"

# Take homebrew installs as well
CMAKE_DIRECTORY=$(dirname "$CMAKE_PATH")
export PATH="$CMAKE_DIRECTORY:$PATH"

PROJECT_ROOT=$(pwd)
SDK_PATH=$PROJECT_ROOT
BINDGEN_PATH=$PROJECT_ROOT/bindgen
BINDING_PATH=$PROJECT_ROOT/binding
SCRIPT=$(basename "${BASH_SOURCE[0]}")

function usage {
echo "Usage: ${SCRIPT} [-c <configuration>] [<platforms>]"
echo ""
echo "Arguments:"
echo " -c : build configuration (Debug or Release)"
echo " <platforms> : platforms to build for (maccatalyst, ios, or iphonesimulator)"
exit 1;
}

CONFIGURATION=Release
SUPPORT_PLATFORMS=(maccatalyst ios iphonesimulator)

function is_supported_platform(){
for platform in "${SUPPORT_PLATFORMS[@]}"; do
[[ "${platform}" == $1 ]] && return 0
done
return 1
}

# Parse the options
while getopts ":c:" opt; do
case "${opt}" in
c) CONFIGURATION=${OPTARG};;
*) usage;;
esac
done

echo "Configuration: ${CONFIGURATION}"

shift $((OPTIND-1))
PLATFORMS=($@)

if [ -z ${PLATFORMS} ]; then
echo "No platform given. building all platforms...";
PLATFORMS=(ios maccatalyst iphonesimulator)
else
echo "Building for...";
for check_platform in "${PLATFORMS[@]}"; do
if ! is_supported_platform $check_platform; then
echo "${check_platform} is not a supported platform"
usage
exit 1
fi
echo ${check_platform};
done
fi

DESTINATIONS=()
LIBRARIES=()
BUILD_LIB_CMDS=()

for platform in "${PLATFORMS[@]}"; do
case "$platform" in
ios)
DESTINATIONS+=(-destination 'generic/platform=iOS')
LIBRARIES+=(-library ./out/$CONFIGURATION-iphoneos/librealm-js-ios.a -headers ./_include)
BUILD_LIB_CMDS+=("xcrun libtool -static -D -o ./out/$CONFIGURATION-iphoneos/librealm-js-ios.a ./out/$CONFIGURATION-iphoneos/*.a")
;;
maccatalyst)
DESTINATIONS+=(-destination 'platform=macOS,arch=x86_64,variant=Mac Catalyst')
LIBRARIES+=(-library ./out/$CONFIGURATION-maccatalyst/librealm-js-ios.a -headers ./_include)
BUILD_LIB_CMDS+=("xcrun libtool -static -D -o ./out/$CONFIGURATION-maccatalyst/librealm-js-ios.a ./out/$CONFIGURATION-maccatalyst/*.a")
;;
iphonesimulator)
DESTINATIONS+=(-destination 'generic/platform=iOS Simulator')
LIBRARIES+=(-library ./out/$CONFIGURATION-iphonesimulator/librealm-js-ios.a -headers ./_include)
BUILD_LIB_CMDS+=("xcrun libtool -static -D -o ./out/$CONFIGURATION-iphonesimulator/librealm-js-ios.a ./out/$CONFIGURATION-iphonesimulator/*.a")
;;
*)
echo "${platform} not supported"
usage
exit 1
;;
esac
done

pushd $SDK_PATH/react-native/ios

mkdir -p build
pushd build

# If the developer directory is not set, use the default Xcode path
SELECTED_DEVELOPER_DIR="$(xcode-select -p)"
DEVELOPER_DIR="${DEVELOPER_DIR:-${SELECTED_DEVELOPER_DIR}}"

# Configure CMake project
env DEVELOPER_DIR="$DEVELOPER_DIR" SDKROOT="$SDKROOT" $CMAKE_PATH "$BINDGEN_PATH" -GXcode \
-DCMAKE_TOOLCHAIN_FILE="$BINDGEN_PATH/vendor/realm-core/tools/cmake/xcode.toolchain.cmake" \
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)/out/$<CONFIG>\$EFFECTIVE_PLATFORM_NAME" \
# -DCMAKE_C_COMPILER="$PROJECT_ROOT/scripts/ccache-clang.sh" \
# -DCMAKE_CXX_COMPILER="$PROJECT_ROOT/scripts/ccache-clang++.sh"
# -DCMAKE_C_COMPILER="/usr/bin/clang" \
# -DCMAKE_CXX_COMPILER="/usr/bin/clang++"\


DEVELOPER_DIR="$DEVELOPER_DIR" xcodebuild build \
-scheme realm-js-ios \
"${DESTINATIONS[@]}" \
-configuration $CONFIGURATION \
# CC="$PROJECT_ROOT/scripts/ccache-clang.sh" \
# CXX="$PROJECT_ROOT/scripts/ccache-clang++.sh" \
ONLY_ACTIVE_ARCH=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
SUPPORTS_MACCATALYST=YES

for cmd in "${BUILD_LIB_CMDS[@]}"; do
eval "${cmd}"
done

rm -rf _include
mkdir -p _include/realm-js-ios
cp "$BINDING_PATH"/jsi/jsi_init.h _include/realm-js-ios/

rm -rf ../realm-js-ios.xcframework
xcodebuild -create-xcframework \
"${LIBRARIES[@]}" \
-output ../realm-js-ios.xcframework
17 changes: 17 additions & 0 deletions packages/realm/scripts/ccache-clang++.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
if command -v ccache &> /dev/null; then
# reference for xcode specific cacche settings: reactnative docs and ccache manpage
# https://reactnative.dev/docs/build-speed#xcode-specific-setup
# https://ccache.dev/manual/4.3.html
export CCACHE_MAXSIZE=10G
export CCACHE_CPP2=true
export CCACHE_DIRECT=true
export CCACHE_DEPEND=true
export CCACHE_HARDLINK=true
export CCACHE_FILECLONE=true
export CCACHE_INODECACHE=true
export CCACHE_SLOPPINESS=file_macro,time_macros,include_file_mtime,include_file_ctime,file_stat_matches,modules,ivfsoverlay,pch_defines,system_headers
exec ccache clang++ "$@"
else
exec clang++ "$@"
fi
17 changes: 17 additions & 0 deletions packages/realm/scripts/ccache-clang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
if command -v ccache &> /dev/null; then
# reference for xcode specific cacche settings: reactnative docs and ccache manpage
# https://reactnative.dev/docs/build-speed#xcode-specific-setup
# https://ccache.dev/manual/4.3.html
export CCACHE_MAXSIZE=10G
export CCACHE_CPP2=true
export CCACHE_DIRECT=true
export CCACHE_DEPEND=true
export CCACHE_HARDLINK=true
export CCACHE_FILECLONE=true
export CCACHE_INODECACHE=true
export CCACHE_SLOPPINESS=file_macro,time_macros,include_file_mtime,include_file_ctime,file_stat_matches,modules,ivfsoverlay,pch_defines,system_headers
exec ccache clang "$@"
else
exec clang "$@"
fi
17 changes: 17 additions & 0 deletions packages/realm/scripts/cmake-ios.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

# If the developer directory is not set, use the default Xcode path
SELECTED_DEVELOPER_DIR="$(xcode-select -p)"
DEVELOPER_DIR="${DEVELOPER_DIR:-${SELECTED_DEVELOPER_DIR}}"

PROJECT_ROOT=$(pwd)
BINDGEN_PATH=$PROJECT_ROOT/bindgen

pushd $PROJECT_ROOT/react-native/ios

mkdir -p build
pushd build

# Configure CMake project
SDKROOT="$DEVELOPER_DIR/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/" cmake "$BINDGEN_PATH" -GXcode \
-DCMAKE_TOOLCHAIN_FILE="$BINDGEN_PATH/vendor/realm-core/tools/cmake/xcode.toolchain.cmake" \
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)/out/$<CONFIG>\$EFFECTIVE_PLATFORM_NAME" \

0 comments on commit c5a5e2a

Please sign in to comment.