meson_install.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. # Copyright 2013-2014 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import sys, pickle, os, shutil, subprocess, gzip, platform, errno
  12. import shlex
  13. from glob import glob
  14. from . import depfixer
  15. from . import destdir_join
  16. from ..mesonlib import is_windows, Popen_safe
  17. install_log_file = None
  18. selinux_updates = []
  19. class DirMaker:
  20. def __init__(self):
  21. self.dirs = []
  22. def makedirs(self, path, exist_ok=False):
  23. dirname = os.path.normpath(path)
  24. dirs = []
  25. while dirname != os.path.dirname(dirname):
  26. if not os.path.exists(dirname):
  27. dirs.append(dirname)
  28. dirname = os.path.dirname(dirname)
  29. os.makedirs(path, exist_ok=exist_ok)
  30. # store the directories in creation order, with the parent directory
  31. # before the child directories. Future calls of makedir() will not
  32. # create the parent directories, so the last element in the list is
  33. # the last one to be created. That is the first one to be removed on
  34. # __exit__
  35. dirs.reverse()
  36. self.dirs += dirs
  37. def __enter__(self):
  38. return self
  39. def __exit__(self, type, value, traceback):
  40. self.dirs.reverse()
  41. for d in self.dirs:
  42. append_to_log(d)
  43. def set_mode(path, mode):
  44. if mode is None:
  45. # Keep mode unchanged
  46. return
  47. if (mode.perms_s or mode.owner or mode.group) is None:
  48. # Nothing to set
  49. return
  50. # No chown() on Windows, and must set one of owner/group
  51. if not is_windows() and (mode.owner or mode.group) is not None:
  52. try:
  53. shutil.chown(path, mode.owner, mode.group)
  54. except PermissionError as e:
  55. msg = '{!r}: Unable to set owner {!r} and group {!r}: {}, ignoring...'
  56. print(msg.format(path, mode.owner, mode.group, e.strerror))
  57. except LookupError:
  58. msg = '{!r}: Non-existent owner {!r} or group {!r}: ignoring...'
  59. print(msg.format(path, mode.owner, mode.group))
  60. except OSError as e:
  61. if e.errno == errno.EINVAL:
  62. msg = '{!r}: Non-existent numeric owner {!r} or group {!r}: ignoring...'
  63. print(msg.format(path, mode.owner, mode.group))
  64. else:
  65. raise
  66. # Must set permissions *after* setting owner/group otherwise the
  67. # setuid/setgid bits will get wiped by chmod
  68. # NOTE: On Windows you can set read/write perms; the rest are ignored
  69. if mode.perms_s is not None:
  70. try:
  71. os.chmod(path, mode.perms)
  72. except PermissionError as e:
  73. msg = '{!r}: Unable to set permissions {!r}: {}, ignoring...'
  74. print(msg.format(path, mode.perms_s, e.strerror))
  75. def restore_selinux_contexts():
  76. '''
  77. Restores the SELinux context for files in @selinux_updates
  78. If $DESTDIR is set, do not warn if the call fails.
  79. '''
  80. try:
  81. subprocess.check_call(['selinuxenabled'])
  82. except (FileNotFoundError, PermissionError, subprocess.CalledProcessError) as e:
  83. # If we don't have selinux or selinuxenabled returned 1, failure
  84. # is ignored quietly.
  85. return
  86. with subprocess.Popen(['restorecon', '-F', '-f-', '-0'],
  87. stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
  88. out, err = proc.communicate(input=b'\0'.join(os.fsencode(f) for f in selinux_updates) + b'\0')
  89. if proc.returncode != 0 and not os.environ.get('DESTDIR'):
  90. print('Failed to restore SELinux context of installed files...',
  91. 'Standard output:', out.decode(),
  92. 'Standard error:', err.decode(), sep='\n')
  93. def append_to_log(line):
  94. install_log_file.write(line)
  95. if not line.endswith('\n'):
  96. install_log_file.write('\n')
  97. install_log_file.flush()
  98. def do_copyfile(from_file, to_file):
  99. if not os.path.isfile(from_file):
  100. raise RuntimeError('Tried to install something that isn\'t a file:'
  101. '{!r}'.format(from_file))
  102. # copyfile fails if the target file already exists, so remove it to
  103. # allow overwriting a previous install. If the target is not a file, we
  104. # want to give a readable error.
  105. if os.path.exists(to_file):
  106. if not os.path.isfile(to_file):
  107. raise RuntimeError('Destination {!r} already exists and is not '
  108. 'a file'.format(to_file))
  109. os.unlink(to_file)
  110. shutil.copyfile(from_file, to_file)
  111. shutil.copystat(from_file, to_file)
  112. selinux_updates.append(to_file)
  113. append_to_log(to_file)
  114. def do_copydir(data, src_prefix, src_dir, dst_dir, exclude):
  115. '''
  116. Copies the directory @src_prefix (full path) into @dst_dir
  117. @src_dir is simply the parent directory of @src_prefix
  118. '''
  119. if exclude is not None:
  120. exclude_files, exclude_dirs = exclude
  121. else:
  122. exclude_files = exclude_dirs = set()
  123. for root, dirs, files in os.walk(src_prefix):
  124. for d in dirs[:]:
  125. abs_src = os.path.join(src_dir, root, d)
  126. filepart = abs_src[len(src_dir) + 1:]
  127. abs_dst = os.path.join(dst_dir, filepart)
  128. # Remove these so they aren't visited by os.walk at all.
  129. if filepart in exclude_dirs:
  130. dirs.remove(d)
  131. continue
  132. if os.path.isdir(abs_dst):
  133. continue
  134. if os.path.exists(abs_dst):
  135. print('Tried to copy directory %s but a file of that name already exists.' % abs_dst)
  136. sys.exit(1)
  137. data.dirmaker.makedirs(abs_dst)
  138. shutil.copystat(abs_src, abs_dst)
  139. for f in files:
  140. abs_src = os.path.join(src_dir, root, f)
  141. filepart = abs_src[len(src_dir) + 1:]
  142. if filepart in exclude_files:
  143. continue
  144. abs_dst = os.path.join(dst_dir, filepart)
  145. if os.path.isdir(abs_dst):
  146. print('Tried to copy file %s but a directory of that name already exists.' % abs_dst)
  147. if os.path.exists(abs_dst):
  148. os.unlink(abs_dst)
  149. parent_dir = os.path.split(abs_dst)[0]
  150. if not os.path.isdir(parent_dir):
  151. os.mkdir(parent_dir)
  152. shutil.copystat(os.path.split(abs_src)[0], parent_dir)
  153. shutil.copy2(abs_src, abs_dst, follow_symlinks=False)
  154. append_to_log(abs_dst)
  155. def get_destdir_path(d, path):
  156. if os.path.isabs(path):
  157. output = destdir_join(d.destdir, path)
  158. else:
  159. output = os.path.join(d.fullprefix, path)
  160. return output
  161. def do_install(datafilename):
  162. with open(datafilename, 'rb') as ifile:
  163. d = pickle.load(ifile)
  164. d.destdir = os.environ.get('DESTDIR', '')
  165. d.fullprefix = destdir_join(d.destdir, d.prefix)
  166. d.dirmaker = DirMaker()
  167. with d.dirmaker:
  168. install_subdirs(d) # Must be first, because it needs to delete the old subtree.
  169. install_targets(d)
  170. install_headers(d)
  171. install_man(d)
  172. install_data(d)
  173. restore_selinux_contexts()
  174. run_install_script(d)
  175. def install_subdirs(d):
  176. for (src_dir, inst_dir, dst_dir, mode, exclude) in d.install_subdirs:
  177. if src_dir.endswith('/') or src_dir.endswith('\\'):
  178. src_dir = src_dir[:-1]
  179. src_prefix = os.path.join(src_dir, inst_dir)
  180. print('Installing subdir %s to %s' % (src_prefix, dst_dir))
  181. dst_dir = get_destdir_path(d, dst_dir)
  182. d.dirmaker.makedirs(dst_dir, exist_ok=True)
  183. do_copydir(d, src_prefix, src_dir, dst_dir, exclude)
  184. dst_prefix = os.path.join(dst_dir, inst_dir)
  185. set_mode(dst_prefix, mode)
  186. def install_data(d):
  187. for i in d.data:
  188. fullfilename = i[0]
  189. outfilename = get_destdir_path(d, i[1])
  190. mode = i[2]
  191. outdir = os.path.split(outfilename)[0]
  192. d.dirmaker.makedirs(outdir, exist_ok=True)
  193. print('Installing %s to %s' % (fullfilename, outdir))
  194. do_copyfile(fullfilename, outfilename)
  195. set_mode(outfilename, mode)
  196. def install_man(d):
  197. for m in d.man:
  198. full_source_filename = m[0]
  199. outfilename = get_destdir_path(d, m[1])
  200. outdir = os.path.split(outfilename)[0]
  201. d.dirmaker.makedirs(outdir, exist_ok=True)
  202. print('Installing %s to %s' % (full_source_filename, outdir))
  203. if outfilename.endswith('.gz') and not full_source_filename.endswith('.gz'):
  204. with open(outfilename, 'wb') as of:
  205. with open(full_source_filename, 'rb') as sf:
  206. # Set mtime and filename for reproducibility.
  207. with gzip.GzipFile(fileobj=of, mode='wb', filename='', mtime=0) as gz:
  208. gz.write(sf.read())
  209. shutil.copystat(full_source_filename, outfilename)
  210. append_to_log(outfilename)
  211. else:
  212. do_copyfile(full_source_filename, outfilename)
  213. def install_headers(d):
  214. for t in d.headers:
  215. fullfilename = t[0]
  216. fname = os.path.split(fullfilename)[1]
  217. outdir = get_destdir_path(d, t[1])
  218. outfilename = os.path.join(outdir, fname)
  219. print('Installing %s to %s' % (fname, outdir))
  220. d.dirmaker.makedirs(outdir, exist_ok=True)
  221. do_copyfile(fullfilename, outfilename)
  222. def run_install_script(d):
  223. env = {'MESON_SOURCE_ROOT': d.source_dir,
  224. 'MESON_BUILD_ROOT': d.build_dir,
  225. 'MESON_INSTALL_PREFIX': d.prefix,
  226. 'MESON_INSTALL_DESTDIR_PREFIX': d.fullprefix,
  227. 'MESONINTROSPECT': ' '.join([shlex.quote(x) for x in d.mesonintrospect]),
  228. }
  229. child_env = os.environ.copy()
  230. child_env.update(env)
  231. for i in d.install_scripts:
  232. script = i['exe']
  233. args = i['args']
  234. name = ' '.join(script + args)
  235. print('Running custom install script {!r}'.format(name))
  236. try:
  237. rc = subprocess.call(script + args, env=child_env)
  238. if rc != 0:
  239. sys.exit(rc)
  240. except:
  241. print('Failed to run install script {!r}'.format(name))
  242. sys.exit(1)
  243. def is_elf_platform():
  244. platname = platform.system().lower()
  245. if platname == 'darwin' or platname == 'windows' or platname == 'cygwin':
  246. return False
  247. return True
  248. def check_for_stampfile(fname):
  249. '''Some languages e.g. Rust have output files
  250. whose names are not known at configure time.
  251. Check if this is the case and return the real
  252. file instead.'''
  253. if fname.endswith('.so') or fname.endswith('.dll'):
  254. if os.stat(fname).st_size == 0:
  255. (base, suffix) = os.path.splitext(fname)
  256. files = glob(base + '-*' + suffix)
  257. if len(files) > 1:
  258. print("Stale dynamic library files in build dir. Can't install.")
  259. sys.exit(1)
  260. if len(files) == 1:
  261. return files[0]
  262. elif fname.endswith('.a') or fname.endswith('.lib'):
  263. if os.stat(fname).st_size == 0:
  264. (base, suffix) = os.path.splitext(fname)
  265. files = glob(base + '-*' + '.rlib')
  266. if len(files) > 1:
  267. print("Stale static library files in build dir. Can't install.")
  268. sys.exit(1)
  269. if len(files) == 1:
  270. return files[0]
  271. return fname
  272. def install_targets(d):
  273. for t in d.targets:
  274. fname = check_for_stampfile(t[0])
  275. outdir = get_destdir_path(d, t[1])
  276. outname = os.path.join(outdir, os.path.split(fname)[-1])
  277. aliases = t[2]
  278. should_strip = t[3]
  279. install_rpath = t[4]
  280. print('Installing %s to %s' % (fname, outname))
  281. d.dirmaker.makedirs(outdir, exist_ok=True)
  282. if not os.path.exists(fname):
  283. raise RuntimeError('File {!r} could not be found'.format(fname))
  284. elif os.path.isfile(fname):
  285. do_copyfile(fname, outname)
  286. if should_strip and d.strip_bin is not None:
  287. if fname.endswith('.jar'):
  288. print('Not stripping jar target:', os.path.split(fname)[1])
  289. continue
  290. print('Stripping target {!r}'.format(fname))
  291. ps, stdo, stde = Popen_safe(d.strip_bin + [outname])
  292. if ps.returncode != 0:
  293. print('Could not strip file.\n')
  294. print('Stdout:\n%s\n' % stdo)
  295. print('Stderr:\n%s\n' % stde)
  296. sys.exit(1)
  297. pdb_filename = os.path.splitext(fname)[0] + '.pdb'
  298. if not should_strip and os.path.exists(pdb_filename):
  299. pdb_outname = os.path.splitext(outname)[0] + '.pdb'
  300. print('Installing pdb file %s to %s' % (pdb_filename, pdb_outname))
  301. do_copyfile(pdb_filename, pdb_outname)
  302. elif os.path.isdir(fname):
  303. fname = os.path.join(d.build_dir, fname.rstrip('/'))
  304. do_copydir(d, fname, os.path.dirname(fname), outdir, None)
  305. else:
  306. raise RuntimeError('Unknown file type for {!r}'.format(fname))
  307. printed_symlink_error = False
  308. for alias, to in aliases.items():
  309. try:
  310. symlinkfilename = os.path.join(outdir, alias)
  311. try:
  312. os.unlink(symlinkfilename)
  313. except FileNotFoundError:
  314. pass
  315. os.symlink(to, symlinkfilename)
  316. append_to_log(symlinkfilename)
  317. except (NotImplementedError, OSError):
  318. if not printed_symlink_error:
  319. print("Symlink creation does not work on this platform. "
  320. "Skipping all symlinking.")
  321. printed_symlink_error = True
  322. if is_elf_platform() and os.path.isfile(outname):
  323. try:
  324. e = depfixer.Elf(outname, False)
  325. e.fix_rpath(install_rpath)
  326. except SystemExit as e:
  327. if isinstance(e.code, int) and e.code == 0:
  328. pass
  329. else:
  330. raise
  331. def run(args):
  332. global install_log_file
  333. if len(args) != 1:
  334. print('Installer script for Meson. Do not run on your own, mmm\'kay?')
  335. print('meson_install.py [install info file]')
  336. datafilename = args[0]
  337. private_dir = os.path.split(datafilename)[0]
  338. log_dir = os.path.join(private_dir, '../meson-logs')
  339. with open(os.path.join(log_dir, 'install-log.txt'), 'w') as lf:
  340. install_log_file = lf
  341. append_to_log('# List of files installed by Meson')
  342. append_to_log('# Does not contain files installed by custom scripts.')
  343. do_install(datafilename)
  344. install_log_file = None
  345. return 0
  346. if __name__ == '__main__':
  347. sys.exit(run(sys.argv[1:]))