############################################ # Copyright (c) 2012 Microsoft Corporation # # Scripts for automatically generating # Window distribution zip files. # # Author: Leonardo de Moura (leonardo) ############################################ import os import subprocess import zipfile import re import getopt import sys import shutil from mk_exception import * from fnmatch import fnmatch def getenv(name, default): try: return os.environ[name].strip(' "\'') except: return default BUILD_DIR = 'build-dist' DIST_DIR = 'dist' BUILD_X64_DIR = os.path.join(BUILD_DIR, 'x64') BUILD_X86_DIR = os.path.join(BUILD_DIR, 'x86') BUILD_ARM64_DIR = os.path.join(BUILD_DIR, 'arm64') VERBOSE = True FORCE_MK = False ASSEMBLY_VERSION = None DOTNET_CORE_ENABLED = True DOTNET_KEY_FILE = None JAVA_ENABLED = True JULIA_ENABLED = False ZIP_BUILD_OUTPUTS = False GIT_HASH = False PYTHON_ENABLED = True X86ONLY = False X64ONLY = False ARM64ONLY = False ARCHITECTURES = [] def set_verbose(flag): global VERBOSE VERBOSE = flag def is_verbose(): return VERBOSE def mk_dir(d): if not os.path.exists(d): if is_verbose(): print("Make directory", d) os.makedirs(d) def get_z3_name(arch): version = "4" if ASSEMBLY_VERSION: version = ASSEMBLY_VERSION print("Assembly version:", version) if GIT_HASH: return 'z3-%s.%s-%s-win' % (version, get_git_hash(), arch) else: return 'z3-%s-%s-win' % (version, arch) def get_build_dir(arch): return ARCHITECTURES[arch] def get_build_dist_path(arch): return os.path.join(get_build_dir(arch), DIST_DIR) def get_bin_path(arch): return os.path.join(get_build_dist_path(arch), "bin") def get_dist_path(arch): return os.path.join(DIST_DIR, arch) def set_build_dir(path): global BUILD_DIR, BUILD_X86_DIR, BUILD_X64_DIR, BUILD_ARM64_DIR, ARCHITECTURES BUILD_DIR = os.path.expanduser(os.path.normpath(path)) BUILD_X86_DIR = os.path.join(path, 'x86') BUILD_X64_DIR = os.path.join(path, 'x64') BUILD_ARM64_DIR = os.path.join(path, 'arm64') # Set ARM64 build directory ARCHITECTURES = {'x64': BUILD_X64_DIR, 'x86':BUILD_X86_DIR, 'arm64':BUILD_ARM64_DIR} mk_dir(BUILD_X86_DIR) mk_dir(BUILD_X64_DIR) mk_dir(BUILD_ARM64_DIR) def display_help(): print("mk_win_dist.py: Z3 Windows distribution generator\n") print("This script generates the zip files containing executables, dlls, header files for Windows.") print("It must be executed from the Z3 root directory.") print("\nOptions:") print(" -h, --help display this message.") print(" -s, --silent do not print verbose messages.") print(" -b , --build= subdirectory where x86 and x64 Z3 versions will be built (default: build-dist).") print(" -f, --force force script to regenerate Makefiles.") print(" --version= release version.") print(" --assembly-version assembly version for dll") print(" --nodotnet do not include .NET bindings in the binary distribution files.") print(" --dotnet-key= strongname sign the .NET assembly with the private key in .") print(" --nojava do not include Java bindings in the binary distribution files.") print(" --nopython do not include Python bindings in the binary distribution files.") print(" --julia build Julia bindings.") print(" --zip package build outputs in zip file.") print(" --githash include git hash in the Zip file.") print(" --x86-only x86 dist only.") print(" --x64-only x64 dist only.") print(" --arm64-only arm64 dist only.") exit(0) # Parse configuration option for mk_make script def parse_options(): global FORCE_MK, JAVA_ENABLED, JULIA_ENABLED, ZIP_BUILD_OUTPUTS, GIT_HASH, DOTNET_CORE_ENABLED, DOTNET_KEY_FILE, ASSEMBLY_VERSION, PYTHON_ENABLED, X86ONLY, X64ONLY, ARM64ONLY path = BUILD_DIR options, remainder = getopt.gnu_getopt(sys.argv[1:], 'b:hsf', ['build=', 'help', 'silent', 'force', 'nojava', 'nodotnet', 'dotnet-key=', 'assembly-version=', 'zip', 'githash', 'nopython', 'julia', 'x86-only', 'x64-only', 'arm64-only' ]) for opt, arg in options: if opt in ('-b', '--build'): if arg == 'src': raise MKException('The src directory should not be used to host the Makefile') path = arg elif opt in ('-s', '--silent'): set_verbose(False) elif opt in ('-h', '--help'): display_help() elif opt in ('-f', '--force'): FORCE_MK = True elif opt == '--nodotnet': DOTNET_CORE_ENABLED = False elif opt == '--assembly-version': ASSEMBLY_VERSION = arg elif opt == '--nopython': PYTHON_ENABLED = False elif opt == '--dotnet-key': DOTNET_KEY_FILE = arg elif opt == '--nojava': JAVA_ENABLED = False elif opt == '--julia': JULIA_ENABLED = True elif opt == '--zip': ZIP_BUILD_OUTPUTS = True elif opt == '--githash': GIT_HASH = True elif opt == '--x86-only' and not X64ONLY: X86ONLY = True elif opt == '--arm64-only' and not X86ONLY and not X64ONLY: ARM64ONLY = True elif opt == '--x64-only' and not X86ONLY: X64ONLY = True else: raise MKException("Invalid command line option '%s'" % opt) set_build_dir(path) # Check whether build directory already exists or not def check_build_dir(path): return os.path.exists(path) and os.path.exists(os.path.join(path, 'Makefile')) def check_output(cmd): out = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] if out != None: enc = sys.getdefaultencoding() if enc != None: return out.decode(enc).rstrip('\r\n') else: return out.rstrip('\r\n') else: return "" def get_git_hash(): try: branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) r = check_output(['git', 'show-ref', '--abbrev=12', 'refs/heads/%s' % branch]) except: raise MKException("Failed to retrieve git hash") ls = r.split(' ') if len(ls) != 2: raise MKException("Unexpected git output " + r) return ls[0] # Create a build directory using mk_make.py def mk_build_dir(arch): build_path = get_build_dir(arch) if not check_build_dir(build_path) or FORCE_MK: mk_dir(build_path) if arch == "arm64": arch = "amd64_arm64" cmds = [] if JULIA_ENABLED: cmds.append('julia -e "using Pkg; Pkg.add(PackageSpec(name=\"libcxxwrap_julia_jll\"))"') cmds.append('julia -e "using libcxxwrap_julia_jll; print(dirname(libcxxwrap_julia_jll.libcxxwrap_julia_path))" > tmp.env') cmds.append('set /P JlCxxDir=