-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.sh
executable file
·295 lines (226 loc) · 8.37 KB
/
compile.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env bash
set -e
# https://github.com/dotnet/samples/blob/main/core/nativeaot/NativeLibrary/README.md
PROJ=./NativeLibrary/libNativeLibrary.csproj
NUGET_PACKAGES=.nuget/packages
PLATFORM=$1
if [ "" == "${PLATFORM}" ]; then
echo "Error, no platform specified"
echo "Usage: ./compile_external.sh macos"
exit 1
fi
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PROJECT_DIR=$(dirname ${SCRIPT_DIR})
function run {
echo "RUN: $*"
$*
}
WINE=
DOTNET=dotnet
ARCH=arm64
CLANG_ARCH=arm64
DOTNET_RID=
if [ ${PLATFORM} == "macos" ]; then
if [ "i386" == "$(arch)" ]; then
ARCH=x64
CLANG_ARCH=x86_64
fi
DOTNET_RID=osx-${ARCH}
elif [ "${PLATFORM}" == "ios" ]; then
DOTNET_RID=osx-${ARCH}
elif [ "${PLATFORM}" == "android" ]; then
DOTNET_RID=linux-bionic-arm64
CLANG_ARCH=aarch64
DOTNET_SDK_VERSION="9.0.0-rc.2.24473.5"
elif [ "${PLATFORM}" == "win64" ]; then
ARCH=x64
DOTNET_RID=win-${ARCH}
CLANG_ARCH=x86_64
# WINE=wine
# DOTNET=${DOTNET_WIN}
# NUGET_PACKAGES=.nuget/win-packages
# lib.exe
export PATH=${PROJECT_DIR}/scripts/win64:${PATH}
#export PATH=/opt/local/dotnet-win:${PATH}
fi
BUILD_DIR=./build/${PLATFORM}
mkdir -p ${BUILD_DIR}
mkdir -p ${NUGET_PACKAGES}
export NUGET_PACKAGES=${SCRIPT_DIR}/${NUGET_PACKAGES}
echo "NUGET_PACKAGES=${NUGET_PACKAGES}"
echo "DOTNET=${DOTNET}"
# echo $(which ${WINE})
# echo $(ls -la ${DOTNET})
# echo $(${WINE} ${DOTNET})
echo DOTNET_CLI_HOME=${DOTNET_CLI_HOME}
export PATH=$PATH:$DOTNET_CLI_HOME
echo "PATH=${PATH}"
DOTNET_MAJOR_VERSION=$(${WINE} ${DOTNET} --version | awk -F'.' '{print $1}')
echo "DOTNET_MAJOR_VERSION=${DOTNET_MAJOR_VERSION}"
DOTNET_VERSION=$(${WINE} ${DOTNET} --version)
echo "DOTNET_VERSION=${DOTNET_VERSION}"
if [ "" == "${DOTNET_SDK_VERSION}" ]; then
DOTNET_SDK_VERSION=$(${WINE} ${DOTNET} --info | python -c "import sys; lns = sys.stdin.readlines(); i = lns.index('Host:\n'); print(lns[i+1].strip().split()[1])")
fi
echo "DOTNET_SDK_VERSION=${DOTNET_SDK_VERSION}"
echo "******************************************************************************************"
dotnet nuget locals all --list
echo "******************************************************************************************"
AOTBASE=${NUGET_PACKAGES}/microsoft.netcore.app.runtime.nativeaot.${DOTNET_RID}/${DOTNET_SDK_VERSION}/runtimes/${DOTNET_RID}/native
echo AOTBASE=${AOTBASE}
PUBLISH_DIR=./NativeLibrary/bin/Release/net${DOTNET_MAJOR_VERSION}.0/${DOTNET_RID}/native
echo PUBLISH_DIR=${PUBLISH_DIR}
rm -rf ./NativeLibrary/obj
rm -rf ./NativeLibrary/bin
rm -rf ./Sdk/obj
rm -rf ./Sdk/bin
#un ${WINE} ${DOTNET} clean -v diag ${PROJ}
echo "Cleaned"
run ${WINE} ${DOTNET} publish -v diag -c Release -r ${DOTNET_RID} /bl:aot.binlog ${PROJ}
set +e
#OPT=-O2
#OPT="-g -O0"
OPT="-g -O2 -flto"
DOSTRIP=true
MIN_OSX_VERSION=12.0
SUFFIX_EXE=
SUFFIX_OBJ=.o
SUFFIX_LIB=.a
PREFIX_LIB=lib
AOTLIB_SUFFIX=
FLAGS=
LINKER_FLAGS=
LIBPATHS="-L${BUILD_DIR}"
LIBS=""
INCLUDES="-I${PROJECT_DIR}/src/external"
DEFINES=""
EXTRA_CS_LIBS=
START_GROUP_STATIC=
END_GROUP_STATIC=
DEFOLDSDK=defoldsdk
CXX=clang++
CC=clang
AR=ar
if [ ${PLATFORM} == "macos" ]; then
FLAGS="-arch ${CLANG_ARCH} -target arm-apple-darwin19 -m64"
LINKER_FLAGS="-Wl,-rpath,@executable_path/ ${LINKER_FLAGS}"
LINKER_FLAGS="-framework Foundation ${LINKER_FLAGS}"
EXTRA_CS_LIBS="${AOTBASE}/${PREFIX_LIB}System.Native${SUFFIX_LIB} ${EXTRA_CS_LIBS}"
#EXTRA_CS_LIBS="${AOTBASE}/${PREFIX_LIB}Runtime.VxsortEnabled${SUFFIX_LIB} ${EXTRA_CS_LIBS}"
elif [ "${PLATFORM}" == "ios" ]; then
FLAGS="-arch ${CLANG_ARCH} -target arm-apple-darwin19 -m64 -miphoneos-version-min=11.0"
LINKER_FLAGS="-Wl,-rpath,@executable_path/ ${LINKER_FLAGS}"
LINKER_FLAGS="-framework Foundation ${LINKER_FLAGS}"
elif [ "${PLATFORM}" == "android" ]; then
ANDROID_HOME=/opt/platformsdk/android/
SDK=${ANDROID_HOME}/android-sdk-linux
NDK=${ANDROID_HOME}/android-ndk-r25b
echo "Using ANDROID_HOME=/opt/platformsdk/android"
echo "Using SDK=${SDK}"
echo "Using NDK=${NDK}"
tool_arch="darwin"
if [ "Linux" == "$(uname)" ]; then
tool_arch="linux"
fi
TARGET_ARCH="aarch64"
CXX="${NDK}/toolchains/llvm/prebuilt/${tool_arch}-x86_64/bin/aarch64-linux-android21-clang++"
CC="${NDK}/toolchains/llvm/prebuilt/${tool_arch}-x86_64/bin/aarch64-linux-android21-clang"
AR="${NDK}/toolchains/llvm/prebuilt/${tool_arch}-x86_64/bin/llvm-ar"
INCLUDES="-I${NDK}/toolchains/llvm/prebuilt/${tool_arch}-x86_64/sysroot/usr/include ${INCLUDES}"
FLAGS="-fPIC ${FLAGS}"
LINKER_FLAGS="-fPIC ${LINKER_FLAGS}"
#SUFFIX_LIB=".so"
START_GROUP_STATIC=-Wl,--start-group
END_GROUP_STATIC=-Wl,--end-group
LIBS="-llog ${LIBS}"
EXTRA_CS_LIBS="${AOTBASE}/${PREFIX_LIB}System.Native${SUFFIX_LIB} ${EXTRA_CS_LIBS}"
elif [ "${PLATFORM}" == "win64" ]; then
FLAGS="-m64 -target x86_64-pc-win32-msvc -fuse-ld=lld -Wl,-subsystem:console -Wl,/entry:mainCRTStartup -Wl,/safeseh:no"
DOSTRIP=
SUFFIX_EXE=.exe
SUFFIX_OBJ=.obj
SUFFIX_LIB=.lib
PREFIX_LIB=
AOTLIB_SUFFIX=.Aot
LIBPATHS="-L${WINDOWS_MSVC_DIR_2022}/lib/x64 ${LIBPATHS}"
LIBPATHS="-L${WINDOWS_MSVC_DIR_2022}/atlmfc/lib/x64 ${LIBPATHS}"
LIBPATHS="-L${WINDOWS_SDK_10_DIR}/Lib/${WINDOWS_SDK_10_20348_VERSION}/ucrt/x64 ${LIBPATHS}"
LIBPATHS="-L${WINDOWS_SDK_10_DIR}/Lib/${WINDOWS_SDK_10_20348_VERSION}/um/x64 ${LIBPATHS}"
LIBS="-lbcrypt ${LIBS}"
LIBS="-lOle32 ${LIBS}"
LIBS="-ladvapi32 ${LIBS}"
INCLUDES="-I${WINDOWS_MSVC_DIR_2022}/include ${INCLUDES}"
INCLUDES="-I${WINDOWS_SDK_10_DIR}/Include/${WINDOWS_SDK_10_20348_VERSION}/ucrt ${INCLUDES}"
INCLUDES="-I${WINDOWS_SDK_10_DIR}/Include/${WINDOWS_SDK_10_20348_VERSION}/um ${INCLUDES}"
INCLUDES="-I${WINDOWS_SDK_10_DIR}/Include/${WINDOWS_SDK_10_20348_VERSION}/shared ${INCLUDES}"
EXTRA_CS_LIBS="${AOTBASE}/${PREFIX_LIB}Runtime.VxsortEnabled${SUFFIX_LIB} ${EXTRA_CS_LIBS}"
fi
echo "Using CXX=${CXX}"
echo "Using CC=${CC}"
echo "Using AR=${AR}"
if [ -z "${CXX}" ]; then
echo "You must specify a CXX variable"
exit 1
fi
if [ -z "${CC}" ]; then
echo "You must specify a CC variable"
exit 1
fi
if [ -z "${AR}" ]; then
echo "You must specify a AR variable"
exit 1
fi
run ${CC} -c ${OPT} ${FLAGS} ${DEFINES} ${INCLUDES} -o ${BUILD_DIR}/clib${SUFFIX_OBJ} ./src/clib.c
run ${AR} rcs ${BUILD_DIR}/${PREFIX_LIB}clib${SUFFIX_LIB} ${BUILD_DIR}/clib${SUFFIX_OBJ}
cp -v ${PUBLISH_DIR}/libNativeLibrary${SUFFIX_LIB} ${BUILD_DIR}/${PREFIX_LIB}NativeLibrary${SUFFIX_LIB}
DEFOLD_LIBRARIES=
if [ "" != "${DEFOLDSDK}" ]; then
DEFOLD_LIBRARIES="-lextension -ldlib -lprofile_null"
if [ ${PLATFORM} == "macos" ]; then
LIBPATHS="-L${DEFOLDSDK}/lib/${CLANG_ARCH}-macos/ ${LIBPATHS}"
elif [ "${PLATFORM}" == "win64" ]; then
LIBPATHS="-L${DEFOLDSDK}/lib/x86_64-win32/ ${LIBPATHS}"
DEFOLD_LIBRARIES="-llibextension -llibdlib -llibprofile_null -lOle32 -lWS2_32 -liphlpapi -lAdvAPI32"
elif [ "${PLATFORM}" == "android" ]; then
LIBPATHS="-L${DEFOLDSDK}/lib/arm64-android/ ${LIBPATHS}"
DEFOLD_LIBRARIES="-lextension -ldlib -lprofile_null"
LINKER_FLAGS="-shared"
# SUFFIX_EXE=".so"
# SUFFIX_LIB=".a"
echo "LIBPATHS = $LIBPATHS"
fi
fi
# -flto seems to remove something vital and the exe crashes
# -force_load instead of --whole-archive and --no-whole-archive
OUTPUT=${BUILD_DIR}/test${SUFFIX_EXE}
echo "Linking..."
run ${CXX} ${OPT} ${FLAGS} ${LINKER_FLAGS} -o ${OUTPUT} \
${INCLUDES} \
${DEFINES} \
${LIBPATHS} \
${START_GROUP_STATIC} \
${DEFOLD_LIBRARIES} \
-lclib \
-llua51 \
-lNativeLibrary \
${LIBS} \
${EXTRA_CS_LIBS} \
${END_GROUP_STATIC} \
${AOTBASE}/${PREFIX_LIB}bootstrapperdll${SUFFIX_OBJ} \
${AOTBASE}/${PREFIX_LIB}Runtime.WorkstationGC${SUFFIX_LIB} \
${AOTBASE}/${PREFIX_LIB}eventpipe-enabled${SUFFIX_LIB} \
${AOTBASE}/${PREFIX_LIB}standalonegc-enabled${SUFFIX_LIB} \
${AOTBASE}/${PREFIX_LIB}System.IO.Compression.Native${AOTLIB_SUFFIX}${SUFFIX_LIB} \
${AOTBASE}/${PREFIX_LIB}System.Globalization.Native${AOTLIB_SUFFIX}${SUFFIX_LIB} \
src/main.cpp
echo "Done."
find . -iname "*NativeLibrary.dll" | xargs rm -v
find . -iname "dmsdk.dll" | xargs rm -v
if [ "${DOSTRIP}" != "" ]; then
echo strip ${OUTPUT}
strip ${OUTPUT}
fi
if [ "$(uname)" == "Darwin" ]; then
otool -L ${OUTPUT}
fi
ls -la ${OUTPUT}