Debian Codegraph @

DEVELOPMENT.md

raw
1 <!-- spell-checker:ignore (flags) Ccodegen Coverflow Cpanic Cinstrument Zpanic reimplementing toybox RUNTEST CARGOFLAGS nextest prereq autopoint gettext texinfo automake findutils shellenv libexec gnubin toolchains gsed -->
2
3 # Setting up your local development environment
4
5 For contributing rules and best practices please refer to [CONTRIBUTING.md](CONTRIBUTING.md)
6
7 ## Before you start
8
9 For this guide we assume that you already have a GitHub account and have `git` and your favorite code editor or IDE installed and configured.
10 Before you start working on coreutils, please follow these steps:
11
12 1. Fork the [coreutils repository](https://github.com/uutils/coreutils) to your GitHub account.
13 ***Tip:*** See [this GitHub guide](https://docs.github.com/en/get-started/quickstart/fork-a-repo) for more information on this step.
14 2. Clone that fork to your local development environment:
15
16 ```shell
17 git clone https://github.com/YOUR-GITHUB-ACCOUNT/coreutils
18 cd coreutils
19 ```
20
21 ## Tools
22
23 You will need the tools mentioned in this section to build and test your code changes locally.
24 This section will explain how to install and configure these tools.
25 We also have an extensive CI that uses these tools and will check your code before it can be merged.
26 The next section [Testing](#testing) will explain how to run those checks locally to avoid waiting for the CI.
27
28 As an alternative to host installation of the tools, you can open the project with the provided development container configuration.
29 For more information about development containers, see the [Visual Studio Code Dev Containers documentation](https://code.visualstudio.com/docs/devcontainers/containers).
30
31 ### Rust toolchain
32
33 [Install Rust](https://www.rust-lang.org/tools/install)
34
35 If you're using rustup to install and manage your Rust toolchains, `clippy` and `rustfmt` are usually already installed. If you are using one of the alternative methods, please make sure to install them manually. See following sub-sections for their usage: [clippy](#clippy) [rustfmt](#rustfmt).
36
37 ***Tip*** You might also need to add 'llvm-tools' component if you are going to [generate code coverage reports locally](#code-coverage-report):
38
39 ```shell
40 rustup component add llvm-tools-preview
41 ```
42
43 ### GNU utils and prerequisites
44
45 If you are developing on Linux, most likely you already have all/most GNU utilities and prerequisites installed.
46
47 To make sure, please check GNU coreutils [README-prereq](https://github.com/coreutils/coreutils/blob/master/README-prereq).
48
49 You will need these to [run uutils against the GNU test suite locally](#comparing-with-gnu).
50
51 For MacOS and Windows platform specific setup please check [MacOS GNU utils](#macos-gnu-utils) and [Windows GNU utils](#windows-gnu-utils) sections respectfully.
52
53 ### pre-commit hooks
54
55 A configuration for `pre-commit` is provided in the repository. It allows
56 automatically checking every git commit you make to ensure it compiles, and
57 passes `clippy` and `rustfmt` without warnings.
58
59 To use the provided hook:
60
61 1. [Install `pre-commit`](https://pre-commit.com/#install)
62 1. Run `pre-commit install` while in the repository directory
63
64 Your git commits will then automatically be checked. If a check fails, an error
65 message will explain why, and your commit will be canceled. You can then make
66 the suggested changes, and run `git commit ...` again.
67
68 ### clippy
69
70 ```shell
71 cargo clippy --workspace --all-targets --all-features
72 ```
73
74 ### rustfmt
75
76 ```shell
77 cargo fmt --all
78 ```
79
80 ### cargo-deny
81
82 This project uses [cargo-deny](https://github.com/EmbarkStudios/cargo-deny/) to
83 detect duplicate dependencies, checks licenses, etc. To run it locally, first
84 install it and then run with:
85
86 ```shell
87 cargo deny --all-features check all
88 ```
89
90 ### Markdown linter
91
92 We use [markdownlint](https://github.com/DavidAnson/markdownlint) to lint the
93 Markdown files in the repository.
94
95 ### Spell checker
96
97 We use `cspell` as spell checker for all files in the project. If you are using
98 VS Code, you can install the
99 [code spell checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker)
100 extension to enable spell checking within your editor. Otherwise, you can
101 install [cspell](https://cspell.org/) separately.
102
103 If you want to make the spell checker ignore a word, you can add
104
105 ```rust
106 // spell-checker:ignore word_to_ignore
107 ```
108
109 at the top of the file.
110
111 ## Testing
112
113 This section explains how to run our CI checks locally.
114 Testing can be done using either Cargo or `make`.
115
116 ### Testing with Cargo
117
118 Just like with building, we follow the standard procedure for testing using
119 Cargo:
120
121 ```shell
122 cargo test
123 ```
124
125 By default, `cargo test` only runs the common programs. To run also platform
126 specific tests, run:
127
128 ```shell
129 cargo test --features unix
130 ```
131
132 If you would prefer to test a select few utilities:
133
134 ```shell
135 cargo test --features "chmod mv tail" --no-default-features
136 ```
137
138 If you also want to test the core utilities:
139
140 ```shell
141 cargo test -p uucore -p coreutils
142 # or
143 cargo test --all-features -p uucore
144 ```
145
146 Running the complete test suite might take a while. We use [nextest](https://nexte.st/index.html) in
147 the CI and you might want to try it out locally. It can speed up the execution time of the whole
148 test run significantly if the cpu has multiple cores.
149
150 ```shell
151 cargo nextest run --features unix --no-fail-fast
152 ```
153
154 To debug:
155
156 ```shell
157 rust-gdb --args target/debug/coreutils ls
158 (gdb) b ls.rs:79
159 (gdb) run
160 ```
161
162 ### Testing with GNU Make
163
164 To simply test all available utilities:
165
166 ```shell
167 make test
168 ```
169
170 To test all but a few of the available utilities:
171
172 ```shell
173 make SKIP_UTILS='UTILITY_1 UTILITY_2' test
174 ```
175
176 To test only a few of the available utilities:
177
178 ```shell
179 make UTILS='UTILITY_1 UTILITY_2' test
180 ```
181
182 To include tests for unimplemented behavior:
183
184 ```shell
185 make UTILS='UTILITY_1 UTILITY_2' SPEC=y test
186 ```
187
188 To run tests with `nextest` just use the nextest target. Note you'll need to
189 [install](https://nexte.st/book/installation.html) `nextest` first. The `nextest` target accepts the
190 same arguments like the default `test` target, so it's possible to pass arguments to `nextest run`
191 via `CARGOFLAGS`:
192
193 ```shell
194 make CARGOFLAGS='--no-fail-fast' UTILS='UTILITY_1 UTILITY_2' nextest
195 ```
196
197 ### Run Busybox Tests
198
199 This testing functionality is only available on Unix-like operating systems and
200 requires `make`.
201
202 To run busybox tests for all utilities for which busybox has tests
203
204 ```shell
205 make busytest
206 ```
207
208 To run busybox tests for a few of the available utilities
209
210 ```shell
211 make UTILS='UTILITY_1 UTILITY_2' busytest
212 ```
213
214 To pass an argument like "-v" to the busybox test runtime
215
216 ```shell
217 make UTILS='UTILITY_1 UTILITY_2' RUNTEST_ARGS='-v' busytest
218 ```
219
220 ### Comparing with GNU
221
222 To run uutils against the GNU test suite locally, run the following commands:
223
224 ```shell
225 bash util/build-gnu.sh
226 # Build uutils with release optimizations
227 env PROFILE=release bash util/build-gnu.sh
228 # Build uutils with SELinux
229 env SELINUX_ENABLED=1 bash util/build-gnu.sh
230 bash util/run-gnu-test.sh
231 # To run a single test:
232 bash util/run-gnu-test.sh tests/touch/not-owner.sh # for example
233 # To run several tests:
234 bash util/run-gnu-test.sh tests/touch/not-owner.sh tests/rm/no-give-up.sh # for example
235 # If this is a perl (.pl) test, to run in debug:
236 DEBUG=1 bash util/run-gnu-test.sh tests/misc/sm3sum.pl
237 ```
238
239 ***Tip:*** First time you run `bash util/build-gnu.sh` command, it will provide instructions on how to checkout GNU coreutils repository at the correct release tag. Please follow those instructions and when done, run `bash util/build-gnu.sh` command again.
240
241 You also need to install [quilt](https://savannah.nongnu.org/projects/quilt), a tool used to manage a stack of patches for modifying GNU tests.
242
243 On FreeBSD, you need to install packages for GNU coreutils and sed (used in shell scripts instead of system commands):
244
245 ```shell
246 pkg install coreutils gsed
247 ```
248
249 ## Code coverage report
250
251 Code coverage report can be generated using [grcov](https://github.com/mozilla/grcov).
252
253 To generate [gcov-based](https://github.com/mozilla/grcov#example-how-to-generate-gcda-files-for-a-rust-project) coverage report
254
255 ```shell
256 export CARGO_INCREMENTAL=0
257 export RUSTFLAGS="-Cinstrument-coverage -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
258 export RUSTDOCFLAGS="-Cpanic=abort"
259 export RUSTUP_TOOLCHAIN="nightly"
260 cargo build <options...> # e.g., --features feat_os_unix
261 cargo test <options...> # e.g., --features feat_os_unix test_pathchk
262 grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing --ignore build.rs --excl-br-line "^\s*((debug_)?assert(_eq|_ne)?\#\[derive\()" -o ./target/debug/coverage/
263 # open target/debug/coverage/index.html in browser
264 ```
265
266 if changes are not reflected in the report then run `cargo clean` and run the above commands.
267
268 ## Tips for setting up on Mac
269
270 ### C Compiler and linker
271
272 On MacOS you'll need to install C compiler & linker:
273
274 ```shell
275 xcode-select --install
276 ```
277
278 ### MacOS GNU utils
279
280 On MacOS you will need to install [Homebrew](https://docs.brew.sh/Installation) and use it to install the following Homebrew formulas:
281
282 ```shell
283 brew install \
284 coreutils \
285 autoconf \
286 gettext \
287 texinfo \
288 xz \
289 automake \
290 gnu-sed \
291 m4 \
292 bison \
293 pre-commit \
294 findutils
295 ```
296
297 After installing these Homebrew formulas, please make sure to add the following lines to your `zsh` or `bash` rc file, i.e. `~/.profile` or `~/.zshrc` or `~/.bashrc` ...
298 (assuming Homebrew is installed at default location `/opt/homebrew`):
299
300 ```shell
301 eval "$(/opt/homebrew/bin/brew shellenv)"
302 export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
303 export PATH="/opt/homebrew/opt/bison/bin:$PATH"
304 export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
305 ```
306
307 Last step is to link Homebrew coreutils version of `timeout` to `/usr/local/bin` (as admin user):
308
309 ```shell
310 sudo ln -s /opt/homebrew/bin/timeout /usr/local/bin/timeout
311 ```
312
313 Do not forget to either source updated rc file or restart you terminal session to update environment variables.
314
315 ## Tips for setting up on Windows
316
317 ### MSVC build tools
318
319 On Windows you'll need the MSVC build tools for Visual Studio 2013 or later.
320
321 If you are using `rustup-init.exe` to install Rust toolchain, it will guide you through the process of downloading and installing these prerequisites.
322
323 Otherwise please follow [this guide](https://learn.microsoft.com/en-us/windows/dev-environment/rust/setup).
324
325 ### Windows GNU utils
326
327 If you have used [Git for Windows](https://gitforwindows.org) to install `git` on you Windows system you might already have some GNU core utilities installed as part of "GNU Bash" included in Git for Windows package, but it is not a complete package. [This article](https://gist.github.com/evanwill/0207876c3243bbb6863e65ec5dc3f058) provides instruction on how to add more to it.
328
329 Alternatively you can install [Cygwin](https://www.cygwin.com) and/or use [WSL2](https://learn.microsoft.com/en-us/windows/wsl/compare-versions#whats-new-in-wsl-2) to get access to all GNU core utilities on Windows.
330
331 ## Preparing a new release
332
333 1. Modify `util/update-version.sh` (FROM & TO) and run it
334 1. Submit a new PR with these changes and wait for it to be merged
335 1. Tag the new release `git tag -a X.Y.Z` and `git push --tags`
336 1. Once the CI is green, a new release will be automatically created in draft mode.
337 Reuse this release and make sure that assets have been added.
338 1. Write the release notes (it takes time) following previous examples
339 1. Run `util/publish.sh --do-it` to publish the new release to crates.io