-
Notifications
You must be signed in to change notification settings - Fork 760
Expand file tree
/
Copy pathbuild-apple-framework.sh
More file actions
executable file
·243 lines (199 loc) · 8.16 KB
/
build-apple-framework.sh
File metadata and controls
executable file
·243 lines (199 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# Defines functions for building various Hermes frameworks.
# See build-ios-framework.sh and build-mac-framework.sh for usage examples.
CURR_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
IMPORT_HOST_COMPILERS_PATH=${HERMES_OVERRIDE_HERMESC_PATH:-$PWD/build_host_hermesc/ImportHostCompilers.cmake}
BUILD_TYPE=${BUILD_TYPE:-Debug}
HERMES_PATH="$CURR_SCRIPT_DIR/.."
HERMES_COMPILER_PACKAGE_PATH="$HERMES_PATH/npm/hermes-compiler"
NUM_CORES=$(sysctl -n hw.ncpu)
PLATFORMS=("macosx" "iphoneos" "iphonesimulator" "catalyst" "xros" "xrsimulator" "appletvos" "appletvsimulator")
if [[ -z "$JSI_PATH" ]]; then
JSI_PATH="$HERMES_PATH/API/jsi"
fi
function use_env_var {
if [[ -n "$1" ]]; then
echo "$1"
else
echo "error: Missing $2 environment variable"
exit 1
fi
}
function get_ios_deployment_target {
use_env_var "${IOS_DEPLOYMENT_TARGET}" "IOS_DEPLOYMENT_TARGET"
}
function get_visionos_deployment_target {
use_env_var "${XROS_DEPLOYMENT_TARGET}" "XROS_DEPLOYMENT_TARGET"
}
function get_mac_deployment_target {
use_env_var "${MAC_DEPLOYMENT_TARGET}" "MAC_DEPLOYMENT_TARGET"
}
function get_release_version {
local package_json_path="$HERMES_COMPILER_PACKAGE_PATH/package.json"
if [[ -f "$package_json_path" ]]; then
local version
version=$(node -e "console.log(JSON.parse(require('fs').readFileSync('$package_json_path', 'utf8')).version)" 2>/dev/null)
if [[ -n "$version" ]]; then
echo "$version"
return
else
echo >&2 "Error: Failed to read version from $package_json_path"
exit 1
fi
else
echo >&2 "Error: Package file not found at $package_json_path"
exit 1
fi
}
# Build host hermes compiler for internal bytecode
function build_host_hermesc {
echo "Building hermesc"
pushd "$HERMES_PATH" > /dev/null || exit 1
cmake -S . -B build_host_hermesc -DJSI_DIR="$JSI_PATH"
cmake --build ./build_host_hermesc --target hermesc -j "${NUM_CORES}"
popd > /dev/null || exit 1
}
# Utility function to configure an Apple framework
function configure_apple_framework {
local enable_debugger cmake_build_type xcode_15_flags xcode_major_version
if [[ $BUILD_TYPE == "Debug" ]]; then
enable_debugger="true"
else
enable_debugger="false"
fi
if [[ $BUILD_TYPE == "Debug" ]]; then
# JS developers aren't VM developers.
# Therefore we're passing as build type Release, to provide a faster build.
cmake_build_type="Release"
else
cmake_build_type="MinSizeRel"
fi
xcode_15_flags=""
xcode_major_version=$(xcodebuild -version | grep -oE '[0-9]*' | head -n 1)
if [[ $xcode_major_version -ge 15 ]]; then
xcode_15_flags="LINKER:-ld_classic"
fi
# For catalyst, we need to set the target triple to use the macabi environment.
# The architecture in -target is overridden by CMake's -arch flags, so we can use
# any architecture here (arm64). CMake will add -arch x86_64 and -arch arm64 which
# correctly override just the architecture portion while preserving ios-macabi.
shared_clang_flags=""
if [[ $1 == "catalyst" ]]; then
shared_clang_flags="-target arm64-apple-ios$3-macabi -isystem ${CMAKE_OSX_SYSROOT}/System/iOSSupport/usr/include"
fi
pushd "$HERMES_PATH" > /dev/null || exit 1
cmake -S . -B "build_$1" \
-DHERMES_EXTRA_LINKER_FLAGS="$xcode_15_flags" \
-DHERMES_APPLE_TARGET_PLATFORM:STRING="$1" \
-DCMAKE_OSX_ARCHITECTURES:STRING="$2" \
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING="$3" \
-DHERMES_ENABLE_DEBUGGER:BOOLEAN="$enable_debugger" \
-DHERMES_ENABLE_INTL:BOOLEAN=true \
-DHERMES_ENABLE_LIBFUZZER:BOOLEAN=false \
-DHERMES_ENABLE_FUZZILLI:BOOLEAN=false \
-DHERMES_ENABLE_TEST_SUITE:BOOLEAN=false \
-DHERMES_ENABLE_BITCODE:BOOLEAN=false \
-DHERMES_BUILD_APPLE_FRAMEWORK:BOOLEAN=true \
-DHERMES_BUILD_SHARED_JSI:BOOLEAN=false \
-DCMAKE_CXX_FLAGS:STRING="-gdwarf $shared_clang_flags" \
-DCMAKE_C_FLAGS:STRING="-gdwarf $shared_clang_flags" \
-DIMPORT_HOST_COMPILERS:PATH="$IMPORT_HOST_COMPILERS_PATH" \
-DHERMES_RELEASE_VERSION="$(get_release_version)" \
-DJSI_DIR="$JSI_PATH" \
-DCMAKE_BUILD_TYPE="$cmake_build_type"
popd > /dev/null || exit 1
}
function build_host_hermesc_if_needed {
if [[ ! -f "$IMPORT_HOST_COMPILERS_PATH" ]]; then
build_host_hermesc
else
echo "[HermesC] Skipping! Found an existent hermesc already at: $IMPORT_HOST_COMPILERS_PATH"
fi
}
# Utility function to build an Apple framework
function build_apple_framework {
# Only build host HermesC if no file found at $IMPORT_HOST_COMPILERS_PATH
build_host_hermesc_if_needed
# Confirm ImportHostCompilers.cmake is now available.
[ ! -f "$IMPORT_HOST_COMPILERS_PATH" ] &&
echo "Host hermesc is required to build apple frameworks!"
# $1: platform, $2: architectures, $3: deployment target
echo "Building $BUILD_TYPE framework for $1 with architectures: $2"
configure_apple_framework "$1" "$2" "$3"
pushd "$HERMES_PATH" > /dev/null || exit 1
mkdir -p "destroot/Library/Frameworks/$1"
cmake --build "./build_$1" --target hermesvm -j "${NUM_CORES}"
# Produce the dSYM.
xcrun dsymutil "./build_$1/lib/hermesvm.framework/hermesvm" -o "./build_$1/lib/hermesvm.framework.dSYM"
cp -R "./build_$1"/lib/hermesvm.framework* "destroot/Library/Frameworks/$1"
# In a MacOS build, also produce the hermes and hermesc CLI tools.
if [[ $1 == macosx ]]; then
cmake --build "./build_$1" --target hermesc hermes -j "${NUM_CORES}"
mkdir -p destroot/bin
cp "./build_$1/bin"/* "destroot/bin"
fi
# Copy over Hermes and JSI API headers.
mkdir -p destroot/include/hermes/Public
cp public/hermes/Public/*.h destroot/include/hermes/Public
mkdir -p destroot/include/hermes
cp API/hermes/*.h destroot/include/hermes
mkdir -p destroot/include/hermes/cdp
cp API/hermes/cdp/*.h destroot/include/hermes/cdp
mkdir -p destroot/include/hermes/inspector
cp API/hermes/inspector/*.h destroot/include/hermes/inspector
mkdir -p destroot/include/hermes/inspector/chrome
cp API/hermes/inspector/chrome/*.h destroot/include/hermes/inspector/chrome
mkdir -p destroot/include/jsi
cp "$JSI_PATH"/jsi/*.h destroot/include/jsi
popd > /dev/null || exit 1
}
function prepare_dest_root_for_ci {
mkdir -p "destroot/bin"
for platform in "${PLATFORMS[@]}"; do
mkdir -p "destroot/Library/Frameworks/$platform"
cp -R "./build_$platform/lib/hermesvm.framework"* "destroot/Library/Frameworks/$platform"
done
cp "./build_macosx/bin/"* "destroot/bin"
# Copy over Hermes and JSI API headers.
mkdir -p destroot/include/hermes/Public
cp public/hermes/Public/*.h destroot/include/hermes/Public
mkdir -p destroot/include/hermes
cp API/hermes/*.h destroot/include/hermes
mkdir -p destroot/include/hermes/cdp
cp API/hermes/cdp/*.h destroot/include/hermes/cdp
mkdir -p destroot/include/hermes/inspector
cp API/hermes/inspector/*.h destroot/include/hermes/inspector
mkdir -p destroot/include/hermes/inspector/chrome
cp API/hermes/inspector/chrome/*.h destroot/include/hermes/inspector/chrome
mkdir -p destroot/include/jsi
cp "$JSI_PATH"/jsi/*.h destroot/include/jsi
}
# Accepts an array of frameworks and will place all of
# the architectures into an universal folder and then remove
# the merged frameworks from destroot
function create_universal_framework {
pushd "$HERMES_PATH/destroot/Library/Frameworks" > /dev/null || exit 1
local platforms=("$@")
local args=""
echo "Creating universal framework for platforms: ${platforms[*]}"
for i in "${!platforms[@]}"; do
local platform="${platforms[$i]}"
local hermes_framework_path="${platform}/hermesvm.framework"
args+="-framework $hermes_framework_path "
done
mkdir -p universal
# shellcheck disable=SC2086
if xcodebuild -create-xcframework $args -output "universal/hermesvm.xcframework"
then
# # Remove the thin iOS hermesvm.frameworks that are now part of the universal
# XCFramework
for platform in "${platforms[@]}"; do
rm -r "$platform"
done
fi
popd > /dev/null || exit 1
}