2019-11-19 08:56:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-04-20 13:45:12 +00:00
|
|
|
import hashlib
|
|
|
|
import re
|
2019-11-19 08:56:01 +00:00
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-06-08 12:03:47 +00:00
|
|
|
import shlex
|
2022-04-20 13:45:12 +00:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from pathlib import Path as P
|
|
|
|
|
|
|
|
PARSER = ArgumentParser()
|
|
|
|
PARSER.add_argument('command', choices=['build', 'test'])
|
|
|
|
PARSER.add_argument('build_dir', type=P)
|
|
|
|
PARSER.add_argument('src_dir', type=P)
|
|
|
|
PARSER.add_argument('root_dir', type=P)
|
|
|
|
PARSER.add_argument('target', choices=['release', 'debug'])
|
|
|
|
PARSER.add_argument('prefix', type=P)
|
|
|
|
PARSER.add_argument('libdir', type=P)
|
|
|
|
PARSER.add_argument('--version', default=None)
|
2022-10-20 17:10:43 +00:00
|
|
|
PARSER.add_argument('--bin', default=None, type=P)
|
2022-12-27 18:27:11 +00:00
|
|
|
PARSER.add_argument('--features', nargs="+", default=[])
|
2022-12-27 18:25:16 +00:00
|
|
|
PARSER.add_argument('--packages', nargs="+", default=[])
|
2022-12-27 18:27:11 +00:00
|
|
|
PARSER.add_argument('--examples', nargs="+", default=[])
|
2022-12-27 18:25:16 +00:00
|
|
|
PARSER.add_argument('--lib-suffixes', nargs="+", default=[])
|
|
|
|
PARSER.add_argument('--exe-suffix')
|
2022-04-20 13:45:12 +00:00
|
|
|
PARSER.add_argument('--depfile')
|
2022-08-25 22:30:08 +00:00
|
|
|
PARSER.add_argument('--disable-doc', action="store_true", default=False)
|
2022-04-20 13:45:12 +00:00
|
|
|
|
|
|
|
|
2023-08-01 05:54:12 +00:00
|
|
|
def shlex_join(args):
|
|
|
|
if hasattr(shlex, 'join'):
|
|
|
|
return shlex.join(args)
|
|
|
|
return ' '.join([shlex.quote(arg) for arg in args])
|
|
|
|
|
|
|
|
|
2022-10-20 17:10:43 +00:00
|
|
|
def generate_depfile_for(fpath):
|
|
|
|
file_stem = fpath.parent / fpath.stem
|
2022-04-20 13:45:12 +00:00
|
|
|
depfile_content = ""
|
|
|
|
with open(f"{file_stem}.d", 'r') as depfile:
|
|
|
|
for l in depfile.readlines():
|
|
|
|
if l.startswith(str(file_stem)):
|
2022-12-19 11:00:30 +00:00
|
|
|
# We can't blindly split on `:` because on Windows that's part
|
|
|
|
# of the drive letter. Lucky for us, the format of the dep file
|
|
|
|
# is one of:
|
|
|
|
#
|
|
|
|
# /path/to/output: /path/to/src1 /path/to/src2
|
|
|
|
# /path/to/output:
|
|
|
|
#
|
|
|
|
# So we parse these two cases specifically
|
|
|
|
if l.endswith(':'):
|
|
|
|
output = l[:-1]
|
|
|
|
srcs = ''
|
|
|
|
else:
|
|
|
|
output, srcs = l.split(": ", maxsplit=2)
|
2022-04-20 13:45:12 +00:00
|
|
|
|
|
|
|
all_deps = []
|
|
|
|
for src in srcs.split(" "):
|
|
|
|
all_deps.append(src)
|
|
|
|
src = P(src)
|
|
|
|
if src.name == 'lib.rs':
|
|
|
|
# `rustc` doesn't take `Cargo.toml` into account
|
|
|
|
# but we need to
|
|
|
|
cargo_toml = src.parent.parent / 'Cargo.toml'
|
|
|
|
if cargo_toml.exists():
|
|
|
|
all_deps.append(str(cargo_toml))
|
|
|
|
|
|
|
|
depfile_content += f"{output}: {' '.join(all_deps)}\n"
|
|
|
|
|
|
|
|
return depfile_content
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
opts = PARSER.parse_args()
|
2022-12-28 22:19:44 +00:00
|
|
|
logdir = opts.root_dir / 'meson-logs'
|
|
|
|
logfile_path = logdir / f'{opts.src_dir.name}-cargo-wrapper.log'
|
|
|
|
logfile = open(logfile_path, mode='w', buffering=1)
|
2022-04-20 13:45:12 +00:00
|
|
|
|
|
|
|
print(opts, file=logfile)
|
|
|
|
cargo_target_dir = opts.build_dir / 'target'
|
|
|
|
|
|
|
|
env = os.environ.copy()
|
2022-12-28 13:40:22 +00:00
|
|
|
if 'PKG_CONFIG_PATH' in env:
|
|
|
|
pkg_config_path = env['PKG_CONFIG_PATH'].split(os.pathsep)
|
|
|
|
else:
|
|
|
|
pkg_config_path = []
|
2022-04-20 13:45:12 +00:00
|
|
|
pkg_config_path.append(str(opts.root_dir / 'meson-uninstalled'))
|
2022-12-19 11:01:10 +00:00
|
|
|
env['PKG_CONFIG_PATH'] = os.pathsep.join(pkg_config_path)
|
2022-04-20 13:45:12 +00:00
|
|
|
|
2024-01-26 17:33:16 +00:00
|
|
|
if 'NASM' in env:
|
|
|
|
env['PATH'] = os.pathsep.join([os.path.dirname(env['NASM']), env['PATH']])
|
|
|
|
|
2023-04-21 14:50:22 +00:00
|
|
|
rustc_target = None
|
2023-06-08 12:03:47 +00:00
|
|
|
if 'RUSTC' in env:
|
|
|
|
rustc_cmdline = shlex.split(env['RUSTC'])
|
2023-04-21 14:50:22 +00:00
|
|
|
# grab target from RUSTFLAGS
|
2023-06-08 12:03:47 +00:00
|
|
|
rust_flags = rustc_cmdline[1:] + shlex.split(env.get('RUSTFLAGS', ''))
|
2023-04-21 14:50:22 +00:00
|
|
|
if '--target' in rust_flags:
|
|
|
|
rustc_target_idx = rust_flags.index('--target')
|
|
|
|
_ = rust_flags.pop(rustc_target_idx) # drop '--target'
|
|
|
|
rustc_target = rust_flags.pop(rustc_target_idx)
|
2023-08-01 05:54:12 +00:00
|
|
|
env['RUSTFLAGS'] = shlex_join(rust_flags)
|
2023-06-08 12:03:47 +00:00
|
|
|
env['RUSTC'] = rustc_cmdline[0]
|
2023-04-21 14:50:22 +00:00
|
|
|
|
2022-12-27 18:27:11 +00:00
|
|
|
features = opts.features
|
2022-04-20 13:45:12 +00:00
|
|
|
if opts.command == 'build':
|
2022-10-20 17:10:43 +00:00
|
|
|
cargo_cmd = ['cargo']
|
2022-12-27 18:27:11 +00:00
|
|
|
if opts.bin or opts.examples:
|
2022-10-20 17:10:43 +00:00
|
|
|
cargo_cmd += ['build']
|
|
|
|
else:
|
|
|
|
cargo_cmd += ['cbuild']
|
|
|
|
if not opts.disable_doc:
|
2022-12-27 18:27:11 +00:00
|
|
|
features += ['doc']
|
2022-04-20 13:45:12 +00:00
|
|
|
if opts.target == 'release':
|
|
|
|
cargo_cmd.append('--release')
|
|
|
|
elif opts.command == 'test':
|
|
|
|
# cargo test
|
|
|
|
cargo_cmd = ['cargo', 'ctest', '--no-fail-fast', '--color=always']
|
|
|
|
else:
|
|
|
|
print("Unknown command:", opts.command, file=logfile)
|
2020-11-26 15:12:42 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2023-04-21 14:50:22 +00:00
|
|
|
if rustc_target:
|
|
|
|
cargo_cmd += ['--target', rustc_target]
|
2022-12-27 18:27:11 +00:00
|
|
|
if features:
|
|
|
|
cargo_cmd += ['--features', ','.join(features)]
|
2022-12-28 13:40:22 +00:00
|
|
|
cargo_cmd += ['--target-dir', cargo_target_dir]
|
2022-12-27 18:27:11 +00:00
|
|
|
cargo_cmd += ['--manifest-path', opts.src_dir / 'Cargo.toml']
|
|
|
|
if opts.bin:
|
|
|
|
cargo_cmd.extend(['--bin', opts.bin.name])
|
|
|
|
else:
|
|
|
|
if not opts.examples:
|
|
|
|
cargo_cmd.extend(['--prefix', opts.prefix, '--libdir',
|
|
|
|
opts.prefix / opts.libdir])
|
2022-12-27 18:25:16 +00:00
|
|
|
for p in opts.packages:
|
2022-10-20 17:10:43 +00:00
|
|
|
cargo_cmd.extend(['-p', p])
|
2022-12-27 18:27:11 +00:00
|
|
|
for e in opts.examples:
|
|
|
|
cargo_cmd.extend(['--example', e])
|
2022-04-20 13:45:12 +00:00
|
|
|
|
2022-12-27 18:27:11 +00:00
|
|
|
def run(cargo_cmd, env):
|
|
|
|
print(cargo_cmd, env, file=logfile)
|
2022-04-20 13:45:12 +00:00
|
|
|
try:
|
2022-12-27 18:27:11 +00:00
|
|
|
subprocess.run(cargo_cmd, env=env, cwd=opts.src_dir, check=True)
|
2022-04-20 13:45:12 +00:00
|
|
|
except subprocess.SubprocessError:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-12-27 18:27:11 +00:00
|
|
|
run(cargo_cmd, env)
|
2022-04-20 13:45:12 +00:00
|
|
|
|
|
|
|
if opts.command == 'build':
|
|
|
|
target_dir = cargo_target_dir / '**' / opts.target
|
2022-10-20 17:10:43 +00:00
|
|
|
if opts.bin:
|
2022-12-27 18:25:16 +00:00
|
|
|
exe = glob.glob(str(target_dir / opts.bin) + opts.exe_suffix, recursive=True)[0]
|
2022-10-20 17:10:43 +00:00
|
|
|
shutil.copy2(exe, opts.build_dir)
|
|
|
|
depfile_content = generate_depfile_for(P(exe))
|
|
|
|
else:
|
|
|
|
# Copy so files to build dir
|
|
|
|
depfile_content = ""
|
2022-12-27 18:25:16 +00:00
|
|
|
for suffix in opts.lib_suffixes:
|
|
|
|
for f in glob.glob(str(target_dir / f'*.{suffix}'), recursive=True):
|
2022-10-20 17:10:43 +00:00
|
|
|
libfile = P(f)
|
|
|
|
|
|
|
|
depfile_content += generate_depfile_for(libfile)
|
|
|
|
|
|
|
|
copied_file = (opts.build_dir / libfile.name)
|
|
|
|
try:
|
|
|
|
if copied_file.stat().st_mtime == libfile.stat().st_mtime:
|
|
|
|
print(f"{copied_file} has not changed.", file=logfile)
|
|
|
|
continue
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
print(f"Copying {copied_file}", file=logfile)
|
|
|
|
shutil.copy2(f, opts.build_dir)
|
2022-12-27 18:27:11 +00:00
|
|
|
# Copy examples to builddir
|
|
|
|
for example in opts.examples:
|
|
|
|
example_glob = str(target_dir / 'examples' / example) + opts.exe_suffix
|
|
|
|
exe = glob.glob(example_glob, recursive=True)[0]
|
|
|
|
shutil.copy2(exe, opts.build_dir)
|
|
|
|
depfile_content += generate_depfile_for(P(exe))
|
2022-04-20 13:45:12 +00:00
|
|
|
|
|
|
|
with open(opts.depfile, 'w') as depfile:
|
|
|
|
depfile.write(depfile_content)
|
|
|
|
|
|
|
|
# Copy generated pkg-config files
|
|
|
|
for f in glob.glob(str(target_dir / '*.pc'), recursive=True):
|
|
|
|
shutil.copy(f, opts.build_dir)
|
|
|
|
|
|
|
|
# Move -uninstalled.pc to meson-uninstalled
|
|
|
|
uninstalled = opts.build_dir / 'meson-uninstalled'
|
|
|
|
os.makedirs(uninstalled, exist_ok=True)
|
|
|
|
|
|
|
|
for f in opts.build_dir.glob('*-uninstalled.pc'):
|
|
|
|
# move() does not allow us to update the file so remove it if it already exists
|
|
|
|
dest = uninstalled / P(f).name
|
|
|
|
if dest.exists():
|
|
|
|
dest.unlink()
|
2022-05-27 19:34:35 +00:00
|
|
|
# move() takes paths from Python3.9 on
|
|
|
|
if ((sys.version_info.major >= 3) and (sys.version_info.minor >= 9)):
|
|
|
|
shutil.move(f, uninstalled)
|
|
|
|
else:
|
|
|
|
shutil.move(str(f), str(uninstalled))
|
|
|
|
|