Description: build-pgo.sh: let the caller supply the instrumented binary The script always builds its own instrumented binary in step 1, with the feature set given by --features (defaulting to "unix") and an explicit --target. The Debian build uses neither: it builds the multicall binary through the GNUmakefile with --no-default-features, an explicit list of utilities, feat_systemd_logind, feat_external_libstdbuf, openssl and selinux. . A profile is only usable by a build that matches the one it was collected from: with a different feature set the symbols and CFG hashes differ, so -Cprofile-use silently finds no data for most functions and PGO becomes a no-op. Add --instrumented-binary so the caller can build that binary with its own flags and let the script do the training, merging and the sanity check. Author: Sylvestre Ledru Forwarded: https://github.com/uutils/coreutils Last-Update: 2026-09-01 --- a/util/build-pgo.sh +++ b/util/build-pgo.sh @@ -13,6 +13,13 @@ # Usage: # util/build-pgo.sh [--target TRIPLE] [--target-dir DIR] [--features LIST] # [--train-only] [--llvm-profdata PATH] +# [--instrumented-binary PATH] +# +# --instrumented-binary skips step 1 and trains the binary given instead. Use it +# when the caller already builds the binary with its own flags (a distribution +# package, say): the profile is only usable by a build that matches the one it +# was collected from, so the instrumented and final builds must be produced by +# the same command, with the same features and profile settings. set -euo pipefail @@ -30,6 +37,7 @@ FEATURES="unix" TARGET="" TRAIN_ONLY=0 LLVM_PROFDATA="" +INSTRUMENTED_BINARY="" while [ $# -gt 0 ]; do case "$1" in @@ -37,8 +45,9 @@ while [ $# -gt 0 ]; do --target-dir) TARGET_DIR="$2"; shift 2 ;; --features) FEATURES="$2"; shift 2 ;; --llvm-profdata) LLVM_PROFDATA="$2"; shift 2 ;; + --instrumented-binary) INSTRUMENTED_BINARY="$2"; shift 2 ;; --train-only) TRAIN_ONLY=1; shift ;; - -h|--help) sed -n '3,16p' "${BASH_SOURCE[0]}"; exit 0 ;; + -h|--help) sed -n '3,22p' "${BASH_SOURCE[0]}"; exit 0 ;; *) echo "unknown argument: $1" >&2; exit 2 ;; esac done @@ -113,9 +122,14 @@ mkdir -p "$PROFILE_DIR" # it always leaves through `std::process::exit`, which on Windows is # `ExitProcess` and skips the `atexit` handler the profiling runtime would # otherwise write the profile from. -cargo_build "$INSTR_DIR" "-Cprofile-generate=${PROFILE_DIR} --cfg pgo_training" - -BIN="${INSTR_DIR}/${TARGET}/release/coreutils${EXE}" +if [ -n "$INSTRUMENTED_BINARY" ]; then + echo "using the caller's instrumented binary: ${INSTRUMENTED_BINARY}" + echo "(it must have been built with -Cprofile-generate=${PROFILE_DIR} --cfg pgo_training)" + BIN="$INSTRUMENTED_BINARY" +else + cargo_build "$INSTR_DIR" "-Cprofile-generate=${PROFILE_DIR} --cfg pgo_training" + BIN="${INSTR_DIR}/${TARGET}/release/coreutils${EXE}" +fi [ -x "$BIN" ] || { echo "instrumented binary not found: ${BIN}" >&2; exit 1; } # Training runs the binary we just built, so a foreign target only works where # the host can execute it (x86_64 on arm64 macOS needs Rosetta, for instance).