55namespace SPC \builder \unix ;
66
77use SPC \builder \BuilderBase ;
8+ use SPC \builder \linux \SystemUtil as LinuxSystemUtil ;
89use SPC \exception \SPCInternalException ;
910use SPC \exception \ValidationException ;
1011use SPC \exception \WrongUsageException ;
1516use SPC \store \pkg \GoXcaddy ;
1617use SPC \toolchain \GccNativeToolchain ;
1718use SPC \toolchain \ToolchainManager ;
18- use SPC \toolchain \ZigToolchain ;
1919use SPC \util \DependencyUtil ;
2020use SPC \util \GlobalEnvManager ;
2121use SPC \util \SPCConfigUtil ;
@@ -32,68 +32,6 @@ abstract class UnixBuilderBase extends BuilderBase
3232 /** @var string LD flags */
3333 public string $ arch_ld_flags ;
3434
35- private ?string $ dynamic_export_list = null ;
36-
37- public function getDynamicExportSymbolsArgument (): ?string
38- {
39- if ($ this ->dynamic_export_list ) {
40- return $ this ->dynamic_export_list ;
41- }
42- if (SPCTarget::isStatic ()) {
43- return null ;
44- }
45-
46- $ defined = [];
47- $ libphp = BUILD_LIB_PATH . '/libphp.a ' ;
48- if (!is_file ($ libphp )) {
49- throw new WrongUsageException ('You must build libphp.a before calling this function. ' );
50- }
51-
52- if ($ out = shell_exec ('nm -g --defined-only -P ' . escapeshellarg ($ libphp ) . ' 2>/dev/null ' )) {
53- foreach (preg_split ('/\R/ ' , trim ($ out )) as $ line ) {
54- if ($ line === '' || str_ends_with ($ line , '.o: ' ) || str_ends_with ($ line , '.o]: ' )) {
55- continue ;
56- }
57- $ name = strtok ($ line , " \t" );
58- if (!$ name ) {
59- continue ;
60- }
61- $ name = preg_replace ('/@.*$/ ' , '' , $ name );
62- if ($ name !== '' && $ name !== false ) {
63- $ defined [] = $ name ;
64- }
65- }
66- }
67- $ defined = array_unique ($ defined );
68- sort ($ defined );
69-
70- $ exportList = BUILD_LIB_PATH . '/export-dynamic.list ' ;
71- $ lines = [];
72- if (SPCTarget::getTargetOS () === 'Linux ' ) {
73- $ lines [] = '{ ' ;
74- foreach ($ defined as $ sym ) {
75- $ lines [] = " {$ sym }; " ;
76- }
77- $ lines [] = '}; ' ;
78- } else {
79- foreach ($ defined as $ sym ) {
80- $ lines [] = $ sym ;
81- }
82- }
83- file_put_contents ($ exportList , implode ("\n" , $ lines ) . "\n" );
84-
85- $ argument = "-Wl,--dynamic-list= {$ exportList }" ;
86- if (ToolchainManager::getToolchainClass () === ZigToolchain::class) {
87- $ argument = '-Wl,--export-dynamic ' ; // https://github.com/ziglang/zig/issues/24662
88- }
89- if (SPCTarget::getTargetOS () !== 'Linux ' ) {
90- $ argument = "-Wl,-exported_symbols_list, {$ exportList }" ;
91- }
92-
93- $ this ->dynamic_export_list = $ argument ;
94- return $ argument ;
95- }
96-
9735 public function proveLibs (array $ sorted_libraries ): void
9836 {
9937 // search all supported libs
@@ -215,11 +153,13 @@ protected function sanityCheck(int $build_target): void
215153 foreach (glob (BUILD_LIB_PATH . "/libphp*. {$ suffix }" ) as $ file ) {
216154 unlink ($ file );
217155 }
218- if ($ dynamicSymbolsArgument = $ this ->getDynamicExportSymbolsArgument ()) {
219- $ dynamic_exports = ' ' . $ dynamicSymbolsArgument ;
156+ // calling linux system util in other unix OS is okay
157+ if ($ dynamic_exports = LinuxSystemUtil::getDynamicExportedSymbols (BUILD_LIB_PATH . '/libphp.a ' )) {
158+ $ dynamic_exports = ' ' . $ dynamic_exports ;
220159 }
221160 }
222- [$ ret , $ out ] = shell ()->cd ($ sample_file_path )->execWithResult (getenv ('CC ' ) . ' -o embed embed.c ' . $ lens . ' ' . $ dynamic_exports );
161+ $ cc = getenv ('CC ' );
162+ [$ ret , $ out ] = shell ()->cd ($ sample_file_path )->execWithResult ("{$ cc } -o embed embed.c {$ lens } {$ dynamic_exports }" );
223163 if ($ ret !== 0 ) {
224164 throw new ValidationException (
225165 'embed failed sanity check: build failed. Error message: ' . implode ("\n" , $ out ),
@@ -338,7 +278,7 @@ protected function buildFrankenphp(): void
338278 if (getenv ('SPC_CMD_VAR_PHP_EMBED_TYPE ' ) === 'shared ' ) {
339279 $ libphpVersion = preg_replace ('/\.\d+$/ ' , '' , $ libphpVersion );
340280 } else {
341- if ($ dynamicSymbolsArgument = $ this -> getDynamicExportSymbolsArgument ( )) {
281+ if ($ dynamicSymbolsArgument = LinuxSystemUtil:: getDynamicExportedSymbols ( BUILD_LIB_PATH . ' /libphp.a ' )) {
342282 $ dynamic_exports = ' ' . $ dynamicSymbolsArgument ;
343283 }
344284 }
0 commit comments