| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from argparse import ArgumentParser |
| 4 |
import os |
| 5 |
from pathlib import Path |
| 6 |
import shutil |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
|
| 10 |
parser = ArgumentParser("Cargo wrapper") |
| 11 |
|
| 12 |
parser.add_argument( |
| 13 |
"--command", |
| 14 |
required=True, |
| 15 |
choices=["cbuild", "test", "build"], |
| 16 |
help="Cargo command", |
| 17 |
) |
| 18 |
|
| 19 |
parser.add_argument( |
| 20 |
"--cargo", required=True, type=Path, help="Path to the cargo executable" |
| 21 |
) |
| 22 |
|
| 23 |
parser.add_argument( |
| 24 |
"--manifest-path", required=True, type=Path, help="Path to Cargo.toml" |
| 25 |
) |
| 26 |
|
| 27 |
parser.add_argument( |
| 28 |
"--current-build-dir", |
| 29 |
required=True, |
| 30 |
type=Path, |
| 31 |
help="Value from meson.current_build_dir()", |
| 32 |
) |
| 33 |
|
| 34 |
parser.add_argument( |
| 35 |
"--current-source-dir", |
| 36 |
required=True, |
| 37 |
type=Path, |
| 38 |
help="Value from meson.current_source_dir()", |
| 39 |
) |
| 40 |
|
| 41 |
parser.add_argument( |
| 42 |
"--project-build-root", |
| 43 |
required=True, |
| 44 |
type=Path, |
| 45 |
help="Value from meson.project_build_root()", |
| 46 |
) |
| 47 |
|
| 48 |
parser.add_argument( |
| 49 |
"--toolchain-version", help="Rust Toolchain Version if needed" |
| 50 |
) |
| 51 |
|
| 52 |
parser.add_argument( |
| 53 |
"--target", help="Target triplet" |
| 54 |
) |
| 55 |
|
| 56 |
parser.add_argument( |
| 57 |
"--build-triplet", help="Build toolchain triplet (for cross builds using specific toolchain version)" |
| 58 |
) |
| 59 |
|
| 60 |
parser.add_argument( |
| 61 |
"--avif", action="store_true", help="Enable AVIF support" |
| 62 |
) |
| 63 |
|
| 64 |
parser.add_argument( |
| 65 |
"--pixbuf", action="store_true", help="Enable GDK-Pixbuf support" |
| 66 |
) |
| 67 |
|
| 68 |
parser.add_argument( |
| 69 |
"--packages", |
| 70 |
nargs="*", |
| 71 |
default=[], |
| 72 |
help='Rust packages to build (names for "cargo cbuild/build -p")', |
| 73 |
) |
| 74 |
|
| 75 |
parser.add_argument( |
| 76 |
"--prefix", type=Path, required=True, help="Value of get_option('prefix')" |
| 77 |
) |
| 78 |
|
| 79 |
parser.add_argument("--libdir", required=True, help="Value of get_option('libdir')") |
| 80 |
|
| 81 |
g = parser.add_argument_group("Outputs") |
| 82 |
group = parser.add_mutually_exclusive_group(required=False) |
| 83 |
group.add_argument( |
| 84 |
"--extension", help="filename extension for the library (so, a, dll, lib, dylib)", |
| 85 |
) |
| 86 |
group.add_argument("--bin", help="Name of binary to build") |
| 87 |
|
| 88 |
g = parser.add_argument_group("Optimizations") |
| 89 |
g.add_argument( |
| 90 |
"--release", action="store_true", help="Build artifacts in release mode" |
| 91 |
) |
| 92 |
g.add_argument( |
| 93 |
'--optimization', choices=['0', '1', '2', '3', 's'], help="Set optimization level" |
| 94 |
) |
| 95 |
g.add_argument( |
| 96 |
'--lto', choices=['fat', 'thin'], help="Set optimization level" |
| 97 |
) |
| 98 |
|
| 99 |
args = parser.parse_args() |
| 100 |
|
| 101 |
if args.toolchain_version is not None and args.target is None and args.build_triplet is None: |
| 102 |
raise ValueError('--target and/or --build-triplet argument required if --toolchain-version is specified') |
| 103 |
|
| 104 |
if args.command == 'test': |
| 105 |
if args.extension or args.bin: |
| 106 |
raise ValueError('Cargo test does not take --extension or --bin') |
| 107 |
|
| 108 |
cargo_target_dir = Path(args.project_build_root) / "target" |
| 109 |
|
| 110 |
# The final rsvg-convert executable will be found in cargo_target_dir/$(TARGET_TRIPLET) |
| 111 |
# if a target triplet is specified |
| 112 |
if args.target: |
| 113 |
cargo_target_output_dir = cargo_target_dir / args.target |
| 114 |
else: |
| 115 |
cargo_target_output_dir = cargo_target_dir |
| 116 |
|
| 117 |
env = os.environ.copy() |
| 118 |
pkg_config_path = [i for i in env.get("PKG_CONFIG_PATH", "").split(os.pathsep) if i] |
| 119 |
pkg_config_path.insert( |
| 120 |
0, (Path(args.project_build_root) / "meson-uninstalled").as_posix() |
| 121 |
) |
| 122 |
env["PKG_CONFIG_PATH"] = os.pathsep.join(pkg_config_path) |
| 123 |
|
| 124 |
features = [] |
| 125 |
|
| 126 |
if args.avif: |
| 127 |
features.append('avif') |
| 128 |
|
| 129 |
if args.pixbuf: |
| 130 |
features.append('pixbuf') |
| 131 |
|
| 132 |
cargo_prefixes = [ |
| 133 |
"--prefix", |
| 134 |
Path(args.prefix).as_posix(), |
| 135 |
"--libdir", |
| 136 |
(Path(args.prefix) / args.libdir).as_posix(), |
| 137 |
] |
| 138 |
|
| 139 |
cargo_cmd = [Path(args.cargo).as_posix()] |
| 140 |
|
| 141 |
if args.toolchain_version is not None: |
| 142 |
if args.build_triplet is not None: |
| 143 |
cargo_cmd.extend(["+%s-%s" % (args.toolchain_version, args.build_triplet)]) |
| 144 |
else: |
| 145 |
cargo_cmd.extend(["+%s-%s" % (args.toolchain_version, args.target)]) |
| 146 |
|
| 147 |
if args.command == "cbuild": |
| 148 |
cargo_cmd.extend(["cbuild", "--locked"]) |
| 149 |
library_type = "staticlib" if args.extension in ("a", "lib") else "cdylib" |
| 150 |
cargo_cmd.extend(cargo_prefixes) |
| 151 |
cargo_cmd.extend(["--library-type", library_type]) |
| 152 |
elif args.command == "test": |
| 153 |
cargo_cmd.extend(["test", "--locked", "--no-fail-fast", "--color=always"]) |
| 154 |
if 'librsvg' in args.packages: |
| 155 |
# These are required for librsvg itself |
| 156 |
# If doing an unqualified cargo build, they'll be called up |
| 157 |
# by rsvg-convert |
| 158 |
# https://github.com/rust-lang/cargo/issues/2911 |
| 159 |
features.extend(["capi", "test-utils"]) |
| 160 |
else: |
| 161 |
cargo_cmd.extend(["build", "--locked"]) |
| 162 |
if args.bin: |
| 163 |
cargo_cmd.extend(["--bin", args.bin]) |
| 164 |
|
| 165 |
cargo_cmd.extend(["--manifest-path", Path(args.manifest_path).as_posix()]) |
| 166 |
cargo_cmd.extend(["--target-dir", cargo_target_dir.as_posix()]) |
| 167 |
|
| 168 |
if args.release: |
| 169 |
buildtype = 'release' |
| 170 |
cargo_cmd.extend(['--release']) |
| 171 |
else: |
| 172 |
buildtype = 'debug' |
| 173 |
|
| 174 |
if args.optimization: |
| 175 |
env[f'CARGO_PROFILE_{buildtype.upper()}_OPT_LEVEL'] = args.optimization |
| 176 |
if args.lto: |
| 177 |
env[f'CARGO_PROFILE_{buildtype.upper()}_CODEGEN_UNITS'] = '1' |
| 178 |
env[f'CARGO_PROFILE_{buildtype.upper()}_LTO'] = args.lto |
| 179 |
|
| 180 |
if args.target: |
| 181 |
cargo_cmd.extend(['--target', args.target]) |
| 182 |
|
| 183 |
if features: |
| 184 |
cargo_cmd.extend(["--features", ",".join(features)]) |
| 185 |
|
| 186 |
for p in args.packages: |
| 187 |
cargo_cmd.extend(["-p", p]) |
| 188 |
|
| 189 |
if args.command == "test": |
| 190 |
cargo_cmd.extend(["--", "--include-ignored"]) |
| 191 |
|
| 192 |
k = {k: v for k, v in env.items() if k.startswith('CARGO_PROFILE')} |
| 193 |
print(f"command: {cargo_cmd}, env: {k}") |
| 194 |
subprocess.run(cargo_cmd, env=env, check=True) |
| 195 |
|
| 196 |
if args.command in ["cbuild", "build"]: |
| 197 |
# Copy so/dll/etc files to build dir |
| 198 |
if args.extension: |
| 199 |
for f in cargo_target_dir.glob(f"**/{buildtype}/*.{args.extension}"): |
| 200 |
shutil.copy(f, args.current_build_dir) |
| 201 |
# Copy binary and, if applicable, the corresponding .pdb file, to build dir |
| 202 |
else: |
| 203 |
binary = Path(cargo_target_output_dir / buildtype / args.bin) |
| 204 |
is_windows_target = False |
| 205 |
if args.target is None and sys.platform == "win32": |
| 206 |
is_windows_target = True |
| 207 |
elif args.target is not None and args.target.split('-')[2] == 'windows': |
| 208 |
is_windows_target = True |
| 209 |
if is_windows_target: |
| 210 |
exe_name = args.bin.replace('rsvg-convert', 'rsvg_convert') |
| 211 |
pdb_src = Path(cargo_target_output_dir / buildtype / exe_name).with_suffix(".pdb") |
| 212 |
pdb_dest = Path(args.current_build_dir / args.bin).with_suffix('.pdb') |
| 213 |
if pdb_src.exists(): |
| 214 |
shutil.copy(pdb_src, pdb_dest) |
| 215 |
binary = binary.with_suffix(".exe") |
| 216 |
shutil.copy(binary, args.current_build_dir) |