#!/usr/bin/env python3 # ################################################################ # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under both the BSD-style license (found in the # LICENSE file in the root directory of this source tree) and the GPLv2 (found # in the COPYING file in the root directory of this source tree). # You may select, at your option, one of the above-listed licenses. # ################################################################ """Build Cargo's staticlib and publish it at a Meson custom-target output.""" from __future__ import print_function import argparse import filecmp import os from pathlib import Path import shutil import subprocess import sys def copy_if_changed(source, destination): if destination.exists() and filecmp.cmp(str(source), str(destination), shallow=False): return temporary = destination.with_name(destination.name + '.tmp') shutil.copyfile(str(source), str(temporary)) os.replace(str(temporary), str(destination)) def main(): parser = argparse.ArgumentParser() parser.add_argument('--cargo', required=True) parser.add_argument('--manifest-path', required=True) parser.add_argument('--target-dir', required=True) parser.add_argument('--staticlib-name', required=True) parser.add_argument('--output', required=True) parser.add_argument('--features', default='') parser.add_argument('--target', default='') args = parser.parse_args() command = [ args.cargo, 'build', '--manifest-path', args.manifest_path, '--release', '--target-dir', args.target_dir, '--no-default-features', ] if args.features: command.extend(['--features', args.features]) if args.target: command.extend(['--target', args.target]) subprocess.check_call(command) artifact = Path(args.target_dir) if args.target: artifact /= args.target artifact /= 'release' artifact /= args.staticlib_name if not artifact.is_file(): print('Cargo did not produce {}'.format(artifact), file=sys.stderr) return 1 output = Path(args.output) output.parent.mkdir(parents=True, exist_ok=True) copy_if_changed(artifact, output) return 0 if __name__ == '__main__': sys.exit(main())