使用 ServBay 进行二次编译
ServBay 提供了完整的编译环境,用户可以方便地重新编译所附带的工具、添加新的模块、重编译 PHP 模块、编译 PostgreSQL 模块等。
要继续下面的步骤,用户需要具备一些编译方面的基础知识,了解如何在 Unix/Linux 环境下进行代码的编译。
安装 ServBay Development Library
进行二次编译,用户首先需要安装 ServBay Development Library。该库包含了所有编译过程中所需的库文件(.so、.a)、头文件(.h)等。ServBay Development Library 可以通过 ServBay 的 Services 面板安装。
同时,用户必须安装 Xcode Command Line Tools。可以通过以下命令安装:
bash
xcode-select --install
1
设置编译环境
安装完 ServBay Development Library 后,用户还需要设置编译参数,进行编译环境的初始化。根据用户 CPU 架构,分别为 Intel 芯片(x86_64)和 Apple Silicon 芯片(M1/M2/M3)。用户可以通过以下命令检查当前 ServBay Runtime 的架构类型,选择不同的初始化命令。
检查 CPU 架构
bash
# Arm64(M1/M2/M3)
$ file /Applications/ServBay/bin/bison
/Applications/ServBay/bin/bison: Mach-O 64-bit executable arm64
1
2
3
2
3
bash
# x86_64(Intel)
$ file /Applications/ServBay/bin/bison
/Applications/ServBay/bin/bison: Mach-O 64-bit executable x86_64
1
2
3
2
3
初始化编译环境
根据上面得到的 CPU 架构,环境初始化命令分别如下:
bash
SERVBAY_BASE_FULL_PATH="/Applications/ServBay"
SERVBAY_PACKAGE_DIR_NAME="package"
SERVBAY_PACKAGE_FULL_PATH="${SERVBAY_BASE_FULL_PATH}/${SERVBAY_PACKAGE_DIR_NAME}"
SERVBAY_BIN_PATH="${SERVBAY_PACKAGE_FULL_PATH}/bin"
SERVBAY_SBIN_PATH="${SERVBAY_PACKAGE_FULL_PATH}/sbin"
SERVBAY_ETC_PATH="${SERVBAY_PACKAGE_FULL_PATH}/etc"
SERVBAY_VAR_PATH="${SERVBAY_PACKAGE_FULL_PATH}/var"
SERVBAY_COMMON_PATH="${SERVBAY_PACKAGE_FULL_PATH}/common"
SERVBAY_COMMON_INCLUDE_PATH="${SERVBAY_COMMON_PATH}/include"
SERVBAY_COMMON_LIB_PATH="${SERVBAY_COMMON_PATH}/lib"
SERVBAY_COMMON_LIBEXEC_PATH="${SERVBAY_COMMON_PATH}/libexec"
SERVBAY_COMMON_SHARE_PATH="${SERVBAY_COMMON_PATH}/share"
SERVBAY_COMMON_MAN_PATH="${SERVBAY_COMMON_PATH}/man"
SERVBAY_COMMON_DOC_PATH="${SERVBAY_COMMON_PATH}/doc"
SERVBAY_BUILD_DIR=$(pwd)
SERVBAY_BUILD_SRC_DIR=${SERVBAY_BUILD_DIR}/src
OPENSSL_SELECTED_VERSION="3.2"
OPENSSL_VERSION="3.2.1"
BUILD_OS_MIN_VERSION="12.00"
BUILD_CPU_ARCH="-arch arm64"
BUILD_CPU_ARCH_CMAKE="arm64"
BUILD_MACOS_TARGET=""
export CFLAGS="-Qunused-arguments -O3 -mmacosx-version-min=${BUILD_OS_MIN_VERSION} ${BUILD_CPU_ARCH} ${BUILD_MACOS_TARGET}"
export LDFLAGS="-Wl,-rpath -Wl,${SERVBAY_COMMON_LIB_PATH} -L${SERVBAY_COMMON_LIB_PATH} -L${SERVBAY_PACKAGE_FULL_PATH} -mmacosx-version-min=${BUILD_OS_MIN_VERSION} ${BUILD_CPU_ARCH} ${BUILD_MACOS_TARGET}"
export CPPFLAGS="-I${SERVBAY_COMMON_INCLUDE_PATH}"
export CXXFLAGS="-Qunused-arguments -O3 -mmacosx-version-min=${BUILD_OS_MIN_VERSION} ${BUILD_CPU_ARCH} ${BUILD_MACOS_TARGET}"
export PATH="${SERVBAY_BIN_PATH}:${SERVBAY_SBIN_PATH}:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin"
export PKG_CONFIG_PATH="${SERVBAY_COMMON_LIB_PATH}/pkgconfig"
export PKG_CONFIG="${SERVBAY_BIN_PATH}/pkg-config"
export PKG_CONFIG_LIBDIR="${SERVBAY_COMMON_LIB_PATH}/pkgconfig"
export PATH=$PATH:"/usr/local/bin:/usr/local/sbin"
export PATH=$PATH:"/opt/homebrew/bin/"
export LDFLAGS="-L${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/lib ${LDFLAGS}"
export CPPFLAGS="-I${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/include ${CPPFLAGS}"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/lib/pkgconfig"
export PKG_CONFIG_LIBDIR="${PKG_CONFIG_LIBDIR}:${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/lib/pkgconfig"
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
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
bash
SERVBAY_BASE_FULL_PATH="/Applications/ServBay"
SERVBAY_PACKAGE_DIR_NAME="package"
SERVBAY_PACKAGE_FULL_PATH="${SERVBAY_BASE_FULL_PATH}/${SERVBAY_PACKAGE_DIR_NAME}"
SERVBAY_BIN_PATH="${SERVBAY_PACKAGE_FULL_PATH}/bin"
SERVBAY_SBIN_PATH="${SERVBAY_PACKAGE_FULL_PATH}/sbin"
SERVBAY_ETC_PATH="${SERVBAY_PACKAGE_FULL_PATH}/etc"
SERVBAY_VAR_PATH="${SERVBAY_PACKAGE_FULL_PATH}/var"
SERVBAY_COMMON_PATH="${SERVBAY_PACKAGE_FULL_PATH}/common"
SERVBAY_COMMON_INCLUDE_PATH="${SERVBAY_COMMON_PATH}/include"
SERVBAY_COMMON_LIB_PATH="${SERVBAY_COMMON_PATH}/lib"
SERVBAY_COMMON_LIBEXEC_PATH="${SERVBAY_COMMON_PATH}/libexec"
SERVBAY_COMMON_SHARE_PATH="${SERVBAY_COMMON_PATH}/share"
SERVBAY_COMMON_MAN_PATH="${SERVBAY_COMMON_PATH}/man"
SERVBAY_COMMON_DOC_PATH="${SERVBAY_COMMON_PATH}/doc"
SERVBAY_BUILD_DIR=$(pwd)
SERVBAY_BUILD_SRC_DIR=${SERVBAY_BUILD_DIR}/src
OPENSSL_SELECTED_VERSION="1.1.1u"
OPENSSL_VERSION="1.1.1u"
BUILD_OS_MIN_VERSION="12.00"
BUILD_CPU_ARCH="-arch x86_64"
BUILD_CPU_ARCH_CMAKE="x86_64"
BUILD_MACOS_TARGET="-target x86_64-apple-macos${BUILD_OS_MIN_VERSION}"
export CFLAGS="-Qunused-arguments -O3 -mmacosx-version-min=${BUILD_OS_MIN_VERSION} ${BUILD_CPU_ARCH} ${BUILD_MACOS_TARGET}"
export LDFLAGS="-Wl,-rpath -Wl,${SERVBAY_COMMON_LIB_PATH} -L${SERVBAY_COMMON_LIB_PATH} -L${SERVBAY_PACKAGE_FULL_PATH} -mmacosx-version-min=${BUILD_OS_MIN_VERSION} ${BUILD_CPU_ARCH} ${BUILD_MACOS_TARGET}"
export CPPFLAGS="-I${SERVBAY_COMMON_INCLUDE_PATH}"
export CXXFLAGS="-Qunused-arguments -O3 -mmacosx-version-min=${BUILD_OS_MIN_VERSION} ${BUILD_CPU_ARCH} ${BUILD_MACOS_TARGET}"
export PATH="${SERVBAY_BIN_PATH}:${SERVBAY_SBIN_PATH}:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin"
export PKG_CONFIG_PATH="${SERVBAY_COMMON_LIB_PATH}/pkgconfig"
export PKG_CONFIG="${SERVBAY_BIN_PATH}/pkg-config"
export PKG_CONFIG_LIBDIR="${SERVBAY_COMMON_LIB_PATH}/pkgconfig"
export PATH=$PATH:"/usr/local/bin:/usr/local/sbin"
export PATH=$PATH:"/opt/homebrew/bin/"
export LDFLAGS="-L${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/lib ${LDFLAGS}"
export CPPFLAGS="-I${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/include ${CPPFLAGS}"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/lib/pkgconfig"
export PKG_CONFIG_LIBDIR="${PKG_CONFIG_LIBDIR}:${SERVBAY_COMMON_PATH}/openssl/${OPENSSL_SELECTED_VERSION}/lib/pkgconfig"
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
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
编译示例
设置完编译环境后,就可以进行代码的二次编译了。下面是一些例子:
编译 ImageMagick
ImageMagick 是 PHP imagick 模块的依赖。ImageMagick 在 ServBay 出厂时已经附带,无需用户自己重新编译。以下只是为了举例说明如何进行软件包的编译。
bash
wget https://imagemagick.org/archive/releases/ImageMagick-7.1.1-33.tar.gz
tar zxvf ImageMagick-7.1.1-33.tar.gz
cd ImageMagick-7.1.1-33
./configure --prefix=${SERVBAY_COMMON_PATH} --libdir=${SERVBAY_COMMON_LIB_PATH} --includedir=${SERVBAY_COMMON_INCLUDE_PATH} --oldincludedir=${SERVBAY_COMMON_INCLUDE_PATH} --bindir=${SERVBAY_BIN_PATH} --sbindir=${SERVBAY_SBIN_PATH} --sysconfdir=${SERVBAY_ETC_PATH} --datarootdir=${SERVBAY_COMMON_SHARE_PATH} --enable-osx-universal-binary=no --disable-silent-rules --disable-opencl --enable-shared --enable-static --with-freetype=yes --with-gvc=no --with-modules --with-webp=yes --with-heic=no --without-gslib --with-fpx=no --without-openexr --with-raw=yes --without-lqr --without-djvu --without-fftw --without-pango --without-wmf --without-x --with-gs-font-dir=${SERVBAY_COMMON_SHARE_PATH}/ghostscript/10.02.1/Resource/Font
make -j ${CPU_NUMBER}
make install
1
2
3
4
5
6
2
3
4
5
6
使用 cmake 编译软件包
对于使用 cmake
来进行编译的软件包,可以参考以下方式。下面以 protobuf
举例:
bash
cmake -S . -B builddir -DCMAKE_INSTALL_PREFIX=${SERVBAY_COMMON_PATH} -DCMAKE_INSTALL_LIBDIR=${SERVBAY_COMMON_LIB_PATH} -DCMAKE_INSTALL_INCLUDEDIR=${SERVBAY_COMMON_INCLUDE_PATH} -DCMAKE_INSTALL_BINDIR=${SERVBAY_BIN_PATH} -DCMAKE_INSTALL_SBINDIR=${SERVBAY_SBIN_PATH} -DCMAKE_INSTALL_SYSCONFDIR=${SERVBAY_ETC_PATH} -DCMAKE_INSTALL_DATAROOTDIR=${SERVBAY_COMMON_SHARE_PATH} -DCMAKE_MACOSX_RPATH=1 -DCMAKE_INSTALL_RPATH=${SERVBAY_COMMON_LIB_PATH} -DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE -DCMAKE_PREFIX_PATH=${SERVBAY_COMMON_PATH} -DBUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_LIBPROTOC=ON -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_INSTALL_EXAMPLES=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_ABSL_PROVIDER=package -Dprotobuf_JSONCPP_PROVIDER=module -DCMAKE_CXX_STANDARD=17
cmake --build builddir -j ${CPU_NUMBER}
cmake --install builddir
1
2
3
2
3
编译 PHP 模块
请参考文章如何编译PHP模块
编译PostgreSQL模块
请参考文章如何编译PostgreSQL模块
通过上述方法,用户可以对 ServBay 进行定制,以满足业务上的需求。