Skip to content

Commit d30d1fc

Browse files
authored
Add php and lib-base as special libraries to add dependencies to the root node (#618)
* Remove E_STRICT * Add lib-base and php as special libs * Remove debug code * Fix phpunit with new config structure * Fix phpunit test and fix license dumper bug for new type of lib * Add missing lib type filter for windows builder
1 parent 15c7e41 commit d30d1fc

18 files changed

+138
-34
lines changed

config/lib.json

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
{
2+
"lib-base": {
3+
"type": "root",
4+
"lib-depends-unix": [
5+
"pkg-config"
6+
]
7+
},
8+
"php": {
9+
"type": "root",
10+
"source": "php-src",
11+
"lib-depends": [
12+
"micro",
13+
"lib-base"
14+
]
15+
},
16+
"micro": {
17+
"type": "target",
18+
"source": "micro"
19+
},
20+
"pkg-config": {
21+
"type": "package",
22+
"source": "pkg-config",
23+
"bin-unix": [
24+
"pkg-config"
25+
]
26+
},
227
"brotli": {
328
"source": "brotli",
429
"static-libs-unix": [
@@ -599,9 +624,6 @@
599624
"zlib"
600625
]
601626
},
602-
"pkg-config": {
603-
"source": "pkg-config"
604-
},
605627
"postgresql": {
606628
"source": "postgresql",
607629
"static-libs-unix": [

src/SPC/builder/LibraryBase.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ public function getHeaders(): array
146146
return Config::getLib(static::NAME, 'headers', []);
147147
}
148148

149+
/**
150+
* Get binary files.
151+
*
152+
* @throws FileSystemException
153+
* @throws WrongUsageException
154+
*/
155+
public function getBinaryFiles(): array
156+
{
157+
return Config::getLib(static::NAME, 'bin', []);
158+
}
159+
149160
/**
150161
* @throws WrongUsageException
151162
* @throws FileSystemException
@@ -203,7 +214,8 @@ public function tryBuild(bool $force_build = false): int
203214
}
204215
// force means just build
205216
if ($force_build) {
206-
logger()->info('Building required library [' . static::NAME . ']');
217+
$type = Config::getLib(static::NAME, 'type', 'lib');
218+
logger()->info('Building required ' . $type . ' [' . static::NAME . ']');
207219

208220
// extract first if not exists
209221
if (!is_dir($this->source_dir)) {
@@ -236,10 +248,14 @@ public function tryBuild(bool $force_build = false): int
236248
return LIB_STATUS_OK;
237249
}
238250
}
239-
// pkg-config is treated specially. If it is pkg-config, check if the pkg-config binary exists
240-
if (static::NAME === 'pkg-config' && !file_exists(BUILD_ROOT_PATH . '/bin/pkg-config')) {
241-
$this->tryBuild(true);
242-
return LIB_STATUS_OK;
251+
// current library is package and binary file is not exists
252+
if (Config::getLib(static::NAME, 'type', 'lib') === 'package') {
253+
foreach ($this->getBinaryFiles() as $name) {
254+
if (!file_exists(BUILD_BIN_PATH . "/{$name}")) {
255+
$this->tryBuild(true);
256+
return LIB_STATUS_OK;
257+
}
258+
}
243259
}
244260
// if all the files exist at this point, skip the compilation process
245261
return LIB_STATUS_ALREADY;

src/SPC/builder/unix/UnixBuilderBase.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ public function proveLibs(array $sorted_libraries): void
110110
$sorted_libraries = DependencyUtil::getLibs($libraries);
111111
}
112112

113-
// pkg-config must be compiled first, whether it is specified or not
114-
if (!in_array('pkg-config', $sorted_libraries)) {
115-
array_unshift($sorted_libraries, 'pkg-config');
116-
}
117-
118113
// add lib object for builder
119114
foreach ($sorted_libraries as $library) {
115+
if (!in_array(Config::getLib($library, 'type', 'lib'), ['lib', 'package'])) {
116+
continue;
117+
}
120118
// if some libs are not supported (but in config "lib.json", throw exception)
121119
if (!isset($support_lib_list[$library])) {
122120
throw new WrongUsageException('library [' . $library . '] is in the lib.json list but not supported to compile, but in the future I will support it!');

src/SPC/builder/windows/WindowsBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public function proveLibs(array $sorted_libraries): void
236236

237237
// add lib object for builder
238238
foreach ($sorted_libraries as $library) {
239+
if (!in_array(Config::getLib($library, 'type', 'lib'), ['lib', 'package'])) {
240+
continue;
241+
}
239242
// if some libs are not supported (but in config "lib.json", throw exception)
240243
if (!isset($support_lib_list[$library])) {
241244
throw new WrongUsageException('library [' . $library . '] is in the lib.json list but not supported to compile, but in the future I will support it!');

src/SPC/command/BaseCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function initialize(InputInterface $input, OutputInterface $output): void
4646
E_USER_ERROR => ['PHP Error: ', 'error'],
4747
E_USER_WARNING => ['PHP Warning: ', 'warning'],
4848
E_USER_NOTICE => ['PHP Notice: ', 'notice'],
49-
E_STRICT => ['PHP Strict: ', 'notice'],
5049
E_RECOVERABLE_ERROR => ['PHP Recoverable Error: ', 'error'],
5150
E_DEPRECATED => ['PHP Deprecated: ', 'notice'],
5251
E_USER_DEPRECATED => ['PHP User Deprecated: ', 'notice'],
@@ -56,7 +55,7 @@ public function initialize(InputInterface $input, OutputInterface $output): void
5655
logger()->{$level_tip[1]}($error);
5756
// 如果 return false 则错误会继续递交给 PHP 标准错误处理
5857
return true;
59-
}, E_ALL | E_STRICT);
58+
});
6059
$version = ConsoleApplication::VERSION;
6160
if (!$this->no_motd) {
6261
echo " _ _ _ _

src/SPC/command/BuildCliCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SPC\builder\BuilderProvider;
88
use SPC\exception\ExceptionHandler;
99
use SPC\exception\WrongUsageException;
10+
use SPC\store\Config;
1011
use SPC\store\FileSystem;
1112
use SPC\store\SourcePatcher;
1213
use SPC\util\DependencyUtil;
@@ -107,13 +108,14 @@ public function handle(): int
107108
$include_suggest_ext = $this->getOption('with-suggested-exts');
108109
$include_suggest_lib = $this->getOption('with-suggested-libs');
109110
[$extensions, $libraries, $not_included] = DependencyUtil::getExtsAndLibs($extensions, $libraries, $include_suggest_ext, $include_suggest_lib);
111+
$display_libs = array_filter($libraries, fn ($lib) => in_array(Config::getLib($lib, 'type', 'lib'), ['lib', 'package']));
110112

111113
// print info
112114
$indent_texts = [
113115
'Build OS' => PHP_OS_FAMILY . ' (' . php_uname('m') . ')',
114116
'Build SAPI' => $builder->getBuildTypeName($rule),
115117
'Extensions (' . count($extensions) . ')' => implode(',', $extensions),
116-
'Libraries (' . count($libraries) . ')' => implode(',', $libraries),
118+
'Libraries (' . count($libraries) . ')' => implode(',', $display_libs),
117119
'Strip Binaries' => $builder->getOption('no-strip') ? 'no' : 'yes',
118120
'Enable ZTS' => $builder->getOption('enable-zts') ? 'yes' : 'no',
119121
];

src/SPC/command/BuildLibsCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SPC\builder\BuilderProvider;
88
use SPC\exception\ExceptionHandler;
99
use SPC\exception\RuntimeException;
10+
use SPC\store\Config;
1011
use SPC\util\DependencyUtil;
1112
use Symfony\Component\Console\Attribute\AsCommand;
1213
use Symfony\Component\Console\Input\InputArgument;
@@ -61,7 +62,9 @@ public function handle(): int
6162
$builder->setLibsOnly();
6263
// 编译和检查库完整
6364
$libraries = DependencyUtil::getLibs($libraries);
64-
logger()->info('Building libraries: ' . implode(',', $libraries));
65+
$display_libs = array_filter($libraries, fn ($lib) => in_array(Config::getLib($lib, 'type', 'lib'), ['lib', 'package']));
66+
67+
logger()->info('Building libraries: ' . implode(',', $display_libs));
6568
sleep(2);
6669
$builder->proveLibs($libraries);
6770
$builder->validateLibsAndExts();

src/SPC/command/DownloadCommand.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ public function initialize(InputInterface $input, OutputInterface $output): void
7272
if ($for_ext = $input->getOption('for-extensions')) {
7373
$ext = $this->parseExtensionList($for_ext);
7474
$sources = $this->calculateSourcesByExt($ext, !$input->getOption('without-suggestions'));
75-
if (PHP_OS_FAMILY !== 'Windows') {
76-
array_unshift($sources, 'pkg-config');
77-
}
78-
array_unshift($sources, 'php-src', 'micro');
7975
$final_sources = array_merge($final_sources, array_diff($sources, $final_sources));
8076
}
8177
// mode: --for-libs
@@ -323,7 +319,10 @@ private function calculateSourcesByExt(array $extensions, bool $include_suggests
323319
}
324320
}
325321
foreach ($libraries as $library) {
326-
$sources[] = Config::getLib($library, 'source');
322+
$source = Config::getLib($library, 'source');
323+
if ($source !== null) {
324+
$sources[] = $source;
325+
}
327326
}
328327
return array_values(array_unique($sources));
329328
}

src/SPC/command/dev/SortConfigCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ public function handle(): int
3333
case 'lib':
3434
$file = json_decode(FileSystem::readFile(ROOT_DIR . '/config/lib.json'), true);
3535
ConfigValidator::validateLibs($file);
36-
ksort($file);
36+
uksort($file, function ($a, $b) use ($file) {
37+
$type_a = $file[$a]['type'] ?? 'lib';
38+
$type_b = $file[$b]['type'] ?? 'lib';
39+
$type_order = ['root', 'target', 'package', 'lib'];
40+
// compare type first
41+
if ($type_a !== $type_b) {
42+
return array_search($type_a, $type_order) <=> array_search($type_b, $type_order);
43+
}
44+
// compare name
45+
return $a <=> $b;
46+
});
3747
if (!file_put_contents(ROOT_DIR . '/config/lib.json', json_encode($file, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n")) {
3848
$this->output->writeln('<error>Write file lib.json failed!</error>');
3949
return static::FAILURE;

src/SPC/store/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function getLib(string $name, ?string $key = null, mixed $default
7373
if (!isset(self::$lib[$name])) {
7474
throw new WrongUsageException('lib [' . $name . '] is not supported yet');
7575
}
76-
$supported_sys_based = ['static-libs', 'headers', 'lib-depends', 'lib-suggests', 'frameworks'];
76+
$supported_sys_based = ['static-libs', 'headers', 'lib-depends', 'lib-suggests', 'frameworks', 'bin'];
7777
if ($key !== null && in_array($key, $supported_sys_based)) {
7878
$m_key = match (PHP_OS_FAMILY) {
7979
'Windows' => ['-windows', '-win', ''],

0 commit comments

Comments
 (0)