| 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 |
import os |
| 10 |
import glob |
| 11 |
import re |
| 12 |
import getopt |
| 13 |
import sys |
| 14 |
import shutil |
| 15 |
import subprocess |
| 16 |
import zipfile |
| 17 |
from mk_exception import * |
| 18 |
from mk_project import * |
| 19 |
import mk_util |
| 20 |
|
| 21 |
BUILD_DIR='build-dist' |
| 22 |
BUILD_X64_DIR=os.path.join('build-dist', 'x64') |
| 23 |
BUILD_X86_DIR=os.path.join('build-dist', 'x86') |
| 24 |
VERBOSE=True |
| 25 |
DIST_DIR='dist' |
| 26 |
FORCE_MK=False |
| 27 |
ASSEMBLY_VERSION=None |
| 28 |
DOTNET_CORE_ENABLED=True |
| 29 |
DOTNET_KEY_FILE=None |
| 30 |
JAVA_ENABLED=True |
| 31 |
ZIP_BUILD_OUTPUTS=False |
| 32 |
GIT_HASH=False |
| 33 |
PYTHON_ENABLED=True |
| 34 |
X86ONLY=False |
| 35 |
X64ONLY=False |
| 36 |
MAKEJOBS=getenv("MAKEJOBS", "24") |
| 37 |
|
| 38 |
def set_verbose(flag): |
| 39 |
global VERBOSE |
| 40 |
VERBOSE = flag |
| 41 |
|
| 42 |
def is_verbose(): |
| 43 |
return VERBOSE |
| 44 |
|
| 45 |
def mk_dir(d): |
| 46 |
if not os.path.exists(d): |
| 47 |
os.makedirs(d) |
| 48 |
|
| 49 |
def set_build_dir(path): |
| 50 |
global BUILD_DIR, BUILD_X86_DIR, BUILD_X64_DIR |
| 51 |
BUILD_DIR = mk_util.norm_path(path) |
| 52 |
BUILD_X86_DIR = os.path.join(path, 'x86') |
| 53 |
BUILD_X64_DIR = os.path.join(path, 'x64') |
| 54 |
mk_dir(BUILD_X86_DIR) |
| 55 |
mk_dir(BUILD_X64_DIR) |
| 56 |
|
| 57 |
def display_help(): |
| 58 |
print("mk_win_dist.py: Z3 Windows distribution generator\n") |
| 59 |
print("This script generates the zip files containing executables, dlls, header files for Windows.") |
| 60 |
print("It must be executed from the Z3 root directory.") |
| 61 |
print("\nOptions:") |
| 62 |
print(" -h, --help display this message.") |
| 63 |
print(" -s, --silent do not print verbose messages.") |
| 64 |
print(" -b <sudir>, --build=<subdir> subdirectory where x86 and x64 Z3 versions will be built (default: build-dist).") |
| 65 |
print(" -f, --force force script to regenerate Makefiles.") |
| 66 |
print(" --assembly-version assembly version for dll") |
| 67 |
print(" --nodotnet do not include .NET bindings in the binary distribution files.") |
| 68 |
print(" --dotnet-key=<file> strongname sign the .NET assembly with the private key in <file>.") |
| 69 |
print(" --nojava do not include Java bindings in the binary distribution files.") |
| 70 |
print(" --nopython do not include Python bindings in the binary distribution files.") |
| 71 |
print(" --zip package build outputs in zip file.") |
| 72 |
print(" --githash include git hash in the Zip file.") |
| 73 |
print(" --x86-only x86 dist only.") |
| 74 |
print(" --x64-only x64 dist only.") |
| 75 |
exit(0) |
| 76 |
|
| 77 |
# Parse configuration option for mk_make script |
| 78 |
def parse_options(): |
| 79 |
global FORCE_MK, JAVA_ENABLED, ZIP_BUILD_OUTPUTS, GIT_HASH, DOTNET_CORE_ENABLED, DOTNET_KEY_FILE, ASSEMBLY_VERSION, PYTHON_ENABLED, X86ONLY, X64ONLY |
| 80 |
path = BUILD_DIR |
| 81 |
options, remainder = getopt.gnu_getopt(sys.argv[1:], 'b:hsf', ['build=', |
| 82 |
'help', |
| 83 |
'silent', |
| 84 |
'force', |
| 85 |
'nojava', |
| 86 |
'nodotnet', |
| 87 |
'dotnet-key=', |
| 88 |
'assembly-version=', |
| 89 |
'zip', |
| 90 |
'githash', |
| 91 |
'nopython', |
| 92 |
'x86-only', |
| 93 |
'x64-only' |
| 94 |
]) |
| 95 |
for opt, arg in options: |
| 96 |
if opt in ('-b', '--build'): |
| 97 |
if arg == 'src': |
| 98 |
raise MKException('The src directory should not be used to host the Makefile') |
| 99 |
path = arg |
| 100 |
elif opt in ('-s', '--silent'): |
| 101 |
set_verbose(False) |
| 102 |
elif opt in ('-h', '--help'): |
| 103 |
display_help() |
| 104 |
elif opt in ('-f', '--force'): |
| 105 |
FORCE_MK = True |
| 106 |
elif opt == '--nodotnet': |
| 107 |
DOTNET_CORE_ENABLED = False |
| 108 |
elif opt == '--assembly-version': |
| 109 |
ASSEMBLY_VERSION = arg |
| 110 |
elif opt == '--nopython': |
| 111 |
PYTHON_ENABLED = False |
| 112 |
elif opt == '--dotnet-key': |
| 113 |
DOTNET_KEY_FILE = arg |
| 114 |
elif opt == '--nojava': |
| 115 |
JAVA_ENABLED = False |
| 116 |
elif opt == '--zip': |
| 117 |
ZIP_BUILD_OUTPUTS = True |
| 118 |
elif opt == '--githash': |
| 119 |
GIT_HASH = True |
| 120 |
elif opt == '--x86-only' and not X64ONLY: |
| 121 |
X86ONLY = True |
| 122 |
elif opt == '--x64-only' and not X86ONLY: |
| 123 |
X64ONLY = True |
| 124 |
else: |
| 125 |
raise MKException("Invalid command line option '%s'" % opt) |
| 126 |
set_build_dir(path) |
| 127 |
|
| 128 |
# Check whether build directory already exists or not |
| 129 |
def check_build_dir(path): |
| 130 |
return os.path.exists(path) and os.path.exists(os.path.join(path, 'Makefile')) |
| 131 |
|
| 132 |
# Create a build directory using mk_make.py |
| 133 |
def mk_build_dir(path, x64): |
| 134 |
if not check_build_dir(path) or FORCE_MK: |
| 135 |
parallel = '--parallel=' + MAKEJOBS |
| 136 |
opts = ["python", os.path.join('scripts', 'mk_make.py'), parallel, "-b", path] |
| 137 |
if DOTNET_CORE_ENABLED: |
| 138 |
opts.append('--dotnet') |
| 139 |
if DOTNET_KEY_FILE is not None: |
| 140 |
opts.append('--dotnet-key=' + DOTNET_KEY_FILE) |
| 141 |
if ASSEMBLY_VERSION is not None: |
| 142 |
opts.append('--assembly-version=' + ASSEMBLY_VERSION) |
| 143 |
if JAVA_ENABLED: |
| 144 |
opts.append('--java') |
| 145 |
if x64: |
| 146 |
opts.append('-x') |
| 147 |
if GIT_HASH: |
| 148 |
opts.append('--githash=%s' % mk_util.git_hash()) |
| 149 |
opts.append('--git-describe') |
| 150 |
if PYTHON_ENABLED: |
| 151 |
opts.append('--python') |
| 152 |
opts.append('--guardcf') |
| 153 |
if subprocess.call(opts) != 0: |
| 154 |
raise MKException("Failed to generate build directory at '%s'" % path) |
| 155 |
|
| 156 |
# Create build directories |
| 157 |
def mk_build_dirs(): |
| 158 |
mk_build_dir(BUILD_X86_DIR, False) |
| 159 |
mk_build_dir(BUILD_X64_DIR, True) |
| 160 |
|
| 161 |
# Check if on Visual Studio command prompt |
| 162 |
def check_vc_cmd_prompt(): |
| 163 |
try: |
| 164 |
DEVNULL = open(os.devnull, 'wb') |
| 165 |
subprocess.call(['cl'], stdout=DEVNULL, stderr=DEVNULL) |
| 166 |
except: |
| 167 |
raise MKException("You must execute the mk_win_dist.py script on a Visual Studio Command Prompt") |
| 168 |
|
| 169 |
def exec_cmds(cmds): |
| 170 |
cmd_file = 'z3_tmp.cmd' |
| 171 |
f = open(cmd_file, 'w') |
| 172 |
for cmd in cmds: |
| 173 |
f.write(cmd) |
| 174 |
f.write('\n') |
| 175 |
f.close() |
| 176 |
res = 0 |
| 177 |
try: |
| 178 |
res = subprocess.call(cmd_file, shell=True) |
| 179 |
except: |
| 180 |
res = 1 |
| 181 |
try: |
| 182 |
os.erase(cmd_file) |
| 183 |
except: |
| 184 |
pass |
| 185 |
return res |
| 186 |
|
| 187 |
# Compile Z3 (if x64 == True, then it builds it in x64 mode). |
| 188 |
def mk_z3(x64): |
| 189 |
cmds = [] |
| 190 |
if x64: |
| 191 |
cmds.append('call "%VCINSTALLDIR%Auxiliary\\build\\vcvarsall.bat" amd64') |
| 192 |
cmds.append('cd %s' % BUILD_X64_DIR) |
| 193 |
else: |
| 194 |
cmds.append('call "%VCINSTALLDIR%Auxiliary\\build\\vcvarsall.bat" x86') |
| 195 |
cmds.append('cd %s' % BUILD_X86_DIR) |
| 196 |
cmds.append('nmake') |
| 197 |
if exec_cmds(cmds) != 0: |
| 198 |
raise MKException("Failed to make z3, x64: %s" % x64) |
| 199 |
|
| 200 |
def mk_z3s(): |
| 201 |
mk_z3(False) |
| 202 |
mk_z3(True) |
| 203 |
|
| 204 |
def get_z3_name(x64): |
| 205 |
major, minor, build, revision = get_version() |
| 206 |
print("Assembly version:", major, minor, build, revision) |
| 207 |
if x64: |
| 208 |
platform = "x64" |
| 209 |
else: |
| 210 |
platform = "x86" |
| 211 |
if GIT_HASH: |
| 212 |
return 'z3-%s.%s.%s.%s-%s-win' % (major, minor, build, mk_util.git_hash(), platform) |
| 213 |
else: |
| 214 |
return 'z3-%s.%s.%s-%s-win' % (major, minor, build, platform) |
| 215 |
|
| 216 |
def mk_dist_dir(x64): |
| 217 |
if x64: |
| 218 |
platform = "x64" |
| 219 |
build_path = BUILD_X64_DIR |
| 220 |
else: |
| 221 |
platform = "x86" |
| 222 |
build_path = BUILD_X86_DIR |
| 223 |
dist_path = os.path.join(DIST_DIR, get_z3_name(x64)) |
| 224 |
mk_dir(dist_path) |
| 225 |
mk_win_dist(build_path, dist_path) |
| 226 |
if is_verbose(): |
| 227 |
print(f"Generated {platform} distribution folder at '{dist_path}'") |
| 228 |
|
| 229 |
def mk_dist_dirs(): |
| 230 |
mk_dist_dir(False) |
| 231 |
mk_dist_dir(True) |
| 232 |
|
| 233 |
def get_dist_path(x64): |
| 234 |
return get_z3_name(x64) |
| 235 |
|
| 236 |
def mk_zip(x64): |
| 237 |
dist_path = get_dist_path(x64) |
| 238 |
old = os.getcwd() |
| 239 |
try: |
| 240 |
os.chdir(DIST_DIR) |
| 241 |
zfname = '%s.zip' % dist_path |
| 242 |
zipout = zipfile.ZipFile(zfname, 'w', zipfile.ZIP_DEFLATED) |
| 243 |
for root, dirs, files in os.walk(dist_path): |
| 244 |
for f in files: |
| 245 |
zipout.write(os.path.join(root, f)) |
| 246 |
if is_verbose(): |
| 247 |
print("Generated '%s'" % zfname) |
| 248 |
except: |
| 249 |
pass |
| 250 |
os.chdir(old) |
| 251 |
|
| 252 |
# Create a zip file for each platform |
| 253 |
def mk_zips(): |
| 254 |
mk_zip(False) |
| 255 |
mk_zip(True) |
| 256 |
|
| 257 |
|
| 258 |
VS_RUNTIME_PATS = [re.compile('vcomp.*\.dll'), |
| 259 |
re.compile('msvcp.*\.dll'), |
| 260 |
re.compile('msvcr.*\.dll'), |
| 261 |
re.compile('vcrun.*\.dll')] |
| 262 |
|
| 263 |
# Copy Visual Studio Runtime libraries |
| 264 |
def cp_vs_runtime(x64): |
| 265 |
if x64: |
| 266 |
platform = "x64" |
| 267 |
else: |
| 268 |
platform = "x86" |
| 269 |
vcdir = os.environ['VCINSTALLDIR'] |
| 270 |
path = '%sredist' % vcdir |
| 271 |
vs_runtime_files = [] |
| 272 |
print("Walking %s" % path) |
| 273 |
# Everything changes with every release of VS |
| 274 |
# Prior versions of VS had DLLs under "redist\x64" |
| 275 |
# There are now several variants of redistributables |
| 276 |
# The naming convention defies my understanding so |
| 277 |
# we use a "check_root" filter to find some hopefully suitable |
| 278 |
# redistributable. |
| 279 |
def check_root(root): |
| 280 |
return platform in root and ("CRT" in root or "MP" in root) and "onecore" not in root and "debug" not in root |
| 281 |
for root, dirs, files in os.walk(path): |
| 282 |
for filename in files: |
| 283 |
if fnmatch(filename, '*.dll') and check_root(root): |
| 284 |
print("Checking %s %s" % (root, filename)) |
| 285 |
for pat in VS_RUNTIME_PATS: |
| 286 |
if pat.match(filename): |
| 287 |
fname = os.path.join(root, filename) |
| 288 |
if not os.path.isdir(fname): |
| 289 |
vs_runtime_files.append(fname) |
| 290 |
if not vs_runtime_files: |
| 291 |
raise MKException("Did not find any runtime files to include") |
| 292 |
bin_dist_path = os.path.join(DIST_DIR, get_dist_path(x64), 'bin') |
| 293 |
for f in vs_runtime_files: |
| 294 |
shutil.copy(f, bin_dist_path) |
| 295 |
if is_verbose(): |
| 296 |
print("Copied '%s' to '%s'" % (f, bin_dist_path)) |
| 297 |
|
| 298 |
def cp_vs_runtimes(): |
| 299 |
cp_vs_runtime(True) |
| 300 |
cp_vs_runtime(False) |
| 301 |
|
| 302 |
def cp_license(x64): |
| 303 |
shutil.copy("LICENSE.txt", os.path.join(DIST_DIR, get_dist_path(x64))) |
| 304 |
|
| 305 |
def cp_licenses(): |
| 306 |
cp_license(True) |
| 307 |
cp_license(False) |
| 308 |
|
| 309 |
def init_flags(): |
| 310 |
global DOTNET_KEY_FILE, JAVA_ENABLED, PYTHON_ENABLED, ASSEMBLY_VERSION |
| 311 |
mk_util.DOTNET_CORE_ENABLED = True |
| 312 |
mk_util.DOTNET_KEY_FILE = DOTNET_KEY_FILE |
| 313 |
mk_util.ASSEMBLY_VERSION = ASSEMBLY_VERSION |
| 314 |
mk_util.JAVA_ENABLED = JAVA_ENABLED |
| 315 |
mk_util.PYTHON_ENABLED = PYTHON_ENABLED |
| 316 |
mk_util.ALWAYS_DYNAMIC_BASE = True |
| 317 |
|
| 318 |
|
| 319 |
# Entry point |
| 320 |
def main(): |
| 321 |
if os.name != 'nt': |
| 322 |
raise MKException("This script is for Windows only") |
| 323 |
|
| 324 |
parse_options() |
| 325 |
check_vc_cmd_prompt() |
| 326 |
init_flags() |
| 327 |
|
| 328 |
if X86ONLY: |
| 329 |
mk_build_dir(BUILD_X86_DIR, False) |
| 330 |
mk_z3(False) |
| 331 |
init_project_def() |
| 332 |
mk_dist_dir(False) |
| 333 |
cp_license(False) |
| 334 |
cp_vs_runtime(False) |
| 335 |
if ZIP_BUILD_OUTPUTS: |
| 336 |
mk_zip(False) |
| 337 |
elif X64ONLY: |
| 338 |
mk_build_dir(BUILD_X64_DIR, True) |
| 339 |
mk_z3(True) |
| 340 |
init_project_def() |
| 341 |
mk_dist_dir(True) |
| 342 |
cp_license(True) |
| 343 |
cp_vs_runtime(True) |
| 344 |
if ZIP_BUILD_OUTPUTS: |
| 345 |
mk_zip(True) |
| 346 |
else: |
| 347 |
mk_build_dirs() |
| 348 |
mk_z3s() |
| 349 |
init_project_def() |
| 350 |
mk_dist_dirs() |
| 351 |
cp_licenses() |
| 352 |
cp_vs_runtimes() |
| 353 |
if ZIP_BUILD_OUTPUTS: |
| 354 |
mk_zips() |
| 355 |
|
| 356 |
main() |
| 357 |
|