-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild-native-libraries.sh
executable file
·281 lines (240 loc) · 10.9 KB
/
build-native-libraries.sh
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
# https://google.github.io/styleguide/shellguide.html
if ! [[ -x "$(command -v cmake)" ]]; then
echo "Error: 'cmake' is not installed" >&2
exit 1
fi
# Get the .NET runtime identifier (RID) for the current operating system
# See: https://learn.microsoft.com/en-us/dotnet/core/rid-catalog
function get_dotnet_rid() {
local UNAME_OS_STRING="$(uname -a)"
case "${UNAME_OS_STRING}" in
*Microsoft*) local RID_OS="win";;
*microsoft*) local RID_OS="win";;
Linux*) local RID_OS="linux";;
Darwin*) local RID_OS="osx";;
CYGWIN*) local RID_OS="linux";;
MINGW*) local RID_OS="win";;
*Msys) local RID_OS="win";;
*) local RID_OS="UNKNOWN:${UNAME_OS_STRING}"
esac
local UNAME_ARCH_STRING="$(uname -m)"
case "${UNAME_ARCH_STRING}" in
x86_64) local RID_ARCH="x64";;
arm64) local RID_ARCH="arm64";;
*) local RID_ARCH="UNKNOWN:${UNAME_ARCH_STRING}"
esac
echo "$RID_OS-$RID_ARCH"
return 0
}
function validate_build_type() {
if [[ ! -z $BUILD_TYPE ]]; then
if [[ $BUILD_TYPE != 'debug' && $BUILD_TYPE != 'release' ]]; then
echo "Build type '$BUILD_TYPE' is not known or can't be used when building native libraries. Please use one of the following: 'debug', 'release'" >&2
exit 1
fi
else
BUILD_TYPE="release"
fi
echo "BUILD_TYPE: $BUILD_TYPE"
}
function validate_rid {
if [[ ! -z $DESIRED_RID ]]; then
if [[ $DESIRED_RID == 'win-x64' || $DESIRED_RID == 'win-arm64' ]]; then
if [[ $RID == 'win-x64' || $RID == 'win-arm64' ]]; then
RID="$DESIRED_RID"
else
echo "RID '$DESIRED_RID' is not known or can not be be used when building native libraries for Windows. Please use one of the following for Windows: 'win-x64', 'win-arm64'" >&2
exit 1
fi
elif [[ $DESIRED_RID == 'osx-x64' || $DESIRED_RID == 'osx-arm64' ]]; then
if [[ $RID == 'osx-x64' || $RID == 'osx-arm64' ]]; then
RID="$DESIRED_RID"
else
echo "RID '$DESIRED_RID' is not known or can not be be used when building native libraries for macOS. Please use one of the following for macOS: 'osx-x64', 'osx-arm64'" >&2
exit 1
fi
elif [[ $DESIRED_RID == 'linux-x64' || $DESIRED_RID == 'linux-arm64' ]]; then
if [[ $RID == 'linux-x64' || $RID == 'linux-arm64' ]]; then
RID="$DESIRED_RID"
else
echo "RID '$DESIRED_RID' is not known or can not be be used when building native libraries for Linux. Please use one of the following for Linux: 'linux-x64', 'linux-arm64'" >&2
exit 1
fi
else
echo "RID '$DESIRED_RID' is not known for building native libraries. Please use one of the following for the appropriate operating system: 'win-x64', 'win-arm64', 'osx-x64', 'osx-arm64', 'linux-x64', 'linux-arm64'" >&2
exit 1
fi
fi
echo "RID: $RID"
}
function initialize() {
DIRECTORY_CURRENT=`pwd`
# Get the directory where this script exists so that this script can be run from anywhere (e.g. doesn't have to be executed in the same directory)
DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Get the name of this script file without extension
FILE_NAME_WITHOUT_EXTENSION=`basename ${BASH_SOURCE[0]} ".sh"`
# Directory used to create this script's files/folders
DIRECTORY_CONTAINER="$DIRECTORY/.$FILE_NAME_WITHOUT_EXTENSION"
mkdir -p "$DIRECTORY_CONTAINER"
# Directory to place CMake build files per project
DIRECTORY_CONTAINER_BUILDS="$DIRECTORY_CONTAINER/builds"
mkdir -p "$DIRECTORY_CONTAINER_BUILDS"
# Directories to place built artifacts
DIRECTORY_OUTPUT="$DIRECTORY_CONTAINER/out/$RID-$BUILD_TYPE"
if [[ -d "$DIRECTORY_OUTPUT" ]]; then
rm -r "$DIRECTORY_OUTPUT"
mkdir "$DIRECTORY_OUTPUT"
fi
}
function build_library_cmake() {
local LIB_NAME=$1
local LIB_VERSION=$2
local LIB_DIRECTORY_SOURCE=$3
local LIB_CMAKE_FLAGS=`echo $4 | tr '\n' ' '`
local LIB_CMAKE_OVERRIDE_FILE_PATH=$5
local LIB_NAME_UPPERCASE="$(echo "$LIB_NAME" | tr 'a-z' 'A-Z')"
if [[ -z $LIB_VERSION ]]; then
local LIB_DIRECTORY_BUILD="$DIRECTORY_CONTAINER_BUILDS/$LIB_NAME-$RID-$BUILD_TYPE"
else
local LIB_DIRECTORY_BUILD="$DIRECTORY_CONTAINER_BUILDS/$LIB_NAME-$LIB_VERSION-$RID-$BUILD_TYPE"
fi
if [ -d "$LIB_DIRECTORY_BUILD" ]; then
rm -r $LIB_DIRECTORY_BUILD
fi
mkdir -p $LIB_DIRECTORY_BUILD
# Make the variables usable outside this function with unique names
eval "LIB_${LIB_NAME_UPPERCASE}_DIRECTORY_BUILD=\"$LIB_DIRECTORY_BUILD\""
echo "Building library '$LIB_NAME' ($BUILD_TYPE) using CMake..."
if [[ -z $LIB_DIRECTORY_SOURCE ]]; then
eval "LIB_DIRECTORY_SOURCE=\"\${LIB_${LIB_NAME_UPPERCASE}_DIRECTORY_SOURCE}\""
if [[ -z $LIB_DIRECTORY_SOURCE ]]; then
echo "Building library '$LIB_NAME' failed: the source directory is not provided" >&2
exit 1
fi
fi
if [ ! -d "$LIB_DIRECTORY_SOURCE" ]; then
echo "Building library '$LIB_NAME' ($BUILD_TYPE) failed: the source directory '$LIB_DIRECTORY_SOURCE' does not exist" >&2
exit 1
fi
if [ ! -f "$LIB_DIRECTORY_SOURCE/CMakeLists.txt" ]; then
echo "Building library '$LIB_NAME' ($BUILD_TYPE) failed: the source directory '$LIB_DIRECTORY_SOURCE' does not have a 'CMakeLists.txt' file" >&2
exit 1
fi
if [[ $BUILD_TYPE == 'debug' ]]; then
local CMAKE_BUILD_TYPE="Debug"
elif [[ $BUILD_TYPE == 'release' ]]; then
local CMAKE_BUILD_TYPE="Release"
else
echo "Unknown build type '$BUILD_TYPE' for CMake" >&2
exit 1
fi
if [[ $RID == 'win-x64' ]]; then
local CMAKE_FLAGS_ARCH="-A x64"
elif [[ $RID == 'win-arm64' ]]; then
local CMAKE_FLAGS_ARCH="-A ARM64"
elif [[ $RID == 'osx-x64' ]]; then
local CMAKE_FLAGS_ARCH="-D CMAKE_SYSTEM_NAME=Darwin -D CMAKE_OSX_ARCHITECTURES=x86_64 -D CMAKE_OSX_DEPLOYMENT_TARGET=13.0 -D CMAKE_C_FLAGS=-mmacosx-version-min=13.0 -D CMAKE_CXX_FLAGS=-mmacosx-version-min=13.0"
elif [[ $RID == 'osx-arm64' ]]; then
# https://en.wikipedia.org/wiki/MacOS_version_history says Apple Silicon is only supported on 11.0 or later
local CMAKE_FLAGS_ARCH="-D CMAKE_SYSTEM_NAME=Darwin -D CMAKE_OSX_ARCHITECTURES=arm64 -D CMAKE_OSX_DEPLOYMENT_TARGET=13.0 -D CMAKE_C_FLAGS=-mmacosx-version-min=13.0 -D CMAKE_CXX_FLAGS=-mmacosx-version-min=13.0"
elif [[ $RID == 'linux-x64' ]]; then
local CMAKE_FLAGS_ARCH="-D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_PROCESSOR=x86_64 -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ -D CMAKE_C_FLAGS=-m64 -D CMAKE_CXX_FLAGS=-m64"
elif [[ $RID == 'linux-arm64' ]]; then
# Requires packages: gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
local CMAKE_FLAGS_ARCH="$CMAKE_FLAGS -D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_PROCESSOR=aarch64 -D CMAKE_C_COMPILER=aarch64-linux-gnu-gcc -D CMAKE_CXX_COMPILER=aarch64-linux-gnu-g++"
else
echo "Unknown RID '$RID' for CMake" >&2
exit 1
fi
local CMAKE_FLAGS_BUILD_TYPE="-D CMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE"
local CMAKE_FLAGS_OUTPUT="-D CMAKE_INSTALL_PREFIX=$DIRECTORY_OUTPUT"
cmake -S "$LIB_DIRECTORY_SOURCE" -B "$LIB_DIRECTORY_BUILD" $LIB_CMAKE_FLAGS $CMAKE_FLAGS_OUTPUT $CMAKE_FLAGS_ARCH $CMAKE_FLAGS_BUILD_TYPE
if [[ $? -ne 0 ]]; then
echo "Building library '$LIB_NAME' ($BUILD_TYPE) failed: CMake was not able to generate build files" >&2
exit 1
fi
cmake --build "$LIB_DIRECTORY_BUILD" --config $CMAKE_BUILD_TYPE --parallel
if [[ $? -ne 0 ]]; then
echo "Building library '$LIB_NAME' ($BUILD_TYPE) failed: CMake build was not successful" >&2
exit 1
fi
cmake --install "$LIB_DIRECTORY_BUILD"
if [[ $? -ne 0 ]]; then
echo "Building library '$LIB_NAME' ($BUILD_TYPE) failed: CMake install was not successful" >&2
exit 1
fi
echo "Building library '$LIB_NAME' ($BUILD_TYPE) using CMake... done"
}
function build_SDL() {
local DIRECTORY_SOURCE="$DIRECTORY/SDL"
local CMAKE_BUILD_FLAGS="
-D SDL_SHARED=ON
-D SDL_STATIC=OFF
-D SDL_TEST_LIBRARY=OFF
-D SDL_TEST_LIBRARY=OFF
-D SDL_TESTS=OFF
-D SDL_DISABLE_INSTALL_DOCS=OFF"
build_library_cmake "SDL3" "" "$DIRECTORY_SOURCE" "$CMAKE_BUILD_FLAGS"
}
function build_SDL_image() {
local DIRECTORY_SOURCE="$DIRECTORY/SDL_image"
local CMAKE_BUILD_FLAGS="
-D SDL3_DIR=$LIB_SDL3_DIRECTORY_BUILD
-D BUILD_SHARED_LIBS=ON
-D SDLIMAGE_DEPS_SHARED=OFF
-D SDLIMAGE_VENDORED=ON
-D SDLIMAGE_STRICT=ON
-D SDLIMAGE_SAMPLES=OFF
-D SDLIMAGE_TESTS=OFF
"
build_library_cmake "SDL3_image" "" "$DIRECTORY_SOURCE" "$CMAKE_BUILD_FLAGS"
}
function copy_files() {
echo "Copying files..."
case $RID in
win*)
local DIRECTORY_COPY_SOURCE="$DIRECTORY_OUTPUT/bin"
;;
*)
local DIRECTORY_COPY_SOURCE="$DIRECTORY_OUTPUT/lib"
;;
esac
local DIRECTORY_COPY_DESTINATION="$DIRECTORY/../lib/$RID"
mkdir -p "$DIRECTORY_COPY_DESTINATION"
find "$DIRECTORY_COPY_SOURCE" -type f \( -name "*.dll" -o -name "*.dylib" -o -name "*.so" \) | while read -r FILE_PATH; do
local RELATIVE_PATH="${FILE_PATH#$DIRECTORY_COPY_SOURCE/}"
local TARGET_FILE_PATH="$DIRECTORY_COPY_DESTINATION/$RELATIVE_PATH"
mkdir -p "$(dirname "$TARGET_FILE_PATH")"
echo "Copying file '$FILE_PATH' to '$TARGET_FILE_PATH'"
cp -p "$FILE_PATH" "$TARGET_FILE_PATH"
done
find "$DIRECTORY_COPY_SOURCE" -type l \( -name "*.dll" -o -name "*.dylib" -o -name "*.so" \) | while read -r FILE_PATH; do
local RELATIVE_PATH="${FILE_PATH#$DIRECTORY_COPY_SOURCE/}"
local TARGET_FILE_PATH="$DIRECTORY_COPY_DESTINATION/$RELATIVE_PATH"
mkdir -p "$(dirname "$TARGET_FILE_PATH")"
echo "Copying symlink '$FILE_PATH' to '$TARGET_FILE_PATH'"
cp -p "$FILE_PATH" "$TARGET_FILE_PATH"
done
cd "$DIRECTORY_COPY_DESTINATION"
if [[ $RID == 'osx-x64' || $RID == 'osx-arm64' ]]; then
rm libSDL3.dylib
mv libSDL3.0.dylib libSDL3.dylib
install_name_tool -id @rpath/libSDL3.dylib libSDL3.dylib
rm libSDL3_image.dylib
rm libSDL3_image.0.dylib
mv libSDL3_image.*.*.*.dylib libSDL3_image.dylib
install_name_tool -id @rpath/libSDL3_image.dylib libSDL3_image.dylib
install_name_tool -change @rpath/libSDL3.0.dylib @rpath/libSDL3.dylib libSDL3_image.dylib
fi
echo "Copying files... done"
}
BUILD_TYPE=$1
validate_build_type
DESIRED_RID=$2
RID=`get_dotnet_rid`
validate_rid
initialize
build_SDL
build_SDL_image
copy_files