-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·1100 lines (987 loc) · 33.8 KB
/
start.sh
File metadata and controls
executable file
·1100 lines (987 loc) · 33.8 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage: ./start.sh
Starts the server inside Docker (Linux runtime), compiling plugins into a temporary directory.
The server/runtime source is copied from a local cached clone of github.com/bedrock-gophers/plugin.
Supported source layouts:
plugins/<name>/*.go -> built as Go plugin (.so)
plugins/<name>/*.csproj -> published as NativeAOT C# plugin (.so)
plugins/<name>/*.cs -> auto-generates csproj + export entrypoint, then publishes NativeAOT C# plugin (.so)
plugins/<name>/Cargo.toml -> built as Rust plugin (.so) from a cdylib crate
plugins/*.so -> prebuilt Go plugin fallback
plugins/csharp/**.so -> prebuilt C# plugin fallback
plugins/rust/**.so -> prebuilt Rust plugin fallback
Environment overrides:
PLUGIN_SRC_DIR Source plugin directory (default: $PWD/plugins, fallback: cached repo ./plugins)
HOST_REPO_URL Host runtime repo URL (default: https://github.com/bedrock-gophers/plugin.git)
USE_LOCAL_HOST_WORKTREE Set to 1 to use the current working tree as host runtime source
(default: 0, uses cached/fetched HOST_REPO_URL source)
BRANCH Optional git branch/tag to clone from HOST_REPO_URL
COMMIT Optional commit SHA to checkout after clone (takes precedence over BRANCH tip)
HOST_REPO_CACHE_DIR Cached host repo directory (default: $TMP_BASE/host-repo-cache)
HOST_REPO_UPDATE Set to 1 to manually update HOST_REPO_CACHE_DIR from origin
PLUGIN_ARTIFACT_CACHE_DIR Cached plugin artifact directory (default: $TMP_BASE/plugin-artifact-cache)
PLUGIN_SHA_CACHE_DIR Plugin source hash directory (default: $TMP_BASE/plugin-sha-cache)
PLUGIN_GO_IMAGE Go image (default: golang:1.25)
PLUGIN_RUST_IMAGE Rust image (default: rust:1.85)
PLUGIN_DOTNET_IMAGE Dotnet SDK image (default: mcr.microsoft.com/dotnet/sdk:8.0)
PLUGIN_DOTNET_AOT_IMAGE_TAG Docker image tag used for NativeAOT publish (default: bedrock-plugin-dotnet-aot:8.0)
PLUGIN_CS_RID C# runtime identifier (default: linux-x64)
DOTNET_INVARIANT Set to 0 to disable invariant globalization mode (default: 1)
SERVER_PORT UDP listen port exposed on host (default: 19132)
KEEP_TEMP Set to 1 to keep temporary build/runtime directory
USAGE
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
PLUGIN_SRC_DIR_SET=0
if [[ -n "${PLUGIN_SRC_DIR+x}" ]]; then
PLUGIN_SRC_DIR_SET=1
fi
PLUGIN_SRC_DIR="${PLUGIN_SRC_DIR:-$PWD/plugins}"
HOST_REPO_URL="${HOST_REPO_URL:-https://github.com/bedrock-gophers/plugin.git}"
USE_LOCAL_HOST_WORKTREE="${USE_LOCAL_HOST_WORKTREE:-0}"
BRANCH="${BRANCH:-}"
COMMIT="${COMMIT:-}"
HOST_REPO_UPDATE="${HOST_REPO_UPDATE:-0}"
PLUGIN_GO_IMAGE_SET=0
if [[ -n "${PLUGIN_GO_IMAGE+x}" ]]; then
PLUGIN_GO_IMAGE_SET=1
fi
PLUGIN_GO_IMAGE="${PLUGIN_GO_IMAGE:-golang:1.25}"
PLUGIN_RUST_IMAGE="${PLUGIN_RUST_IMAGE:-rust:1.85}"
PLUGIN_DOTNET_IMAGE="${PLUGIN_DOTNET_IMAGE:-mcr.microsoft.com/dotnet/sdk:8.0}"
PLUGIN_DOTNET_AOT_IMAGE_TAG="${PLUGIN_DOTNET_AOT_IMAGE_TAG:-bedrock-plugin-dotnet-aot:8.0}"
PLUGIN_CS_RID="${PLUGIN_CS_RID:-linux-x64}"
GOEXPERIMENT_SET=0
if [[ -n "${GOEXPERIMENT+x}" ]]; then
GOEXPERIMENT_SET=1
fi
GOEXPERIMENT_VALUE="${GOEXPERIMENT:-}"
DOTNET_INVARIANT="${DOTNET_INVARIANT:-1}"
SERVER_PORT="${SERVER_PORT:-19132}"
KEEP_TEMP="${KEEP_TEMP:-0}"
DOCKER_RUN_USER="$(id -u):$(id -g)"
TMP_BASE="${TMP_BASE:-${XDG_CACHE_HOME:-$HOME/.cache}/bedrock-plugin}"
RUNTIME_DIR="$TMP_BASE/start-$(date +%s)-$$-$RANDOM"
RUNTIME_PLUGIN_DIR="$RUNTIME_DIR/plugins"
RUNTIME_BUILD_DIR="$RUNTIME_DIR/build"
HOST_REPO_DIR="$RUNTIME_BUILD_DIR/host-repo"
STAGED_PLUGIN_SRC_REL=".start-plugin-src"
STAGED_PLUGIN_SRC_DIR="$HOST_REPO_DIR/$STAGED_PLUGIN_SRC_REL"
GO_BUILD_CACHE_DIR="$TMP_BASE/go-build-cache"
GO_MOD_CACHE_DIR="$TMP_BASE/go-mod-cache"
CARGO_REGISTRY_CACHE_DIR="$TMP_BASE/cargo-registry-cache"
CARGO_GIT_CACHE_DIR="$TMP_BASE/cargo-git-cache"
DOTNET_NUGET_CACHE_DIR="$TMP_BASE/dotnet-nuget-cache"
HOST_REPO_CACHE_DIR="${HOST_REPO_CACHE_DIR:-$TMP_BASE/host-repo-cache}"
PLUGIN_ARTIFACT_CACHE_DIR="${PLUGIN_ARTIFACT_CACHE_DIR:-$TMP_BASE/plugin-artifact-cache}"
PLUGIN_SHA_CACHE_DIR="${PLUGIN_SHA_CACHE_DIR:-$TMP_BASE/plugin-sha-cache}"
HOST_RUNTIME_ABI_SHA=""
if [[ "$USE_LOCAL_HOST_WORKTREE" != "0" && "$USE_LOCAL_HOST_WORKTREE" != "1" ]]; then
echo "error: USE_LOCAL_HOST_WORKTREE must be 0 or 1 (got: $USE_LOCAL_HOST_WORKTREE)" >&2
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
echo "error: git CLI not found. Install git first." >&2
exit 1
fi
if ! command -v docker >/dev/null 2>&1; then
echo "error: docker CLI not found. Install Docker Desktop / Docker Engine first." >&2
exit 1
fi
if ! docker info >/dev/null 2>&1; then
echo "error: docker daemon is not reachable. Is Docker running?" >&2
exit 1
fi
if [[ "$PLUGIN_CS_RID" != linux-* ]]; then
echo "error: PLUGIN_CS_RID must target linux-* for this Docker Linux runtime (got: $PLUGIN_CS_RID)" >&2
exit 1
fi
mkdir -p "$RUNTIME_PLUGIN_DIR" "$RUNTIME_PLUGIN_DIR/csharp" "$RUNTIME_PLUGIN_DIR/rust" "$RUNTIME_BUILD_DIR" "$HOST_REPO_DIR" "$HOST_REPO_CACHE_DIR"
mkdir -p "$GO_BUILD_CACHE_DIR" "$GO_MOD_CACHE_DIR" "$CARGO_REGISTRY_CACHE_DIR" "$CARGO_GIT_CACHE_DIR" "$DOTNET_NUGET_CACHE_DIR"
mkdir -p "$PLUGIN_ARTIFACT_CACHE_DIR" "$PLUGIN_SHA_CACHE_DIR"
cleanup() {
if [[ "$KEEP_TEMP" == "1" ]]; then
echo "temporary runtime directory kept at: $RUNTIME_DIR"
return
fi
rm -rf "$RUNTIME_DIR"
}
trap cleanup EXIT
host_path_for_docker() {
local path="$1"
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*)
if command -v cygpath >/dev/null 2>&1; then
cygpath -m "$path"
else
printf '%s' "$path"
fi
;;
*)
printf '%s' "$path"
;;
esac
}
ensure_host_repo_cache() {
if [[ "$USE_LOCAL_HOST_WORKTREE" == "1" ]]; then
return
fi
if [[ -d "$HOST_REPO_CACHE_DIR/.git" ]]; then
local current_origin
current_origin="$(git -C "$HOST_REPO_CACHE_DIR" remote get-url origin 2>/dev/null || true)"
if [[ -n "$current_origin" && "$current_origin" != "$HOST_REPO_URL" ]]; then
rm -rf "$HOST_REPO_CACHE_DIR"
fi
elif [[ -d "$HOST_REPO_CACHE_DIR" ]] && [[ -n "$(ls -A "$HOST_REPO_CACHE_DIR" 2>/dev/null)" ]]; then
rm -rf "$HOST_REPO_CACHE_DIR"
fi
if [[ ! -d "$HOST_REPO_CACHE_DIR/.git" ]]; then
local clone_args=("--depth" "1")
if [[ -n "$BRANCH" ]]; then
clone_args+=("--branch" "$BRANCH")
fi
echo "initializing host runtime repo cache: $HOST_REPO_CACHE_DIR"
git clone --quiet "${clone_args[@]}" "$HOST_REPO_URL" "$HOST_REPO_CACHE_DIR"
elif [[ "$HOST_REPO_UPDATE" == "1" ]]; then
echo "updating host runtime repo cache from origin"
if [[ -n "$BRANCH" ]]; then
git -C "$HOST_REPO_CACHE_DIR" fetch --quiet --depth 1 origin "$BRANCH"
else
git -C "$HOST_REPO_CACHE_DIR" fetch --quiet --depth 1 origin
fi
fi
if [[ -n "$BRANCH" ]] && ! git -C "$HOST_REPO_CACHE_DIR" show-ref --verify --quiet "refs/remotes/origin/$BRANCH"; then
if [[ "$HOST_REPO_UPDATE" == "1" ]]; then
git -C "$HOST_REPO_CACHE_DIR" fetch --quiet --depth 1 origin "$BRANCH"
else
echo "error: branch '$BRANCH' not found in cached host repo. Set HOST_REPO_UPDATE=1 to refresh cache." >&2
exit 1
fi
fi
if [[ -n "$COMMIT" ]] && ! git -C "$HOST_REPO_CACHE_DIR" rev-parse --verify --quiet "$COMMIT^{commit}" >/dev/null; then
if [[ "$HOST_REPO_UPDATE" == "1" ]]; then
git -C "$HOST_REPO_CACHE_DIR" fetch --quiet --depth 1 origin "$COMMIT"
else
echo "error: commit '$COMMIT' not found in cached host repo. Set HOST_REPO_UPDATE=1 to refresh cache." >&2
exit 1
fi
fi
}
prepare_host_repo() {
rm -rf "$HOST_REPO_DIR"
if [[ "$USE_LOCAL_HOST_WORKTREE" == "1" ]]; then
echo "using local host runtime source from working tree: $PWD"
if command -v rsync >/dev/null 2>&1; then
rsync -a --delete \
--exclude ".git" \
--exclude ".tmp" \
--exclude ".data" \
"$PWD"/ "$HOST_REPO_DIR"/
else
mkdir -p "$HOST_REPO_DIR"
cp -a "$PWD"/. "$HOST_REPO_DIR"/
rm -rf "$HOST_REPO_DIR/.git" "$HOST_REPO_DIR/.tmp" "$HOST_REPO_DIR/.data"
fi
local resolved_ref
resolved_ref="$(git -C "$PWD" rev-parse --short=12 HEAD 2>/dev/null || echo "worktree")"
echo "using host runtime source: local worktree ($resolved_ref)"
return
fi
echo "copying host runtime repo from cache"
git clone --quiet --no-hardlinks "$HOST_REPO_CACHE_DIR" "$HOST_REPO_DIR"
if [[ -n "$COMMIT" ]]; then
git -C "$HOST_REPO_DIR" checkout --quiet --detach "$COMMIT"
elif [[ -n "$BRANCH" ]]; then
git -C "$HOST_REPO_DIR" checkout --quiet --detach "origin/$BRANCH"
fi
if [[ ! -f "$HOST_REPO_DIR/cmd/main.go" ]]; then
echo "error: cloned host repo is missing cmd/main.go" >&2
exit 1
fi
local resolved_ref
resolved_ref="$(git -C "$HOST_REPO_DIR" rev-parse --short=12 HEAD)"
echo "using host runtime ref: $resolved_ref"
}
resolve_plugin_source_dir() {
if [[ -d "$PLUGIN_SRC_DIR" ]]; then
return
fi
if [[ "$PLUGIN_SRC_DIR_SET" == "0" && -d "$HOST_REPO_DIR/plugins" ]]; then
PLUGIN_SRC_DIR="$HOST_REPO_DIR/plugins"
return
fi
echo "error: plugin source directory not found: $PLUGIN_SRC_DIR" >&2
exit 1
}
stage_plugin_sources() {
rm -rf "$STAGED_PLUGIN_SRC_DIR"
mkdir -p "$STAGED_PLUGIN_SRC_DIR"
cp -a "$PLUGIN_SRC_DIR"/. "$STAGED_PLUGIN_SRC_DIR"/
PLUGIN_SRC_DIR="$STAGED_PLUGIN_SRC_DIR"
PLUGIN_SRC_REL="$STAGED_PLUGIN_SRC_REL"
PLUGIN_SRC_CONTAINER="/workspace/$PLUGIN_SRC_REL"
}
ensure_host_repo_cache
prepare_host_repo
resolve_plugin_source_dir
stage_plugin_sources
ROOT_MOUNT="$(host_path_for_docker "$HOST_REPO_DIR")"
RUNTIME_MOUNT="$(host_path_for_docker "$RUNTIME_DIR")"
GO_BUILD_CACHE_MOUNT="$(host_path_for_docker "$GO_BUILD_CACHE_DIR")"
GO_MOD_CACHE_MOUNT="$(host_path_for_docker "$GO_MOD_CACHE_DIR")"
CARGO_REGISTRY_CACHE_MOUNT="$(host_path_for_docker "$CARGO_REGISTRY_CACHE_DIR")"
CARGO_GIT_CACHE_MOUNT="$(host_path_for_docker "$CARGO_GIT_CACHE_DIR")"
DOTNET_NUGET_CACHE_MOUNT="$(host_path_for_docker "$DOTNET_NUGET_CACHE_DIR")"
run_go() {
docker run --rm \
--user "$DOCKER_RUN_USER" \
--mount "type=bind,src=$ROOT_MOUNT,dst=/workspace" \
--mount "type=bind,src=$RUNTIME_MOUNT,dst=/runtime" \
--mount "type=bind,src=$GO_BUILD_CACHE_MOUNT,dst=/cache/go-build" \
--mount "type=bind,src=$GO_MOD_CACHE_MOUNT,dst=/cache/go-mod" \
-w /workspace \
-e CGO_ENABLED=1 \
-e GOFLAGS=-buildvcs=false \
-e GOEXPERIMENT="$GOEXPERIMENT_VALUE" \
-e GOCACHE=/cache/go-build \
-e GOMODCACHE=/cache/go-mod \
"$PLUGIN_GO_IMAGE" \
"$@"
}
run_dotnet() {
ensure_dotnet_aot_image
docker run --rm \
--user "$DOCKER_RUN_USER" \
--mount "type=bind,src=$ROOT_MOUNT,dst=/workspace" \
--mount "type=bind,src=$RUNTIME_MOUNT,dst=/runtime" \
--mount "type=bind,src=$DOTNET_NUGET_CACHE_MOUNT,dst=/cache/nuget" \
-w /workspace \
-e HOME=/tmp \
-e DOTNET_CLI_HOME=/tmp \
-e DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 \
-e DOTNET_CLI_TELEMETRY_OPTOUT=1 \
-e NUGET_PACKAGES=/cache/nuget \
"$PLUGIN_DOTNET_AOT_IMAGE_TAG" \
"$@"
}
run_rust() {
docker run --rm \
--user "$DOCKER_RUN_USER" \
--mount "type=bind,src=$ROOT_MOUNT,dst=/workspace" \
--mount "type=bind,src=$RUNTIME_MOUNT,dst=/runtime" \
--mount "type=bind,src=$CARGO_REGISTRY_CACHE_MOUNT,dst=/cache/registry" \
--mount "type=bind,src=$CARGO_GIT_CACHE_MOUNT,dst=/cache/git" \
-w /workspace \
-e CARGO_HOME=/cache \
"$PLUGIN_RUST_IMAGE" \
"$@"
}
ensure_dotnet_aot_image() {
if docker image inspect "$PLUGIN_DOTNET_AOT_IMAGE_TAG" >/dev/null 2>&1; then
return
fi
echo "building NativeAOT dotnet image: $PLUGIN_DOTNET_AOT_IMAGE_TAG"
DOCKER_BUILDKIT=1 docker build -t "$PLUGIN_DOTNET_AOT_IMAGE_TAG" - >/dev/null <<DOCKER
FROM $PLUGIN_DOTNET_IMAGE
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends clang zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
DOCKER
}
collect_source_go_plugin_names() {
shopt -s nullglob
local entries=("$PLUGIN_SRC_DIR"/*)
shopt -u nullglob
for entry in "${entries[@]}"; do
[[ -d "$entry" ]] || continue
shopt -s nullglob
local go_files=("$entry"/*.go)
shopt -u nullglob
if [[ ${#go_files[@]} -eq 0 ]]; then
continue
fi
local name
name="$(basename "$entry")"
SOURCE_GO_NAMES["$name"]=1
done
}
detect_prebuilt_go_toolchain() {
shopt -s nullglob
local prebuilt_root=("$PLUGIN_SRC_DIR"/*.so)
shopt -u nullglob
if [[ ${#prebuilt_root[@]} -eq 0 ]]; then
return
fi
local detected_version=""
local detected_experiment=""
local scanned=0
for so_file in "${prebuilt_root[@]}"; do
local name
name="$(basename "${so_file%.so}")"
if [[ -n "${SOURCE_GO_NAMES[$name]:-}" ]]; then
continue
fi
local so_container="$PLUGIN_SRC_CONTAINER/$(basename "$so_file")"
local meta
if ! meta="$(run_go go version -m "$so_container" 2>&1)"; then
echo "error: failed to inspect prebuilt Go plugin metadata: $so_file" >&2
echo "$meta" >&2
exit 1
fi
local version
version="$(printf '%s\n' "$meta" | awk 'NR==1{for(i=1;i<=NF;i++){if($i ~ /^go[0-9]/){print $i; exit}}}')"
if [[ -z "$version" ]]; then
echo "error: could not read Go toolchain version from prebuilt plugin: $so_file" >&2
exit 1
fi
if [[ -z "$detected_version" ]]; then
detected_version="$version"
elif [[ "$detected_version" != "$version" ]]; then
echo "error: prebuilt Go plugins use different Go versions ($detected_version vs $version)." >&2
echo " Rebuild them with one toolchain, or keep only one toolchain's artifacts." >&2
exit 1
fi
local experiment
experiment="$(printf '%s\n' "$meta" | awk -F= '/^[[:space:]]*build[[:space:]]+GOEXPERIMENT=/{print $2; exit}')"
if [[ -n "$experiment" ]]; then
if [[ -z "$detected_experiment" ]]; then
detected_experiment="$experiment"
elif [[ "$detected_experiment" != "$experiment" ]]; then
echo "error: prebuilt Go plugins use different GOEXPERIMENT values ($detected_experiment vs $experiment)." >&2
exit 1
fi
fi
scanned=1
done
if [[ "$scanned" -eq 0 ]]; then
return
fi
if [[ "$PLUGIN_GO_IMAGE_SET" != "1" ]]; then
PLUGIN_GO_IMAGE="golang:${detected_version#go}"
fi
if [[ "$GOEXPERIMENT_SET" != "1" && -n "$detected_experiment" ]]; then
GOEXPERIMENT_VALUE="$detected_experiment"
fi
echo "using prebuilt Go plugin toolchain: $detected_version${detected_experiment:+ (GOEXPERIMENT=$detected_experiment)}"
echo "using Go image: $PLUGIN_GO_IMAGE"
}
resolve_linux_so() {
local out_dir="$1"
local base_name="$2"
local candidate=""
if [[ -f "$out_dir/$base_name.so" ]]; then
candidate="$out_dir/$base_name.so"
elif [[ -f "$out_dir/lib$base_name.so" ]]; then
candidate="$out_dir/lib$base_name.so"
else
shopt -s nullglob
local so_candidates=("$out_dir"/*.so)
shopt -u nullglob
if [[ ${#so_candidates[@]} -gt 0 ]]; then
candidate="${so_candidates[0]}"
fi
fi
printf '%s' "$candidate"
}
cargo_library_name() {
local manifest="$1"
local name=""
if [[ -f "$manifest" ]]; then
name="$(awk -F= '
BEGIN { section = "" }
/^[[:space:]]*\[/ {
line = $0
gsub(/[[:space:]]/, "", line)
section = line
next
}
section == "[lib]" && $1 ~ /^[[:space:]]*name[[:space:]]*$/ {
value = $2
gsub(/"/, "", value)
gsub(/[[:space:]]/, "", value)
print value
exit
}
' "$manifest")"
if [[ -z "$name" ]]; then
name="$(awk -F= '
BEGIN { section = "" }
/^[[:space:]]*\[/ {
line = $0
gsub(/[[:space:]]/, "", line)
section = line
next
}
section == "[package]" && $1 ~ /^[[:space:]]*name[[:space:]]*$/ {
value = $2
gsub(/"/, "", value)
gsub(/[[:space:]]/, "", value)
print value
exit
}
' "$manifest")"
fi
fi
if [[ -z "$name" ]]; then
name="$(basename "$(dirname "$manifest")")"
fi
name="${name//-/_}"
printf '%s' "$name"
}
hash_text_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 | awk '{print $1}'
else
echo "error: neither sha256sum nor shasum is available to hash plugin sources." >&2
exit 1
fi
}
hash_file_sha256() {
local file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | awk '{print $1}'
else
echo "error: neither sha256sum nor shasum is available to hash plugin sources." >&2
exit 1
fi
}
host_runtime_abi_sha() {
local digest_input=""
local found=0
while IFS= read -r -d '' source_file; do
found=1
local rel="${source_file#"$HOST_REPO_DIR"/}"
local file_sha
file_sha="$(hash_file_sha256 "$source_file")"
digest_input+="$rel:$file_sha"$'\n'
done < <(find "$HOST_REPO_DIR/plugin" -type f -name '*.go' -print0 | sort -z)
while IFS= read -r -d '' source_file; do
found=1
local rel="${source_file#"$HOST_REPO_DIR"/}"
local file_sha
file_sha="$(hash_file_sha256 "$source_file")"
digest_input+="$rel:$file_sha"$'\n'
done < <(find "$HOST_REPO_DIR/plugin/sdk/csharp" -type f \( -name '*.cs' -o -name '*.csproj' -o -name '*.json' -o -name '*.props' -o -name '*.targets' \) -print0 | sort -z)
while IFS= read -r -d '' source_file; do
found=1
local rel="${source_file#"$HOST_REPO_DIR"/}"
local file_sha
file_sha="$(hash_file_sha256 "$source_file")"
digest_input+="$rel:$file_sha"$'\n'
done < <(find "$HOST_REPO_DIR/plugin/sdk/rust" -type f \( -name '*.rs' -o -name '*.toml' -o -name '*.lock' \) -print0 2>/dev/null | sort -z)
while IFS= read -r -d '' source_file; do
found=1
local rel="${source_file#"$HOST_REPO_DIR"/}"
local file_sha
file_sha="$(hash_file_sha256 "$source_file")"
digest_input+="$rel:$file_sha"$'\n'
done < <(find "$HOST_REPO_DIR/internal/generator/output" -type f \( -name '*.go' -o -name '*.cs' -o -name '*.h' \) -print0 2>/dev/null | sort -z)
while IFS= read -r -d '' source_file; do
found=1
local rel="${source_file#"$HOST_REPO_DIR"/}"
local file_sha
file_sha="$(hash_file_sha256 "$source_file")"
digest_input+="$rel:$file_sha"$'\n'
done < <(find "$HOST_REPO_DIR/cmd" -type f -name '*.go' -print0 2>/dev/null | sort -z)
for mod_file in "$HOST_REPO_DIR/go.mod" "$HOST_REPO_DIR/go.sum"; do
[[ -f "$mod_file" ]] || continue
found=1
local rel="${mod_file#"$HOST_REPO_DIR"/}"
local file_sha
file_sha="$(hash_file_sha256 "$mod_file")"
digest_input+="$rel:$file_sha"$'\n'
done
if [[ "$found" -eq 0 ]]; then
printf ''
return
fi
printf '%s' "$digest_input" | hash_text_sha256
}
plugin_code_sha() {
local plugin_dir="$1"
local digest_input=""
local found=0
while IFS= read -r -d '' source_file; do
found=1
local rel="${source_file#"$plugin_dir"/}"
local file_sha
file_sha="$(hash_file_sha256 "$source_file")"
digest_input+="$rel:$file_sha"$'\n'
done < <(
find "$plugin_dir" \
-type d \( -name target -o -name obj -o -name bin \) -prune -o \
-type f \( \
-name '*.go' -o \
-name '*.cs' -o \
-name '*.csproj' -o \
-name '*.json' -o \
-name '*.props' -o \
-name '*.targets' -o \
-name '*.rs' -o \
-name '*.toml' -o \
-name '*.lock' \
\) -print0 | sort -z
)
if [[ "$found" -eq 0 ]]; then
printf ''
return
fi
printf '%s' "$digest_input" | hash_text_sha256
}
cache_key_for_id() {
local id="$1"
id="${id//\//__}"
id="${id//\\/__}"
printf '%s' "$id"
}
plugin_sha_file_path() {
local kind="$1"
local id="$2"
local safe_id
safe_id="$(cache_key_for_id "$id")"
mkdir -p "$PLUGIN_SHA_CACHE_DIR/$kind"
printf '%s' "$PLUGIN_SHA_CACHE_DIR/$kind/$safe_id.sha"
}
plugin_artifact_cache_path() {
local kind="$1"
local id="$2"
local sha="$3"
local safe_id
safe_id="$(cache_key_for_id "$id")"
mkdir -p "$PLUGIN_ARTIFACT_CACHE_DIR/$kind/$safe_id"
printf '%s' "$PLUGIN_ARTIFACT_CACHE_DIR/$kind/$safe_id/$sha.so"
}
restore_cached_plugin_artifact() {
local kind="$1"
local id="$2"
local sha="$3"
local dest="$4"
local sha_file
sha_file="$(plugin_sha_file_path "$kind" "$id")"
if [[ ! -f "$sha_file" ]]; then
return 1
fi
local cached_sha
cached_sha="$(cat "$sha_file" 2>/dev/null || true)"
if [[ "$cached_sha" != "$sha" ]]; then
return 1
fi
local cache_path
cache_path="$(plugin_artifact_cache_path "$kind" "$id" "$sha")"
if [[ ! -f "$cache_path" ]]; then
return 1
fi
mkdir -p "$(dirname "$dest")"
cp "$cache_path" "$dest"
return 0
}
store_cached_plugin_artifact() {
local kind="$1"
local id="$2"
local sha="$3"
local src="$4"
local sha_file
sha_file="$(plugin_sha_file_path "$kind" "$id")"
local cache_path
cache_path="$(plugin_artifact_cache_path "$kind" "$id" "$sha")"
mkdir -p "$(dirname "$cache_path")"
cp "$src" "$cache_path"
printf '%s\n' "$sha" >"$sha_file"
}
publish_csproj() {
local csproj_container_path="$1"
local project_name="$2"
local plugin_group="$3"
local out_dir="$RUNTIME_BUILD_DIR/csharp/$plugin_group/$project_name"
local out_container="/runtime/build/csharp/$plugin_group/$project_name"
mkdir -p "$out_dir"
local publish_output
if ! publish_output="$(run_dotnet dotnet publish "$csproj_container_path" \
-c Release \
-o "$out_container" \
-r "$PLUGIN_CS_RID" \
/p:PublishAot=true \
/p:NativeLib=Shared \
/p:SelfContained=true \
--nologo 2>&1)"; then
printf '%s\n' "$publish_output" >&2
return 1
fi
local artifact
artifact="$(resolve_linux_so "$out_dir" "$project_name")"
if [[ -z "$artifact" ]]; then
echo "error: failed to find C# plugin artifact (.so) for $project_name" >&2
exit 1
fi
local target_dir="$RUNTIME_PLUGIN_DIR/csharp/$project_name"
mkdir -p "$target_dir"
cp "$artifact" "$target_dir/$project_name.so"
BUILT_CS+=("$target_dir/$project_name.so")
}
generate_and_publish_cs() {
local plugin_dir="$1"
local plugin_name="$2"
local gen_dir="$RUNTIME_BUILD_DIR/generated-cs/$plugin_name"
local out_dir="$RUNTIME_BUILD_DIR/generated-cs-out/$plugin_name"
mkdir -p "$gen_dir" "$out_dir"
shopt -s nullglob
local cs_files=("$plugin_dir"/*.cs)
shopt -u nullglob
if [[ ${#cs_files[@]} -eq 0 ]]; then
return
fi
cp "${cs_files[@]}" "$gen_dir/"
cat > "$gen_dir/__plugin_exports.generated.cs" <<'CSWRAP'
using BedrockPlugin.Sdk.Guest;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace GeneratedPluginEntrypoint;
public static unsafe class GeneratedPluginExports
{
[ModuleInitializer]
internal static void RegisterFactory()
{
NativePlugin.Register((name, host) =>
{
var pluginType = Assembly
.GetExecutingAssembly()
.GetTypes()
.FirstOrDefault(t => !t.IsAbstract && typeof(Plugin).IsAssignableFrom(t));
if (pluginType is null)
{
throw new InvalidOperationException("No non-abstract type deriving BedrockPlugin.Sdk.Guest.Plugin was found.");
}
var ctor = pluginType.GetConstructor(new[] { typeof(string), typeof(IGuestHost) });
if (ctor is null)
{
throw new InvalidOperationException($"Type {pluginType.FullName} must define constructor (string name, IGuestHost host).");
}
return (Plugin)ctor.Invoke(new object?[] { name, host });
});
}
[UnmanagedCallersOnly(EntryPoint = "PluginLoad")]
public static int PluginLoad(NativeHostApi* hostApi, byte* pluginName) => NativePlugin.Load(hostApi, pluginName);
[UnmanagedCallersOnly(EntryPoint = "PluginUnload")]
public static void PluginUnload() => NativePlugin.Unload();
[UnmanagedCallersOnly(EntryPoint = "PluginDispatchEvent")]
public static void PluginDispatchEvent(ushort version, ushort eventId, uint flags, ulong playerId, ulong requestKey, byte* payload, uint payloadLen)
=> NativePlugin.DispatchEvent(version, eventId, flags, playerId, requestKey, payload, payloadLen);
}
CSWRAP
cat > "$gen_dir/$plugin_name.csproj" <<CSPROJ
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<OutputType>Library</OutputType>
<PublishAot>true</PublishAot>
<SelfContained>true</SelfContained>
<NativeLib>Shared</NativeLib>
<StripSymbols>false</StripSymbols>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="/workspace/plugin/sdk/csharp/BedrockPlugin.Abi.csproj" />
</ItemGroup>
</Project>
CSPROJ
publish_csproj "/runtime/build/generated-cs/$plugin_name/$plugin_name.csproj" "$plugin_name" "$plugin_name"
}
ensure_csproj_exports() {
local plugin_dir="$1"
local generated_exports="$plugin_dir/__plugin_exports.generated.cs"
shopt -s nullglob
local cs_files=("$plugin_dir"/*.cs)
shopt -u nullglob
if [[ ${#cs_files[@]} -gt 0 ]] && rg -n 'EntryPoint\s*=\s*"PluginLoad"' "${cs_files[@]}" >/dev/null 2>&1; then
return
fi
cat > "$generated_exports" <<'CSWRAP'
using BedrockPlugin.Sdk.Guest;
using System.Runtime.InteropServices;
namespace GeneratedPluginEntrypoint;
public static unsafe class GeneratedPluginExports
{
[UnmanagedCallersOnly(EntryPoint = "PluginLoad")]
public static int PluginLoad(NativeHostApi* hostApi, byte* pluginName) => NativePlugin.Load(hostApi, pluginName);
[UnmanagedCallersOnly(EntryPoint = "PluginUnload")]
public static void PluginUnload() => NativePlugin.Unload();
[UnmanagedCallersOnly(EntryPoint = "PluginDispatchEvent")]
public static void PluginDispatchEvent(ushort version, ushort eventId, uint flags, ulong playerId, ulong requestKey, byte* payload, uint payloadLen)
=> NativePlugin.DispatchEvent(version, eventId, flags, playerId, requestKey, payload, payloadLen);
}
CSWRAP
}
compile_plugin_sources() {
shopt -s nullglob
local entries=("$PLUGIN_SRC_DIR"/*)
shopt -u nullglob
for entry in "${entries[@]}"; do
[[ -d "$entry" ]] || continue
local name
name="$(basename "$entry")"
shopt -s nullglob
local go_files=("$entry"/*.go)
local csproj_files=("$entry"/*.csproj)
local cs_files=("$entry"/*.cs)
shopt -u nullglob
local cargo_manifest="$entry/Cargo.toml"
if [[ ${#go_files[@]} -gt 0 ]]; then
local go_sha
go_sha="$(plugin_code_sha "$entry")"
if [[ -n "$go_sha" && -n "$HOST_RUNTIME_ABI_SHA" ]]; then
go_sha="$(printf '%s\n%s\n' "$go_sha" "$HOST_RUNTIME_ABI_SHA" | hash_text_sha256)"
fi
local go_target="$RUNTIME_PLUGIN_DIR/$name.so"
if [[ -n "$go_sha" ]] && restore_cached_plugin_artifact "go" "$name" "$go_sha" "$go_target"; then
echo "using cached Go plugin for $PLUGIN_SRC_REL/$name"
BUILT_GO+=("$go_target")
else
echo "building Go plugin from $PLUGIN_SRC_REL/$name"
run_go go build -buildmode=plugin -o "/runtime/plugins/$name.so" "$PLUGIN_SRC_CONTAINER/$name"
BUILT_GO+=("$go_target")
if [[ -n "$go_sha" && -f "$go_target" ]]; then
store_cached_plugin_artifact "go" "$name" "$go_sha" "$go_target"
fi
fi
fi
if [[ ${#csproj_files[@]} -gt 0 ]]; then
ensure_csproj_exports "$entry"
local cs_sha
cs_sha="$(plugin_code_sha "$entry")"
if [[ -n "$cs_sha" && -n "$HOST_RUNTIME_ABI_SHA" ]]; then
cs_sha="$(printf '%s\n%s\n' "$cs_sha" "$HOST_RUNTIME_ABI_SHA" | hash_text_sha256)"
fi
for csproj in "${csproj_files[@]}"; do
local project_name
project_name="$(basename "${csproj%.csproj}")"
local cs_target="$RUNTIME_PLUGIN_DIR/csharp/$project_name/$project_name.so"
local cache_id="$name/$project_name"
if [[ -n "$cs_sha" ]] && restore_cached_plugin_artifact "csharp" "$cache_id" "$cs_sha" "$cs_target"; then
echo "using cached C# plugin for $PLUGIN_SRC_REL/$name/$(basename "$csproj")"
BUILT_CS+=("$cs_target")
continue
fi
echo "building C# plugin from $PLUGIN_SRC_REL/$name/$(basename "$csproj")"
publish_csproj "$PLUGIN_SRC_CONTAINER/$name/$project_name.csproj" "$project_name" "$name"
if [[ -n "$cs_sha" && -f "$cs_target" ]]; then
store_cached_plugin_artifact "csharp" "$cache_id" "$cs_sha" "$cs_target"
fi
done
elif [[ ${#cs_files[@]} -gt 0 ]]; then
local cs_sha
cs_sha="$(plugin_code_sha "$entry")"
if [[ -n "$cs_sha" && -n "$HOST_RUNTIME_ABI_SHA" ]]; then
cs_sha="$(printf '%s\n%s\n' "$cs_sha" "$HOST_RUNTIME_ABI_SHA" | hash_text_sha256)"
fi
local cs_target="$RUNTIME_PLUGIN_DIR/csharp/$name/$name.so"
if [[ -n "$cs_sha" ]] && restore_cached_plugin_artifact "csharp" "$name" "$cs_sha" "$cs_target"; then
echo "using cached C# plugin for $PLUGIN_SRC_REL/$name/*.cs"
BUILT_CS+=("$cs_target")
else
echo "building C# plugin from $PLUGIN_SRC_REL/$name/*.cs"
generate_and_publish_cs "$entry" "$name"
if [[ -n "$cs_sha" && -f "$cs_target" ]]; then
store_cached_plugin_artifact "csharp" "$name" "$cs_sha" "$cs_target"
fi
fi
fi
if [[ -f "$cargo_manifest" ]]; then
local rust_sha
rust_sha="$(plugin_code_sha "$entry")"
if [[ -n "$rust_sha" && -n "$HOST_RUNTIME_ABI_SHA" ]]; then
rust_sha="$(printf '%s\n%s\n' "$rust_sha" "$HOST_RUNTIME_ABI_SHA" | hash_text_sha256)"
fi
local rust_target="$RUNTIME_PLUGIN_DIR/rust/$name/$name.so"
if [[ -n "$rust_sha" ]] && restore_cached_plugin_artifact "rust" "$name" "$rust_sha" "$rust_target"; then
echo "using cached Rust plugin for $PLUGIN_SRC_REL/$name/Cargo.toml"
BUILT_RUST+=("$rust_target")
continue
fi
local rust_lib_name
rust_lib_name="$(cargo_library_name "$cargo_manifest")"
echo "building Rust plugin from $PLUGIN_SRC_REL/$name/Cargo.toml"
run_rust cargo build --release --manifest-path "$PLUGIN_SRC_CONTAINER/$name/Cargo.toml"
local rust_out_dir="$entry/target/release"
local rust_artifact
rust_artifact="$(resolve_linux_so "$rust_out_dir" "$rust_lib_name")"
if [[ -z "$rust_artifact" ]]; then
echo "error: failed to find Rust plugin artifact (.so) for $name (expected base: $rust_lib_name)" >&2
exit 1
fi
mkdir -p "$(dirname "$rust_target")"
cp "$rust_artifact" "$rust_target"
BUILT_RUST+=("$rust_target")
if [[ -n "$rust_sha" && -f "$rust_target" ]]; then
store_cached_plugin_artifact "rust" "$name" "$rust_sha" "$rust_target"
fi
fi
done
}
copy_prebuilt_artifacts() {
shopt -s nullglob
local prebuilt_root=("$PLUGIN_SRC_DIR"/*.so)
shopt -u nullglob
for so_file in "${prebuilt_root[@]}"; do
local name
name="$(basename "${so_file%.so}")"
if [[ -n "${BUILT_BY_NAME[$name]:-}" ]]; then
continue
fi
local dst="$RUNTIME_PLUGIN_DIR/$(basename "$so_file")"
cp "$so_file" "$dst"
COPIED_PREBUILT+=("$dst")
PREBUILT_GO_COPIED+=("$dst")
done
if [[ -d "$PLUGIN_SRC_DIR/csharp" ]]; then
while IFS= read -r -d '' so_file; do
local name
name="$(basename "${so_file%.so}")"
if [[ -n "${BUILT_BY_NAME[$name]:-}" ]]; then
continue
fi
local rel="${so_file#"$PLUGIN_SRC_DIR"/}"
local dst="$RUNTIME_PLUGIN_DIR/$rel"
mkdir -p "$(dirname "$dst")"
cp "$so_file" "$dst"
COPIED_PREBUILT+=("$dst")
done < <(find "$PLUGIN_SRC_DIR/csharp" -type f -name '*.so' -print0)
fi
if [[ -d "$PLUGIN_SRC_DIR/rust" ]]; then
while IFS= read -r -d '' so_file; do
local name
name="$(basename "${so_file%.so}")"
if [[ -n "${BUILT_BY_NAME[$name]:-}" ]]; then
continue
fi
local rel="${so_file#"$PLUGIN_SRC_DIR"/}"
local dst="$RUNTIME_PLUGIN_DIR/$rel"
mkdir -p "$(dirname "$dst")"
cp "$so_file" "$dst"
COPIED_PREBUILT+=("$dst")
done < <(find "$PLUGIN_SRC_DIR/rust" -type f -name '*.so' -print0)
fi
}
validate_prebuilt_go_artifacts() {
if [[ ${#PREBUILT_GO_COPIED[@]} -eq 0 ]]; then
return
fi
cat > "$RUNTIME_BUILD_DIR/check_prebuilt_go_plugin.go" <<'GOLOAD'
package main
import (
"fmt"
"os"
goplugin "plugin"
_ "github.com/bedrock-gophers/plugin/plugin/abi"
_ "github.com/bedrock-gophers/plugin/plugin/sdk/go"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "usage: check_prebuilt_go_plugin <plugin.so>")