Debian Codegraph @

scripts/mk_unix_dist.py

raw
1 ############################################
2 # Copyright (c) 2013 Microsoft Corporation
3 #
4 # Scripts for automatically generating
5 # Linux/OSX/BSD 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 VERBOSE=True
23 DIST_DIR='dist'
24 FORCE_MK=False
25 DOTNET_CORE_ENABLED=True
26 DOTNET_KEY_FILE=None
27 JAVA_ENABLED=True
28 GIT_HASH=False
29 PYTHON_ENABLED=True
30 MAKEJOBS=getenv("MAKEJOBS", '8')
31 OS_NAME=None
32
33 def set_verbose(flag):
34 global VERBOSE
35 VERBOSE = flag
36
37 def is_verbose():
38 return VERBOSE
39
40 def mk_dir(d):
41 if not os.path.exists(d):
42 os.makedirs(d)
43
44 def set_build_dir(path):
45 global BUILD_DIR
46 BUILD_DIR = mk_util.norm_path(path)
47 mk_dir(BUILD_DIR)
48
49 def display_help():
50 print("mk_unix_dist.py: Z3 Linux/OSX/BSD distribution generator\n")
51 print("This script generates the zip files containing executables, shared objects, header files for Linux/OSX/BSD.")
52 print("It must be executed from the Z3 root directory.")
53 print("\nOptions:")
54 print(" -h, --help display this message.")
55 print(" -s, --silent do not print verbose messages.")
56 print(" -b <sudir>, --build=<subdir> subdirectory where x86 and x64 Z3 versions will be built (default: build-dist).")
57 print(" -f, --force force script to regenerate Makefiles.")
58 print(" --nodotnet do not include .NET bindings in the binary distribution files.")
59 print(" --dotnet-key=<file> sign the .NET assembly with the private key in <file>.")
60 print(" --arch=<arch> set architecture (to arm64) to force arm64 build")
61 print(" --nojava do not include Java bindings in the binary distribution files.")
62 print(" --os=<os> set OS version.")
63 print(" --nopython do not include Python bindings in the binary distribution files.")
64 print(" --githash include git hash in the Zip file.")
65 exit(0)
66
67 # Parse configuration option for mk_make script
68 def parse_options():
69 global FORCE_MK, JAVA_ENABLED, GIT_HASH, DOTNET_CORE_ENABLED, DOTNET_KEY_FILE, PYTHON_ENABLED, OS_NAME
70 path = BUILD_DIR
71 options, remainder = getopt.gnu_getopt(sys.argv[1:], 'b:hsf', ['build=',
72 'help',
73 'silent',
74 'force',
75 'nojava',
76 'nodotnet',
77 'dotnet-key=',
78 'arch=',
79 'os=',
80 'githash',
81 'nopython'
82 ])
83 for opt, arg in options:
84 if opt in ('-b', '--build'):
85 if arg == 'src':
86 raise MKException('The src directory should not be used to host the Makefile')
87 path = arg
88 elif opt in ('-s', '--silent'):
89 set_verbose(False)
90 elif opt in ('-h', '--help'):
91 display_help()
92 elif opt in ('-f', '--force'):
93 FORCE_MK = True
94 elif opt == '--nodotnet':
95 DOTNET_CORE_ENABLED = False
96 elif opt == '--nopython':
97 PYTHON_ENABLED = False
98 elif opt == '--dotnet-key':
99 DOTNET_KEY_FILE = arg
100 elif opt == '--nojava':
101 JAVA_ENABLED = False
102 elif opt == '--githash':
103 GIT_HASH = True
104 elif opt == '--arch':
105 if arg == "arm64":
106 mk_util.IS_ARCH_ARM64 = True
107 else:
108 raise MKException("Invalid architecture directive '%s'. Legal directives: arm64" % arg)
109 elif opt == '--os':
110 OS_NAME = arg
111 else:
112 raise MKException("Invalid command line option '%s'" % opt)
113 set_build_dir(path)
114
115 # Check whether build directory already exists or not
116 def check_build_dir(path):
117 return os.path.exists(path) and os.path.exists(os.path.join(path, 'Makefile'))
118
119 # Create a build directory using mk_make.py
120 def mk_build_dir(path):
121 global LINUX_X64
122 if not check_build_dir(path) or FORCE_MK:
123 env = os.environ
124 opts = [sys.executable, os.path.join('scripts', 'mk_make.py'), "-b", path, "--staticlib"]
125 if DOTNET_CORE_ENABLED:
126 opts.append('--dotnet')
127 if not DOTNET_KEY_FILE is None:
128 opts.append('--dotnet-key=' + DOTNET_KEY_FILE)
129 if JAVA_ENABLED:
130 opts.append('--java')
131 if GIT_HASH:
132 opts.append('--githash=%s' % mk_util.git_hash())
133 opts.append('--git-describe')
134 if PYTHON_ENABLED:
135 opts.append('--python')
136 if mk_util.IS_ARCH_ARM64:
137 opts.append('--arm64=true')
138 if mk_util.IS_ARCH_ARM64 and LINUX_X64:
139 # we are machine x64 but build against arm64
140 # so we have to do cross compiling
141 # the cross compiler is download from ARM GNU
142 # toolchain
143 myvar = {
144 "CC": "aarch64-none-linux-gnu-gcc",
145 "CXX": "aarch64-none-linux-gnu-g++"
146 }
147 env.update(myvar)
148 if subprocess.call(opts, env=env) != 0:
149 raise MKException("Failed to generate build directory at '%s'" % path)
150
151 # Create build directories
152 def mk_build_dirs():
153 mk_build_dir(BUILD_DIR)
154
155 class cd:
156 def __init__(self, newPath):
157 self.newPath = newPath
158
159 def __enter__(self):
160 self.savedPath = os.getcwd()
161 os.chdir(self.newPath)
162
163 def __exit__(self, etype, value, traceback):
164 os.chdir(self.savedPath)
165
166 def mk_z3():
167 with cd(BUILD_DIR):
168 try:
169 return subprocess.call(['make', '-j', MAKEJOBS])
170 except:
171 return 1
172
173 def get_os_name():
174 global LINUX_X64
175 if OS_NAME is not None:
176 return OS_NAME
177 import platform
178 basic = os.uname()[0].lower()
179 if basic == 'linux':
180 if mk_util.IS_ARCH_ARM64 and LINUX_X64:
181 # handle cross compiling
182 # example: 'ldd (GNU) 2.34'
183 lines = subprocess.check_output(["ldd", "--version"]).decode('ascii')
184 first_line = lines.split("\n")[0]
185 ldd_version = first_line.split()[-1]
186 # coerce the format to platform.libc_ver() return type
187 dist = ('glibc', ldd_version)
188 else:
189 dist = platform.libc_ver()
190 if len(dist) == 2 and len(dist[0]) > 0 and len(dist[1]) > 0:
191 return '%s-%s' % (dist[0].lower(), dist[1].lower())
192 else:
193 return basic
194 elif basic == 'darwin':
195 ver = platform.mac_ver()
196 if len(ver) == 3 and len(ver[0]) > 0:
197 return 'osx-%s' % ver[0]
198 else:
199 return 'osx'
200 elif basic == 'freebsd':
201 ver = platform.version()
202 idx1 = ver.find(' ')
203 idx2 = ver.find('-')
204 if idx1 < 0 or idx2 < 0 or idx1 >= idx2:
205 return basic
206 else:
207 return 'freebsd-%s' % ver[(idx1+1):idx2]
208 else:
209 return basic
210
211 def get_z3_name():
212 import platform as platform_module
213 # Note that the platform name this function return
214 # has to work together with setup.py
215 # It's not the typical output from platform.machine()
216 major, minor, build, revision = get_version()
217 if mk_util.IS_ARCH_ARM64 or platform_module.machine() == "aarch64":
218 # the second case handle native build on aarch64
219 # TODO: we don't handle cross compile on host aarch64 to target x64
220 platform = "arm64"
221 elif sys.maxsize >= 2**32:
222 platform = "x64"
223 else:
224 platform = "x86"
225 osname = get_os_name()
226 if GIT_HASH:
227 return 'z3-%s.%s.%s.%s-%s-%s' % (major, minor, build, mk_util.git_hash(), platform, osname)
228 else:
229 return 'z3-%s.%s.%s-%s-%s' % (major, minor, build, platform, osname)
230
231 def mk_dist_dir():
232 build_path = BUILD_DIR
233 dist_path = os.path.join(DIST_DIR, get_z3_name())
234 mk_dir(dist_path)
235 mk_util.DOTNET_CORE_ENABLED = DOTNET_CORE_ENABLED
236 mk_util.DOTNET_ENABLED = False
237 mk_util.DOTNET_KEY_FILE = DOTNET_KEY_FILE
238 mk_util.JAVA_ENABLED = JAVA_ENABLED
239 mk_util.PYTHON_ENABLED = PYTHON_ENABLED
240 mk_unix_dist(build_path, dist_path)
241 if is_verbose():
242 print("Generated distribution folder at '%s'" % dist_path)
243
244 def get_dist_path():
245 return get_z3_name()
246
247 def mk_zip():
248 dist_path = get_dist_path()
249 old = os.getcwd()
250 try:
251 os.chdir(DIST_DIR)
252 zfname = '%s.zip' % dist_path
253 zipout = zipfile.ZipFile(zfname, 'w', zipfile.ZIP_DEFLATED)
254 for root, dirs, files in os.walk(dist_path):
255 for f in files:
256 zipout.write(os.path.join(root, f))
257 if is_verbose():
258 print("Generated '%s'" % zfname)
259 except:
260 pass
261 os.chdir(old)
262
263 def cp_license():
264 shutil.copy("LICENSE.txt", os.path.join(DIST_DIR, get_dist_path()))
265
266 # Entry point
267 def main():
268 parse_options()
269 mk_build_dirs()
270 mk_z3()
271 init_project_def()
272 mk_dist_dir()
273 cp_license()
274 mk_zip()
275
276 main()
277