Skip to content

Commit 7478a14

Browse files
committed
fix(build): fix dependency linking and system header conflicts
- Support `lib64` library paths for OpenSSL and Curl static libraries. - Configure `CURL_CFLAGS` and `CURL_LIBS` explicitly with `-DCURL_STATICLIB` to ensure proper static linking. - Strip `-I/usr/include` from generated PHP Makefile to prevent conflicts with custom dependency headers. - Export `OPENSSL_CFLAGS` to the build environment to ensure correct header resolution.
1 parent 959050d commit 7478a14

3 files changed

Lines changed: 71 additions & 9 deletions

File tree

internal/build/php.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ func (b *Builder) Build(ctx context.Context, opts BuildOptions) error {
128128
if err := b.configure(ctx, version, sourceDir, buildDir, installDir); err != nil {
129129
return fmt.Errorf("configure: %w", err)
130130
}
131+
if deps.NeedsDeps(version) {
132+
if err := stripSystemInclude(buildDir); err != nil {
133+
return fmt.Errorf("patch makefile includes: %w", err)
134+
}
135+
}
131136

132137
// Build
133138
if err := b.make(ctx, version, buildDir); err != nil {
@@ -272,6 +277,22 @@ func (b *Builder) configure(ctx context.Context, version, sourceDir, buildDir, i
272277
return nil
273278
}
274279

280+
func stripSystemInclude(buildDir string) error {
281+
makefilePath := filepath.Join(buildDir, "Makefile")
282+
content, err := os.ReadFile(makefilePath)
283+
if err != nil {
284+
return err
285+
}
286+
287+
updated := strings.ReplaceAll(string(content), "CFLAGS_CLEAN = -I/usr/include ", "CFLAGS_CLEAN = ")
288+
updated = strings.ReplaceAll(updated, "CFLAGS_CLEAN = -I/usr/include", "CFLAGS_CLEAN =")
289+
if updated == string(content) {
290+
return nil
291+
}
292+
293+
return os.WriteFile(makefilePath, []byte(updated), 0644)
294+
}
295+
275296
// make runs make.
276297
func (b *Builder) make(ctx context.Context, version, buildDir string) error {
277298
log.Info("Building (this may take a while)...")

internal/deps/deps.go

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ func (m *DepsManager) GetBuildEnv(phpVersion string) []string {
426426
var ldflags []string
427427
var cflags []string
428428
var libs []string
429+
var opensslCflags []string
429430
var env []string
430431

431432
for _, dep := range deps {
@@ -456,25 +457,56 @@ func (m *DepsManager) GetBuildEnv(phpVersion string) []string {
456457

457458
// Add explicit static library paths for OpenSSL
458459
if dep.Name == "openssl" {
459-
libssl := filepath.Join(prefix, "lib", "libssl.a")
460-
libcrypto := filepath.Join(prefix, "lib", "libcrypto.a")
460+
libDir := filepath.Join(prefix, "lib")
461+
libDir64 := filepath.Join(prefix, "lib64")
462+
libssl := filepath.Join(libDir, "libssl.a")
463+
libcrypto := filepath.Join(libDir, "libcrypto.a")
464+
if !fsutil.Exists(libssl) || !fsutil.Exists(libcrypto) {
465+
libssl = filepath.Join(libDir64, "libssl.a")
466+
libcrypto = filepath.Join(libDir64, "libcrypto.a")
467+
}
461468
if fsutil.Exists(libssl) && fsutil.Exists(libcrypto) {
462469
libs = append(libs, libssl, libcrypto, "-ldl", "-lpthread")
463470
}
471+
includeDir := filepath.Join(prefix, "include")
472+
if fsutil.Exists(includeDir) {
473+
opensslCflags = append(opensslCflags, "-I"+includeDir)
474+
}
464475
}
465476

466477
// Add curl library with OpenSSL dependencies
467478
if dep.Name == "curl" {
468479
opensslPrefix := filepath.Join(depsDir, "openssl")
469-
libssl := filepath.Join(opensslPrefix, "lib", "libssl.a")
470-
libcrypto := filepath.Join(opensslPrefix, "lib", "libcrypto.a")
471-
libcurl := filepath.Join(prefix, "lib", "libcurl.a")
472-
if fsutil.Exists(libcurl) && fsutil.Exists(libssl) && fsutil.Exists(libcrypto) {
473-
// Add curl libs with its OpenSSL dependencies
474-
curlLibs := fmt.Sprintf("-L%s/lib %s %s %s -ldl -lpthread -lz",
475-
prefix, libcurl, libssl, libcrypto)
480+
libDir := filepath.Join(prefix, "lib")
481+
libDir64 := filepath.Join(prefix, "lib64")
482+
curlLibDir := libDir
483+
libcurl := filepath.Join(curlLibDir, "libcurl.a")
484+
if !fsutil.Exists(libcurl) {
485+
curlLibDir = libDir64
486+
libcurl = filepath.Join(curlLibDir, "libcurl.a")
487+
}
488+
489+
opensslLibDir := filepath.Join(opensslPrefix, "lib")
490+
opensslLibDir64 := filepath.Join(opensslPrefix, "lib64")
491+
libssl := filepath.Join(opensslLibDir, "libssl.a")
492+
libcrypto := filepath.Join(opensslLibDir, "libcrypto.a")
493+
if !fsutil.Exists(libssl) || !fsutil.Exists(libcrypto) {
494+
libssl = filepath.Join(opensslLibDir64, "libssl.a")
495+
libcrypto = filepath.Join(opensslLibDir64, "libcrypto.a")
496+
}
497+
498+
if fsutil.Exists(libcurl) {
499+
// Override pkg-config to control link order for static libcurl.
500+
curlCflags := fmt.Sprintf("-I%s/include -DCURL_STATICLIB", prefix)
501+
curlLibs := fmt.Sprintf("-L%s -lcurl", curlLibDir)
502+
env = append(env, "CURL_CFLAGS="+curlCflags)
476503
env = append(env, "CURL_LIBS="+curlLibs)
477504
}
505+
506+
// Ensure OpenSSL libs are available in LIBS (appended at end by configure).
507+
if fsutil.Exists(libssl) && fsutil.Exists(libcrypto) {
508+
libs = append(libs, libssl, libcrypto, "-ldl", "-lpthread", "-lz")
509+
}
478510
}
479511
}
480512

@@ -517,6 +549,15 @@ func (m *DepsManager) GetBuildEnv(phpVersion string) []string {
517549
env = append(env, "LIBS="+libsStr)
518550
}
519551

552+
if len(opensslCflags) > 0 {
553+
existing := os.Getenv("OPENSSL_CFLAGS")
554+
newFlags := strings.Join(opensslCflags, " ")
555+
if existing != "" {
556+
newFlags = newFlags + " " + existing
557+
}
558+
env = append(env, "OPENSSL_CFLAGS="+newFlags)
559+
}
560+
520561
return env
521562
}
522563

phvm

-12.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)