44
55namespace SPC \builder \unix \library ;
66
7- use SPC \builder \linux \library \LinuxLibraryBase ;
8- use SPC \exception \BuildFailureException ;
9- use SPC \exception \FileSystemException ;
107use SPC \store \FileSystem ;
8+ use SPC \util \PkgConfigUtil ;
9+ use SPC \util \SPCConfigUtil ;
1110use SPC \util \SPCTarget ;
1211
1312trait postgresql
1413{
15- protected function build (): void
14+ public function patchBeforeBuild (): bool
1615 {
17- $ builddir = BUILD_ROOT_PATH ;
18- $ envs = '' ;
19- $ packages = 'zlib openssl readline libxml-2.0 ' ;
20- $ optional_packages = [
21- 'zstd ' => 'libzstd ' ,
22- 'ldap ' => 'ldap ' ,
23- 'libxslt ' => 'libxslt ' ,
24- 'icu ' => 'icu-i18n ' ,
25- ];
26- $ error_exec_cnt = 0 ;
27-
28- foreach ($ optional_packages as $ lib => $ pkg ) {
29- if ($ this ->getBuilder ()->getLib ($ lib )) {
30- $ packages .= ' ' . $ pkg ;
31- $ output = shell ()->execWithResult ("pkg-config --static {$ pkg }" );
32- $ error_exec_cnt += $ output [0 ] === 0 ? 0 : 1 ;
33- logger ()->info (var_export ($ output [1 ], true ));
34- }
16+ if (SPCTarget::getLibcVersion () === '2.17 ' && GNU_ARCH === 'aarch64 ' ) {
17+ FileSystem::replaceFileStr (
18+ $ this ->source_dir . '/src/port/pg_popcount_aarch64.c ' ,
19+ 'HWCAP_SVE ' ,
20+ '0 ' ,
21+ );
22+ FileSystem::replaceFileStr (
23+ $ this ->source_dir . '/src/port/pg_crc32c_armv8_choose.c ' ,
24+ '#if defined(__linux__) && !defined(__aarch64__) && !defined(HWCAP2_CRC32) ' ,
25+ '#if defined(__linux__) && !defined(HWCAP_CRC32) ' ,
26+ );
3527 }
28+ // skip the test on platforms where libpq infrastructure may be provided by statically-linked libraries
29+ FileSystem::replaceFileStr ("{$ this ->source_dir }/src/interfaces/libpq/Makefile " , 'invokes exit \'; exit 1; ' , 'invokes exit \'; ' );
30+ // disable shared libs build
31+ FileSystem::replaceFileStr (
32+ "{$ this ->source_dir }/src/Makefile.shlib " ,
33+ [
34+ '$(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) ' ,
35+ '$(INSTALL_SHLIB) $< \'$(DESTDIR)$(pkglibdir)/$(shlib) \'' ,
36+ '$(INSTALL_SHLIB) $< \'$(DESTDIR)$(libdir)/$(shlib) \'' ,
37+ '$(INSTALL_SHLIB) $< \'$(DESTDIR)$(bindir)/$(shlib) \'' ,
38+ ],
39+ ''
40+ );
41+ return true ;
42+ }
43+
44+ protected function build (): void
45+ {
46+ $ libs = array_map (fn ($ x ) => $ x ->getName (), $ this ->getDependencies ());
47+ $ spc = new SPCConfigUtil ($ this ->getBuilder (), ['no_php ' => true , 'libs_only_deps ' => true ]);
48+ $ config = $ spc ->config (libraries: $ libs , include_suggest_lib: $ this ->builder ->getOption ('with-suggested-libs ' ));
3649
37- $ output = shell ()->execWithResult ("pkg-config --cflags-only-I --static {$ packages }" );
38- $ error_exec_cnt += $ output [0 ] === 0 ? 0 : 1 ;
3950 $ macos_15_bug_cflags = PHP_OS_FAMILY === 'Darwin ' ? ' -Wno-unguarded-availability-new ' : '' ;
40- $ cflags = '' ;
41- if (!empty ($ output [1 ][0 ])) {
42- $ cflags = $ output [1 ][0 ];
43- $ envs .= ' CPPFLAGS="-DPIC" ' ;
44- $ cflags = "{$ cflags } -fno-ident {$ macos_15_bug_cflags }" ;
45- }
46- $ output = shell ()->execWithResult ("pkg-config --libs-only-L --static {$ packages }" );
47- $ error_exec_cnt += $ output [0 ] === 0 ? 0 : 1 ;
48- if (!empty ($ output [1 ][0 ])) {
49- $ ldflags = $ output [1 ][0 ];
50- $ envs .= SPCTarget::isStatic () ? " LDFLAGS= \"{$ ldflags } -static \" " : " LDFLAGS= \"{$ ldflags }\" " ;
51- }
52- $ output = shell ()->execWithResult ("pkg-config --libs-only-l --static {$ packages }" );
53- $ error_exec_cnt += $ output [0 ] === 0 ? 0 : 1 ;
54- if (!empty ($ output [1 ][0 ])) {
55- $ libs = $ output [1 ][0 ];
56- $ libcpp = '' ;
57- if ($ this ->builder ->getLib ('icu ' )) {
58- $ libcpp = $ this instanceof LinuxLibraryBase ? ' -lstdc++ ' : ' -lc++ ' ;
59- }
60- $ envs .= " LIBS= \"{$ libs }{$ libcpp }\" " ;
61- }
62- if ($ error_exec_cnt > 0 ) {
63- throw new BuildFailureException ('Failed to get pkg-config information! ' );
51+
52+ $ env_vars = [
53+ 'CFLAGS ' => "{$ config ['cflags ' ]} -fno-ident {$ macos_15_bug_cflags }" ,
54+ 'CPPFLAGS ' => '-DPIC ' ,
55+ 'LDFLAGS ' => $ config ['ldflags ' ],
56+ 'LIBS ' => $ config ['libs ' ],
57+ ];
58+
59+ if ($ ldLibraryPath = getenv ('SPC_LD_LIBRARY_PATH ' )) {
60+ $ env_vars ['LD_LIBRARY_PATH ' ] = $ ldLibraryPath ;
6461 }
6562
6663 FileSystem::resetDir ($ this ->source_dir . '/build ' );
6764
68- $ version = $ this ->getVersion ();
69- // 16.1 workaround
70- if (version_compare ($ version , '16.1 ' ) >= 0 ) {
71- # 有静态链接配置 参考文件: src/interfaces/libpq/Makefile
72- shell ()->cd ($ this ->source_dir . '/build ' )
73- ->exec ('sed -i.backup "s/invokes exit \'; exit 1;/invokes exit \';/" ../src/interfaces/libpq/Makefile ' )
74- ->exec ('sed -i.backup "278 s/^/# /" ../src/Makefile.shlib ' )
75- ->exec ('sed -i.backup "402 s/^/# /" ../src/Makefile.shlib ' );
76- } else {
77- throw new BuildFailureException ('Unsupported version for postgresql: ' . $ version . ' ! ' );
78- }
65+ // php source relies on the non-private encoding functions in libpgcommon.a
66+ FileSystem::replaceFileStr (
67+ "{$ this ->source_dir }/src/common/Makefile " ,
68+ '$(OBJS_FRONTEND): CPPFLAGS += -DUSE_PRIVATE_ENCODING_FUNCS ' ,
69+ '$(OBJS_FRONTEND): CPPFLAGS += -UUSE_PRIVATE_ENCODING_FUNCS -DFRONTEND ' ,
70+ );
7971
8072 // configure
81- shell ()->cd ($ this ->source_dir . ' /build ' )->initializeEnv ($ this )
82- ->appendEnv ([ ' CFLAGS ' => $ cflags ] )
73+ $ shell = shell ()->cd ("{ $ this ->source_dir } /build " )->initializeEnv ($ this )
74+ ->appendEnv ($ env_vars )
8375 ->exec (
84- "{$ envs } ../configure " .
85- "--prefix= {$ builddir } " .
86- ($ this ->builder ->getOption ('enable-zts ' ) ? '--enable-thread-safety ' : '--disable-thread-safety ' ) .
76+ '../configure ' .
77+ "--prefix= {$ this ->getBuildRootPath ()} " .
8778 '--enable-coverage=no ' .
8879 '--with-ssl=openssl ' .
8980 '--with-readline ' .
9081 '--with-libxml ' .
9182 ($ this ->builder ->getLib ('icu ' ) ? '--with-icu ' : '--without-icu ' ) .
9283 ($ this ->builder ->getLib ('ldap ' ) ? '--with-ldap ' : '--without-ldap ' ) .
93- // '--without-ldap ' .
9484 ($ this ->builder ->getLib ('libxslt ' ) ? '--with-libxslt ' : '--without-libxslt ' ) .
9585 ($ this ->builder ->getLib ('zstd ' ) ? '--with-zstd ' : '--without-zstd ' ) .
9686 '--without-lz4 ' .
@@ -99,32 +89,29 @@ protected function build(): void
9989 '--without-pam ' .
10090 '--without-bonjour ' .
10191 '--without-tcl '
102- )
103- ->exec ($ envs . ' make -C src/bin/pg_config install ' )
104- ->exec ($ envs . ' make -C src/include install ' )
105- ->exec ($ envs . ' make -C src/common install ' )
106- ->exec ($ envs . ' make -C src/port install ' )
107- ->exec ($ envs . ' make -C src/interfaces/libpq install ' );
92+ );
93+
94+ // patch ldap lib
95+ if ($ this ->builder ->getLib ('ldap ' )) {
96+ $ libs = PkgConfigUtil::getLibsArray ('ldap ' );
97+ $ libs = clean_spaces (implode (' ' , $ libs ));
98+ FileSystem::replaceFileStr ($ this ->source_dir . '/build/config.status ' , '-lldap ' , $ libs );
99+ FileSystem::replaceFileStr ($ this ->source_dir . '/build/src/Makefile.global ' , '-lldap ' , $ libs );
100+ }
101+
102+ $ shell
103+ ->exec ('make -C src/bin/pg_config install ' )
104+ ->exec ('make -C src/include install ' )
105+ ->exec ('make -C src/common install ' )
106+ ->exec ('make -C src/port install ' )
107+ ->exec ('make -C src/interfaces/libpq install ' );
108108
109109 // remove dynamic libs
110110 shell ()->cd ($ this ->source_dir . '/build ' )
111- ->exec ("rm -rf {$ builddir }/lib/*.so.* " )
112- ->exec ("rm -rf {$ builddir }/lib/*.so " )
113- ->exec ("rm -rf {$ builddir }/lib/*.dylib " );
114-
115- FileSystem::replaceFileStr (BUILD_LIB_PATH . '/pkgconfig/libpq.pc ' , '-lldap ' , '-lldap -llber ' );
116- }
111+ ->exec ("rm -rf {$ this ->getBuildRootPath ()}/lib/*.so.* " )
112+ ->exec ("rm -rf {$ this ->getBuildRootPath ()}/lib/*.so " )
113+ ->exec ("rm -rf {$ this ->getBuildRootPath ()}/lib/*.dylib " );
117114
118- private function getVersion (): string
119- {
120- try {
121- $ file = FileSystem::readFile ($ this ->source_dir . '/meson.build ' );
122- if (preg_match ("/^ \\s+version: \\s?'(.*)'/m " , $ file , $ match )) {
123- return $ match [1 ];
124- }
125- return 'unknown ' ;
126- } catch (FileSystemException ) {
127- return 'unknown ' ;
128- }
115+ FileSystem::replaceFileStr ("{$ this ->getLibDir ()}/pkgconfig/libpq.pc " , '-lldap ' , '-lldap -llber ' );
129116 }
130117}
0 commit comments