| 1 |
############################################ |
| 2 |
# Copyright (c) 2012 Microsoft Corporation |
| 3 |
# |
| 4 |
# Scripts for automatically generating |
| 5 |
# Window distribution zip files. |
| 6 |
# |
| 7 |
# Author: Leonardo de Moura (leonardo) |
| 8 |
############################################ |
| 9 |
|
| 10 |
import os |
| 11 |
import subprocess |
| 12 |
import zipfile |
| 13 |
import re |
| 14 |
import getopt |
| 15 |
import sys |
| 16 |
import shutil |
| 17 |
from mk_exception import * |
| 18 |
from fnmatch import fnmatch |
| 19 |
|
| 20 |
def getenv(name, default): |
| 21 |
try: |
| 22 |
return os.environ[name].strip(' "\'') |
| 23 |
except: |
| 24 |
return default |
| 25 |
|
| 26 |
BUILD_DIR = 'build-dist' |
| 27 |
DIST_DIR = 'dist' |
| 28 |
BUILD_X64_DIR = os.path.join(BUILD_DIR, 'x64') |
| 29 |
BUILD_X86_DIR = os.path.join(BUILD_DIR, 'x86') |
| 30 |
BUILD_ARM64_DIR = os.path.join(BUILD_DIR, 'arm64') |
| 31 |
VERBOSE = True |
| 32 |
FORCE_MK = False |
| 33 |
ASSEMBLY_VERSION = None |
| 34 |
DOTNET_CORE_ENABLED = True |
| 35 |
DOTNET_KEY_FILE = None |
| 36 |
JAVA_ENABLED = True |
| 37 |
ZIP_BUILD_OUTPUTS = False |
| 38 |
GIT_HASH = False |
| 39 |
PYTHON_ENABLED = True |
| 40 |
X86ONLY = False |
| 41 |
X64ONLY = False |
| 42 |
ARM64ONLY = False |
| 43 |
ARCHITECTURES = [] |
| 44 |
|
| 45 |
def set_verbose(flag): |
| 46 |
global VERBOSE |
| 47 |
VERBOSE = flag |
| 48 |
|
| 49 |
def is_verbose(): |
| 50 |
return VERBOSE |
| 51 |
|
| 52 |
def mk_dir(d): |
| 53 |
if not os.path.exists(d): |
| 54 |
if is_verbose(): |
| 55 |
print("Make directory", d) |
| 56 |
os.makedirs(d) |
| 57 |
|
| 58 |
def get_z3_name(arch): |
| 59 |
version = "4" |
| 60 |
if ASSEMBLY_VERSION: |
| 61 |
version = ASSEMBLY_VERSION |
| 62 |
print("Assembly version:", version) |
| 63 |
if GIT_HASH: |
| 64 |
return 'z3-%s.%s-%s-win' % (version, get_git_hash(), arch) |
| 65 |
else: |
| 66 |
return 'z3-%s-%s-win' % (version, arch) |
| 67 |
|
| 68 |
def get_build_dir(arch): |
| 69 |
return ARCHITECTURES[arch] |
| 70 |
|
| 71 |
def get_build_dist(arch): |
| 72 |
return os.path.join(get_build_dir(arch), DIST_DIR) |
| 73 |
|
| 74 |
def get_build_dist_path(arch): |
| 75 |
return os.path.join(get_build_dir(arch), DIST_DIR, get_z3_name(arch)) |
| 76 |
|
| 77 |
def get_bin_dist_path(arch): |
| 78 |
return os.path.join(get_build_dist_path(arch), "bin") |
| 79 |
|
| 80 |
def get_lib_dist_path(arch): |
| 81 |
return os.path.join(get_build_dist_path(arch), "lib") |
| 82 |
|
| 83 |
def get_java_dist_path(arch): |
| 84 |
return os.path.join(get_build_dist_path(arch), "java") |
| 85 |
|
| 86 |
def get_dist_path(arch): |
| 87 |
return os.path.join(DIST_DIR, arch) |
| 88 |
|
| 89 |
def set_build_dir(path): |
| 90 |
global BUILD_DIR, BUILD_X86_DIR, BUILD_X64_DIR, BUILD_ARM64_DIR, ARCHITECTURES |
| 91 |
BUILD_DIR = os.path.expanduser(os.path.normpath(path)) |
| 92 |
BUILD_X86_DIR = os.path.join(path, 'x86') |
| 93 |
BUILD_X64_DIR = os.path.join(path, 'x64') |
| 94 |
BUILD_ARM64_DIR = os.path.join(path, 'arm64') # Set ARM64 build directory |
| 95 |
ARCHITECTURES = {'x64': BUILD_X64_DIR, 'x86':BUILD_X86_DIR, 'arm64':BUILD_ARM64_DIR} |
| 96 |
|
| 97 |
def display_help(): |
| 98 |
print("mk_win_dist.py: Z3 Windows distribution generator\n") |
| 99 |
print("This script generates the zip files containing executables, dlls, header files for Windows.") |
| 100 |
print("It must be executed from the Z3 root directory.") |
| 101 |
print("\nOptions:") |
| 102 |
print(" -h, --help display this message.") |
| 103 |
print(" -s, --silent do not print verbose messages.") |
| 104 |
print(" -b <sudir>, --build=<subdir> subdirectory where x86 and x64 Z3 versions will be built (default: build-dist).") |
| 105 |
print(" -f, --force force script to regenerate Makefiles.") |
| 106 |
print(" --version=<version> release version.") |
| 107 |
print(" --assembly-version assembly version for dll") |
| 108 |
print(" --nodotnet do not include .NET bindings in the binary distribution files.") |
| 109 |
print(" --dotnet-key=<file> strongname sign the .NET assembly with the private key in <file>.") |
| 110 |
print(" --nojava do not include Java bindings in the binary distribution files.") |
| 111 |
print(" --nopython do not include Python bindings in the binary distribution files.") |
| 112 |
print(" --zip package build outputs in zip file.") |
| 113 |
print(" --githash include git hash in the Zip file.") |
| 114 |
print(" --x86-only x86 dist only.") |
| 115 |
print(" --x64-only x64 dist only.") |
| 116 |
print(" --arm64-only arm64 dist only.") |
| 117 |
exit(0) |
| 118 |
|
| 119 |
# Parse configuration option for mk_make script |
| 120 |
def parse_options(): |
| 121 |
global FORCE_MK, JAVA_ENABLED, ZIP_BUILD_OUTPUTS, GIT_HASH, DOTNET_CORE_ENABLED, DOTNET_KEY_FILE, ASSEMBLY_VERSION, PYTHON_ENABLED, X86ONLY, X64ONLY, ARM64ONLY |
| 122 |
path = BUILD_DIR |
| 123 |
options, remainder = getopt.gnu_getopt(sys.argv[1:], 'b:hsf', ['build=', |
| 124 |
'help', |
| 125 |
'silent', |
| 126 |
'force', |
| 127 |
'nojava', |
| 128 |
'nodotnet', |
| 129 |
'dotnet-key=', |
| 130 |
'assembly-version=', |
| 131 |
'zip', |
| 132 |
'githash', |
| 133 |
'nopython', |
| 134 |
'x86-only', |
| 135 |
'x64-only', |
| 136 |
'arm64-only' |
| 137 |
]) |
| 138 |
for opt, arg in options: |
| 139 |
if opt in ('-b', '--build'): |
| 140 |
if arg == 'src': |
| 141 |
raise MKException('The src directory should not be used to host the Makefile') |
| 142 |
path = arg |
| 143 |
elif opt in ('-s', '--silent'): |
| 144 |
set_verbose(False) |
| 145 |
elif opt in ('-h', '--help'): |
| 146 |
display_help() |
| 147 |
elif opt in ('-f', '--force'): |
| 148 |
FORCE_MK = True |
| 149 |
elif opt == '--nodotnet': |
| 150 |
DOTNET_CORE_ENABLED = False |
| 151 |
elif opt == '--assembly-version': |
| 152 |
ASSEMBLY_VERSION = arg |
| 153 |
elif opt == '--nopython': |
| 154 |
PYTHON_ENABLED = False |
| 155 |
elif opt == '--dotnet-key': |
| 156 |
DOTNET_KEY_FILE = arg |
| 157 |
elif opt == '--nojava': |
| 158 |
JAVA_ENABLED = False |
| 159 |
elif opt == '--zip': |
| 160 |
ZIP_BUILD_OUTPUTS = True |
| 161 |
elif opt == '--githash': |
| 162 |
GIT_HASH = True |
| 163 |
elif opt == '--x86-only' and not X64ONLY: |
| 164 |
X86ONLY = True |
| 165 |
elif opt == '--arm64-only' and not X86ONLY and not X64ONLY: |
| 166 |
ARM64ONLY = True |
| 167 |
elif opt == '--x64-only' and not X86ONLY: |
| 168 |
X64ONLY = True |
| 169 |
else: |
| 170 |
raise MKException("Invalid command line option '%s'" % opt) |
| 171 |
set_build_dir(path) |
| 172 |
|
| 173 |
# Check whether build directory already exists or not |
| 174 |
def check_build_dir(path): |
| 175 |
return os.path.exists(path) and os.path.exists(os.path.join(path, 'Makefile')) |
| 176 |
|
| 177 |
def check_output(cmd): |
| 178 |
out = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 179 |
if out != None: |
| 180 |
enc = sys.getdefaultencoding() |
| 181 |
if enc != None: return out.decode(enc).rstrip('\r\n') |
| 182 |
else: return out.rstrip('\r\n') |
| 183 |
else: |
| 184 |
return "" |
| 185 |
|
| 186 |
def get_git_hash(): |
| 187 |
try: |
| 188 |
branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) |
| 189 |
r = check_output(['git', 'show-ref', '--abbrev=12', 'refs/heads/%s' % branch]) |
| 190 |
except: |
| 191 |
raise MKException("Failed to retrieve git hash") |
| 192 |
ls = r.split(' ') |
| 193 |
if len(ls) != 2: |
| 194 |
raise MKException("Unexpected git output " + r) |
| 195 |
return ls[0] |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
# Create a build directory using mk_make.py |
| 200 |
def mk_build_dir(arch): |
| 201 |
build_path = get_build_dir(arch) |
| 202 |
if not check_build_dir(build_path) or FORCE_MK: |
| 203 |
mk_dir(build_path) |
| 204 |
vsarch = arch |
| 205 |
if arch == "arm64": |
| 206 |
vsarch = "amd64_arm64" |
| 207 |
|
| 208 |
cmds = [] |
| 209 |
cmds.append(f"cd {build_path}") |
| 210 |
cmds.append('call "%VCINSTALLDIR%Auxiliary\\build\\vcvarsall.bat" ' + vsarch) |
| 211 |
cmd = [] |
| 212 |
cmd.append("cmake -S .") |
| 213 |
if DOTNET_CORE_ENABLED: |
| 214 |
cmd.append(' -DZ3_BUILD_DOTNET_BINDINGS=ON') |
| 215 |
# cmd.append(' -DZ3_INSTALL_DOTNET_BINDINGS=ON') |
| 216 |
if JAVA_ENABLED: |
| 217 |
cmd.append(' -DZ3_BUILD_JAVA_BINDINGS=ON') |
| 218 |
cmd.append(' -DZ3_INSTALL_JAVA_BINDINGS=ON') |
| 219 |
cmd.append(' -DZ3_JAVA_JAR_INSTALLDIR=java') |
| 220 |
cmd.append(' -DZ3_JAVA_JNI_LIB_INSTALLDIR=bin/java') |
| 221 |
if PYTHON_ENABLED: |
| 222 |
cmd.append(' -DZ3_BUILD_PYTHON_BINDINGS=ON') |
| 223 |
cmd.append(' -DZ3_INSTALL_PYTHON_BINDINGS=ON') |
| 224 |
cmd.append(' -DCMAKE_INSTALL_PYTHON_PKG_DIR=bin/python') |
| 225 |
|
| 226 |
if GIT_HASH: |
| 227 |
git_hash = get_git_hash() |
| 228 |
cmd.append(' -DGIT_HASH=' + git_hash) |
| 229 |
cmd.append(' -DZ3_USE_LIB_GMP=OFF') |
| 230 |
cmd.append(' -DZ3_BUILD_LIBZ3_SHARED=ON') |
| 231 |
cmd.append(' -DCMAKE_BUILD_TYPE=RelWithDebInfo') |
| 232 |
cmd.append(' -DCMAKE_INSTALL_PREFIX=' + os.path.join(DIST_DIR, get_z3_name(arch))) |
| 233 |
cmd.append(' -G "Ninja"') |
| 234 |
cmd.append(' ../..\n') |
| 235 |
cmds.append("".join(cmd)) |
| 236 |
print("CMAKE commands:", cmds) |
| 237 |
sys.stdout.flush() |
| 238 |
if exec_cmds(cmds) != 0: |
| 239 |
raise MKException("failed to run commands") |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
# Check if on Visual Studio command prompt |
| 244 |
def check_vc_cmd_prompt(): |
| 245 |
try: |
| 246 |
DEVNULL = open(os.devnull, 'wb') |
| 247 |
subprocess.call(['cl'], stdout=DEVNULL, stderr=DEVNULL) |
| 248 |
except: |
| 249 |
raise MKException("You must execute the mk_win_dist.py script on a Visual Studio Command Prompt") |
| 250 |
|
| 251 |
def exec_cmds(cmds): |
| 252 |
cmd_file = 'z3_tmp.cmd' |
| 253 |
f = open(cmd_file, 'w') |
| 254 |
for cmd in cmds: |
| 255 |
f.write(cmd) |
| 256 |
f.write('\n') |
| 257 |
f.close() |
| 258 |
res = 0 |
| 259 |
try: |
| 260 |
res = subprocess.call(cmd_file, shell=True) |
| 261 |
except: |
| 262 |
res = 1 |
| 263 |
try: |
| 264 |
os.erase(cmd_file) |
| 265 |
except: |
| 266 |
pass |
| 267 |
return res |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
def build_z3(arch): |
| 272 |
if is_verbose(): |
| 273 |
print("build z3") |
| 274 |
build_dir = get_build_dir(arch) |
| 275 |
if arch == "arm64": |
| 276 |
arch = "amd64_arm64" |
| 277 |
cmds = [] |
| 278 |
cmds.append('call "%VCINSTALLDIR%Auxiliary\\build\\vcvarsall.bat" ' + arch) |
| 279 |
cmds.append('cd %s' % build_dir) |
| 280 |
cmds.append('ninja install') |
| 281 |
if exec_cmds(cmds) != 0: |
| 282 |
raise MKException("Failed to make z3") |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
def mk_zip(arch): |
| 287 |
if not ZIP_BUILD_OUTPUTS: |
| 288 |
return |
| 289 |
build_dist = get_build_dist_path(arch) |
| 290 |
dist_name = get_z3_name(arch) |
| 291 |
dist_path = get_dist_path(arch) |
| 292 |
build_dir = get_build_dir(arch) |
| 293 |
old = os.getcwd() |
| 294 |
try: |
| 295 |
if is_verbose(): |
| 296 |
print("dist path", dist_path) |
| 297 |
mk_dir(dist_path) |
| 298 |
zfname = os.path.join(dist_path, '%s.zip' % dist_name) |
| 299 |
zipout = zipfile.ZipFile(zfname, 'w', zipfile.ZIP_DEFLATED) |
| 300 |
os.chdir(get_build_dist(arch)) |
| 301 |
for root, dirs, files in os.walk("."): |
| 302 |
for f in files: |
| 303 |
if is_verbose(): |
| 304 |
print("adding ", os.path.join(root, f)) |
| 305 |
zipout.write(os.path.join(root, f)) |
| 306 |
if is_verbose(): |
| 307 |
print("Generated '%s'" % zfname) |
| 308 |
except: |
| 309 |
pass |
| 310 |
os.chdir(old) |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
VS_RUNTIME_PATS = [re.compile(r'vcomp.*\.dll'), |
| 315 |
re.compile(r'msvcp.*\.dll'), |
| 316 |
re.compile(r'msvcr.*\.dll'), |
| 317 |
re.compile(r'vcrun.*\.dll')] |
| 318 |
|
| 319 |
# Copy Visual Studio Runtime libraries |
| 320 |
def cp_vs_runtime(arch): |
| 321 |
platform = arch |
| 322 |
vcdir = os.environ['VCINSTALLDIR'] |
| 323 |
path = '%sredist' % vcdir |
| 324 |
vs_runtime_files = [] |
| 325 |
print("Walking %s" % path) |
| 326 |
# Everything changes with every release of VS |
| 327 |
# Prior versions of VS had DLLs under "redist\x64" |
| 328 |
# There are now several variants of redistributables |
| 329 |
# The naming convention defies my understanding so |
| 330 |
# we use a "check_root" filter to find some hopefully suitable |
| 331 |
# redistributable. |
| 332 |
def check_root(root): |
| 333 |
return platform in root and ("CRT" in root or "MP" in root) and "onecore" not in root and "debug" not in root |
| 334 |
for root, dirs, files in os.walk(path): |
| 335 |
for filename in files: |
| 336 |
if fnmatch(filename, '*.dll') and check_root(root): |
| 337 |
print("Checking %s %s" % (root, filename)) |
| 338 |
for pat in VS_RUNTIME_PATS: |
| 339 |
if pat.match(filename): |
| 340 |
fname = os.path.join(root, filename) |
| 341 |
if not os.path.isdir(fname): |
| 342 |
vs_runtime_files.append(fname) |
| 343 |
if not vs_runtime_files: |
| 344 |
raise MKException("Did not find any runtime files to include") |
| 345 |
bin_dist_path = get_bin_dist_path(arch) |
| 346 |
for f in vs_runtime_files: |
| 347 |
shutil.copy(f, bin_dist_path) |
| 348 |
if is_verbose(): |
| 349 |
print("Copied '%s' to '%s'" % (f, bin_dist_path)) |
| 350 |
|
| 351 |
def cp_license(arch): |
| 352 |
if is_verbose(): |
| 353 |
print("copy licence") |
| 354 |
path = get_build_dist_path(arch) |
| 355 |
mk_dir(path) |
| 356 |
shutil.copy("LICENSE.txt", path) |
| 357 |
|
| 358 |
def cp_dotnet(arch): |
| 359 |
if not DOTNET_CORE_ENABLED: |
| 360 |
return |
| 361 |
if is_verbose(): |
| 362 |
print("copy dotnet") |
| 363 |
build_dir = get_build_dir(arch) |
| 364 |
dist_dir = get_bin_dist_path(arch) |
| 365 |
shutil.copytree(os.path.join(build_dir, "Microsoft.Z3"), |
| 366 |
dist_dir, |
| 367 |
dirs_exist_ok=True) |
| 368 |
|
| 369 |
def cp_into_bin(arch): |
| 370 |
if is_verbose(): |
| 371 |
print("copy lib") |
| 372 |
lib_dir = get_lib_dist_path(arch) |
| 373 |
bin_dir = get_bin_dist_path(arch) |
| 374 |
shutil.copyfile(os.path.join(lib_dir, "libz3.lib"), |
| 375 |
os.path.join(bin_dir, "libz3.lib")) |
| 376 |
shutil.rmtree(lib_dir) |
| 377 |
if JAVA_ENABLED: |
| 378 |
java_dir = os.path.join(bin_dir, "java") |
| 379 |
for file in os.listdir(java_dir): |
| 380 |
src_path = os.path.join(java_dir, file) |
| 381 |
dst_path = os.path.join(bin_dir, file) |
| 382 |
shutil.copy2(src_path, dst_path) |
| 383 |
shutil.rmtree(java_dir) |
| 384 |
|
| 385 |
def cp_pdb(arch): |
| 386 |
if is_verbose(): |
| 387 |
print("copy pdb") |
| 388 |
build_dir = get_build_dir(arch) |
| 389 |
bin_path = get_bin_dist_path(arch) |
| 390 |
mk_dir(bin_path) |
| 391 |
for f in os.listdir(build_dir): |
| 392 |
if f.endswith("libz3.pdb"): |
| 393 |
shutil.copy(os.path.join(build_dir, f), bin_path) |
| 394 |
|
| 395 |
def build_for_arch(arch): |
| 396 |
mk_build_dir(arch) |
| 397 |
build_z3(arch) |
| 398 |
cp_license(arch) |
| 399 |
cp_pdb(arch) |
| 400 |
cp_dotnet(arch) |
| 401 |
cp_vs_runtime(arch) |
| 402 |
cp_into_bin(arch) |
| 403 |
mk_zip(arch) |
| 404 |
|
| 405 |
# Entry point |
| 406 |
def main(): |
| 407 |
if os.name != 'nt': |
| 408 |
raise MKException("This script is for Windows only") |
| 409 |
|
| 410 |
parse_options() |
| 411 |
check_vc_cmd_prompt() |
| 412 |
|
| 413 |
if X86ONLY: |
| 414 |
build_for_arch("x86") |
| 415 |
elif X64ONLY: |
| 416 |
build_for_arch("x64") |
| 417 |
elif ARM64ONLY: |
| 418 |
build_for_arch("arm64") |
| 419 |
else: |
| 420 |
for arch in ARCHITECTURES: |
| 421 |
build_for_arch(arch) |
| 422 |
|
| 423 |
main() |
| 424 |
|