Last active
October 29, 2023 15:22
-
-
Save crazywhalecc/00abd8a86dfaf69783f3ac91e207d568 to your computer and use it in GitHub Desktop.
static-php-cli add feature module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace SPC\builder\unix\library; | |
use SPC\exception\FileSystemException; | |
use SPC\exception\RuntimeException; | |
use SPC\store\FileSystem; | |
trait curl | |
{ | |
private array $features = []; | |
/** | |
* This will be written in LibraryBase or FeatureTrait. | |
*/ | |
public function applyFeatures(array $features): void | |
{ | |
foreach ($features as $feature => $enabled) { | |
$this->features[$feature] = $enabled; | |
} | |
} | |
/** | |
* This will be called before build() method. | |
*/ | |
protected function initFeatures(): void | |
{ | |
$this->features = [ | |
'openssl' => (bool) $this->builder->getLib('openssl'), | |
'brotli' => (bool) $this->builder->getLib('brotli'), | |
'libssh2' => (bool) $this->builder->getLib('libssh2'), | |
'nghttp2' => (bool) $this->builder->getLib('nghttp2'), | |
'ldap' => (bool) $this->builder->getLib('ldap'), | |
'zstd' => (bool) $this->builder->getLib('zstd'), | |
]; | |
} | |
/** | |
* This will be written in LibraryBase or FeatureTrait. | |
*/ | |
protected function isFeatureEnabled(string $feature): bool | |
{ | |
return $this->features[$feature] ?? false; | |
} | |
/** | |
* @throws RuntimeException | |
* @throws FileSystemException | |
*/ | |
protected function build(): void | |
{ | |
// This will be called before `build()` | |
// $this->initFeatures(); | |
$extra = ''; | |
// lib:openssl | |
$extra .= $this->isFeatureEnabled('openssl') ? '-DCURL_USE_OPENSSL=ON ' : '-DCURL_USE_OPENSSL=OFF -DCURL_ENABLE_SSL=OFF '; | |
// lib:brotli | |
$extra .= $this->isFeatureEnabled('brotli') ? '-DCURL_BROTLI=ON ' : '-DCURL_BROTLI=OFF '; | |
// lib:libssh2 | |
$extra .= $this->isFeatureEnabled('libssh2') ? | |
"-DLIBSSH2_LIBRARY={$this->builder->getLib('libssh2')->getStaticLibFiles(style: 'cmake')} -DLIBSSH2_INCLUDE_DIR=" . BUILD_INCLUDE_PATH . ' ' : | |
'-DCURL_USE_LIBSSH2=OFF '; | |
// lib:nghttp2 | |
$extra .= $this->isFeatureEnabled('nghttp2') ? | |
"-DUSE_NGHTTP2=ON -DNGHTTP2_LIBRARY={$this->builder->getLib('nghttp2')->getStaticLibFiles(style: 'cmake')} -DNGHTTP2_INCLUDE_DIR=" . BUILD_INCLUDE_PATH . ' ' : | |
'-DUSE_NGHTTP2=OFF '; | |
// lib:ldap | |
$extra .= $this->isFeatureEnabled('ldap') ? '-DCURL_DISABLE_LDAP=OFF ' : '-DCURL_DISABLE_LDAP=ON '; | |
// lib:zstd | |
$extra .= $this->isFeatureEnabled('zstd') ? '-DCURL_ZSTD=ON ' : '-DCURL_ZSTD=OFF '; | |
// lib:idn2 | |
// $extra .= $this->isFeatureEnabled('idn2') ? '-DUSE_LIBIDN2=ON ' : '-DUSE_LIBIDN2=OFF '; | |
// lib:psl | |
// $extra .= $this->isFeatureEnabled('psl') ? '-DCURL_USE_LIBPSL=ON ' : '-DCURL_USE_LIBPSL=OFF '; | |
FileSystem::resetDir($this->source_dir . '/build'); | |
// compile! | |
shell()->cd($this->source_dir . '/build') | |
->exec('sed -i.save s@\${CMAKE_C_IMPLICIT_LINK_LIBRARIES}@@ ../CMakeLists.txt') | |
->exec("{$this->builder->configure_env} cmake {$this->builder->makeCmakeArgs()} -DBUILD_SHARED_LIBS=OFF -DBUILD_CURL_EXE=OFF {$extra} ..") | |
->exec("make -j{$this->builder->concurrency}") | |
->exec('make install DESTDIR=' . BUILD_ROOT_PATH); | |
$this->patchPkgconfPrefix(['libcurl.pc']); | |
shell()->cd(BUILD_LIB_PATH . '/cmake/CURL/') | |
->exec("sed -ie 's|\"/lib/libcurl.a\"|\"" . BUILD_LIB_PATH . "/libcurl.a\"|g' CURLTargets-release.cmake"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment