|
| 1 | +#!/bin/sh |
| 2 | +set -ex |
| 3 | + |
| 4 | +SDK_DIR="/opt/typephp/vendor/swoole/phpx/full-static/sdk" |
| 5 | +mkdir -p "$SDK_DIR/lib/musl" "$SDK_DIR/include/php" "$SDK_DIR/include/phpx" "$SDK_DIR/include" |
| 6 | + |
| 7 | +# 1. 拷贝 musl 启动目标文件 |
| 8 | +cp -f /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o "$SDK_DIR/lib/musl/" |
| 9 | + |
| 10 | +# 2. 编译 static embed libphp.a |
| 11 | +PHP_BUILD_DIR="/tmp/php-src" |
| 12 | +mkdir -p "$PHP_BUILD_DIR" |
| 13 | +cd "$PHP_BUILD_DIR" |
| 14 | +echo "[SDK] Downloading PHP 8.5.0 source for static embed build..." |
| 15 | +curl -sSL "https://www.php.net/distributions/php-8.5.0.tar.xz" | tar -xJ --strip-components=1 |
| 16 | + |
| 17 | +./configure \ |
| 18 | + --prefix=/usr \ |
| 19 | + --enable-embed=static \ |
| 20 | + --disable-all \ |
| 21 | + --enable-cli \ |
| 22 | + --enable-session \ |
| 23 | + --enable-filter \ |
| 24 | + --enable-json \ |
| 25 | + --enable-ctype \ |
| 26 | + --enable-tokenizer \ |
| 27 | + --enable-posix \ |
| 28 | + --enable-pcntl \ |
| 29 | + --enable-sockets \ |
| 30 | + --with-zlib \ |
| 31 | + --enable-mbstring \ |
| 32 | + --enable-fileinfo \ |
| 33 | + --enable-phar \ |
| 34 | + --enable-bcmath \ |
| 35 | + --without-pear |
| 36 | + |
| 37 | +make -j$(nproc) libphp.la |
| 38 | + |
| 39 | +LIBPHP_STATIC=$(find "$PHP_BUILD_DIR" -name "libphp.a" | head -n 1) |
| 40 | +make install-headers INSTALL_ROOT=/tmp/php-install |
| 41 | +cp -r /tmp/php-install/usr/include/php/. "$SDK_DIR/include/php/" |
| 42 | + |
| 43 | +# 3. 生成 gmp 桩代码 |
| 44 | +cat << 'EOF' > /tmp/gmp_compat_stubs.c |
| 45 | +#include <stddef.h> |
| 46 | +const char * const __gmp_version = "6.3.0"; |
| 47 | +const char * const mpfr_version = "4.2.1"; |
| 48 | +int __gmpz_init(void *x) { return 0; } |
| 49 | +int __gmpz_init_set(void *x, const void *y) { return 0; } |
| 50 | +int __gmpz_init_set_ui(void *x, unsigned long y) { return 0; } |
| 51 | +int __gmpz_init_set_si(void *x, long y) { return 0; } |
| 52 | +int __gmpz_init_set_d(void *x, double y) { return 0; } |
| 53 | +int __gmpz_init_set_str(void *x, const char *s, int base) { return 0; } |
| 54 | +void __gmpz_clear(void *x) {} |
| 55 | +void __gmpz_clears(void *x, ...) {} |
| 56 | +void __gmpz_inits(void *x, ...) {} |
| 57 | +int __gmpz_set(void *x, const void *y) { return 0; } |
| 58 | +int __gmpz_set_ui(void *x, unsigned long y) { return 0; } |
| 59 | +int __gmpz_set_si(void *x, long y) { return 0; } |
| 60 | +int __gmpz_set_d(void *x, double y) { return 0; } |
| 61 | +int __gmpz_set_str(void *rop, const char *str, int base) { return 0; } |
| 62 | +int __gmpz_cmp(const void *x, const void *y) { return 0; } |
| 63 | +int __gmpz_cmp_si(const void *x, long y) { return 0; } |
| 64 | +int __gmpz_cmp_ui(const void *x, unsigned long y) { return 0; } |
| 65 | +int __gmpz_cmp_d(const void *x, double y) { return 0; } |
| 66 | +void __gmpz_add(void *r, const void *a, const void *b) {} |
| 67 | +void __gmpz_sub(void *r, const void *a, const void *b) {} |
| 68 | +void __gmpz_mul(void *r, const void *a, const void *b) {} |
| 69 | +void __gmpz_mul_si(void *r, const void *a, long b) {} |
| 70 | +void __gmpz_mul_ui(void *r, const void *a, unsigned long b) {} |
| 71 | +void __gmpz_mul_2exp(void *r, const void *a, unsigned long b) {} |
| 72 | +void __gmpz_tdiv_q(void *q, const void *a, const void *b) {} |
| 73 | +void __gmpz_tdiv_r(void *r, const void *a, const void *b) {} |
| 74 | +void __gmpz_tdiv_qr(void *q, void *r, const void *a, const void *b) {} |
| 75 | +void __gmpz_mod(void *r, const void *a, const void *b) {} |
| 76 | +void __gmpz_neg(void *r, const void *a) {} |
| 77 | +void __gmpz_abs(void *r, const void *a) {} |
| 78 | +int __gmpz_sizeinbase(const void *a, int base) { return 0; } |
| 79 | +char *__gmpz_get_str(char *s, int base, const void *a) { return (char*)0; } |
| 80 | +unsigned long __gmpz_get_ui(const void *a) { return 0; } |
| 81 | +long __gmpz_get_si(const void *a) { return 0; } |
| 82 | +double __gmpz_get_d(const void *a) { return 0.0; } |
| 83 | +void *__gmp_default_allocate(size_t s) { return (void*)0; } |
| 84 | +void *__gmp_default_reallocate(void *p, size_t os, size_t ns) { return (void*)0; } |
| 85 | +void __gmp_default_free(void *p, size_t s) {} |
| 86 | +void (*__gmp_allocate_func)(size_t) = (void*)0; |
| 87 | +void *(*__gmp_reallocate_func)(void *, size_t, size_t) = (void*)0; |
| 88 | +void (*__gmp_free_func)(void *, size_t) = (void*)0; |
| 89 | +EOF |
| 90 | +gcc -fPIC -c /tmp/gmp_compat_stubs.c -o /tmp/gmp_compat_stubs.o |
| 91 | + |
| 92 | +# 4. 合并 fat archive |
| 93 | +MERGE_DIR="/tmp/merge_objects" |
| 94 | +mkdir -p "$MERGE_DIR" |
| 95 | +for lib in "$LIBPHP_STATIC" /usr/lib/libc.a /usr/lib/libstdc++.a /usr/lib/libz.a /usr/lib/libonig.a; do |
| 96 | + if [ -f "$lib" ]; then |
| 97 | + libname="$(basename "$lib" .a)" |
| 98 | + subdir="$MERGE_DIR/$libname" |
| 99 | + mkdir -p "$subdir" |
| 100 | + ( |
| 101 | + cd "$subdir" |
| 102 | + ar x "$lib" |
| 103 | + for f in *.o; do |
| 104 | + [ -f "$f" ] && mv "$f" "${libname}_${f}" |
| 105 | + done |
| 106 | + ) || true |
| 107 | + fi |
| 108 | +done |
| 109 | +cp /tmp/gmp_compat_stubs.o "$MERGE_DIR/" |
| 110 | +find "$MERGE_DIR" -name "*.o" | sort | xargs ar rcs "$SDK_DIR/lib/libphp.a" |
| 111 | +ranlib "$SDK_DIR/lib/libphp.a" |
| 112 | + |
| 113 | +# 5. 编译 static libphpx.a |
| 114 | +cmake -S /opt/typephp/vendor/swoole/phpx/full-static -B /tmp/phpx-static-build \ |
| 115 | + -DCMAKE_BUILD_TYPE=Release \ |
| 116 | + -DPHPX_PHP_INCLUDE_DIR="$SDK_DIR/include/php" \ |
| 117 | + -DPHPX_GMP_INCLUDE_DIR=/usr/include \ |
| 118 | + -DPHPX_GMP_LIB_DIR=/usr/lib \ |
| 119 | + -DPHPX_MPFR_INCLUDE_DIR=/usr/include \ |
| 120 | + -DPHPX_MPFR_LIB_DIR=/usr/lib |
| 121 | + |
| 122 | +cmake --build /tmp/phpx-static-build --parallel $(nproc) |
| 123 | +cp -f /tmp/phpx-static-build/libphpx.a "$SDK_DIR/lib/libphpx.a" |
| 124 | + |
| 125 | +# 6. 头文件同步 |
| 126 | +cp -r /opt/typephp/vendor/swoole/phpx/include/. "$SDK_DIR/include/phpx/" |
| 127 | +cp -f /opt/typephp/vendor/swoole/phpx/thirdparty/mpdecimal/libmpdec++/decimal.hh "$SDK_DIR/include/phpx/" 2>/dev/null || true |
| 128 | +cp -f /opt/typephp/vendor/swoole/phpx/thirdparty/mpdecimal/libmpdec/mpdecimal.h "$SDK_DIR/include/phpx/" 2>/dev/null || true |
| 129 | +cp -f /usr/include/gmp.h /usr/include/gmpxx.h /usr/include/mpfr.h "$SDK_DIR/include/" 2>/dev/null || true |
| 130 | +cp -f /usr/include/gmp.h /usr/include/gmpxx.h /usr/include/mpfr.h "$SDK_DIR/include/phpx/" 2>/dev/null || true |
| 131 | +cp -f /usr/include/pcre2*.h "$SDK_DIR/include/" 2>/dev/null || true |
| 132 | +cp -f /usr/include/pcre2*.h "$SDK_DIR/include/php/" 2>/dev/null || true |
| 133 | + |
| 134 | +# 清理临时编译缓存 |
| 135 | +rm -rf /tmp/* "$PHP_BUILD_DIR" |
| 136 | +echo "[SDK] Static SDK built and staged successfully at $SDK_DIR" |
0 commit comments