| 1 |
# Packaging coreutils |
| 2 |
|
| 3 |
<!-- spell-checker:ignore debuginfo manpages backtraces profdata profraw sysroot rustlib msvc Cprofile DESTDIR --> |
| 4 |
|
| 5 |
> **Note**: This page is intended as a guide for packaging the uutils coreutils |
| 6 |
> for package maintainers. Normal users probably do not need to read this. If you |
| 7 |
> just want to install the coreutils, look at the |
| 8 |
> [installation](installation.md) instructions. |
| 9 |
|
| 10 |
The maintainers of this project do not have the capacity to maintain packages |
| 11 |
for every distribution and package manager out there. Therefore, we encourage |
| 12 |
other people to package the uutils coreutils for their preferred distributions. |
| 13 |
You do not need to ask permission for this and you can do this however you want |
| 14 |
as long as you comply with the license. However, we do like to hear and |
| 15 |
advertise where the uutils coreutils are available, so please do let us know! |
| 16 |
|
| 17 |
## License |
| 18 |
|
| 19 |
The uutils coreutils are licensed under the MIT license. See the |
| 20 |
[LICENSE](https://github.com/uutils/coreutils/blob/main/LICENSE) for the full |
| 21 |
license text. Make sure to add attribution and the license text to the package |
| 22 |
to comply with the license. |
| 23 |
|
| 24 |
## Package |
| 25 |
|
| 26 |
We recommend to name the package `uutils-coreutils`. Just `uutils` is incorrect, |
| 27 |
because that is the name of the organization, which also includes other |
| 28 |
projects. |
| 29 |
|
| 30 |
## Selecting the utils to include |
| 31 |
|
| 32 |
Not all utils are available on all platforms. To get the full set of utils for a |
| 33 |
particular platform, you must enable the feature flag corresponding to the platform name. |
| 34 |
For example, on Unix-like system, use `--features unix` and `--features windows` |
| 35 |
on Windows. |
| 36 |
|
| 37 |
For a more fine-grained selection, you can enable just the features with the |
| 38 |
name of the utils you want to include and disable the default feature set. |
| 39 |
|
| 40 |
Additionally, support for SELinux must be explicitly enabled with the |
| 41 |
`feat_selinux` feature. |
| 42 |
|
| 43 |
We recommend including all the utilities that a platform supports. |
| 44 |
|
| 45 |
## Compilation parameters |
| 46 |
|
| 47 |
There are several compile-time flags that allow you to tune the coreutils to |
| 48 |
your particular needs. Some distributions, for example, might choose to |
| 49 |
minimize the binary size as much as possible. |
| 50 |
|
| 51 |
This can be achieved by customizing the configuration passed to cargo. You can |
| 52 |
view the full documentation in the |
| 53 |
[cargo documentation](https://doc.rust-lang.org/cargo/reference/profiles.html). |
| 54 |
|
| 55 |
We provide three release profiles out of the box, though you may want to tweak |
| 56 |
them: |
| 57 |
|
| 58 |
- `release`: The profile with all performance optimization enabled. |
| 59 |
- `release-small`: Optimize binary size. |
| 60 |
|
| 61 |
They include panic abort which removes stack traces on old rust [https://blog.rust-lang.org/2025/12/11/Rust-1.92.0/]. |
| 62 |
For the precise definition of these profiles, you can look at the root |
| 63 |
[`Cargo.toml`](https://github.com/uutils/coreutils/blob/main/Cargo.toml). |
| 64 |
|
| 65 |
The profiles above are just examples. We encourage package maintainers to decide |
| 66 |
for themselves what the best parameters for their distribution are. For example, |
| 67 |
a distribution focused on embedded systems would probably choose |
| 68 |
`release-small`, but another distribution focused on security might enable |
| 69 |
bounds checks. |
| 70 |
|
| 71 |
It is also possible to split the debuginfo into a separate package. See the |
| 72 |
[`split-debuginfo`](https://doc.rust-lang.org/cargo/reference/profiles.html#split-debuginfo) |
| 73 |
option in `cargo`. |
| 74 |
|
| 75 |
## Profile-Guided Optimization (PGO) |
| 76 |
|
| 77 |
The release binaries we publish are built with [Profile-Guided |
| 78 |
Optimization](https://doc.rust-lang.org/rustc/profile-guided-optimization.html) |
| 79 |
on every target whose build machine can run the binary it produces: Linux |
| 80 |
x86_64 and aarch64, macOS x86_64 and arm64, and Windows x86_64 and aarch64 |
| 81 |
(msvc). Packagers are encouraged to do the same: it costs nothing but build |
| 82 |
time and needs no source change. |
| 83 |
|
| 84 |
`util/build-pgo.sh` drives the whole process. It runs on Linux, macOS and |
| 85 |
Windows: |
| 86 |
|
| 87 |
```bash |
| 88 |
rustup component add llvm-tools # provides llvm-profdata |
| 89 |
./util/build-pgo.sh --features unix |
| 90 |
``` |
| 91 |
|
| 92 |
It performs the four usual PGO steps: |
| 93 |
|
| 94 |
1. build an instrumented multicall binary (`-Cprofile-generate`), |
| 95 |
2. run a set of representative workloads (sort, wc, cat, cut, hashing, cp/mv/ls, |
| 96 |
...) against a corpus it generates itself, |
| 97 |
3. merge the raw profiles with `llvm-profdata`, |
| 98 |
4. rebuild with `-Cprofile-use`, producing |
| 99 |
`target/coreutils-pgo/<target>/release/coreutils[.exe]`. |
| 100 |
|
| 101 |
Useful options: |
| 102 |
|
| 103 |
- `--features LIST` — feature set to build with (defaults to `unix`); pass the |
| 104 |
same value you use for the real package build (`feat_os_windows` on Windows). |
| 105 |
- `--target TRIPLE` — target to build for; defaults to the host. The |
| 106 |
instrumented binary still has to run on the build machine (see below). |
| 107 |
- `--target-dir DIR` — where the instrumented build, the corpus and the merged |
| 108 |
profile go (defaults to `target/coreutils-pgo`). |
| 109 |
- `--train-only` — stop after step 3, so you can feed the profile to your own |
| 110 |
build command. This is what our CI does: |
| 111 |
|
| 112 |
```bash |
| 113 |
./util/build-pgo.sh --features unix --train-only |
| 114 |
export RUSTFLAGS="${RUSTFLAGS:+$RUSTFLAGS }-Cprofile-use=$(cat target/coreutils-pgo/profdata-path.txt)" |
| 115 |
cargo build --release --features unix |
| 116 |
``` |
| 117 |
|
| 118 |
Use this if your build system already owns the final `cargo build` (extra |
| 119 |
rustflags, a different profile, a `DESTDIR` install step, ...). |
| 120 |
`profdata-path.txt` holds the absolute path of the merged profile, spelled the |
| 121 |
way the native toolchain expects it — on Windows that matters, since git-bash |
| 122 |
paths (`/d/a/...`) mean nothing to `rustc`. |
| 123 |
- `--llvm-profdata PATH` — explicit path to `llvm-profdata`. |
| 124 |
|
| 125 |
Things to know before wiring it into a package build: |
| 126 |
|
| 127 |
- **`llvm-profdata` must match the toolchain that instrumented the binary.** By |
| 128 |
default the script picks the one from the active `rustc` sysroot |
| 129 |
(`rustup component add llvm-tools`). A distribution `llvm-profdata` from a |
| 130 |
different LLVM major version will fail to read the profiles; point at the |
| 131 |
matching one with `--llvm-profdata`. |
| 132 |
- **Training runs the freshly built binary**, so the build machine must be able |
| 133 |
to execute it. That rules out emulator-less cross-builds (the script checks |
| 134 |
and fails early rather than producing an empty profile); a foreign target only |
| 135 |
works where the host can run it, such as x86_64 on an arm64 macOS with |
| 136 |
Rosetta. For a true cross-build, either skip PGO or train on the target |
| 137 |
architecture and carry the `.profdata` over. |
| 138 |
- **The corpus is generated from scratch** (no reading of `/etc/passwd`, |
| 139 |
`/usr/share/dict/words`, ...) so the profile does not depend on the contents |
| 140 |
of the build machine, and it uses no external tools beyond bash itself. The |
| 141 |
workloads do read this checkout's `src/uu` tree for the `cp`/`ls`/`du` |
| 142 |
training. |
| 143 |
- **For reproducible builds, freeze the profile.** The merged `.profdata` is an |
| 144 |
input to the final build, so re-training on another machine can change the |
| 145 |
resulting code layout. Generate it once, ship it as a source artifact, and |
| 146 |
build with `-Cprofile-use=<that file>` instead of re-running the training. |
| 147 |
- **Train with the same `lto` and `codegen-units` as the final build.** This is |
| 148 |
the easy way to get a profile that makes things *slower*. Inlining happens |
| 149 |
before instrumentation, so a training build with different settings records |
| 150 |
counters for a call graph the final build no longer has. Our `[profile.release]` |
| 151 |
uses `lto = "fat"` and `codegen-units = 1`, and the script trains with them; if |
| 152 |
you use `--train-only` and then run your own `cargo build`, keep those two |
| 153 |
values identical. When they did not match, `wc -w` came out **57% slower** than |
| 154 |
a plain non-PGO release build, while the profile itself looked perfectly |
| 155 |
healthy. |
| 156 |
- **A bad profile fails the build rather than silently degrading it**: the |
| 157 |
script refuses to continue if the merged profile covers fewer than 500 |
| 158 |
functions, which is what an environment where the training workloads did not |
| 159 |
actually run looks like. Note that this only catches a profile that is |
| 160 |
*missing*, not one that is *mismatched*: the mismatch above produced a profile |
| 161 |
covering more functions (4098) than the correct one (1883), since a non-LTO |
| 162 |
build still has all the symbols that whole-program codegen later merges away. |
| 163 |
If you change the training or build settings, measure the result. |
| 164 |
- It costs a second full build plus the training run, so expect the package |
| 165 |
build to take noticeably longer. On a 24-core x86_64 machine the full script |
| 166 |
takes about 3 minutes for the `unix` feature set, and the resulting binary is |
| 167 |
~0.8% larger. |
| 168 |
|
| 169 |
## Additional artifacts |
| 170 |
|
| 171 |
This project supports automatically generating manpages and shell completion |
| 172 |
files which you may want to include in the package. See the page on |
| 173 |
[building from source](build.md) for how to generate these. |