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