Commands and Working Directory in Make Recipes
A build recipe contains commands, and their process boundaries determine which environment changes carry over between commands.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace separate invocations
In ordinary GNU Make recipes, separate recipe lines normally run in separate shell invocations. A directory change on one line need not affect the next. Use the documented build facility or supply the working directory within the same command context. Relative paths explain why this matters.
Verify where output landed
A command returning success can still write to an unintended directory. A stage output contract can check for the file at the expected path.[1]
Trace working directory across two recipe lines
In a usual GNU Make recipe, commands on separate lines run in separate shells unless configuration changes that behavior. A change of directory on one line may therefore leave the next command starting in the original directory. Combine dependent operations in one shell context or use their explicit paths. See absolute and relative paths.
Recipe line one: cd into build directory
Recipe line two: command starts in original directory under default separate-shell behavior
Explicit path or one command context makes the target location clear
Verify what happens when the first command fails
Joining operations casually can run the second even when changing directory fails. Use the shell's conditional execution semantics when the second requires first success. Test a working directory and a missing one.