The Meson Build System
http://mesonbuild.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
210 lines
7.6 KiB
210 lines
7.6 KiB
8 years ago
|
# Copyright 2017 The Meson development team
|
||
|
|
||
|
# 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.
|
||
|
|
||
|
|
||
8 years ago
|
import lzma
|
||
8 years ago
|
import os
|
||
6 years ago
|
import sys
|
||
8 years ago
|
import shutil
|
||
|
import subprocess
|
||
|
import pickle
|
||
|
import hashlib
|
||
|
import tarfile, zipfile
|
||
|
from glob import glob
|
||
8 years ago
|
from mesonbuild.environment import detect_ninja
|
||
8 years ago
|
from mesonbuild.mesonlib import windows_proof_rmtree
|
||
5 years ago
|
from mesonbuild import mlog, build
|
||
8 years ago
|
|
||
|
def create_hash(fname):
|
||
|
hashname = fname + '.sha256sum'
|
||
|
m = hashlib.sha256()
|
||
|
m.update(open(fname, 'rb').read())
|
||
|
with open(hashname, 'w') as f:
|
||
8 years ago
|
f.write('%s %s\n' % (m.hexdigest(), os.path.basename(fname)))
|
||
|
|
||
8 years ago
|
|
||
|
def create_zip(zipfilename, packaging_dir):
|
||
8 years ago
|
prefix = os.path.dirname(packaging_dir)
|
||
8 years ago
|
removelen = len(prefix) + 1
|
||
|
with zipfile.ZipFile(zipfilename,
|
||
|
'w',
|
||
|
compression=zipfile.ZIP_DEFLATED,
|
||
|
allowZip64=True) as zf:
|
||
|
zf.write(packaging_dir, packaging_dir[removelen:])
|
||
|
for root, dirs, files in os.walk(packaging_dir):
|
||
|
for d in dirs:
|
||
|
dname = os.path.join(root, d)
|
||
|
zf.write(dname, dname[removelen:])
|
||
|
for f in files:
|
||
|
fname = os.path.join(root, f)
|
||
|
zf.write(fname, fname[removelen:])
|
||
8 years ago
|
|
||
8 years ago
|
def del_gitfiles(dirname):
|
||
|
for f in glob(os.path.join(dirname, '.git*')):
|
||
|
if os.path.isdir(f) and not os.path.islink(f):
|
||
8 years ago
|
windows_proof_rmtree(f)
|
||
8 years ago
|
else:
|
||
|
os.unlink(f)
|
||
|
|
||
|
def process_submodules(dirname):
|
||
|
module_file = os.path.join(dirname, '.gitmodules')
|
||
|
if not os.path.exists(module_file):
|
||
|
return
|
||
6 years ago
|
subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'], cwd=dirname)
|
||
8 years ago
|
for line in open(module_file):
|
||
|
line = line.strip()
|
||
|
if '=' not in line:
|
||
|
continue
|
||
|
k, v = line.split('=', 1)
|
||
|
k = k.strip()
|
||
|
v = v.strip()
|
||
|
if k != 'path':
|
||
|
continue
|
||
|
del_gitfiles(os.path.join(dirname, v))
|
||
|
|
||
8 years ago
|
|
||
6 years ago
|
def run_dist_scripts(dist_root, dist_scripts):
|
||
|
assert(os.path.isabs(dist_root))
|
||
|
env = os.environ.copy()
|
||
|
env['MESON_DIST_ROOT'] = dist_root
|
||
|
for d in dist_scripts:
|
||
6 years ago
|
script = d['exe']
|
||
|
args = d['args']
|
||
|
name = ' '.join(script + args)
|
||
|
print('Running custom dist script {!r}'.format(name))
|
||
|
try:
|
||
|
rc = subprocess.call(script + args, env=env)
|
||
|
if rc != 0:
|
||
|
sys.exit('Dist script errored out')
|
||
|
except OSError:
|
||
|
print('Failed to run dist script {!r}'.format(name))
|
||
|
sys.exit(1)
|
||
6 years ago
|
|
||
6 years ago
|
|
||
|
def git_have_dirty_index(src_root):
|
||
|
'''Check whether there are uncommitted changes in git'''
|
||
|
ret = subprocess.call(['git', '-C', src_root, 'diff-index', '--quiet', 'HEAD'])
|
||
|
return ret == 1
|
||
|
|
||
6 years ago
|
def create_dist_git(dist_name, src_root, bld_root, dist_sub, dist_scripts):
|
||
6 years ago
|
if git_have_dirty_index(src_root):
|
||
|
mlog.warning('Repository has uncommitted changes that will not be included in the dist tarball')
|
||
8 years ago
|
distdir = os.path.join(dist_sub, dist_name)
|
||
|
if os.path.exists(distdir):
|
||
|
shutil.rmtree(distdir)
|
||
|
os.makedirs(distdir)
|
||
8 years ago
|
subprocess.check_call(['git', 'clone', '--shared', src_root, distdir])
|
||
|
process_submodules(distdir)
|
||
|
del_gitfiles(distdir)
|
||
6 years ago
|
run_dist_scripts(distdir, dist_scripts)
|
||
8 years ago
|
xzname = distdir + '.tar.xz'
|
||
8 years ago
|
# Should use shutil but it got xz support only in 3.5.
|
||
|
with tarfile.open(xzname, 'w:xz') as tf:
|
||
8 years ago
|
tf.add(distdir, dist_name)
|
||
8 years ago
|
# Create only .tar.xz for now.
|
||
8 years ago
|
# zipname = distdir + '.zip'
|
||
|
# create_zip(zipname, distdir)
|
||
8 years ago
|
shutil.rmtree(distdir)
|
||
8 years ago
|
return (xzname, )
|
||
8 years ago
|
|
||
8 years ago
|
|
||
6 years ago
|
def hg_have_dirty_index(src_root):
|
||
|
'''Check whether there are uncommitted changes in hg'''
|
||
|
out = subprocess.check_output(['hg', '-R', src_root, 'summary'])
|
||
|
return b'commit: (clean)' not in out
|
||
|
|
||
6 years ago
|
def create_dist_hg(dist_name, src_root, bld_root, dist_sub, dist_scripts):
|
||
6 years ago
|
if hg_have_dirty_index(src_root):
|
||
|
mlog.warning('Repository has uncommitted changes that will not be included in the dist tarball')
|
||
8 years ago
|
|
||
6 years ago
|
os.makedirs(dist_sub, exist_ok=True)
|
||
8 years ago
|
tarname = os.path.join(dist_sub, dist_name + '.tar')
|
||
|
xzname = tarname + '.xz'
|
||
|
subprocess.check_call(['hg', 'archive', '-R', src_root, '-S', '-t', 'tar', tarname])
|
||
6 years ago
|
if dist_scripts:
|
||
6 years ago
|
mlog.warning('dist scripts are not supported in Mercurial projects')
|
||
8 years ago
|
with lzma.open(xzname, 'wb') as xf, open(tarname, 'rb') as tf:
|
||
|
shutil.copyfileobj(tf, xf)
|
||
|
os.unlink(tarname)
|
||
|
# Create only .tar.xz for now.
|
||
|
# zipname = os.path.join(dist_sub, dist_name + '.zip')
|
||
|
# subprocess.check_call(['hg', 'archive', '-R', src_root, '-S', '-t', 'zip', zipname])
|
||
|
return (xzname, )
|
||
|
|
||
|
|
||
5 years ago
|
def check_dist(packagename, meson_command, privdir):
|
||
6 years ago
|
print('Testing distribution package %s' % packagename)
|
||
5 years ago
|
unpackdir = os.path.join(privdir, 'dist-unpack')
|
||
|
builddir = os.path.join(privdir, 'dist-build')
|
||
|
installdir = os.path.join(privdir, 'dist-install')
|
||
|
for p in (unpackdir, builddir, installdir):
|
||
|
if os.path.exists(p):
|
||
|
shutil.rmtree(p)
|
||
|
os.mkdir(p)
|
||
8 years ago
|
ninja_bin = detect_ninja()
|
||
|
try:
|
||
|
tf = tarfile.open(packagename)
|
||
|
tf.extractall(unpackdir)
|
||
|
srcdir = glob(os.path.join(unpackdir, '*'))[0]
|
||
8 years ago
|
if subprocess.call(meson_command + ['--backend=ninja', srcdir, builddir]) != 0:
|
||
8 years ago
|
print('Running Meson on distribution package failed')
|
||
|
return 1
|
||
|
if subprocess.call([ninja_bin], cwd=builddir) != 0:
|
||
6 years ago
|
print('Compiling the distribution package failed')
|
||
8 years ago
|
return 1
|
||
|
if subprocess.call([ninja_bin, 'test'], cwd=builddir) != 0:
|
||
6 years ago
|
print('Running unit tests on the distribution package failed')
|
||
8 years ago
|
return 1
|
||
|
myenv = os.environ.copy()
|
||
|
myenv['DESTDIR'] = installdir
|
||
|
if subprocess.call([ninja_bin, 'install'], cwd=builddir, env=myenv) != 0:
|
||
6 years ago
|
print('Installing the distribution package failed')
|
||
8 years ago
|
return 1
|
||
|
finally:
|
||
8 years ago
|
shutil.rmtree(unpackdir)
|
||
8 years ago
|
shutil.rmtree(builddir)
|
||
|
shutil.rmtree(installdir)
|
||
6 years ago
|
print('Distribution package %s tested' % packagename)
|
||
8 years ago
|
return 0
|
||
8 years ago
|
|
||
5 years ago
|
def run(opts):
|
||
|
b = build.load('.')
|
||
|
# This import must be load delayed, otherwise it will get the default
|
||
|
# value of None.
|
||
|
from mesonbuild.mesonlib import meson_command
|
||
|
src_root = b.environment.source_dir
|
||
|
bld_root = b.environment.build_dir
|
||
8 years ago
|
priv_dir = os.path.join(bld_root, 'meson-private')
|
||
|
dist_sub = os.path.join(bld_root, 'meson-dist')
|
||
|
|
||
5 years ago
|
dist_name = b.project_name + '-' + b.project_version
|
||
8 years ago
|
|
||
6 years ago
|
_git = os.path.join(src_root, '.git')
|
||
|
if os.path.isdir(_git) or os.path.isfile(_git):
|
||
5 years ago
|
names = create_dist_git(dist_name, src_root, bld_root, dist_sub, b.dist_scripts)
|
||
8 years ago
|
elif os.path.isdir(os.path.join(src_root, '.hg')):
|
||
5 years ago
|
names = create_dist_hg(dist_name, src_root, bld_root, dist_sub, b.dist_scripts)
|
||
8 years ago
|
else:
|
||
6 years ago
|
print('Dist currently only works with Git or Mercurial repos')
|
||
8 years ago
|
return 1
|
||
8 years ago
|
if names is None:
|
||
|
return 1
|
||
8 years ago
|
error_count = 0
|
||
|
for name in names:
|
||
5 years ago
|
rc = check_dist(name, meson_command, priv_dir) # Check only one.
|
||
8 years ago
|
if rc == 0:
|
||
|
create_hash(name)
|
||
|
error_count += rc
|
||
8 years ago
|
return 1 if error_count else 0
|