fix(postgres): 手编 libscws 绕开 automake 不兼容(老项目 Makefile.am 跟新 automake 冲突)

This commit is contained in:
mavis
2026-06-15 18:57:48 +08:00
parent 1fe986e96c
commit 21c0b5559a

View File

@@ -4,43 +4,56 @@ FROM postgres:16-bookworm
# Debian / PGDG 仓库里都没有 postgresql-16-zhparser / libscws-dev / scws 包, # Debian / PGDG 仓库里都没有 postgresql-16-zhparser / libscws-dev / scws 包,
# 必须从 GitHub 拉源码自己 make。 # 必须从 GitHub 拉源码自己 make。
# #
# 关键点:scws 仓库年代久远,自带的 autotools 脚本跟新 automake (>=1.16) 不兼容
# (Makefile.am:24: error: '#' comment at start of rule is unportable)
# 所以绕过 autotools,直接 gcc 编译 libscws/*.c 然后 ld 拼 .so
#
# 步骤: # 步骤:
# 1) 装编译工具链 + PG dev headers # 1) 装编译工具链 + PG dev headers
# 2) 编译 scws(autotools 项目,先 ./acprep 生成 configure) # 2) 手编 libscws(绕过 autotools)+ 装到 /usr/local
# 3) 编译 zhparser(PGXS 扩展,直接 make) # 3) 编译 zhparser(PGXS 扩展,直接 make)
# #
# build 时间:~3-5 分钟 # build 时间:~3-5 分钟
# 产物:/usr/lib/postgresql/16/lib/zhparser.so + /usr/lib/libscws.so # 产物:
# /usr/local/lib/libscws.so*
# /usr/local/include/scws/*.h
# /usr/lib/postgresql/16/lib/zhparser.so
RUN set -eux; \ RUN set -eux; \
# 1) 编译工具 + PG dev headers
apt-get update; \ apt-get update; \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
build-essential \ build-essential \
git \ git \
ca-certificates \ ca-certificates \
# autotools(scws 依赖)
autoconf \
automake \
libtool \
# pg_config / pgxs(编译 PG 扩展用) # pg_config / pgxs(编译 PG 扩展用)
postgresql-server-dev-16; \ postgresql-server-dev-16; \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*
# 2) 编译装 scws(autotools:先 ./acprep 生成 configure,再 ./configure && make) # 2) 手编 libscws(绕过 autotools — Makefile.am 不兼容新 automake)
RUN set -eux; \ RUN set -eux; \
cd /tmp; \ cd /tmp; \
git clone --depth 1 https://github.com/hightman/scws.git; \ git clone --depth 1 https://github.com/hightman/scws.git; \
cd scws; \ cd scws/libscws; \
./acprep; \ # scws 的 autotools 会生成 version.h,手编自己造
./configure --prefix=/usr; \ printf '#define SCWS_VERSION "1.2.3"\n#define SCWS_BUILT __DATE__ " " __TIME__\n' > version.h; \
make -j$(nproc); \ # 编译所有 .c
make install; \ gcc -c -O2 -fPIC -Wall \
charset.c crc32.c pool.c scws.c xdict.c darray.c rule.c lock.c xdb.c xtree.c; \
# 装头文件
mkdir -p /usr/local/include/scws; \
cp charset.h crc32.h pool.h scws.h xdict.h darray.h rule.h xdb.h xtree.h version.h /usr/local/include/scws/; \
# 拼 .so
gcc -shared -Wl,-soname,libscws.so.1 -o libscws.so.1.0.0 *.o -lc; \
cp libscws.so.1.0.0 /usr/local/lib/; \
ln -sf libscws.so.1.0.0 /usr/local/lib/libscws.so.1; \
ln -sf libscws.so.1.0.0 /usr/local/lib/libscws.so; \
ldconfig; \ ldconfig; \
cd /; \ cd /; \
rm -rf /tmp/scws rm -rf /tmp/scws
# 3) 编译 zhparser(PGXS 扩展,直接 make) # 3) 编译 zhparser(PGXS 直接 make)
# 注意:zhparser 内部用 #include <scws/scws.h>,需要 /usr/local/include 在 CPATH 上
ENV CPATH=/usr/local/include
RUN set -eux; \ RUN set -eux; \
cd /tmp; \ cd /tmp; \
git clone --depth 1 https://github.com/zhparser/zhparser.git; \ git clone --depth 1 https://github.com/zhparser/zhparser.git; \
@@ -50,5 +63,5 @@ RUN set -eux; \
cd /; \ cd /; \
rm -rf /tmp/zhparser rm -rf /tmp/zhparser
# 验证:扩展文件 + scws .so 都已就位 # 验证
RUN ls -la /usr/lib/postgresql/16/lib/zhparser.so /usr/lib/libscws.so* 2>&1 | head -5 RUN ls -la /usr/lib/postgresql/16/lib/zhparser.so /usr/local/lib/libscws.so* 2>&1 | head -5