diff --git a/config/ext.json b/config/ext.json index af586a0d..ceb1ee09 100644 --- a/config/ext.json +++ b/config/ext.json @@ -4,7 +4,7 @@ }, "bz2": { "type": "builtin", - "arg-type": "custom", + "arg-type": "with-prefix", "lib-depends": [ "bzip2" ] @@ -17,7 +17,7 @@ }, "curl": { "type": "builtin", - "arg-type": "with", + "arg-type": "custom", "lib-depends": [ "curl" ] @@ -28,6 +28,7 @@ }, "dom": { "type": "builtin", + "arg-type": "custom", "arg-type-windows": "with", "lib-depends": [ "libxml2", @@ -38,7 +39,7 @@ "type": "builtin" }, "ffi": { - "arg-type": "with", + "arg-type": "custom", "type": "builtin", "lib-depends": [ "libffi" @@ -58,6 +59,7 @@ }, "gd": { "type": "builtin", + "arg-type": "custom", "arg-type-windows": "with", "lib-depends": [ "zlib", @@ -97,14 +99,14 @@ }, "gmp": { "type": "builtin", - "arg-type": "none", + "arg-type": "with-prefix", "lib-depends": [ "gmp" ] }, "iconv": { "type": "builtin", - "arg-type": "custom", + "arg-type": "with-prefix", "lib-depends-windows": [ "libiconv" ] @@ -140,6 +142,7 @@ }, "mbstring": { "type": "builtin", + "arg-type": "custom", "lib-depends": [ "onig" ] @@ -164,7 +167,7 @@ }, "openssl": { "type": "builtin", - "arg-type": "with", + "arg-type": "custom", "lib-depends": [ "openssl" ] @@ -207,7 +210,7 @@ }, "phar": { "type": "builtin", - "lib-suggests": [ + "ext-depends": [ "zlib" ] }, @@ -239,7 +242,8 @@ }, "redis": { "type": "external", - "source": "redis" + "source": "redis", + "arg-type": "custom" }, "session": { "type": "builtin" @@ -249,6 +253,7 @@ }, "simplexml": { "type": "builtin", + "arg-type": "custom", "arg-type-windows": "with", "lib-depends": [ "libxml2" @@ -263,6 +268,7 @@ }, "soap": { "type": "builtin", + "arg-type": "custom", "lib-depends": [ "libxml2" ] @@ -279,7 +285,7 @@ }, "sqlite3": { "type": "builtin", - "arg-type": "with", + "arg-type": "custom", "lib-depends": [ "sqlite" ] @@ -287,6 +293,7 @@ "swoole": { "type": "external", "source": "swoole", + "arg-type": "custom", "lib-depends": [ "openssl" ], @@ -305,6 +312,7 @@ "swow": { "type": "external", "source": "swow", + "arg-type": "custom", "lib-suggests": [ "openssl", "curl" @@ -338,6 +346,7 @@ }, "xml": { "type": "builtin", + "arg-type": "custom", "arg-type-windows": "with", "lib-depends": [ "libxml2" @@ -345,12 +354,14 @@ }, "xmlreader": { "type": "builtin", + "arg-type": "custom", "lib-depends": [ "libxml2" ] }, "xmlwriter": { "type": "builtin", + "arg-type": "custom", "lib-depends": [ "libxml2" ] @@ -365,14 +376,14 @@ "yaml": { "type": "external", "source": "yaml", - "arg-type": "with", + "arg-type": "with-prefix", "lib-depends": [ "libyaml" ] }, "zip": { "type": "builtin", - "arg-type": "with", + "arg-type": "custom", "arg-type-windows": "enable", "lib-depends": [ "libzip" @@ -389,6 +400,7 @@ "zstd": { "type": "external", "source": "ext-zstd", + "arg-type": "custom", "lib-depends": [ "zstd" ] diff --git a/src/SPC/builder/Extension.php b/src/SPC/builder/Extension.php index abe60f8a..7e81cb88 100644 --- a/src/SPC/builder/Extension.php +++ b/src/SPC/builder/Extension.php @@ -14,8 +14,9 @@ class Extension protected array $dependencies = []; /** - * @throws RuntimeException * @throws FileSystemException + * @throws RuntimeException + * @throws WrongUsageException */ public function __construct(protected string $name, protected BuilderBase $builder) { @@ -34,13 +35,14 @@ class Extension * 获取开启该扩展的 PHP 编译添加的参数 * * @throws FileSystemException|RuntimeException + * @throws WrongUsageException */ public function getConfigureArg(): string { $arg = $this->getEnableArg(); switch (PHP_OS_FAMILY) { case 'Windows': - $arg .= $this->getWindowsConfigureArg(); + $arg = $this->getWindowsConfigureArg(); break; case 'Darwin': case 'Linux': @@ -55,15 +57,17 @@ class Extension * * @throws FileSystemException * @throws RuntimeException + * @throws WrongUsageException */ public function getEnableArg(): string { $_name = str_replace('_', '-', $this->name); return match ($arg_type = Config::getExt($this->name, 'arg-type', 'enable')) { - 'enable' => '--enable-' . $_name, - 'with' => '--with-' . $_name, + 'enable' => '--enable-' . $_name . ' ', + 'with' => '--with-' . $_name . ' ', + 'with-prefix' => '--with-' . $_name . '="' . BUILD_ROOT_PATH . '" ', 'none', 'custom' => '', - default => throw new WrongUsageException("argType does not accept {$arg_type}, use [enable/with] ."), + default => throw new WrongUsageException("argType does not accept {$arg_type}, use [enable/with/with-prefix] ."), }; } @@ -84,6 +88,7 @@ class Extension * * @throws RuntimeException * @throws FileSystemException + * @throws WrongUsageException */ public function checkDependency(): static { @@ -117,10 +122,18 @@ class Extension */ public function getDistName(): string { - return match ($this->name) { - 'mbregex' => 'mbstring', - default => $this->name, - }; + return $this->name; + } + + public function getWindowsConfigureArg(): string + { + return ''; + // Windows is not supported yet + } + + public function getUnixConfigureArg(): string + { + return ''; } /** @@ -155,164 +168,6 @@ class Extension } } - private function getWindowsConfigureArg(): string - { - $arg = ''; - switch ($this->name) { - case 'redis': - // $arg = '--enable-redis'; - // if ($this->builder->getLib('zstd')) { - // $arg .= ' --enable-redis-zstd --with-libzstd '; - // } - break; - case 'xml': - case 'soap': - case 'xmlreader': - case 'xmlwriter': - case 'dom': - $arg .= ' --with-libxml '; - break; - case 'swow': - if ($this->builder->getLib('openssl')) { - $arg .= ' --enable-swow-ssl'; - } - if ($this->builder->getLib('curl')) { - $arg .= ' --enable-swow-curl'; - } - break; - } - return $arg; - } - - private function getUnixConfigureArg(): string - { - $arg = ''; - switch ($this->name) { - /*case 'event': - $arg = ' --with-event-core --with-event-libevent-dir="' . BUILD_ROOT_PATH . '"'; - if ($this->builder->getLib('openssl')) { - $arg .= ' --with-event-openssl --with-openssl-dir="' . BUILD_ROOT_PATH . '"'; - } - break;*/ - case 'enchant': - $glibs = [ - '/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgio-2.0.a', - '/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libglib-2.0.a', - '/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgmodule-2.0.a', - '/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgobject-2.0.a', - '/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libgthread-2.0.a', - '/Users/jerry/project/git-project/static-php-cli/buildroot/lib/libintl.a', - ]; - $arg = ' --with-enchant="' . BUILD_ROOT_PATH . '"'; - $arg .= ' ENCHANT2_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '/enchant-2"'; - $arg .= ' ENCHANT2_LIBS="' . $this->getLibFilesString() . '"'; - $arg .= ' GLIB_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '"'; - $arg .= ' GLIB_LIBS="' . implode(' ', $glibs) . '"'; - break; - case 'iconv': - $arg = ' --with-iconv="' . BUILD_ROOT_PATH . '"'; - break; - case 'mbstring': - $arg = ' --disable-mbregex ONIG_CFLAGS=-I"' . BUILD_ROOT_PATH . '" ' . - 'ONIG_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'gmp': - $arg = ' --with-gmp="' . BUILD_ROOT_PATH . '" '; - break; - case 'sqlite3': - $arg = ' --with-sqlite3="' . BUILD_ROOT_PATH . '" ' . - 'SQLITE_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'SQLITE_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'redis': - $arg = ' --enable-redis --disable-redis-session'; - if ($this->builder->getLib('zstd')) { - $arg .= ' --enable-redis-zstd --with-libzstd="' . BUILD_ROOT_PATH . '" '; - } - break; - case 'yaml': - $arg .= ' --with-yaml="' . BUILD_ROOT_PATH . '" '; - break; - case 'zstd': - $arg .= ' --with-libzstd'; - break; - case 'bz2': - $arg = ' --with-bz2="' . BUILD_ROOT_PATH . '" '; - break; - case 'openssl': - $arg .= ' ' . - 'OPENSSL_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'OPENSSL_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'curl': - $arg .= ' ' . - 'CURL_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'CURL_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'gd': - if ($this->builder->getLib('freetype')) { - $arg .= ' --with-freetype ' . - 'FREETYPE2_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '/freetype2" ' . - 'FREETYPE2_LIBS="' . $this->getLibFilesString() . '" '; - } - $arg .= ' ' . - 'PNG_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'PNG_LIBS="' . $this->getLibFilesString() . '" '; - break; - // TODO: other libraries - case 'phar': - case 'zlib': - $arg .= ' ' . - 'ZLIB_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'ZLIB_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'xml': // xml may use expat - if ($this->getLibraryDependencies()['expat'] ?? null) { - $arg .= ' --with-expat="' . BUILD_ROOT_PATH . '" ' . - 'EXPAT_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'EXPAT_LIBS="' . $this->getLibFilesString() . '" '; - break; - } - // no break - case 'soap': - case 'xmlreader': - case 'xmlwriter': - case 'dom': - $arg .= ' --with-libxml="' . BUILD_ROOT_PATH . '" ' . - 'LIBXML_CFLAGS=-I"' . realpath('include/libxml2') . '" ' . - 'LIBXML_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'ffi': - $arg .= ' ' . - 'FFI_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'FFI_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'zip': - $arg .= ' ' . - 'LIBZIP_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'LIBZIP_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'mbregex': - $arg .= ' ' . - 'ONIG_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . - 'ONIG_LIBS="' . $this->getLibFilesString() . '" '; - break; - case 'swow': - $arg .= $this->builder->getLib('openssl') ? ' --enable-swow-ssl' : ' --disable-swow-ssl'; - $arg .= $this->builder->getLib('curl') ? ' --enable-swow-curl' : ' --disable-swow-curl'; - break; - case 'swoole': - if ($this->builder->getLib('openssl')) { - $arg .= ' --enable-openssl'; - } else { - $arg .= ' --disable-openssl --without-openssl'; - } - // curl hook is buggy for static php - $arg .= ' --disable-swoole-curl'; - } - return $arg; - } - private function getLibraryDependencies(bool $recursive = false): array { $ret = array_filter($this->dependencies, fn ($x) => $x instanceof LibraryBase); diff --git a/src/SPC/builder/extension/curl.php b/src/SPC/builder/extension/curl.php new file mode 100644 index 00000000..e7805ff3 --- /dev/null +++ b/src/SPC/builder/extension/curl.php @@ -0,0 +1,18 @@ +getLibFilesString() . '"'; + } +} diff --git a/src/SPC/builder/extension/enchant.php b/src/SPC/builder/extension/enchant.php new file mode 100644 index 00000000..26578519 --- /dev/null +++ b/src/SPC/builder/extension/enchant.php @@ -0,0 +1,30 @@ +getLibFilesString() . '"'; + $arg .= ' GLIB_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '"'; + $arg .= ' GLIB_LIBS="' . implode(' ', $glibs) . '"'; + return $arg; + } +} diff --git a/src/SPC/builder/extension/ffi.php b/src/SPC/builder/extension/ffi.php new file mode 100644 index 00000000..106f2a64 --- /dev/null +++ b/src/SPC/builder/extension/ffi.php @@ -0,0 +1,18 @@ +getLibFilesString() . '"'; + } +} diff --git a/src/SPC/builder/extension/gd.php b/src/SPC/builder/extension/gd.php new file mode 100644 index 00000000..b1efe170 --- /dev/null +++ b/src/SPC/builder/extension/gd.php @@ -0,0 +1,25 @@ +builder->getLib('freetype')) { + $arg .= ' --with-freetype ' . + 'FREETYPE2_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '/freetype2" ' . + 'FREETYPE2_LIBS="' . $this->getLibFilesString() . '"'; + } + $arg .= ' PNG_CFLAGS=-I"' . BUILD_INCLUDE_PATH . '" ' . + 'PNG_LIBS="' . $this->getLibFilesString() . '"'; + return $arg; + } +} diff --git a/src/SPC/builder/extension/mbregex.php b/src/SPC/builder/extension/mbregex.php new file mode 100644 index 00000000..48d15128 --- /dev/null +++ b/src/SPC/builder/extension/mbregex.php @@ -0,0 +1,17 @@ +builder->getExt('mbregex') === null) { + $arg .= ' --disable-mbregex'; + } + return $arg . ' ONIG_CFLAGS=-I"' . BUILD_ROOT_PATH . '" ONIG_LIBS="' . $this->getLibFilesString() . '"'; + } +} diff --git a/src/SPC/builder/extension/openssl.php b/src/SPC/builder/extension/openssl.php new file mode 100644 index 00000000..0668bf67 --- /dev/null +++ b/src/SPC/builder/extension/openssl.php @@ -0,0 +1,18 @@ +getLibFilesString() . '" '; + } +} diff --git a/src/SPC/builder/extension/redis.php b/src/SPC/builder/extension/redis.php new file mode 100644 index 00000000..0c0d6600 --- /dev/null +++ b/src/SPC/builder/extension/redis.php @@ -0,0 +1,21 @@ +builder->getLib('zstd')) { + $arg .= ' --enable-redis-zstd --with-libzstd="' . BUILD_ROOT_PATH . '"'; + } + return $arg; + } +} diff --git a/src/SPC/builder/extension/sqlite3.php b/src/SPC/builder/extension/sqlite3.php new file mode 100644 index 00000000..c4623f63 --- /dev/null +++ b/src/SPC/builder/extension/sqlite3.php @@ -0,0 +1,19 @@ +getLibFilesString() . '"'; + } +} diff --git a/src/SPC/builder/extension/swoole.php b/src/SPC/builder/extension/swoole.php new file mode 100644 index 00000000..f848d77d --- /dev/null +++ b/src/SPC/builder/extension/swoole.php @@ -0,0 +1,25 @@ +builder->getLib('openssl')) { + $arg .= ' --enable-openssl'; + } else { + $arg .= ' --disable-openssl --without-openssl'; + } + // curl hook is buggy for static php + $arg .= ' --disable-swoole-curl'; + return $arg; + } +} diff --git a/src/SPC/builder/extension/swow.php b/src/SPC/builder/extension/swow.php new file mode 100644 index 00000000..97b5c337 --- /dev/null +++ b/src/SPC/builder/extension/swow.php @@ -0,0 +1,20 @@ +builder->getLib('openssl') ? ' --enable-swow-ssl' : ' --disable-swow-ssl'; + $arg .= $this->builder->getLib('curl') ? ' --enable-swow-curl' : ' --disable-swow-curl'; + return $arg; + } +} diff --git a/src/SPC/builder/extension/xml.php b/src/SPC/builder/extension/xml.php new file mode 100644 index 00000000..1110d783 --- /dev/null +++ b/src/SPC/builder/extension/xml.php @@ -0,0 +1,38 @@ +name) { + 'xml' => '--enable-xml', + 'soap' => '--enable-soap', + 'xmlreader' => '--enable-xmlreader', + 'xmlwriter' => '--enable-xmlwriter', + 'dom' => '--enable-dom', + 'simplexml' => '--enable-simplexml', + default => throw new RuntimeException('Not accept non-xml extension'), + }; + $arg .= ' --with-libxml="' . BUILD_ROOT_PATH . '" ' . + 'LIBXML_CFLAGS=-I"' . realpath('include/libxml2') . '" ' . + 'LIBXML_LIBS="' . $this->getLibFilesString() . '" '; + return $arg; + } +} diff --git a/src/SPC/builder/extension/zip.php b/src/SPC/builder/extension/zip.php new file mode 100644 index 00000000..d259441f --- /dev/null +++ b/src/SPC/builder/extension/zip.php @@ -0,0 +1,18 @@ +getLibFilesString() . '"'; + } +} diff --git a/src/SPC/builder/extension/zlib.php b/src/SPC/builder/extension/zlib.php new file mode 100644 index 00000000..ed0ff4f1 --- /dev/null +++ b/src/SPC/builder/extension/zlib.php @@ -0,0 +1,18 @@ +getLibFilesString() . '"'; + } +} diff --git a/src/SPC/builder/extension/zstd.php b/src/SPC/builder/extension/zstd.php new file mode 100644 index 00000000..6ea6ee65 --- /dev/null +++ b/src/SPC/builder/extension/zstd.php @@ -0,0 +1,17 @@ +getAttributes(CustomExt::class) as $attribute) { + self::$custom_ext_class[$attribute->getArguments()[0]] = $class; + } + } + } + + public static function getExtClass(string $ext_name): string + { + return self::$custom_ext_class[$ext_name] ?? Extension::class; + } +}