|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SPC\util; |
| 6 | + |
| 7 | +use SPC\builder\BuilderBase; |
| 8 | +use SPC\builder\BuilderProvider; |
| 9 | +use SPC\builder\macos\MacOSBuilder; |
| 10 | +use SPC\store\Config; |
| 11 | +use Symfony\Component\Console\Input\ArgvInput; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | + |
| 14 | +class SPCConfigUtil |
| 15 | +{ |
| 16 | + public function __construct(private ?BuilderBase $builder = null, ?InputInterface $input = null) |
| 17 | + { |
| 18 | + if ($builder === null) { |
| 19 | + $this->builder = BuilderProvider::makeBuilderByInput($input ?? new ArgvInput()); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public function config(array $extensions = [], array $libraries = [], bool $include_suggest_ext = false, bool $include_suggest_lib = false): array |
| 24 | + { |
| 25 | + [$extensions, $libraries] = DependencyUtil::getExtsAndLibs($extensions, $libraries, $include_suggest_ext, $include_suggest_lib); |
| 26 | + |
| 27 | + ob_start(); |
| 28 | + $this->builder->proveLibs($libraries); |
| 29 | + $this->builder->proveExts($extensions); |
| 30 | + ob_get_clean(); |
| 31 | + $ldflags = $this->getLdflagsString(); |
| 32 | + $libs = $this->getLibsString($libraries); |
| 33 | + $cflags = $this->getIncludesString(); |
| 34 | + |
| 35 | + // embed |
| 36 | + $libs = '-lphp -lc ' . $libs; |
| 37 | + $extra_env = getenv('SPC_CMD_VAR_PHP_MAKE_EXTRA_LIBS'); |
| 38 | + if (is_string($extra_env)) { |
| 39 | + $libs .= ' ' . $extra_env; |
| 40 | + } |
| 41 | + // c++ |
| 42 | + if ($this->builder->hasCpp()) { |
| 43 | + $libs .= $this->builder instanceof MacOSBuilder ? ' -lc++' : ' -lstdc++'; |
| 44 | + } |
| 45 | + return [ |
| 46 | + 'cflags' => $cflags, |
| 47 | + 'ldflags' => $ldflags, |
| 48 | + 'libs' => $libs, |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + private function getIncludesString(): string |
| 53 | + { |
| 54 | + $base = BUILD_INCLUDE_PATH; |
| 55 | + $php_embed_includes = [ |
| 56 | + "-I{$base}", |
| 57 | + "-I{$base}/php", |
| 58 | + "-I{$base}/php/main", |
| 59 | + "-I{$base}/php/TSRM", |
| 60 | + "-I{$base}/php/Zend", |
| 61 | + "-I{$base}/php/ext", |
| 62 | + ]; |
| 63 | + return implode(' ', $php_embed_includes); |
| 64 | + } |
| 65 | + |
| 66 | + private function getLdflagsString(): string |
| 67 | + { |
| 68 | + return '-L' . BUILD_LIB_PATH; |
| 69 | + } |
| 70 | + |
| 71 | + private function getLibsString(array $libraries): string |
| 72 | + { |
| 73 | + $short_name = []; |
| 74 | + foreach (array_reverse($libraries) as $library) { |
| 75 | + $libs = Config::getLib($library, 'static-libs', []); |
| 76 | + foreach ($libs as $lib) { |
| 77 | + $short_name[] = $this->getShortLibName($lib); |
| 78 | + } |
| 79 | + if (PHP_OS_FAMILY !== 'Darwin') { |
| 80 | + continue; |
| 81 | + } |
| 82 | + foreach (Config::getLib($library, 'frameworks', []) as $fw) { |
| 83 | + $ks = '-framework ' . $fw; |
| 84 | + if (!in_array($ks, $short_name)) { |
| 85 | + $short_name[] = $ks; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + // patch: imagick (imagemagick wrapper) for linux needs -lgomp |
| 90 | + if (in_array('imagemagick', $libraries) && PHP_OS_FAMILY === 'Linux') { |
| 91 | + $short_name[] = '-lgomp'; |
| 92 | + } |
| 93 | + return implode(' ', $short_name); |
| 94 | + } |
| 95 | + |
| 96 | + private function getShortLibName(string $lib): string |
| 97 | + { |
| 98 | + if (!str_starts_with($lib, 'lib') || !str_ends_with($lib, '.a')) { |
| 99 | + return BUILD_LIB_PATH . '/' . $lib; |
| 100 | + } |
| 101 | + // get short name |
| 102 | + return '-l' . substr($lib, 3, -2); |
| 103 | + } |
| 104 | +} |
0 commit comments