在aarch64机器上安装clang来生成codonjit python模块

一开始想用docker安装, 结果拉取的镜像CPU架构不对,无法启动。

复制代码
nbs@kylin-pc:~/桌面$ sudo docker pull ghcr.io/silkeh/clang:21-trixie
输入密码         
21-trixie: Pulling from silkeh/clang
0b233165d073: Pull complete 
bce2073474a5: Pull complete 
c7a5eef2ddc9: Pull complete 
Digest: sha256:8c5aee059ffdc87dcf5d177f50fbf85602568e42d1580e41b01d8a4e7563c65d
Status: Downloaded newer image for ghcr.io/silkeh/clang:21-trixie
ghcr.io/silkeh/clang:21-trixie


nbs@kylin-pc:~/桌面$ sudo docker run -itd -v /home/nbs/par:/par --network=host --name clang21 ghcr.io/silkeh/clang:21-trixie
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
648b20a8afb2517453fab6287f60060b07893f07869cd6cf87b2a8523b4cd70d

改用apt来安装,先进入一个空的ubuntu容器。先修改apt源为清华源,然后依次执行apt update,apt install clang。

复制代码
nbs@kylin-pc:~/桌面$ sudo docker start ubuntu26
ubuntu26
nbs@kylin-pc:~/桌面$ sudo docker exec -it ubuntu26 bash
root@kylin-pc:/# vi /etc/apt/sources.list
bash: vi: command not found
root@kylin-pc:/# mv /etc/apt/sources.list /etc/apt/sources.list.old

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute-backports main restricted universe multiverse

# 以下安全更新软件源为官方源配置
deb http://security.ubuntu.com/ubuntu/ resolute-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ resolute-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ resolute-proposed main restricted universe multiverse

root@kylin-pc:/# cp /par/sources.list /etc/apt/sources.list


root@kylin-pc:/# apt update
Get:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu resolute InRelease [136 kB]
...
Get:24 http://ports.ubuntu.com/ubuntu-ports resolute-security/main arm64 Packages [60.1 kB]
Fetched 24.0 MB in 4s (6087 kB/s)   
18 packages can be upgraded. Run 'apt list --upgradable' to see them.
Notice: Some sources can be modernized. Run 'apt modernize-sources' to do so.
root@kylin-pc:/# apt install clang
Upgrading:                      
  libc-bin  libc-gconv-modules-extra  libc6

Installing:
  clang

Installing dependencies:
  binfmt-support              gcc-15-base             libclang-cpp21      libffi-dev     libjansson4     libobjc4          libubsan1       llvm-21-linker-tools  python3-yaml
  binutils                    libasan8                libclang-rt-21-dev  libgc1         libllvm21       libpfm4           libxml2-dev     llvm-21-runtime       rpcsvc-proto
  binutils-aarch64-linux-gnu  libbinutils             libclang1-21        libgcc-15-dev  liblsan0        libpipeline1      libyaml-0-2     llvm-21-tools         zlib1g-dev
  binutils-common             libc-dev-bin            libctf-nobfd0       libgprofng0    liblzma-dev     libsframe3        linux-libc-dev  manpages
  clang-21                    libc6-dev               libctf0             libhwasan0     libncurses-dev  libstdc++-15-dev  llvm-21         manpages-dev
  clang-tools-21              libclang-common-21-dev  libedit2            libitm1        libobjc-15-dev  libtsan2          llvm-21-dev     python3-pygments

Suggested packages:
  binutils-doc  binutils-gold  wasi-libc      glibc-doc    ncurses-doc       pkgconf      man-browser          ttf-bitstream-vera
  gprofng-gui   clang-21-doc   libc-devtools  liblzma-doc  libstdc++-15-doc  llvm-21-doc  python-pygments-doc

Summary:
  Upgrading: 3, Installing: 52, Removing: 0, Not Upgrading: 15
  Download size: 168 MB
  Space needed: 928 MB / 140 GB available

Continue? [Y/n] 
...
Setting up clang (1:21.1.6-71) ...
Setting up llvm-21-dev (1:21.1.8-6ubuntu1) ...
Processing triggers for libc-bin (2.43-2ubuntu2) ...
root@kylin-pc:/# clang --version
Ubuntu clang version 21.1.8 (6ubuntu1)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-21/bin

安装的版本是21.1.8。

然后用sqlite3源码测试clang编译。比gcc编译的sqlite3运行速度要慢一点。

复制代码
root@kylin-pc:/# cd /par/sqlite*8
root@kylin-pc:/par/sqlite-20260511102558-45a2152908# clang shell.c sqlite3.c -O3 -lpthread -lm -ldl -o sqlite3cl
root@kylin-pc:/par/sqlite-20260511102558-45a2152908# time ./sqlite3cl < ../sum.sql
╭──────────────────╮
│    sum(value)    │
╞══════════════════╡
│ 5000000050000000 │
╰──────────────────╯

real	0m3.057s
user	0m3.039s
sys	0m0.001s
root@kylin-pc:/par/sqlite-20260511102558-45a2152908# time ./sqlite3o2 < ../sum.sql
╭──────────────────╮
│    sum(value)    │
╞══════════════════╡
│ 5000000050000000 │
╰──────────────────╯

real	0m2.599s
user	0m2.574s
sys	0m0.004s

检查python版本,建立虚拟环境,设定codon安装目录为搜索路径,用pip安装codon-jit模块。

复制代码
root@kylin-pc:/# python3
Python 3.14.4 (main, Apr  8 2026, 04:02:31) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 


root@kylin-pc:/par/tpy3145# python/bin/python -m venv codonenv
root@kylin-pc:/par/tpy3145# source codonenv/bin/activate

