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