使用 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 進行定製,以滿足業務上的需求。