Skip to content

Commit e47a47f

Browse files
committed
优化build工作流
1 parent be7c1a1 commit e47a47f

File tree

2 files changed

+211
-5
lines changed

2 files changed

+211
-5
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ RUN apt-get update && \
1313
qemu-system-riscv64 \
1414
cmake \
1515
python3 \
16+
python3-cryptography \
1617
python3-pip \
18+
python3-venv \
1719
git \
1820
build-essential \
1921
&& \
@@ -23,4 +25,4 @@ RUN apt-get update && \
2325
WORKDIR /workspace
2426

2527
# 可选:设置默认命令(比如启动 shell)
26-
CMD ["/bin/bash"]
28+
CMD ["/bin/bash"]

build.sh

100644100755
Lines changed: 208 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,211 @@
1-
set -e
1+
#!/usr/bin/env bash
2+
set -euo pipefail
23

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)"
45

5-
cmake --build build/ --target run -j 12
6+
BUILD_TYPE="Debug"
7+
TARGET_ARCH="x86_64"
8+
TARGET="run"
9+
JOBS="12"
610

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

Comments
 (0)