Skip to content

Commit

Permalink
fix build env and run env
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Nov 22, 2024
1 parent 55107c4 commit b6f0c69
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/internal/create/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import (
func main() {
Initialize()
defer Finalize()
println("Hello, World!")
}
4 changes: 2 additions & 2 deletions cmd/internal/install/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ print(f'{version}\n{is_freethreaded}')
// Template for the pkg-config files
embedTemplate := `prefix=${pcfiledir}/../..
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
libdir=${exec_prefix}
includedir=${prefix}/include
Name: Python
Expand All @@ -214,7 +214,7 @@ Cflags: -I${includedir}

normalTemplate := `prefix=${pcfiledir}/../..
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
libdir=${exec_prefix}
includedir=${prefix}/include
Name: Python
Expand Down
4 changes: 1 addition & 3 deletions cmd/internal/rungo/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ func RunCommand(command string, args []string) error {
cmd.Env = append(goEnv, os.Environ()...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if command == "run" {
cmd.Stdin = os.Stdin
}
cmd.Stdin = os.Stdin

// Execute the command
if err := cmd.Run(); err != nil {
Expand Down
10 changes: 8 additions & 2 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func GetPythonRoot(projectPath string) string {

// GetPythonBinDir returns the Python binary directory path relative to project path
func GetPythonBinDir(projectPath string) string {
if runtime.GOOS == "windows" {
return filepath.Join(GetPythonRoot(projectPath))
}
return filepath.Join(GetPythonRoot(projectPath), "bin")
}

Expand Down Expand Up @@ -93,6 +96,7 @@ func SetBuildEnv(projectPath string) {
}
path := os.Getenv("PATH")
path = GetGoBinDir(absPath) + pathSeparator() + path
path = GetPythonBinDir(absPath) + pathSeparator() + path
if runtime.GOOS == "windows" {
path = GetMingwRoot(absPath) + pathSeparator() + path
path = GetTinyPkgConfigDir(absPath) + pathSeparator() + path
Expand All @@ -101,6 +105,8 @@ func SetBuildEnv(projectPath string) {
os.Setenv("GOPATH", GetGoPath(absPath))
os.Setenv("GOROOT", GetGoRoot(absPath))
os.Setenv("GOCACHE", GetGoCacheDir(absPath))
os.Setenv("PKG_CONFIG_PATH", GetPythonPkgConfigDir(absPath))
os.Setenv("CGO_ENABLED", "1")
}

func pathSeparator() string {
Expand All @@ -110,13 +116,13 @@ func pathSeparator() string {
return ":"
}

// WriteEnvFile writes environment variables to .python/env.txt
// WriteEnvFile writes environment variables to .deps/env.txt
func WriteEnvFile(projectPath, pythonHome, pythonPath string) error {
// Prepare environment variables
envVars := []string{
fmt.Sprintf("PKG_CONFIG_PATH=%s", filepath.Join(pythonHome, "lib", "pkgconfig")),
fmt.Sprintf("PYTHONPATH=%s", strings.TrimSpace(pythonPath)),
fmt.Sprintf("PYTHONHOME=%s", pythonHome),
fmt.Sprintf("PATH=%s", GetPythonBinDir(projectPath)),
}

// Write to env.txt
Expand Down
3 changes: 1 addition & 2 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package env
import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -106,7 +105,7 @@ func TestWriteEnvFile(t *testing.T) {
// Verify the content contains expected environment variables
envContent := string(content)
expectedVars := []string{
fmt.Sprintf("PKG_CONFIG_PATH=%s", filepath.Join(pythonDir, "lib", "pkgconfig")),
fmt.Sprintf("PATH=%s", GetPythonBinDir(projectDir)),
fmt.Sprintf("PYTHONPATH=/mock/path1%s/mock/path2", pathSep),
fmt.Sprintf("PYTHONHOME=%s", pythonDir),
}
Expand Down

0 comments on commit b6f0c69

Please sign in to comment.