This repository was archived by the owner on Aug 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
393 lines (357 loc) · 15.6 KB
/
build_libraries.yml
File metadata and controls
393 lines (357 loc) · 15.6 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
name: 构建CodeNothing库文件
on:
# 手动触发
workflow_dispatch:
inputs:
libraries:
description: '要构建的库列表 (all 表示全部, 或用逗号分隔的名称列表如 io,example)'
required: true
default: 'all'
# 发布版本时自动触发
release:
types: [created]
# 添加权限设置
permissions:
contents: write
env:
CARGO_TERM_COLOR: always
jobs:
prepare:
name: 准备库列表
runs-on: ubuntu-latest
outputs:
library_matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v3
- id: set-matrix
name: 准备库矩阵
run: |
# 如果是手动触发并指定了库,则使用指定的库
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.libraries }}" != "all" ]]; then
# 使用提供的库列表
LIBRARIES=$(echo "${{ github.event.inputs.libraries }}" | tr ',' '\n' | jq -R . | jq -cs .)
else
# 否则构建所有库
LIBRARIES=$(find . -maxdepth 1 -type d -name "library_*" | sed 's|./library_||g' | jq -R . | jq -cs .)
fi
echo "matrix=${LIBRARIES}" >> $GITHUB_OUTPUT
shell: bash
build:
name: 构建库
needs: prepare
runs-on: ${{ matrix.platform.os }}
continue-on-error: true
strategy:
fail-fast: false
matrix:
library: ${{ fromJson(needs.prepare.outputs.library_matrix) }}
platform:
# Windows 平台
- os: windows-latest
target: x86_64-pc-windows-msvc
lib_extension: 'dll'
archive_format: 'zip'
platform_name: 'windows-x64'
- os: windows-latest
target: i686-pc-windows-msvc
lib_extension: 'dll'
archive_format: 'zip'
platform_name: 'windows-x32'
- os: windows-latest
target: aarch64-pc-windows-msvc
lib_extension: 'dll'
archive_format: 'zip'
platform_name: 'windows-arm64'
# Linux 平台
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
lib_extension: 'so'
archive_format: 'tar.gz'
platform_name: 'linux-x64'
- os: ubuntu-latest
target: i686-unknown-linux-gnu
lib_extension: 'so'
archive_format: 'tar.gz'
platform_name: 'linux-x32'
- os: ubuntu-latest
target: aarch64-unknown-linux-gnu
lib_extension: 'so'
archive_format: 'tar.gz'
platform_name: 'linux-arm64'
# Android 平台
- os: ubuntu-latest
target: aarch64-linux-android
lib_extension: 'so'
archive_format: 'tar.gz'
platform_name: 'android-arm64'
# macOS 平台
- os: macos-latest
target: x86_64-apple-darwin
lib_extension: 'dylib'
archive_format: 'tar.gz'
platform_name: 'macos-intel'
- os: macos-latest
target: aarch64-apple-darwin
lib_extension: 'dylib'
archive_format: 'tar.gz'
platform_name: 'macos-silicon'
steps:
- uses: actions/checkout@v3
- name: 设置Rust工具链
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.platform.target }}
override: true
- name: 安装 Windows 交叉编译工具
if: matrix.platform.os == 'windows-latest' && matrix.platform.target != 'x86_64-pc-windows-msvc'
run: |
# Windows 交叉编译通常由 Rust 工具链自动处理
echo "Windows 交叉编译目标: ${{ matrix.platform.target }}"
- name: 安装交叉编译依赖 (Linux)
if: matrix.platform.os == 'ubuntu-latest'
run: |
case ${{ matrix.platform.target }} in
i686-unknown-linux-gnu)
sudo apt-get update
sudo apt-get install -y gcc-multilib g++-multilib
echo "CC_i686_unknown_linux_gnu=i686-linux-gnu-gcc" >> $GITHUB_ENV
echo "CXX_i686_unknown_linux_gnu=i686-linux-gnu-g++" >> $GITHUB_ENV
echo "CARGO_TARGET_I686_UNKNOWN_LINUX_GNU_LINKER=i686-linux-gnu-gcc" >> $GITHUB_ENV
;;
aarch64-unknown-linux-gnu)
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
echo "CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
echo "CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++" >> $GITHUB_ENV
echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
;;
aarch64-linux-android)
# Android NDK 通过 setup-ndk 安装
;;
esac
- name: 验证交叉编译工具
if: matrix.platform.os == 'ubuntu-latest' && matrix.platform.target != 'x86_64-unknown-linux-gnu'
run: |
case ${{ matrix.platform.target }} in
i686-unknown-linux-gnu)
which i686-linux-gnu-gcc || echo "警告: i686-linux-gnu-gcc 未找到"
;;
aarch64-unknown-linux-gnu)
which aarch64-linux-gnu-gcc || echo "警告: aarch64-linux-gnu-gcc 未找到"
;;
esac
- name: 设置 Android NDK
if: matrix.platform.target == 'aarch64-linux-android'
uses: nttld/setup-ndk@v1
with:
ndk-version: r25c
add-to-path: false
- name: 配置 Android 环境变量
if: matrix.platform.target == 'aarch64-linux-android'
run: |
echo "CC_aarch64_linux_android=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang" >> $GITHUB_ENV
echo "CXX_aarch64_linux_android=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang++" >> $GITHUB_ENV
echo "AR_aarch64_linux_android=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar" >> $GITHUB_ENV
echo "CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang" >> $GITHUB_ENV
# 配置 OpenSSL 相关环境变量以避免交叉编译问题
echo "OPENSSL_NO_VENDOR=1" >> $GITHUB_ENV
echo "PKG_CONFIG_ALLOW_CROSS=1" >> $GITHUB_ENV
- name: 检查库目录是否存在
id: check_dir
run: |
if [ -d "library_${{ matrix.library }}" ]; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "警告: 库 ${{ matrix.library }} 的目录不存在,跳过构建"
fi
shell: bash
- name: 获取库配置
if: steps.check_dir.outputs.exists == 'true'
id: get_config
run: |
if [ -f "library_${{ matrix.library }}/library.json" ]; then
OUTPUT_NAME=$(jq -r '.output_name // "${{ matrix.library }}"' library_${{ matrix.library }}/library.json)
else
OUTPUT_NAME="${{ matrix.library }}"
fi
echo "output_name=${OUTPUT_NAME}" >> $GITHUB_OUTPUT
shell: bash
- name: 显示构建环境信息
if: steps.check_dir.outputs.exists == 'true'
run: |
echo "构建目标: ${{ matrix.platform.target }}"
echo "运行环境: ${{ matrix.platform.os }}"
echo "库名称: ${{ matrix.library }}"
rustc --version
cargo --version
echo "已安装的目标:"
rustup target list --installed
shell: bash
- name: 构建库 (交叉编译平台 - 跳过有问题的依赖)
if: steps.check_dir.outputs.exists == 'true' && (contains(matrix.platform.target, 'android') || matrix.platform.target == 'i686-unknown-linux-gnu' || matrix.platform.target == 'aarch64-unknown-linux-gnu' || matrix.platform.target == 'i686-pc-windows-msvc' || matrix.platform.target == 'aarch64-pc-windows-msvc')
run: |
cd library_${{ matrix.library }}
# 尝试使用最小特性集构建,避免 OpenSSL 等问题依赖
RUST_BACKTRACE=1 OPENSSL_NO_VENDOR=1 PKG_CONFIG_ALLOW_CROSS=1 cargo build --release --target ${{ matrix.platform.target }} --no-default-features --features minimal 2>/dev/null || \
RUST_BACKTRACE=1 OPENSSL_NO_VENDOR=1 PKG_CONFIG_ALLOW_CROSS=1 cargo build --release --target ${{ matrix.platform.target }} --no-default-features 2>/dev/null || \
RUST_BACKTRACE=1 OPENSSL_NO_VENDOR=1 PKG_CONFIG_ALLOW_CROSS=1 cargo build --release --target ${{ matrix.platform.target }} || \
echo "警告: 库 ${{ matrix.library }} 在平台 ${{ matrix.platform.target }} 上构建失败,跳过"
shell: bash
- name: 构建库 (原生平台)
if: steps.check_dir.outputs.exists == 'true' && !contains(matrix.platform.target, 'android') && matrix.platform.target != 'i686-unknown-linux-gnu' && matrix.platform.target != 'aarch64-unknown-linux-gnu' && matrix.platform.target != 'i686-pc-windows-msvc' && matrix.platform.target != 'aarch64-pc-windows-msvc'
run: |
cd library_${{ matrix.library }}
RUST_BACKTRACE=1 cargo build --release --target ${{ matrix.platform.target }}
shell: bash
- name: 创建输出目录
if: steps.check_dir.outputs.exists == 'true'
run: mkdir -p library-package
shell: bash
- name: 复制构建产物 (Windows)
if: steps.check_dir.outputs.exists == 'true' && matrix.platform.os == 'windows-latest'
run: |
LIB_PATH="library_${{ matrix.library }}/target/${{ matrix.platform.target }}/release/${{ steps.get_config.outputs.output_name }}.dll"
if [ -f "$LIB_PATH" ]; then
cp "$LIB_PATH" "library-package/${{ matrix.library }}.${{ matrix.platform.lib_extension }}"
else
echo "找不到库文件,尝试备选路径..."
# 尝试Cargo.toml中的包名
PACKAGE_NAME=$(grep -m 1 "name" "library_${{ matrix.library }}/Cargo.toml" | cut -d'"' -f2 | tr -d '\r')
cp "library_${{ matrix.library }}/target/${{ matrix.platform.target }}/release/${PACKAGE_NAME}.dll" "library-package/${{ matrix.library }}.${{ matrix.platform.lib_extension }}"
fi
shell: bash
- name: 复制构建产物 (Linux/macOS/Android)
if: steps.check_dir.outputs.exists == 'true' && matrix.platform.os != 'windows-latest'
run: |
LIB_PATH="library_${{ matrix.library }}/target/${{ matrix.platform.target }}/release/lib${{ steps.get_config.outputs.output_name }}.${{ matrix.platform.lib_extension }}"
if [ -f "$LIB_PATH" ]; then
cp "$LIB_PATH" "library-package/${{ matrix.library }}.${{ matrix.platform.lib_extension }}"
else
echo "找不到库文件,尝试备选路径..."
# 尝试Cargo.toml中的包名
PACKAGE_NAME=$(grep -m 1 "name" "library_${{ matrix.library }}/Cargo.toml" | cut -d'"' -f2 | tr -d '\r')
cp "library_${{ matrix.library }}/target/${{ matrix.platform.target }}/release/lib${PACKAGE_NAME}.${{ matrix.platform.lib_extension }}" "library-package/${{ matrix.library }}.${{ matrix.platform.lib_extension }}"
fi
shell: bash
- name: 创建档案 (Windows)
if: steps.check_dir.outputs.exists == 'true' && matrix.platform.os == 'windows-latest'
run: |
cd library-package
7z a "../${{ matrix.library }}-${{ matrix.platform.platform_name }}.${{ matrix.platform.archive_format }}" *
shell: bash
- name: 创建档案 (Linux/macOS/Android)
if: steps.check_dir.outputs.exists == 'true' && matrix.platform.os != 'windows-latest'
run: |
tar -czvf "${{ matrix.library }}-${{ matrix.platform.platform_name }}.${{ matrix.platform.archive_format }}" -C library-package .
shell: bash
- name: 上传构建产物
if: steps.check_dir.outputs.exists == 'true'
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.library }}-${{ matrix.platform.platform_name }}
path: ${{ matrix.library }}-${{ matrix.platform.platform_name }}.${{ matrix.platform.archive_format }}
- name: 创建发布
if: steps.check_dir.outputs.exists == 'true' && github.event_name == 'release'
uses: softprops/action-gh-release@v1
with:
files: ${{ matrix.library }}-${{ matrix.platform.platform_name }}.${{ matrix.platform.archive_format }}
tag_name: ${{ github.ref_name }}
draft: false
generate_release_notes: true
# 额外的工作以合并所有库为一个包
package-all:
name: 打包所有库
needs: build
runs-on: ${{ matrix.os }}
continue-on-error: true
# 在手动触发并指定'all'或发布版本时打包所有库
if: ${{ github.event.inputs.libraries == 'all' || github.event_name == 'release' }}
strategy:
fail-fast: false
matrix:
include:
# Windows 平台
- os: windows-latest
platform_name: 'windows-x64'
archive_format: 'zip'
- os: windows-latest
platform_name: 'windows-x32'
archive_format: 'zip'
- os: windows-latest
platform_name: 'windows-arm64'
archive_format: 'zip'
# Linux 平台
- os: ubuntu-latest
platform_name: 'linux-x64'
archive_format: 'tar.gz'
- os: ubuntu-latest
platform_name: 'linux-x32'
archive_format: 'tar.gz'
- os: ubuntu-latest
platform_name: 'linux-arm64'
archive_format: 'tar.gz'
# Android 平台
- os: ubuntu-latest
platform_name: 'android-arm64'
archive_format: 'tar.gz'
# macOS 平台
- os: macos-latest
platform_name: 'macos-intel'
archive_format: 'tar.gz'
- os: macos-latest
platform_name: 'macos-silicon'
archive_format: 'tar.gz'
steps:
- name: 下载所有构建产物
uses: actions/download-artifact@v4
- name: 准备合并目录
run: |
mkdir -p all-libraries
shell: bash
- name: 解压所有库 (Windows)
if: matrix.os == 'windows-latest'
run: |
for d in *-${{ matrix.platform_name }}; do
if [ -d "$d" ]; then
cd "$d"
7z x "*.zip" -o../all-libraries
cd ..
fi
done
shell: bash
- name: 解压所有库 (Linux/macOS/Android)
if: matrix.os != 'windows-latest'
run: |
for d in *-${{ matrix.platform_name }}; do
if [ -d "$d" ]; then
cd "$d"
tar -xzvf *.tar.gz -C ../all-libraries
cd ..
fi
done
shell: bash
- name: 创建合并档案 (Windows)
if: matrix.os == 'windows-latest'
run: |
7z a "codenothing-all-libraries-${{ matrix.platform_name }}.${{ matrix.archive_format }}" ./all-libraries/*
shell: bash
- name: 创建合并档案 (Linux/macOS/Android)
if: matrix.os != 'windows-latest'
run: |
tar -czvf "codenothing-all-libraries-${{ matrix.platform_name }}.${{ matrix.archive_format }}" -C all-libraries .
shell: bash
- name: 上传合并构建产物
uses: actions/upload-artifact@v4
with:
name: codenothing-all-libraries-${{ matrix.platform_name }}
path: codenothing-all-libraries-${{ matrix.platform_name }}.${{ matrix.archive_format }}
- name: 创建发布
if: github.event_name == 'release'
uses: softprops/action-gh-release@v1
with:
files: codenothing-all-libraries-${{ matrix.platform_name }}.${{ matrix.archive_format }}
tag_name: ${{ github.ref_name }}
draft: false
generate_release_notes: true