meson/meson_install.py

204 lines
7.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2013-02-09 02:22:47 +08:00
2014-08-11 05:20:17 +08:00
# Copyright 2013-2014 Jussi Pakkanen
2013-02-09 02:22:47 +08:00
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys, pickle, os, shutil, subprocess, gzip, platform
2014-06-24 01:29:18 +08:00
from glob import glob
class InstallData():
2014-07-12 01:53:50 +08:00
def __init__(self, source_dir, build_dir, prefix, depfixer):
self.source_dir = source_dir
self.build_dir= build_dir
self.prefix = prefix
2013-02-09 03:07:53 +08:00
self.targets = []
2013-02-09 03:29:02 +08:00
self.depfixer = depfixer
2013-02-09 04:05:46 +08:00
self.headers = []
2013-02-09 04:17:17 +08:00
self.man = []
2013-02-09 04:28:06 +08:00
self.data = []
self.po_package_name = ''
self.po = []
self.install_script = None
def do_install(datafilename):
ifile = open(datafilename, 'rb')
d = pickle.load(ifile)
2013-02-23 18:46:11 +08:00
destdir_var = 'DESTDIR'
if destdir_var in os.environ:
if d.prefix[0] == '/':
subdir = d.prefix[1:]
else:
subdir = d.prefix
d.prefix = os.path.join(os.environ[destdir_var], subdir)
2013-02-09 04:05:46 +08:00
install_targets(d)
install_headers(d)
2013-02-09 04:17:17 +08:00
install_man(d)
2013-02-09 04:28:06 +08:00
install_data(d)
install_po(d)
run_install_script(d)
def install_po(d):
packagename = d.po_package_name
for f in d.po:
srcfile = f[0]
localedir = f[1]
languagename = f[2]
outfile = os.path.join(d.prefix, localedir, languagename, 'LC_MESSAGES',
packagename + '.mo')
os.makedirs(os.path.split(outfile)[0], exist_ok=True)
shutil.copyfile(srcfile, outfile)
shutil.copystat(srcfile, outfile)
print('Installing %s to %s.' % (srcfile, outfile))
2013-02-09 04:28:06 +08:00
def install_data(d):
for i in d.data:
fullfilename = i[0]
outfilerel = i[1]
outdir = os.path.join(d.prefix, os.path.split(outfilerel)[0])
outfilename = os.path.join(outdir, os.path.split(outfilerel)[1])
2013-02-09 04:28:06 +08:00
os.makedirs(outdir, exist_ok=True)
print('Installing %s to %s.' % (fullfilename, outdir))
shutil.copyfile(fullfilename, outfilename)
2013-02-09 04:28:06 +08:00
shutil.copystat(fullfilename, outfilename)
2013-02-09 04:17:17 +08:00
def install_man(d):
for m in d.man:
outfileroot = m[1]
outfilename = os.path.join(d.prefix, outfileroot)
full_source_filename = m[0]
2013-02-09 04:17:17 +08:00
outdir = os.path.split(outfilename)[0]
os.makedirs(outdir, exist_ok=True)
print('Installing %s to %s.' % (full_source_filename, outdir))
if outfilename.endswith('.gz') and not full_source_filename.endswith('.gz'):
open(outfilename, 'wb').write(gzip.compress(open(full_source_filename, 'rb').read()))
2013-02-11 03:12:15 +08:00
else:
shutil.copyfile(full_source_filename, outfilename)
shutil.copystat(full_source_filename, outfilename)
2013-02-09 04:05:46 +08:00
def install_headers(d):
for t in d.headers:
fullfilename = t[0]
outdir = os.path.join(d.prefix, t[1])
2013-02-09 04:05:46 +08:00
fname = os.path.split(fullfilename)[1]
2013-02-09 04:28:06 +08:00
outfilename = os.path.join(outdir, fname)
2013-02-09 04:05:46 +08:00
print('Installing %s to %s' % (fname, outdir))
os.makedirs(outdir, exist_ok=True)
2013-02-09 04:28:06 +08:00
shutil.copyfile(fullfilename, outfilename)
shutil.copystat(fullfilename, outfilename)
2013-02-09 04:05:46 +08:00
def run_install_script(d):
if d.install_script is None:
return
env = {'MESON_SOURCE_ROOT' : d.source_dir,
'MESON_BUILD_ROOT' : d.build_dir,
'MESON_INSTALL_PREFIX' : d.prefix
}
script = d.install_script
print('Running custom install script %s' % script)
2014-09-26 23:17:10 +08:00
suffix = os.path.splitext(script)[1].lower()
2014-09-26 23:31:07 +08:00
if platform.system().lower() == 'windows' and suffix != '.bat':
2014-09-26 23:17:10 +08:00
first_line = open(script).readline().strip()
if first_line.startswith('#!'):
commands = first_line[2:].split('#')[0].strip().split()
commands[0] = shutil.which(commands[0].split('/')[-1])
if commands[0] is None:
raise RuntimeError("Don't know how to run script %s." % script)
script = commands + [script]
child_env = os.environ.copy()
child_env.update(env)
subprocess.check_call(script, env=child_env)
def is_elf_platform():
platname = platform.system().lower()
2013-03-03 22:09:49 +08:00
if platname == 'darwin' or platname == 'windows':
return False
return True
2014-06-24 01:29:18 +08:00
def check_for_stampfile(fname):
'''Some languages e.g. Rust have output files
whose names are not known at configure time.
Check if this is the case and return the real
file instead.'''
if fname.endswith('.so') or fname.endswith('.dll'):
if os.stat(fname).st_size == 0:
(base, suffix) = os.path.splitext(fname)
files = glob(base + '-*' + suffix)
if len(files) > 1:
2014-06-24 01:53:20 +08:00
print("Stale dynamic library files in build dir. Can't install.")
2014-06-24 01:29:18 +08:00
sys.exit(1)
if len(files) == 1:
return files[0]
2014-06-24 01:53:20 +08:00
elif fname.endswith('.a') or fname.endswith('.lib'):
if os.stat(fname).st_size == 0:
(base, suffix) = os.path.splitext(fname)
files = glob(base + '-*' + '.rlib')
if len(files) > 1:
print("Stale static library files in build dir. Can't install.")
sys.exit(1)
if len(files) == 1:
return files[0]
2014-06-24 01:29:18 +08:00
return fname
2013-02-09 04:05:46 +08:00
def install_targets(d):
2013-02-09 03:07:53 +08:00
for t in d.targets:
2014-06-24 01:29:18 +08:00
fname = check_for_stampfile(t[0])
outdir = os.path.join(d.prefix, t[1])
aliases = t[2]
outname = os.path.join(outdir, os.path.split(fname)[-1])
should_strip = t[3]
2014-07-12 01:53:50 +08:00
install_rpath = t[4]
print('Installing %s to %s' % (fname, outname))
2013-02-09 03:07:53 +08:00
os.makedirs(outdir, exist_ok=True)
shutil.copyfile(fname, outname)
shutil.copystat(fname, outname)
if should_strip:
print('Stripping target')
ps = subprocess.Popen(['strip', outname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdo, stde) = ps.communicate()
if ps.returncode != 0:
print('Could not strip file.\n')
print('Stdout:\n%s\n' % stdo.decode())
print('Stderr:\n%s\n' % stde.decode())
sys.exit(1)
2013-03-09 01:10:52 +08:00
printed_symlink_error = False
for alias in aliases:
2013-03-09 01:10:52 +08:00
try:
symlinkfilename = os.path.join(outdir, alias)
try:
os.unlink(symlinkfilename)
except FileNotFoundError:
pass
os.symlink(fname, symlinkfilename)
2013-03-09 01:10:52 +08:00
except NotImplementedError:
if not printed_symlink_error:
print("Symlink creation does not work on this platform.")
printed_symlink_error = True
if is_elf_platform():
2013-10-05 04:15:56 +08:00
p = subprocess.Popen([d.depfixer, outname, install_rpath], stdout=subprocess.PIPE,
2013-02-09 03:29:02 +08:00
stderr=subprocess.PIPE)
(stdo, stde) = p.communicate()
if p.returncode != 0:
print('Could not fix dependency info.\n')
print('Stdout:\n%s\n' % stdo.decode())
print('Stderr:\n%s\n' % stde.decode())
sys.exit(1)
2013-02-09 02:22:47 +08:00
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Installer script for Meson. Do not run on your own, mmm\'kay?')
2013-02-09 02:22:47 +08:00
print('%s [install info file]' % sys.argv[0])
datafilename = sys.argv[1]
do_install(datafilename)