# build.sh
## Overview
`build.sh` is a shell script designed to automate a series of build and cleanup operations using the Ninja build system. It sequentially builds multiple targets defined in a Ninja build file named `build.linux.ninja`, and after each build, it performs a cleanup of the generated build artifacts. This script streamlines the process of building various components or configurations of the project and ensures the build environment is clean after each build step, likely to avoid cross-contamination between builds.
## Script Structure and Workflow
The script uses the following pattern repetitively for each build target:
1. **Cleanup:** Runs `ninja` with the `-t clean` target to remove all build outputs.
2. **Build:** Invokes `ninja` to build a specific target or the default one.
3. **Cleanup:** Runs the cleanup command again to remove build outputs before moving to the next target.
This sequence is repeated for the following targets:
* The default (main) target (no explicit target name)
* `memory`
* `perf`
* `perf-long`
* `perf-alloc`
* `perf-alloc-long`
* `perf-json-c`
* `perf-json-c-long`
Each `ninja` invocation references the same build manifest file, `build.linux.ninja`, with the `-f` flag, explicitly specifying which Ninja build file to use.
## Detailed Explanation of Commands
### Cleanup Command
```bash
ninja -f build.linux.ninja -t clean > /dev/null 2>&1
ninja: The command-line tool for the Ninja build system.-f build.linux.ninja: Specifies the build manifest file.-t clean: A Ninja tool mode that cleans all build outputs.> /dev/null 2>&1: Redirects both stdout and stderr to/dev/nullto suppress all output, ensuring the script output remains clean.
This command is executed before and after each build target to ensure a clean state.
Build Commands
Examples:
ninja -f build.linux.ninja
ninja -f build.linux.ninja memory
The first builds the default target as specified in the Ninja build file.
The second builds the
memorytarget explicitly.No output redirection is used here, so build progress and results are displayed to the user by default.
Usage Example
To execute the script, run:
./build.sh
This will sequentially build and clean each target as described.
Implementation Details
The script starts with a shebang specifying
bashwith the-eoption, which causes the script to exit immediately if any command exits with a non-zero status. This behavior ensures that failures in any build or cleanup step stop the script.The repeated pattern of cleaning before and after each build target ensures that no residual files from prior builds affect subsequent ones, which is particularly important for performance or memory profiling targets that might be sensitive to build artifacts.
The output of the cleanup commands is suppressed to avoid cluttering the console, focusing user attention on build outputs and errors.
Interaction with Other System Components
Ninja Build System: The script directly invokes Ninja commands, relying on the presence of the
build.linux.ninjabuild manifest file. This file contains the build rules and targets.Build Targets: The script refers to multiple targets defined in
build.linux.ninja, which might correspond to different build configurations or test cases (e.g., performance tests, long-running tests, memory tests).Shell Environment: Requires a Unix-like shell environment with Bash and Ninja installed.
Flowchart of Script Execution
flowchart TD
Start --> Clean1[Cleanup All Builds]
Clean1 --> BuildMain[Build Default Target]
BuildMain --> Clean2[Cleanup All Builds]
Clean2 --> BuildMemory[Build "memory" Target]
BuildMemory --> Clean3[Cleanup All Builds]
Clean3 --> BuildPerf[Build "perf" Target]
BuildPerf --> Clean4[Cleanup All Builds]
Clean4 --> BuildPerfLong[Build "perf-long" Target]
BuildPerfLong --> Clean5[Cleanup All Builds]
Clean5 --> BuildPerfAlloc[Build "perf-alloc" Target]
BuildPerfAlloc --> Clean6[Cleanup All Builds]
Clean6 --> BuildPerfAllocLong[Build "perf-alloc-long" Target]
BuildPerfAllocLong --> Clean7[Cleanup All Builds]
Clean7 --> BuildPerfJsonC[Build "perf-json-c" Target]
BuildPerfJsonC --> Clean8[Cleanup All Builds]
Clean8 --> BuildPerfJsonCLong[Build "perf-json-c-long" Target]
BuildPerfJsonCLong --> Clean9[Cleanup All Builds]
Clean9 --> End[End]
This flowchart illustrates the sequential execution order of the cleanup and build steps for each target.
This script is focused exclusively on invoking Ninja build commands in a controlled sequence and does not contain any functions, classes, or complex algorithms. Its purpose is to automate repetitive build and cleanup tasks cleanly and reliably.
```