@@ -220,7 +220,7 @@ public function patchBeforeSharedConfigure(): bool
220220 */
221221 public function patchBeforeSharedMake (): bool
222222 {
223- $ config = (new SPCConfigUtil ($ this ->builder ))->config ([ $ this -> getName ()], array_map ( fn ( $ l ) => $ l -> getName (), $ this -> builder -> getLibs ()) );
223+ $ config = (new SPCConfigUtil ($ this ->builder ))->getExtensionConfig ( $ this );
224224 [$ staticLibs , $ sharedLibs ] = $ this ->splitLibsIntoStaticAndShared ($ config ['libs ' ]);
225225 $ lstdcpp = str_contains ($ sharedLibs , '-l:libstdc++.a ' ) ? '-l:libstdc++.a ' : null ;
226226 $ lstdcpp ??= str_contains ($ sharedLibs , '-lstdc++ ' ) ? '-lstdc++ ' : '' ;
@@ -486,18 +486,46 @@ public function isBuildStatic(): bool
486486 return $ this ->build_static ;
487487 }
488488
489+ /**
490+ * Get the library dependencies that current extension depends on.
491+ *
492+ * @param bool $recursive Whether it includes dependencies recursively
493+ */
494+ public function getLibraryDependencies (bool $ recursive = false ): array
495+ {
496+ $ ret = array_filter ($ this ->dependencies , fn ($ x ) => $ x instanceof LibraryBase);
497+ if (!$ recursive ) {
498+ return $ ret ;
499+ }
500+
501+ $ deps = [];
502+
503+ $ added = 1 ;
504+ while ($ added !== 0 ) {
505+ $ added = 0 ;
506+ foreach ($ ret as $ depName => $ dep ) {
507+ foreach ($ dep ->getDependencies (true ) as $ depdepName => $ depdep ) {
508+ if (!array_key_exists ($ depdepName , $ deps )) {
509+ $ deps [$ depdepName ] = $ depdep ;
510+ ++$ added ;
511+ }
512+ }
513+ if (!array_key_exists ($ depName , $ deps )) {
514+ $ deps [$ depName ] = $ dep ;
515+ }
516+ }
517+ }
518+
519+ return $ deps ;
520+ }
521+
489522 /**
490523 * Returns the environment variables a shared extension needs to be built.
491524 * CFLAGS, CXXFLAGS, LDFLAGS and so on.
492525 */
493526 protected function getSharedExtensionEnv (): array
494527 {
495- $ config = (new SPCConfigUtil ($ this ->builder ))->config (
496- [$ this ->getName ()],
497- array_map (fn ($ l ) => $ l ->getName (), $ this ->getLibraryDependencies (recursive: true )),
498- $ this ->builder ->getOption ('with-suggested-exts ' ),
499- $ this ->builder ->getOption ('with-suggested-libs ' ),
500- );
528+ $ config = (new SPCConfigUtil ($ this ->builder ))->getExtensionConfig ($ this );
501529 [$ staticLibs , $ sharedLibs ] = $ this ->splitLibsIntoStaticAndShared ($ config ['libs ' ]);
502530 $ preStatic = PHP_OS_FAMILY === 'Darwin ' ? '' : '-Wl,--start-group ' ;
503531 $ postStatic = PHP_OS_FAMILY === 'Darwin ' ? '' : ' -Wl,--end-group ' ;
@@ -519,7 +547,7 @@ protected function addLibraryDependency(string $name, bool $optional = false): v
519547 }
520548 logger ()->info ("enabling {$ this ->name } without library {$ name }" );
521549 } else {
522- $ this ->dependencies [] = $ depLib ;
550+ $ this ->dependencies [$ name ] = $ depLib ;
523551 }
524552 }
525553
@@ -532,7 +560,7 @@ protected function addExtensionDependency(string $name, bool $optional = false):
532560 }
533561 logger ()->info ("enabling {$ this ->name } without extension {$ name }" );
534562 } else {
535- $ this ->dependencies [] = $ depExt ;
563+ $ this ->dependencies [$ name ] = $ depExt ;
536564 }
537565 }
538566
@@ -567,37 +595,4 @@ protected function splitLibsIntoStaticAndShared(string $allLibs): array
567595 }
568596 return [trim ($ staticLibString ), trim ($ sharedLibString )];
569597 }
570-
571- private function getLibraryDependencies (bool $ recursive = false ): array
572- {
573- $ ret = array_filter ($ this ->dependencies , fn ($ x ) => $ x instanceof LibraryBase);
574- if (!$ recursive ) {
575- return $ ret ;
576- }
577-
578- $ deps = [];
579-
580- $ added = 1 ;
581- while ($ added !== 0 ) {
582- $ added = 0 ;
583- foreach ($ ret as $ depName => $ dep ) {
584- foreach ($ dep ->getDependencies (true ) as $ depdepName => $ depdep ) {
585- if (!array_key_exists ($ depdepName , $ deps )) {
586- $ deps [$ depdepName ] = $ depdep ;
587- ++$ added ;
588- }
589- }
590- if (!array_key_exists ($ depName , $ deps )) {
591- $ deps [$ depName ] = $ dep ;
592- }
593- }
594- }
595-
596- if (array_key_exists (0 , $ deps )) {
597- $ zero = [0 => $ deps [0 ]];
598- unset($ deps [0 ]);
599- return $ zero + $ deps ;
600- }
601- return $ deps ;
602- }
603598}
0 commit comments