(codonenv) root@kylin-pc:/par/tpy3145# export PATH=/par/codon/bin:$PATH
(codonenv) root@kylin-pc:/par/tpy3145# pip install codon-jit --no-index -f ../tpy314/314
Looking in links: ../tpy314/314
Processing /par/tpy314/314/codon_jit-0.4.6.tar.gz
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Processing /par/tpy314/314/cython-3.2.4-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (from codon-jit)
Processing /par/tpy314/314/astunparse-1.6.3-py2.py3-none-any.whl (from codon-jit)
Processing /par/tpy314/314/wheel-0.47.0-py3-none-any.whl (from astunparse->codon-jit)
Processing /par/tpy314/314/six-1.17.0-py2.py3-none-any.whl (from astunparse->codon-jit)
Processing /par/tpy314/314/packaging-26.2-py3-none-any.whl (from wheel<1.0,>=0.23.0->astunparse->codon-jit)
Building wheels for collected packages: codon-jit
  Building wheel for codon-jit (pyproject.toml) ... done
  Created wheel for codon-jit: filename=codon_jit-0.4.6-cp314-cp314-linux_aarch64.whl size=108289 sha256=afb7a1ccdf2f0c1719a4242aeac4d795401da68c1bc1dd62c2664041c396e146
  Stored in directory: /root/.cache/pip/wheels/9f/36/32/3748d95f34594fa442964a7d1b6f99f14360459c5ca6a36caf
Successfully built codon-jit
Installing collected packages: six, packaging, cython, wheel, astunparse, codon-jit
Successfully installed astunparse-1.6.3 codon-jit-0.4.6 cython-3.2.4 packaging-26.2 six-1.17.0 wheel-0.47.0

执行测试脚本报错,缺少numpy,安装后就能够执行了。

复制代码
(codonenv) root@kylin-pc:/par/tpy3145# time python ../test-codonjit.py
Traceback (most recent call last):
  File "/par/tpy3145/../test-codonjit.py", line 1, in <module>
    import codon
  File "/par/tpy3145/codonenv/lib/python3.14/site-packages/codon/__init__.py", line 7, in <module>
    from .decorator import jit, convert, execute, JITError, JITWrapper, _jit_register_fn, _jit_callback_fn, _jit
  File "/par/tpy3145/codonenv/lib/python3.14/site-packages/codon/decorator.py", line 13, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

real	0m0.062s
user	0m0.049s
sys	0m0.012s
(codonenv) root@kylin-pc:/par/tpy3145# pip install numpy --no-index -f ../tpy314/314
Looking in links: ../tpy314/314
Processing /par/tpy314/314/numpy-2.4.4-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Installing collected packages: numpy
Successfully installed numpy-2.4.4
(codonenv) root@kylin-pc:/par/tpy3145# time python ../test-codonjit.py
[python] 8392 | took 56.61279606819153 seconds
[codon]  8392 | took 3.6273505687713623 seconds

real	1m10.909s
user	1m10.861s
sys	0m0.638s
(codonenv) root@kylin-pc:/par/tpy3145# time codon run ../test-codon.py
[python] 8392 | took 1.32626 seconds

real	0m3.896s
user	0m4.436s
sys	0m0.143s
(codonenv) root@kylin-pc:/par/tpy3145# time codon run --release ../test-codon.py
[python] 8392 | took 1.39554 seconds

real	0m3.903s
user	0m4.487s
sys	0m0.076s

但是build二进制文件不行。报g++错误,但已经生成了test-codon.o目标文件。

复制代码
(codonenv) root@kylin-pc:/par/tpy3145# time codon build --release ../test-codon.py
error: process for 'g++' exited with status 255

real	0m2.461s
user	0m3.045s
sys	0m0.109s
(codonenv) root@kylin-pc:/par/tpy3145# clang ../test-codon.o -o ../test-codoncl


/usr/bin/aarch64-linux-gnu-ld.bfd: ../test-codon.o: in function `main':
test-codon.py:(.text+0x24): undefined reference to `seq_init'
/usr/bin/aarch64-linux-gnu-ld.bfd: test-codon.py:(.text+0x28): undefined reference to `seq_stdout'
/usr/bin/aarch64-linux-gnu-ld.bfd: test-codon.py:(.text+0x30): undefined reference to `seq_stdin'
...
/usr/bin/aarch64-linux-gnu-ld.bfd: test-codon.py:(.text+0x150): undefined reference to `seq_alloc_atomic'
/usr/bin/aarch64-linux-gnu-ld.bfd: test-codon.py:(.text+0x1c8): undefined reference to `seq_print_full'
/usr/bin/aarch64-linux-gnu-ld.bfd: test-codon.py:(.text+0x1dc): undefined reference to `seq_print_full'
/usr/bin/aarch64-linux-gnu-ld.bfd: ../test-codon.o:(.data.DW.ref.seq_personality[DW.ref.seq_personality]+0x0): undefined reference to `seq_personality'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

想用clang代替g++链接.o文件,也不行。

相关推荐
z落落38 分钟前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
CTA量化套保1 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
allway21 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446231 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了1 小时前
安装git bash选项推荐
开发语言·git·bash
GIS数据转换器1 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
ct9781 小时前
React 状态管理方案深度对比
开发语言·前端·react
贤哥哥yyds1 小时前
GBK转UTF\-8编码自动转换工具 使用文档
python
数量技术宅2 小时前
2026量化前沿:从Reddit热帖到Python实战,如何用赫斯特指数(Hurst)狙击虚假突破?
开发语言·python
华如锦2 小时前
面了很多 Java转AI Agent方向,一些面试题总结
java·开发语言·人工智能·python·ai