|
| 1 | +--- |
| 2 | +title: 在 RISC-V 架构设备上安装 |
| 3 | +--- |
| 4 | + |
| 5 | +## 系统环境 |
| 6 | + |
| 7 | +本文编写基于 Sipeed Lichee Pi 4A 16G, 使用 RevyOS 20250526_182059 固件,感谢 [甲辰计划](https://rv2036.org/) 提供的硬件支持, |
| 8 | +感谢 [Anillc](https://github.com/Anillc) 和 [NickCao](https://github.com/NickCao) 提供的帮助。 |
| 9 | + |
| 10 | +## 初始化 Nix 环境 |
| 11 | + |
| 12 | +```bash |
| 13 | +curl -sSL https://hydro.ac/nix.sh | bash |
| 14 | +``` |
| 15 | + |
| 16 | +为了节约生命,配置额外的 binary cache: `https://cache.nichi.co https://nix-community.cachix.org` |
| 17 | +`nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs= hydra.nichi.co-0:P3nkYHhmcLR3eNJgOAnHDjmQLkfqheGyhZ6GLrUVHwk=` |
| 18 | + |
| 19 | +## 构建 MongoDB |
| 20 | + |
| 21 | +MongoDB 没有提供官方的 RISC-V 支持,因此我们需要自行编译 MongoDB (如果使用 MongoDB Atlas 则可跳过该步骤)。 |
| 22 | + |
| 23 | +克隆 MongoDB 存储库: |
| 24 | + |
| 25 | +```bash |
| 26 | +git clone [email protected]:mongodb/mongo --depth=1 --branch=r7.0.22 |
| 27 | +``` |
| 28 | + |
| 29 | +下载 mozjs release: |
| 30 | + |
| 31 | +```bash |
| 32 | +cd src/third_party/mozjs |
| 33 | +bash get-sources.sh |
| 34 | +``` |
| 35 | + |
| 36 | +给同目录的 `gen-config.sh` 添加 RISC-V 支持: |
| 37 | + |
| 38 | +```patch |
| 39 | +diff --git a/src/third_party/mozjs/gen-config.sh b/src/third_party/mozjs/gen-config.sh |
| 40 | +index 9144a461..141e089b 100755 |
| 41 | +--- a/src/third_party/mozjs/gen-config.sh |
| 42 | ++++ b/src/third_party/mozjs/gen-config.sh |
| 43 | +@@ -61,6 +61,9 @@ case "$_Path" in |
| 44 | + _xcode_setup "macosx" "x86_64" "macos-version-min=10.9" |
| 45 | + _CONFIG_OPTS="--host=x86_64-apple-darwin" |
| 46 | + ;; |
| 47 | ++ "platform/riscv64/linux") |
| 48 | ++ _CONFIG_OPTS="--host=riscv64-linux" |
| 49 | ++ ;; |
| 50 | + "platform/aarch64/iOS") |
| 51 | + _xcode_setup "iphoneos" "arm64" "iphoneos-version-min=10.2" |
| 52 | + _CONFIG_OPTS="--target=aarch64-apple-darwin" |
| 53 | +``` |
| 54 | + |
| 55 | +运行 `gen-config.sh` 得到 `platform/riscv64/linux` 目录: |
| 56 | + |
| 57 | +```bash |
| 58 | +bash gen-config.sh riscv64 linux |
| 59 | +``` |
| 60 | + |
| 61 | +```patch |
| 62 | +diff --git a/src/third_party/mozjs/platform/riscv64/linux/build/Unified_cpp_js_src0.cpp b/src/third_party/mozjs/platform/riscv64/linux/build/Unified_cpp_js_src0.cpp |
| 63 | +new file mode 100644 |
| 64 | +index 00000000..624b0d97 |
| 65 | +--- /dev/null |
| 66 | ++++ b/src/third_party/mozjs/platform/riscv64/linux/build/Unified_cpp_js_src0.cpp |
| 67 | +@@ -0,0 +1,55 @@ |
| 68 | ++#define MOZ_UNIFIED_BUILD |
| 69 | ++#include "builtin/Array.cpp" |
| 70 | ++#ifdef PL_ARENA_CONST_ALIGN_MASK |
| 71 | ++#error "builtin/Array.cpp uses PL_ARENA_CONST_ALIGN_MASK, so it cannot be built in unified mode." |
| 72 | ++#undef PL_ARENA_CONST_ALIGN_MASK |
| 73 | ++#endif |
| 74 | ++#ifdef INITGUID |
| 75 | +# 行数过多,下略 |
| 76 | +``` |
| 77 | + |
| 78 | +修改 MongoDB 的 `platform/pause.h` ,添加 RISC-V 支持: |
| 79 | + |
| 80 | +```patch |
| 81 | +diff --git a/src/mongo/platform/pause.h b/src/mongo/platform/pause.h |
| 82 | +index 67fe3d94..b3f50efb 100644 |
| 83 | +--- a/src/mongo/platform/pause.h |
| 84 | ++++ b/src/mongo/platform/pause.h |
| 85 | +@@ -69,7 +69,7 @@ |
| 86 | + |
| 87 | + #define MONGO_YIELD_CORE_FOR_SMT() __asm__ volatile("rd %%ccr, %%g0" ::: "memory") |
| 88 | + |
| 89 | +-#elif defined(__EMSCRIPTEN__) |
| 90 | ++#elif defined(__EMSCRIPTEN__) || defined(__riscv) |
| 91 | + |
| 92 | + // TODO: What should this be? |
| 93 | + #define MONGO_YIELD_CORE_FOR_SMT() |
| 94 | +``` |
| 95 | + |
| 96 | +将上述 patch 传入 nixpkgs: |
| 97 | + |
| 98 | +```patch |
| 99 | +diff --git a/pkgs/servers/nosql/mongodb/7.0.nix b/pkgs/servers/nosql/mongodb/7.0.nix |
| 100 | +index cb4bbf53279f..0526976d4e8b 100644 |
| 101 | +--- a/pkgs/servers/nosql/mongodb/7.0.nix |
| 102 | ++++ b/pkgs/servers/nosql/mongodb/7.0.nix |
| 103 | +@@ -35,7 +35,11 @@ buildMongoDB { |
| 104 | + # mongodb-7_0's mozjs uses avx2 instructions |
| 105 | + # https://github.com/GermanAizek/mongodb-without-avx/issues/16 |
| 106 | + ] |
| 107 | +- ++ lib.optionals (!avxSupport) [ ./mozjs-noavx.patch ]; |
| 108 | ++ ++ lib.optionals (!avxSupport) [ ./mozjs-noavx.patch ] |
| 109 | ++ ++ lib.optionals (stdenv.hostPlatform.isRiscV64) [ |
| 110 | ++ ./mongodb-riscv.patch |
| 111 | ++ ./mozjs-riscv.patch |
| 112 | ++ ]; |
| 113 | + |
| 114 | + passthru.tests = { |
| 115 | + inherit (nixosTests) mongodb; |
| 116 | +``` |
| 117 | + |
| 118 | +以上修改可参照 [Nixpkgs PR#430577](https://github.com/NixOS/nixpkgs/pull/430577) |
| 119 | + |
| 120 | +构建得到 MongoDB 7.0.22 的 RISC-V 版本,使用 `nix copy` 传入目标设备。 |
| 121 | + |
| 122 | +## 安装各种组件并启动 |
| 123 | + |
| 124 | +```bash |
| 125 | +nix profile add nixpkgs#bashInteractive nixpkgs#nodejs nixpkgs#yarn nixpkgs#pm2 nixpkgs#gawk \ |
| 126 | + nixpkgs#go-judge nixpkgs#gcc # and other compilers... |
| 127 | +yarn global add hydrooj @hydrooj/ui-default @hydrooj/hydrojudge |
| 128 | +hydrooj addon add @hydrooj/ui-default |
| 129 | +hydrooj addon add @hydrooj/hydrojudge |
| 130 | +pm2 start hydrooj |
| 131 | +``` |
| 132 | + |
| 133 | +## 编译器/语言兼容性 |
| 134 | + |
| 135 | +- 测试正常:bash, c, c++, python3, nodejs, go, ruby |
| 136 | +- 暂无法使用:java, pascal, kotlin, php, rust, csharp |
0 commit comments