|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Automatically exit on error |
| 4 | +set -e |
| 5 | + |
| 6 | +# ========================= # |
| 7 | +# PRELUDE: A note on rpaths # |
| 8 | +# ========================= # |
| 9 | + |
| 10 | +# Unlike Windows, macOS doesn't search the current directory by default when searching for shared libraries (.dylib) |
| 11 | +# It only searches the system default directories (usually /lib and /usr/lib) and the paths in LD_LIBRARY_PATH |
| 12 | + |
| 13 | +# The .NET Runtime will find the CSFML library in its NuGet packages just fine, but that library will then request |
| 14 | +# the OS for libsfml-(module).dylib, and the .NET Runtime will have no say in how that SFML library is found. |
| 15 | + |
| 16 | +# Without SFML installed globally on the system, this will fail, causing the loading of CSFML to fail, causing the |
| 17 | +# .NET Runtime to think the CSFML library doesn't exist or is invalid. |
| 18 | + |
| 19 | +# And so, we need to set the rpath of the CSFML library. |
| 20 | +# The rpath is a special value embedded straight into a library that specifies to the OS a list of folders where |
| 21 | +# other libraries that it references may be found. |
| 22 | +# $ORIGIN, a kind-of environment variable, can be used in rpath to point to the folder where the library currently is. |
| 23 | +# To let the OS know that we intend to use $ORIGIN, we need to add the ORIGIN flag to our ELF with the -z origin |
| 24 | +# gcc linker option |
| 25 | + |
| 26 | +# Since CSFML and SFML will always be deployed on the same folder by NuGet, we just need to add an rpath to CSFML |
| 27 | +# that points to $ORIGIN, causing the OS to search the current folder for SFML, without interference from .NET |
| 28 | + |
| 29 | +# We also add the same rpath to SFML itself for future-proofing, in case we ever decide to ship some Linux SFML |
| 30 | +# dependencies on the Native package. |
| 31 | + |
| 32 | +# You may need to `brew install coreutils` first for grealpath |
| 33 | +# See supported RID at https://docs.microsoft.com/en-us/dotnet/core/rid-catalog |
| 34 | + |
| 35 | +# =================================================== # |
| 36 | +# STEP 1: Setup all variables needed during the build # |
| 37 | +# =================================================== # |
| 38 | + |
| 39 | +if [[ -z "$1" ]]; then |
| 40 | + echo "Please specify the platform Runtime Identifier as an argument to this script" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +echo "Please note that all SFML dependencies must be installed and available to cmake. SFML does not ship with its linux dependencies." |
| 45 | + |
| 46 | +RID="$1" |
| 47 | + |
| 48 | +SFMLBranch="2.5.x" # The branch or tag of the SFML repository to be cloned |
| 49 | +CSFMLDir="$(grealpath "$(git rev-parse --show-toplevel)")" # The directory of the source code of CSFML |
| 50 | + |
| 51 | +OutDir="./CSFML/runtimes/$RID/native" # The base directory of all CSFML modules, used to copy the final libraries |
| 52 | +mkdir -p "$OutDir" |
| 53 | +OutDir="$(grealpath "$OutDir")" |
| 54 | + |
| 55 | +echo "Building $RID" |
| 56 | + |
| 57 | +mkdir -p "Build" |
| 58 | +pushd "Build" |
| 59 | + |
| 60 | +# ================== # |
| 61 | +# STEP 2: Clone SFML # |
| 62 | +# ================== # |
| 63 | + |
| 64 | +if [[ ! -d "SFML/.git" ]]; then |
| 65 | + echo "Cloning SFML" |
| 66 | + rm -rf "SFML" |
| 67 | + git clone --branch "$SFMLBranch" --depth 1 "git://github.com/SFML/SFML.git" "SFML" |
| 68 | +fi |
| 69 | + |
| 70 | +SFMLDir="$(grealpath SFML)" |
| 71 | + |
| 72 | +# ================== # |
| 73 | +# STEP 3: Build SFML # |
| 74 | +# ================== # |
| 75 | + |
| 76 | +rm -rf "$RID" |
| 77 | +mkdir -p "$RID" |
| 78 | +pushd "$RID" |
| 79 | + |
| 80 | +echo "Building SFML" |
| 81 | +mkdir -p SFML |
| 82 | +pushd SFML |
| 83 | + |
| 84 | +SFMLBuiltDir="$(grealpath .)" # The directory where SFML was built to. Used later to direct cmake when building CSFML |
| 85 | + |
| 86 | +mkdir -p lib |
| 87 | +# The directory that contains the final SFML libraries |
| 88 | +# Since linux libraries don't support static linking from a shared library, this is used to copy the |
| 89 | +# SFML shared libraries together with the CSFML shared libraries into SFML.Net |
| 90 | +SFMLLibDir="$(grealpath lib)" |
| 91 | + |
| 92 | +if [ $RID == "osx.10.15-x64" ]; then |
| 93 | + ARCHITECTURE="x86_64" |
| 94 | + TARGET="10.15" |
| 95 | + SYSROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/" |
| 96 | +elif [ $RID == "osx.11.0-x64" ]; then |
| 97 | + ARCHITECTURE="x86_64" |
| 98 | + TARGET="11.0" |
| 99 | + SYSROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/" |
| 100 | +elif [ $RID == "osx.11.0-arm64" ]; then |
| 101 | + ARCHITECTURE="arm64" |
| 102 | + TARGET="11.0" |
| 103 | + SYSROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk/" |
| 104 | + |
| 105 | + echo "Note: arm64 is only supported with SFML 2.6" |
| 106 | +else |
| 107 | + echo "Unsupported RID provided. Use 'osx.10.15-x64', 'osx.11.0-x64' or 'osx.11.0-arm64'" |
| 108 | + exit 1 |
| 109 | +fi |
| 110 | + |
| 111 | +cmake -E env \ |
| 112 | + cmake -G "Unix Makefiles" \ |
| 113 | + -D 'BUILD_SHARED_LIBS=ON' \ |
| 114 | + -D 'CMAKE_BUILD_TYPE=Release' \ |
| 115 | + -D "CMAKE_OSX_ARCHITECTURES=$ARCHITECTURE" \ |
| 116 | + -D "CMAKE_OSX_DEPLOYMENT_TARGET=$TARGET" \ |
| 117 | + -D "CMAKE_OSX_SYSROOT=$SYSROOT" \ |
| 118 | + -D "CMAKE_LIBRARY_OUTPUT_DIRECTORY=$SFMLLibDir" \ |
| 119 | + -D 'CMAKE_BUILD_WITH_INSTALL_RPATH=ON' \ |
| 120 | + -D 'CMAKE_INSTALL_RPATH=@executable_path' \ |
| 121 | + "$SFMLDir" |
| 122 | + |
| 123 | +cmake --build . --config Release |
| 124 | + |
| 125 | +popd # Pop SFML |
| 126 | + |
| 127 | +# =================== # |
| 128 | +# STEP 4: Build CSFML # |
| 129 | +# =================== # |
| 130 | + |
| 131 | +echo "Building CSFML using SFML at $SFMLBuiltDir" |
| 132 | +mkdir -p CSFML |
| 133 | +pushd CSFML |
| 134 | + |
| 135 | +mkdir -p lib |
| 136 | +CSFMLLibDir="$(realpath lib)" # The directory that contains the final CSFML libraries. Used to copy the result into SFML.Net |
| 137 | + |
| 138 | +cmake -E env \ |
| 139 | + cmake -G "Unix Makefiles" \ |
| 140 | + -D "SFML_DIR=$SFMLBuiltDir" \ |
| 141 | + -D 'BUILD_SHARED_LIBS=ON' \ |
| 142 | + -D 'CMAKE_BUILD_TYPE=Release' \ |
| 143 | + -D "CMAKE_OSX_ARCHITECTURES=$ARCHITECTURE" \ |
| 144 | + -D "CMAKE_OSX_DEPLOYMENT_TARGET=$TARGET" \ |
| 145 | + -D "CMAKE_OSX_SYSROOT=$SYSROOT" \ |
| 146 | + -D "CMAKE_LIBRARY_OUTPUT_DIRECTORY=$CSFMLLibDir" \ |
| 147 | + -D 'CMAKE_BUILD_WITH_INSTALL_RPATH=ON' \ |
| 148 | + -D 'CMAKE_INSTALL_RPATH=@executable_path' \ |
| 149 | + "$CSFMLDir" |
| 150 | +cmake --build . --config Release |
| 151 | + |
| 152 | +# ======================================== # |
| 153 | +# STEP 5: Copy result to the NuGet folders # |
| 154 | +# ======================================== # |
| 155 | + |
| 156 | +# Copies one SFML and CSFML module into the NuGet package |
| 157 | +# The module name must be passed to this function as an argument, in lowercase |
| 158 | +# This function then copies $SFMLLibDir/libsfml-(module).so and |
| 159 | +# $CSFMLLibDir/libcsfml-(module).so into $OutDir |
| 160 | +copymodule() |
| 161 | +{ |
| 162 | + MODULE="$1" |
| 163 | + |
| 164 | + mkdir -p "$OutDir" |
| 165 | + |
| 166 | + # Note the wildcard at the end of the first argument |
| 167 | + # We are copying every versioned file here, not just the .dylib |
| 168 | + # (libsfml-audio.dylib, libsfml-audio.2.dylib, libsfml-audio.2.5.dylib, etc) |
| 169 | + # This is needed because of the way macOS searches for libraries based |
| 170 | + # one their SONAME |
| 171 | + cp "$SFMLLibDir/libsfml-$MODULE."* "$OutDir" |
| 172 | + |
| 173 | + cp "$CSFMLLibDir/libcsfml-$MODULE."* "$OutDir" |
| 174 | +} |
| 175 | + |
| 176 | +copymodule audio |
| 177 | +copymodule graphics |
| 178 | +copymodule system |
| 179 | +copymodule window |
| 180 | + |
| 181 | +popd # Pop CSFML |
| 182 | +popd # Pop $RID |
| 183 | +popd # Pop Build |
0 commit comments