Skip to content

Commit ced9adc

Browse files
committed
fix pkg-config generation on windows
1 parent d3c2025 commit ced9adc

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

cmd/internal/install/deps.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ func Dependencies(projectPath string, goVersion, tinyPkgConfigVersion, pyVersion
3636
return err
3737
}
3838

39-
if runtime.GOOS == "windows" {
40-
pythonPath := env.GetPythonRoot(projectPath)
41-
pkgConfigDir := env.GetPythonPkgConfigDir(projectPath)
42-
if err := generatePkgConfig(pythonPath, pkgConfigDir); err != nil {
43-
return err
44-
}
45-
}
46-
47-
// Update pkg-config files
48-
if err := updatePkgConfig(projectPath); err != nil {
49-
return err
50-
}
51-
5239
return nil
5340
}
5441

cmd/internal/install/python.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ func getPythonURL(version, buildDate, arch, os string, freeThreaded, debug bool)
106106

107107
// updateMacOSDylibs updates the install names of dylib files on macOS
108108
func updateMacOSDylibs(pythonDir string, verbose bool) error {
109-
if runtime.GOOS != "darwin" {
110-
return nil
111-
}
112-
113109
libDir := filepath.Join(pythonDir, "lib")
114110
entries, err := os.ReadDir(libDir)
115111
if err != nil {
@@ -159,14 +155,15 @@ func updateMacOSDylibs(pythonDir string, verbose bool) error {
159155
return nil
160156
}
161157

162-
// generatePkgConfig generates pkg-config files for Windows
163-
func generatePkgConfig(pythonPath, pkgConfigDir string) error {
158+
// genWinPyPkgConfig generates pkg-config files for Windows
159+
func genWinPyPkgConfig(pythonRoot, pkgConfigDir string) error {
160+
fmt.Printf("Generating pkg-config files in %s\n", pkgConfigDir)
164161
if err := os.MkdirAll(pkgConfigDir, 0755); err != nil {
165162
return fmt.Errorf("failed to create pkgconfig directory: %v", err)
166163
}
167164

168165
// Get Python environment
169-
pyEnv := env.NewPythonEnv(pythonPath)
166+
pyEnv := env.NewPythonEnv(pythonRoot)
170167
pythonBin, err := pyEnv.Python()
171168
if err != nil {
172169
return fmt.Errorf("failed to get Python executable: %v", err)
@@ -190,8 +187,8 @@ print(f'{version}\n{is_freethreaded}')
190187
return fmt.Errorf("unexpected Python info output format")
191188
}
192189

193-
version := info[0]
194-
isFreethreaded := info[1] == "True"
190+
version := strings.TrimSpace(info[0])
191+
isFreethreaded := strings.TrimSpace(info[1]) == "True"
195192

196193
// Prepare version-specific library names
197194
versionNoPoints := strings.ReplaceAll(version, ".", "")
@@ -210,7 +207,7 @@ Name: Python
210207
Description: Embed Python into an application
211208
Requires:
212209
Version: %s
213-
Libs.private:
210+
Libs.private:
214211
Libs: -L${libdir} -lpython%s%s
215212
Cflags: -I${includedir}
216213
`
@@ -224,7 +221,7 @@ Name: Python
224221
Description: Python library
225222
Requires:
226223
Version: %s
227-
Libs.private:
224+
Libs.private:
228225
Libs: -L${libdir} -lpython3%s
229226
Cflags: -I${includedir}
230227
`
@@ -265,7 +262,6 @@ Cflags: -I${includedir}
265262
} else {
266263
content = fmt.Sprintf(pair.template, version, libSuffix)
267264
}
268-
269265
if err := os.WriteFile(pcPath, []byte(content), 0644); err != nil {
270266
return fmt.Errorf("failed to write %s: %v", pair.name, err)
271267
}
@@ -367,10 +363,10 @@ func updatePkgConfig(projectPath string) error {
367363
// installPythonEnv downloads and installs Python standalone build
368364
func installPythonEnv(projectPath string, version, buildDate string, freeThreaded, debug bool, verbose bool) error {
369365
fmt.Printf("Installing Python %s in %s\n", version, projectPath)
370-
pythonDir := env.GetPythonRoot(projectPath)
366+
pythonRoot := env.GetPythonRoot(projectPath)
371367

372368
// Remove existing Python directory if it exists
373-
if err := os.RemoveAll(pythonDir); err != nil {
369+
if err := os.RemoveAll(pythonRoot); err != nil {
374370
return fmt.Errorf("error removing existing Python directory: %v", err)
375371
}
376372

@@ -380,17 +376,30 @@ func installPythonEnv(projectPath string, version, buildDate string, freeThreade
380376
return fmt.Errorf("unsupported platform")
381377
}
382378

383-
if err := downloadAndExtract("Python", version, url, pythonDir, "python/install", verbose); err != nil {
379+
if err := downloadAndExtract("Python", version, url, pythonRoot, "python/install", verbose); err != nil {
384380
return fmt.Errorf("error downloading and extracting Python: %v", err)
385381
}
386382

387383
// After extraction, update dylib install names on macOS
388-
if err := updateMacOSDylibs(pythonDir, verbose); err != nil {
389-
return fmt.Errorf("error updating dylib install names: %v", err)
384+
if runtime.GOOS == "darwin" {
385+
if err := updateMacOSDylibs(pythonRoot, verbose); err != nil {
386+
return fmt.Errorf("error updating dylib install names: %v", err)
387+
}
388+
}
389+
390+
if runtime.GOOS == "windows" {
391+
pkgConfigDir := env.GetPythonPkgConfigDir(projectPath)
392+
if err := genWinPyPkgConfig(pythonRoot, pkgConfigDir); err != nil {
393+
return err
394+
}
395+
}
396+
397+
if err := updatePkgConfig(projectPath); err != nil {
398+
return fmt.Errorf("error updating pkg-config: %v", err)
390399
}
391400

392401
// Create Python environment
393-
pyEnv := env.NewPythonEnv(pythonDir)
402+
pyEnv := env.NewPythonEnv(pythonRoot)
394403

395404
if verbose {
396405
fmt.Println("Installing Python dependencies...")
@@ -400,17 +409,12 @@ func installPythonEnv(projectPath string, version, buildDate string, freeThreade
400409
return fmt.Errorf("error upgrading pip, setuptools, whell")
401410
}
402411

403-
if err := updatePkgConfig(projectPath); err != nil {
404-
return fmt.Errorf("error updating pkg-config: %v", err)
405-
}
406-
407-
pythonHome := env.GetPythonRoot(projectPath)
408412
pythonPath, err := pyEnv.GetPythonPath()
409413
if err != nil {
410414
return fmt.Errorf("failed to get Python path: %v", err)
411415
}
412416
// Write environment variables to env.txt
413-
if err := env.WriteEnvFile(projectPath, pythonHome, pythonPath); err != nil {
417+
if err := env.WriteEnvFile(projectPath, pythonRoot, pythonPath); err != nil {
414418
return fmt.Errorf("error writing environment file: %v", err)
415419
}
416420

0 commit comments

Comments
 (0)