This commit is contained in:
2025-05-23 15:11:03 +04:00
parent c86165f88c
commit 92b9c356b8
83 changed files with 450 additions and 810 deletions

View File

@@ -6,7 +6,7 @@ import (
"os/exec"
"path/filepath"
"github.com/freeflowuniverse/heroagent/pkg/system/builders/postgresql/postgres"
"git.ourworld.tf/herocode/heroagent/pkg/system/builders/postgresql/postgres"
)
// Constants for Go stored procedure
@@ -46,10 +46,10 @@ func (b *GoSPBuilder) run(cmd string, args ...string) error {
fmt.Println("Running:", cmd, args)
c := exec.Command(cmd, args...)
// Set environment variables
c.Env = append(os.Environ(),
c.Env = append(os.Environ(),
"GOROOT=/usr/local/go",
"GOPATH=/root/go",
"PATH=/usr/local/go/bin:" + os.Getenv("PATH"))
"GOPATH=/root/go",
"PATH=/usr/local/go/bin:"+os.Getenv("PATH"))
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
@@ -58,7 +58,7 @@ func (b *GoSPBuilder) run(cmd string, args ...string) error {
// Build builds a Go stored procedure
func (b *GoSPBuilder) Build() error {
fmt.Println("Building Go stored procedure...")
// Use the explicitly provided Go path if available
var goExePath string
if b.GoPath != "" {
@@ -74,7 +74,7 @@ func (b *GoSPBuilder) Build() error {
}
fmt.Printf("Using detected Go executable from: %s\n", goExePath)
}
if err := os.MkdirAll(b.GoSharedLibDir, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
@@ -98,27 +98,27 @@ func main() {}
// Use the full path to Go rather than relying on PATH
fmt.Println("Running Go build with full path:", goExePath)
// Show debug information
fmt.Println("Environment variables that will be set:")
fmt.Println(" GOROOT=/usr/local/go")
fmt.Println(" GOPATH=/root/go")
fmt.Println(" PATH=/usr/local/go/bin:" + os.Getenv("PATH"))
// Verify that the Go executable exists before using it
if _, err := os.Stat(goExePath); err != nil {
return fmt.Errorf("Go executable not found at %s: %w", goExePath, err)
}
// Create the output directory if it doesn't exist
outputDir := filepath.Join(b.InstallPrefix, "lib")
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory %s: %w", outputDir, err)
}
// Prepare output path
outputPath := filepath.Join(outputDir, "libgosp.so")
// Instead of relying on environment variables, create a wrapper shell script
// that sets all required environment variables and then calls the Go executable
tempDir, err := os.MkdirTemp("", "go-build-")
@@ -126,7 +126,7 @@ func main() {}
return fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tempDir) // Clean up when done
goRoot := filepath.Dir(filepath.Dir(goExePath)) // /usr/local/go
wrapperScript := filepath.Join(tempDir, "go-wrapper.sh")
wrapperContent := fmt.Sprintf(`#!/bin/sh
@@ -143,25 +143,25 @@ echo "PATH=$PATH"
echo "=== Running Go command ==="
echo "%s $@"
exec %s "$@"
`,
goRoot,
`,
goRoot,
filepath.Dir(goExePath),
goExePath,
goExePath)
// Write the wrapper script
if err := os.WriteFile(wrapperScript, []byte(wrapperContent), 0755); err != nil {
return fmt.Errorf("failed to write wrapper script: %w", err)
}
fmt.Printf("Created wrapper script at %s\n", wrapperScript)
// Use the wrapper script to build the Go shared library
cmd := exec.Command(wrapperScript, "build", "-buildmode=c-shared", "-o", outputPath, libPath)
cmd.Dir = filepath.Dir(libPath) // Set working directory to where the source file is
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Printf("Executing Go build via wrapper script\n")
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to build Go stored procedure: %w", err)