|
1 | | -set -e |
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
2 | 3 |
|
3 | | -cmake -S . -B build/ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug -DTARGET_ARCH=x86_64 |
| 4 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
4 | 5 |
|
5 | | -cmake --build build/ --target run -j 12 |
| 6 | +BUILD_TYPE="Debug" |
| 7 | +TARGET_ARCH="x86_64" |
| 8 | +TARGET="run" |
| 9 | +JOBS="12" |
6 | 10 |
|
7 | | -cp build/compile_commands.json compile_commands.json |
| 11 | +MODE="" |
| 12 | + |
| 13 | +DOCKER=0 |
| 14 | +DOCKER_IMAGE="coolpotos" |
| 15 | +DOCKER_PLATFORM="linux/amd64" |
| 16 | +DOCKER_BUILD=1 |
| 17 | + |
| 18 | +usage() { |
| 19 | + cat <<'EOF' |
| 20 | +Usage: |
| 21 | + ./build.sh -docker [docker options] [build options] |
| 22 | + ./build.sh -direct [build options] |
| 23 | +
|
| 24 | +Docker options: |
| 25 | + --no-docker-build Skip docker build step (reuse existing image). |
| 26 | + --docker-image <name> Docker image name (default: coolpotos). |
| 27 | + --docker-platform <plat> Docker platform (default: linux/amd64). |
| 28 | +
|
| 29 | +Build options: |
| 30 | + --arch <arch> Target arch (default: x86_64). |
| 31 | + --build-type <type> Debug | Release (default: Debug). |
| 32 | + --target <target> CMake target (default: run). |
| 33 | + -j, --jobs <n> Build parallel jobs (default: 12). |
| 34 | + -h, --help Show this help. |
| 35 | +EOF |
| 36 | +} |
| 37 | + |
| 38 | +usage_docker() { |
| 39 | + cat <<'EOF' |
| 40 | +Usage: |
| 41 | + ./build.sh -docker [docker options] [build options] |
| 42 | +
|
| 43 | +Docker options: |
| 44 | + --no-docker-build Skip docker build step (reuse existing image). |
| 45 | + --docker-image <name> Docker image name (default: coolpotos). |
| 46 | + --docker-platform <plat> Docker platform (default: linux/amd64). |
| 47 | +
|
| 48 | +Build options: |
| 49 | + --arch <arch> Target arch (default: x86_64). |
| 50 | + --build-type <type> Debug | Release (default: Debug). |
| 51 | + --target <target> CMake target (default: run). |
| 52 | + -j, --jobs <n> Build parallel jobs (default: 12). |
| 53 | +EOF |
| 54 | +} |
| 55 | + |
| 56 | +usage_direct() { |
| 57 | + cat <<'EOF' |
| 58 | +Usage: |
| 59 | + ./build.sh -direct [build options] |
| 60 | +
|
| 61 | +Build options: |
| 62 | + --arch <arch> Target arch (default: x86_64). |
| 63 | + --build-type <type> Debug | Release (default: Debug). |
| 64 | + --target <target> CMake target (default: run). |
| 65 | + -j, --jobs <n> Build parallel jobs (default: 12). |
| 66 | +EOF |
| 67 | +} |
| 68 | + |
| 69 | +require_value() { |
| 70 | + if [[ $# -lt 2 || -z "${2:-}" ]]; then |
| 71 | + echo "Option $1 requires a value." >&2 |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +if [[ $# -eq 0 ]]; then |
| 77 | + usage |
| 78 | + exit 1 |
| 79 | +fi |
| 80 | + |
| 81 | +case "$1" in |
| 82 | + -docker) |
| 83 | + MODE="docker" |
| 84 | + shift |
| 85 | + ;; |
| 86 | + -direct) |
| 87 | + MODE="direct" |
| 88 | + shift |
| 89 | + ;; |
| 90 | + -h|--help) |
| 91 | + usage |
| 92 | + exit 0 |
| 93 | + ;; |
| 94 | + *) |
| 95 | + echo "First argument must be -docker or -direct." >&2 |
| 96 | + usage |
| 97 | + exit 1 |
| 98 | + ;; |
| 99 | +esac |
| 100 | + |
| 101 | +while [[ $# -gt 0 ]]; do |
| 102 | + case "$1" in |
| 103 | + --no-docker-build) |
| 104 | + if [[ "$MODE" != "docker" ]]; then |
| 105 | + echo "Option $1 is only valid with -docker." >&2 |
| 106 | + usage_direct |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + DOCKER_BUILD=0 |
| 110 | + shift |
| 111 | + ;; |
| 112 | + --docker-image) |
| 113 | + if [[ "$MODE" != "docker" ]]; then |
| 114 | + echo "Option $1 is only valid with -docker." >&2 |
| 115 | + usage_direct |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | + require_value "$1" "${2:-}" |
| 119 | + DOCKER_IMAGE="$2" |
| 120 | + shift 2 |
| 121 | + ;; |
| 122 | + --docker-platform) |
| 123 | + if [[ "$MODE" != "docker" ]]; then |
| 124 | + echo "Option $1 is only valid with -docker." >&2 |
| 125 | + usage_direct |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + require_value "$1" "${2:-}" |
| 129 | + DOCKER_PLATFORM="$2" |
| 130 | + shift 2 |
| 131 | + ;; |
| 132 | + --arch) |
| 133 | + require_value "$1" "${2:-}" |
| 134 | + TARGET_ARCH="$2" |
| 135 | + shift 2 |
| 136 | + ;; |
| 137 | + --build-type) |
| 138 | + require_value "$1" "${2:-}" |
| 139 | + BUILD_TYPE="$2" |
| 140 | + shift 2 |
| 141 | + ;; |
| 142 | + --target) |
| 143 | + require_value "$1" "${2:-}" |
| 144 | + TARGET="$2" |
| 145 | + shift 2 |
| 146 | + ;; |
| 147 | + -j|--jobs) |
| 148 | + require_value "$1" "${2:-}" |
| 149 | + JOBS="$2" |
| 150 | + shift 2 |
| 151 | + ;; |
| 152 | + -h|--help) |
| 153 | + if [[ "$MODE" == "docker" ]]; then |
| 154 | + usage_docker |
| 155 | + else |
| 156 | + usage_direct |
| 157 | + fi |
| 158 | + exit 0 |
| 159 | + ;; |
| 160 | + *) |
| 161 | + echo "Unknown option: $1" >&2 |
| 162 | + if [[ "$MODE" == "docker" ]]; then |
| 163 | + usage_docker |
| 164 | + else |
| 165 | + usage_direct |
| 166 | + fi |
| 167 | + exit 1 |
| 168 | + ;; |
| 169 | + esac |
| 170 | +done |
| 171 | + |
| 172 | +if [[ "$MODE" == "docker" ]]; then |
| 173 | + DOCKER=1 |
| 174 | +fi |
| 175 | + |
| 176 | +if [[ "$DOCKER" -eq 1 ]]; then |
| 177 | + if [[ "$DOCKER_BUILD" -eq 1 ]]; then |
| 178 | + docker build --platform "$DOCKER_PLATFORM" -t "$DOCKER_IMAGE" "$ROOT_DIR" |
| 179 | + fi |
| 180 | + |
| 181 | + docker run --rm -it \ |
| 182 | + --platform "$DOCKER_PLATFORM" \ |
| 183 | + -v "$ROOT_DIR":/workspace \ |
| 184 | + -w /workspace \ |
| 185 | + -e BUILD_TYPE="$BUILD_TYPE" \ |
| 186 | + -e TARGET_ARCH="$TARGET_ARCH" \ |
| 187 | + -e TARGET="$TARGET" \ |
| 188 | + -e JOBS="$JOBS" \ |
| 189 | + "$DOCKER_IMAGE" \ |
| 190 | + bash -lc 'set -e; \ |
| 191 | + if ! python3 -c "import importlib.util, sys; sys.exit(0 if importlib.util.find_spec(\"cryptography\") else 1)"; then \ |
| 192 | + if python3 -m venv /tmp/cpos-venv >/dev/null 2>&1; then \ |
| 193 | + /tmp/cpos-venv/bin/pip install -q cryptography; \ |
| 194 | + export PATH="/tmp/cpos-venv/bin:$PATH"; \ |
| 195 | + else \ |
| 196 | + python3 -m pip install -q --break-system-packages cryptography; \ |
| 197 | + fi; \ |
| 198 | + fi; \ |
| 199 | + cmake -S . -B build/ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE="$BUILD_TYPE" -DTARGET_ARCH="$TARGET_ARCH"; \ |
| 200 | + cmake --build build/ --target "$TARGET" -j "${JOBS:-$(nproc)}"; \ |
| 201 | + cp build/compile_commands.json ./compile_commands.json' |
| 202 | +else |
| 203 | + cmake -S "$ROOT_DIR" -B "$ROOT_DIR/build/" \ |
| 204 | + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ |
| 205 | + -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \ |
| 206 | + -DTARGET_ARCH="$TARGET_ARCH" |
| 207 | + |
| 208 | + cmake --build "$ROOT_DIR/build/" --target "$TARGET" -j "$JOBS" |
| 209 | + |
| 210 | + cp "$ROOT_DIR/build/compile_commands.json" "$ROOT_DIR/compile_commands.json" |
| 211 | +fi |
0 commit comments