@@ -129,6 +129,36 @@ public function getLibExtraLdFlags(): string
129129 return trim ($ env );
130130 }
131131
132+ /**
133+ * Patch pkgconfig file prefix, exec_prefix, libdir, includedir for correct build.
134+ *
135+ * @param array $files File list to patch, if empty, will use pkg-configs from config (e.g. ['zlib.pc', 'openssl.pc'])
136+ * @param int $patch_option Patch options
137+ * @param null|array $custom_replace Custom replace rules, if provided, will be used to replace in the format [regex, replacement]
138+ */
139+ public function patchPkgconfPrefix (array $ files = [], int $ patch_option = PKGCONF_PATCH_ALL , ?array $ custom_replace = null ): void
140+ {
141+ logger ()->info ("Patching library [ {$ this ->getName ()}] pkgconfig " );
142+ if ($ files === [] && ($ conf_pc = PackageConfig::get ($ this ->getName (), 'pkg-configs ' , [])) !== []) {
143+ $ files = array_map (fn ($ x ) => "{$ x }.pc " , $ conf_pc );
144+ }
145+ foreach ($ files as $ name ) {
146+ $ realpath = realpath ("{$ this ->getLibDir ()}/pkgconfig/ {$ name }" );
147+ if ($ realpath === false ) {
148+ throw new PatchException ('pkg-config prefix patcher ' , "Cannot find library [ {$ this ->getName ()}] pkgconfig file [ {$ name }] in {$ this ->getLibDir ()}/pkgconfig/ ! " );
149+ }
150+ logger ()->debug ("Patching {$ realpath }" );
151+ // replace prefix
152+ $ file = FileSystem::readFile ($ realpath );
153+ $ file = ($ patch_option & PKGCONF_PATCH_PREFIX ) === PKGCONF_PATCH_PREFIX ? preg_replace ('/^prefix\s*=.*$/m ' , 'prefix= ' . BUILD_ROOT_PATH , $ file ) : $ file ;
154+ $ file = ($ patch_option & PKGCONF_PATCH_EXEC_PREFIX ) === PKGCONF_PATCH_EXEC_PREFIX ? preg_replace ('/^exec_prefix\s*=.*$/m ' , 'exec_prefix=${prefix} ' , $ file ) : $ file ;
155+ $ file = ($ patch_option & PKGCONF_PATCH_LIBDIR ) === PKGCONF_PATCH_LIBDIR ? preg_replace ('/^libdir\s*=.*$/m ' , 'libdir=${prefix}/lib ' , $ file ) : $ file ;
156+ $ file = ($ patch_option & PKGCONF_PATCH_INCLUDEDIR ) === PKGCONF_PATCH_INCLUDEDIR ? preg_replace ('/^includedir\s*=.*$/m ' , 'includedir=${prefix}/include ' , $ file ) : $ file ;
157+ $ file = ($ patch_option & PKGCONF_PATCH_CUSTOM ) === PKGCONF_PATCH_CUSTOM && $ custom_replace !== null ? preg_replace ($ custom_replace [0 ], $ custom_replace [1 ], $ file ) : $ file ;
158+ FileSystem::writeFile ($ realpath , $ file );
159+ }
160+ }
161+
132162 /**
133163 * Get extra LIBS for current package.
134164 * You need to define the environment variable in the format of {LIBRARY_NAME}_LIBS
0 commit comments