cloudflare workerd把玩-编译workerd(一)

背景

最近迷上了cloudflare的workers和pages,这个东西能做不少事情主要是还有免费额度。当然现在的博客就是部署在cloudflare的pages上面的,还能写api服务、前端打包部署(通过Wrangler CLI这个工具)能做部署大多数框架生成的前端工程;这样就能前后端都部署在cloudflare workers中了;安装Wrangler CLI可以参考下面的命令,当然官方文件中也写得很清楚:

1
yarn add --user wrangler

我也是无意中发现的cloudflare他们开源了自己的workers引擎,具体是在哪里发现的也记不太清楚了;于是呼就研究了一番workerd。

编译workerd

要研究这个项目肯定是少不了自己编译,参考官方的github给出的文档进行了编译,官方给出在linux下面编译环境如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Bazel
If you use Bazelisk (recommended), it will automatically download and use the right version of Bazel for building workerd.

On Linux:
We use the clang/LLVM toolchain to build workerd and support version 16 and higher. Earlier versions of clang may still work, but are not officially supported.

Clang 16+ (e.g. package clang-16 on Debian Bookworm). If clang is installed as clang-<version> please create a symlink to it in your PATH named clang, or use --action_env=CC=clang-<version> on bazel command lines to specify the compiler name.

libc++ 16+ (e.g. packages libc++-16-dev and libc++abi-16-dev)

LLD 16+ (e.g. package lld-16).

python3, python3-distutils, and tcl8.6

You may then build workerd at the command-line with:
1
bazel build //src/workerd/server:workerd

我准备的系统和官方推荐的一致:
1.系统:Debian Bookworm
2.编译器:Clang 16
3.其他依赖的组件有clang-16,libc++-16-dev,libc++abi-16-dev,lld-16,python3,python3-distutils,tcl8.6
这里需要注意我使用的lxc创建的debian bookworm, 官方的这个文档中在我准备的这个系统中缺少了一些组件和一些配置:
1.因为我是使用官方推荐的Bazelisk,官方给的编译命令是Bazel build …,因为Bazelisk自动安装的Bazel在系统环境变量中找不到,所以应到替换成Bazelisk build …;这样就会自动下载特定版本的Bazel并运行了。
2.因为使用lxc创建的debian,如果直接安装好官方所需的组件后直接编译会提示缺少git;故最好在编译前安装好git。
3.到后边编译的时候会提示tclsh命令找不到,这是因为tclsh8.6没有生成软连接;最好在编译前手动生成一下。
3.后面生成带调试信息的workerd运行结束的时候会提示找不到llvm-symbolizer;同理手动生成一下llvm-symbolizer-16的软连接到llvm-symbolizer。

1
2
3
4
➜  helloworld git:(main) ../../bazel-bin/src/workerd/server/workerd serve config.capnp           
^Ckj/exception.c++:357: warning: llvm-symbolizer was not found. To symbolize stack traces, install it in your $PATH or set $LLVM_SYMBOLIZER to the location of the binary. When running tests under bazel, use `--test_env=LLVM_SYMBOLIZER=<path>`.
*** Received signal #2: Interrupt
stack: /lib/x86_64-linux-gnu/libc.so.6@108c1d ../../bazel-bin/src/workerd/server/workerd@a82418a ../../bazel-bin/src/workerd/server/workerd@a77784d ../../bazel-bin/src/workerd/server/workerd@a778ee8 ../../bazel-bin/src/workerd/server/workerd@3e9458e ../../bazel-bin/src/workerd/server/workerd@3ea9112 ../../bazel-bin/src/workerd/server/workerd@3ea8bac ../../bazel-bin/src/workerd/server/workerd@3ea8b88 ../../bazel-bin/src/workerd/server/workerd@3ea8b5e ../../bazel-bin/src/workerd/server/workerd@3ea8ab3 ../../bazel-bin/src/workerd/server/workerd@3ea8a83 ../../bazel-bin/src/workerd/server/workerd@a86356f ../../bazel-bin/src/workerd/server/workerd@a862035 ../../bazel-bin/src/workerd/server/workerd@a86a690 ../../bazel-bin/src/workerd/server/workerd@a863c51 ../../bazel-bin/src/workerd/server/workerd@a860f86 ../../bazel-bin/src/workerd/server/workerd@a86a690 ../../bazel-bin/src/workerd/server/workerd@a863c51 ../../bazel-bin/src/workerd/server/workerd@a86742b ../../bazel-bin/src/workerd/server/workerd@a85d52c ../../bazel-bin/src/workerd/server/workerd@a85d31a ../../bazel-bin/src/workerd/server/workerd@3e63189 /lib/x86_64-linux-gnu/libc.so.6@27249 /lib/x86_64-linux-gnu/libc.so.6@27304 ../../bazel-bin/src/workerd/server/workerd@3e63020

编译workerd会下载很多的依赖的第三方库,很多东西都是在墙外需要自行准备好梯子。

编译带调试信息的workerd

要研究workerd,不可避免的需要调试它;因为workerd是使用Bazel编译的,通过查找官方文档得知只需要在build后面加上-c dbg参数就能生成带调试信息的workerd了,如下:

1
bazelisk build -c dbg //src/workerd/server:workerd

这样生成的workerd相当的庞大,我这边生成后文件大小来到了1.3G。生成后的文件在:bazel-bin/src/workerd/server/workerd

1
2
➜  helloworld git:(main) ls -alh ../../bazel-bin/src/workerd/server/workerd
-r-xr-xr-x 1 root root 1.3G Jan 6 16:47 ../../bazel-bin/src/workerd/server/workerd