-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmakeZ3Generic.sh
More file actions
executable file
·357 lines (317 loc) · 9.36 KB
/
Copy pathmakeZ3Generic.sh
File metadata and controls
executable file
·357 lines (317 loc) · 9.36 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
#
# Creates a statically linked version of z3 from the repository using generic
# CFLAGS in order to allow for maximum compatibility with different
# architectures.
#
# The output binary of z3 will be put in the directory DEST_DIR, default is z3.
#
# For usage, see the -h flag.
#
# License: LGPLv3
#
# (C) 2017 Marius Greitschus, University of Freiburg
# (C) 2023 Daniel Dietsch, University of Freiburg
set -e
GIT_FETCH_URL="https://github.com/Z3Prover/z3.git"
# see https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels
COMMON_FLAGS="-O2 -march=x86-64 -mtune=generic -pipe"
USE_CMAKE=1
# finding available variables is tricky:
# - some can be displayed with cmake -LAH
# - some can be found in CMakeLists.txt
# - some are in README-CMake.md
CMAKE_FLAGS=(
-DCMAKE_BUILD_TYPE=Release
-DZ3_BUILD_EXECUTABLE:BOOL=ON
-DZ3_BUILD_LIBZ3_MSVC_STATIC=ON
-DZ3_BUILD_TEST_EXECUTABLES=OFF
-DZ3_ENABLE_EXAMPLE_TARGETS=OFF
-DZ3_LINK_TIME_OPTIMIZATION=ON
-DZ3_SINGLE_THREADED:BOOL=OFF
-DZ3_USE_LIB_GMP=OFF
-DBUILD_SHARED_LIBS=OFF
-DZ3_BUILD_LIBZ3_SHARED=OFF
-DCMAKE_EXE_LINKER_FLAGS='-static -Wl,--whole-archive -Wl,--no-whole-archive'
# -DCMAKE_STATIC_LINKER_FLAGS='-static -Wl,--whole-archive -Wl,--no-whole-archive'
)
MK_MAKE_FLAGS=(
--staticbin
--optimize
)
WORKING_DIR="z3temp"
DEFAULT_WORKING=true
BUILD_DIR="buildtemp"
DEST_DIR="z3"
DEFAULT_DEST=true
NUMCPUS=$(grep -c '^processor' /proc/cpuinfo)
NUMCPUS=$((NUMCPUS + 1))
NOUPDATE=false
DONTREMOVE=false
WINDOWS=false
ROOT="$(pwd)"
trap 'cd "${ROOT}"' EXIT
print_help()
{
echo "Usage: ${0} [options]"
echo
echo " Options:"
echo " -g|--git-dir z3-gitdir Directory of z3 Git-checkout."
echo " If no directory is specified, a new"
echo " checkout will be performed into a"
echo " temporary directory which will be"
echo " removed when done, unless the --no-remove-temp"
echo " option is specified."
echo
echo " -d|--dest-dir dest-dir Specifies the destination directory"
echo " in which the z3 binary will be placed."
echo " Default: ${DEST_DIR}"
echo
echo " -j|--jobs number Defines the number of parallel jobs"
echo " that should be used when compiling z3."
echo " Default: Number of CPUs + 1 on the current"
echo " system. Here: ${NUMCPUS}"
echo
echo " -n|--no-pull Do not update the repository to the newest"
echo " version. This only works with the -g flag."
echo
echo " --no-remove-temp Do not remove temporary files and directories"
echo " after z3 has been built."
echo
echo " --windows Cross-compile with mingw-w64 for Windows."
echo " For debian system, you need to"
echo " - apt-get install mingw-w64"
echo " - update-alternatives --config x86_64-w64-mingw32-g++"
echo " and select POSIX compliant threading"
echo
echo " -h | --help Print this help."
}
print_setup()
{
echo "Using the following setup:"
echo -e " Fetch-URL:\t\t\t${GIT_FETCH_URL}"
echo -en " git-dir:\t\t\t${WORKING_DIR}"
if [ ${DEFAULT_WORKING} = true ]; then
echo " (default)"
else
echo
fi
echo -en " dest-dir:\t\t\t${DEST_DIR}"
if [ ${DEFAULT_DEST} = true ]; then
echo " (default)"
else
echo
fi
echo -e " Additional parameters:\t${CMAKE_FLAGS[*]}"
echo -e " CFLAGS and CXXFLAGS:\t\t${COMMON_FLAGS}"
echo -e " Number of parallel jobs:\t${NUMCPUS}"
echo
}
checkout_z3()
{
if [ ${DEFAULT_WORKING} = false ]; then
return
fi
if [ -d "${WORKING_DIR}" ]; then
echo "Error: The directory for the Git checkout \"${WORKING_DIR}\" already exists."
echo " Please remove the directory first or use the -g option."
exit 1
fi
echo "Cloning z3 into ${WORKING_DIR} ..."
mkdir "${WORKING_DIR}"
cd "${WORKING_DIR}"
git clone "${GIT_FETCH_URL}" .
cd "${ROOT}"
}
update_z3()
{
if [ ${DEFAULT_WORKING} = true ]; then
return
fi
if [ ! -d "${WORKING_DIR}" ]; then
echo "Error: z3 git directory ${WORKING_DIR} does not exist."
exit 1
fi
cd "${WORKING_DIR}"
if [ ${NOUPDATE} = true ]; then
echo "Skipping update of the repository. Using current head: $(git rev-parse --short HEAD)"
cd "${ROOT}"
return
fi
echo "Updating z3 in ${WORKING_DIR} ..."
git pull
cd "${ROOT}"
}
version_z3()
{
cd "${WORKING_DIR}"
TAG=$(git describe --tags "$(git rev-list --tags --max-count=1)")
VERSION_HASH=$(git rev-parse --short HEAD)
BRANCH=$(git branch | grep \* | cut -d ' ' -f2-)
cd "${BUILD_DIR}"
Z3_VERSION="${TAG}-${BRANCH}-${VERSION_HASH}"
echo "z3 version is: ${Z3_VERSION}"
echo "${Z3_VERSION}" > "VERSION.z3"
cd "${ROOT}"
}
compile_z3()
{
echo "Building z3 ..."
cd "${WORKING_DIR}"
if [ -d "${BUILD_DIR}" ]; then
rm -rf "${BUILD_DIR}"
fi
export CFLAGS="${COMMON_FLAGS}"
export CXXFLAGS="${COMMON_FLAGS}"
export CC=gcc
export CXX=g++
if [ ${WINDOWS} = true ] ; then
export CXX=x86_64-w64-mingw32-g++
export CC=x86_64-w64-mingw32-gcc
export AR=x86_64-w64-mingw32-ar
fi
if [ $USE_CMAKE == 1 ] && [ ${WINDOWS} = false ] ; then
echo "Generating makefiles with cmake"
mkdir "${BUILD_DIR}"
cd "${BUILD_DIR}"
cmake -G "Unix Makefiles" ../ "${CMAKE_FLAGS[@]}"
else
# build static binary without respecting cflags
echo "Generating makefiles with mk_make"
python scripts/mk_make.py --build="${BUILD_DIR}" "${MK_MAKE_FLAGS[@]}"
cd "${BUILD_DIR}"
fi
echo "Compiling z3 ..."
make -j ${NUMCPUS} VERBOSE=1
strip -s z3
# check for dynamic vs static linking
if command -v readelf &> /dev/null ; then
readelf -d z3
elif command -v objdump &> /dev/null ; then
objdump -p z3
else
echo "No readelf or objdump available"
fi
# check which cpuflags are required
if command -v ~/.cargo/bin/elfx86exts &> /dev/null ; then
~/.cargo/bin/elfx86exts z3
else
echo "No elfx86exts available"
echo "Install it by visiting https://github.com/pkgw/elfx86exts"
fi
cd "${ROOT}"
echo "Build successful."
version_z3
}
deploy_z3()
{
echo "Copying created executable to ${DEST_DIR} ..."
if [ -d "${DEST_DIR}" ]; then
if [ -f "${DEST_DIR}/z3" ]; then
echo -n "Warning: z3 already exists in ${DEST_DIR}. Overwrite? [Y/n] "
read -r answer
if [ -n "${answer}" ]; then
case ${answer} in
y|Y|yes|Yes|YES)
;;
*)
echo "Aborting ..."
exit 1
;;
esac
fi
fi
else
mkdir "${DEST_DIR}"
fi
cp "${WORKING_DIR}/${BUILD_DIR}/z3" "${DEST_DIR}/"
if [ ! -f "${WORKING_DIR}/${BUILD_DIR}/VERSION.z3" ]; then
echo "Warning: z3 version information not available."
echo " Remove the directory ${WORKING_DIR}/${BUILD_DIR}"
echo " and run this script again if version information is required."
fi
cp "${WORKING_DIR}/${BUILD_DIR}/VERSION.z3" "${DEST_DIR}/"
}
cleanup()
{
if [ ${DONTREMOVE} = true ]; then
return
fi
cd "${WORKING_DIR}"
rm -r "${BUILD_DIR}"
cd "${ROOT}"
if [ ${DEFAULT_WORKING} = true ]; then
rm -rf "${WORKING_DIR}/.git"
rm -r "${WORKING_DIR}"
fi
}
# Handle arguments
while [[ $# -gt 0 ]]; do
arg="$1"
case $arg in
-g|--git-dir)
if [ -z "${2}" ]; then
echo "Error: No git source directory specified"
echo
print_help
exit 1
fi
WORKING_DIR="$2"
DEFAULT_WORKING=false
shift
shift
;;
-d|--dest-dir)
if [ -z "${2}" ]; then
echo "Error: No destination directory specified"
echo
print_help
exit 1
fi
DEST_DIR="$2"
DEFAULT_DEST=false
shift
shift
;;
-j|--jobs)
if [[ ! ${2} =~ ^[0-9]+$ ]]; then
echo "Error: Invalid number of jobs: ${2}"
echo
print_help
exit 1
fi
NUMCPUS="${2}"
shift
shift
;;
-n|--no-pull)
NOUPDATE=true
shift
;;
--no-remove-temp)
DONTREMOVE=true
shift
;;
--windows)
WINDOWS=true
shift
;;
-h|--help)
print_help
exit 0
;;
*)
echo "Unrecognized argument: $arg"
print_help
exit 1
;;
esac
done
echo "Preparing environment..."
print_setup
checkout_z3
update_z3
compile_z3
deploy_z3
cleanup
echo "All done."