Skip to content

Commit 6226bd7

Browse files
committed
gopy,bind: add -fPIC --shared, use Windows-friendly pkg-config
1 parent 5e81fca commit 6226bd7

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

bind/gen.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ File is generated by gopy. Do not edit.
4646
package main
4747
4848
/*
49-
#cgo pkg-config: %[3]s
49+
%[3]s
5050
// #define Py_LIMITED_API // need full API for PyRun*
5151
#include <Python.h>
5252
typedef uint8_t bool;
@@ -268,13 +268,12 @@ GOCMD=go
268268
GOBUILD=$(GOCMD) build
269269
GOIMPORTS=goimports
270270
PYTHON=%[4]s
271-
PYTHON_CFG=$(PYTHON)-config
272271
LIBEXT=%[5]s
273272
274273
# get the CC and flags used to build python:
275274
GCC = $(shell $(GOCMD) env CC)
276-
CFLAGS = $(shell $(PYTHON_CFG) --cflags)
277-
LDFLAGS = $(shell $(PYTHON_CFG) --ldflags)
275+
CFLAGS = %[7]s
276+
LDFLAGS = %[8]s
278277
279278
all: gen build
280279
@@ -294,7 +293,7 @@ build:
294293
$(PYTHON) build.py
295294
# build the _%[1]s$(LIBEXT) library that contains the cgo and CPython wrappers
296295
# generated %[1]s.py python wrapper imports this c-code package
297-
$(GCC) %[1]s.c %[6]s %[1]s_go$(LIBEXT) -o _%[1]s$(LIBEXT) $(CFLAGS) $(LDFLAGS) -w
296+
$(GCC) %[1]s.c %[6]s %[1]s_go$(LIBEXT) -o _%[1]s$(LIBEXT) $(CFLAGS) $(LDFLAGS) -fPIC --shared -w
298297
299298
`
300299

@@ -307,13 +306,12 @@ GOCMD=go
307306
GOBUILD=$(GOCMD) build
308307
GOIMPORTS=goimports
309308
PYTHON=%[4]s
310-
PYTHON_CFG=$(PYTHON)-config
311309
LIBEXT=%[5]s
310+
CFLAGS = %[6]s
311+
LDFLAGS = %[7]s
312312
313313
# get the flags used to build python:
314314
GCC = $(shell $(GOCMD) env CC)
315-
CFLAGS = $(shell $(PYTHON_CFG) --cflags)
316-
LDFLAGS = $(shell $(PYTHON_CFG) --ldflags)
317315
318316
all: gen build
319317
@@ -476,9 +474,19 @@ func (g *pyGen) genGoPreamble() {
476474
for pi, _ := range current.imports {
477475
pkgimport += fmt.Sprintf("\n\t%q", pi)
478476
}
479-
pypath, pyonly := filepath.Split(g.vm)
480-
pyroot, _ := filepath.Split(filepath.Clean(pypath))
481-
libcfg := filepath.Join(filepath.Join(filepath.Join(pyroot, "lib"), "pkgconfig"), pyonly+".pc")
477+
libcfg := func() string {
478+
pycfg, err := getPythonConfig(g.vm)
479+
if err != nil {
480+
panic(err)
481+
}
482+
pkgcfg := fmt.Sprintf(`
483+
#cgo CFLAGS: %s
484+
#cgo LDFLAGS: %s
485+
`, pycfg.cflags, pycfg.ldflags)
486+
487+
return pkgcfg
488+
}()
489+
482490
if g.mode == ModeExe && g.mainstr == "" {
483491
g.mainstr = "GoPyMainRun()" // default is just to run main
484492
}
@@ -549,10 +557,16 @@ func CmdStrToMakefile(cmdstr string) string {
549557
func (g *pyGen) genMakefile() {
550558
gencmd := strings.Replace(g.cmdstr, "gopy build", "gopy gen", 1)
551559
gencmd = CmdStrToMakefile(gencmd)
560+
561+
pycfg, err := getPythonConfig(g.vm)
562+
if err != nil {
563+
panic(err)
564+
}
565+
552566
if g.mode == ModeExe {
553-
g.makefile.Printf(MakefileExeTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext)
567+
g.makefile.Printf(MakefileExeTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, pycfg.cflags, pycfg.ldflags)
554568
} else {
555-
g.makefile.Printf(MakefileTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, g.extraGccArgs)
569+
g.makefile.Printf(MakefileTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, g.extraGccArgs, pycfg.cflags, pycfg.ldflags)
556570
}
557571
}
558572

cmd_build.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ func runBuild(mode bind.BuildMode, odir, outname, cmdstr, vm, mainstr string, sy
191191
gccargs := []string{outname + ".c", extraGccArgs, outname + "_go" + libExt, "-o", modlib}
192192
gccargs = append(gccargs, strings.Split(strings.TrimSpace(string(cflags)), " ")...)
193193
gccargs = append(gccargs, strings.Split(strings.TrimSpace(string(ldflags)), " ")...)
194+
gccargs = append(gccargs, "-fPIC", "--shared")
195+
196+
gccargs = func(vs []string) []string {
197+
o := make([]string, 0, len(gccargs))
198+
for _, v := range vs {
199+
if v == "" {
200+
continue
201+
}
202+
o = append(o, v)
203+
}
204+
return o
205+
}(gccargs)
194206

195207
fmt.Printf("%s %v\n", cccmd, strings.Join(gccargs, " "))
196208
cmd = exec.Command(cccmd, gccargs...)

0 commit comments

Comments
 (0)