Skip to content

Commit 910e17c

Browse files
author
Randall C. O'Reilly
committed
use bind/GetPythonConfig for cmd_build instead of its own separate code for that -- fixes building on mac..
1 parent c89dca9 commit 910e17c

File tree

5 files changed

+57
-38
lines changed

5 files changed

+57
-38
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,25 @@ package hi exposes a few Go functions to be wrapped and used from Python.
175175
```
176176
177177
### From the command line
178+
178179
```sh
180+
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./
179181
$ gopy build -output=out -vm=`which python3` github.com/go-python/gopy/_examples/hi
180182
$ ls out
181-
build.py go.py hi.c hi.go hi_go.h hi_go.so hi.py _hi.so* __init__.py Makefile
183+
Makefile __init__.py __pycache__/ _hi.so* build.py go.py hi.c hi.go hi.py hi_go.h hi_go.so patch-leaks.go
184+
```
182185
186+
Note: the above may fail with link errors! If so, go into the directory and use the auto-generated makefile:
187+
188+
```sh
189+
$ cd out
190+
$ make
191+
```
192+
193+
Once you get a clean build with no link errors, you can run it like this:
194+
195+
```sh
183196
$ cd out
184-
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./
185197
$ python3
186198
>>> import hi
187199
>>> dir(hi)

bind/gen.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ func (g *pyGen) genGoPreamble() {
616616
pkgimport += fmt.Sprintf("\n\t%q", pi)
617617
}
618618
libcfg := func() string {
619-
pycfg, err := getPythonConfig(g.vm)
619+
pycfg, err := GetPythonConfig(g.vm)
620620
if err != nil {
621621
panic(err)
622622
}
@@ -625,7 +625,7 @@ func (g *pyGen) genGoPreamble() {
625625
pkgcfg := fmt.Sprintf(`
626626
#cgo CFLAGS: %s
627627
#cgo LDFLAGS: %s
628-
`, pycfg.cflags+exflags, pycfg.ldflags)
628+
`, pycfg.CFlags+exflags, pycfg.LdFlags)
629629

630630
return pkgcfg
631631
}()
@@ -712,15 +712,15 @@ func (g *pyGen) genMakefile() {
712712
gencmd := strings.Replace(g.cmdstr, "gopy build", "gopy gen", 1)
713713
gencmd = CmdStrToMakefile(gencmd)
714714

715-
pycfg, err := getPythonConfig(g.vm)
715+
pycfg, err := GetPythonConfig(g.vm)
716716
if err != nil {
717717
panic(err)
718718
}
719719

720720
if g.mode == ModeExe {
721-
g.makefile.Printf(MakefileExeTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, pycfg.cflags, pycfg.ldflags)
721+
g.makefile.Printf(MakefileExeTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, pycfg.CFlags, pycfg.LdFlags)
722722
} else {
723-
g.makefile.Printf(MakefileTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, g.extraGccArgs, pycfg.cflags, pycfg.ldflags)
723+
g.makefile.Printf(MakefileTemplate, g.outname, g.cmdstr, gencmd, g.vm, g.libext, g.extraGccArgs, pycfg.CFlags, pycfg.LdFlags)
724724
}
725725
}
726726

bind/utils.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,20 @@ func isConstructor(sig *types.Signature) bool {
8585
return false
8686
}
8787

88-
type pyconfig struct {
89-
version int
90-
cflags string
91-
ldflags string
88+
type PyConfig struct {
89+
Version int
90+
CFlags string
91+
LdFlags string
9292
}
9393

94-
// getPythonConfig returns the needed python configuration for the given
94+
// AllFlags returns CFlags + " " + LdFlags
95+
func (pc *PyConfig) AllFlags() string {
96+
return strings.TrimSpace(pc.CFlags) + " " + strings.TrimSpace(pc.LdFlags)
97+
}
98+
99+
// GetPythonConfig returns the needed python configuration for the given
95100
// python VM (python, python2, python3, pypy, etc...)
96-
func getPythonConfig(vm string) (pyconfig, error) {
101+
func GetPythonConfig(vm string) (PyConfig, error) {
97102
code := `import sys
98103
import distutils.sysconfig as ds
99104
import json
@@ -122,7 +127,7 @@ else:
122127
}))
123128
`
124129

125-
var cfg pyconfig
130+
var cfg PyConfig
126131
bin, err := exec.LookPath(vm)
127132
if err != nil {
128133
return cfg, errors.Wrapf(err, "could not locate python vm %q", vm)
@@ -161,11 +166,11 @@ else:
161166
raw.LibPy = raw.LibPy[len("lib"):]
162167
}
163168

164-
cfg.version = raw.Version
165-
cfg.cflags = strings.Join([]string{
169+
cfg.Version = raw.Version
170+
cfg.CFlags = strings.Join([]string{
166171
"-I" + raw.IncDir,
167172
}, " ")
168-
cfg.ldflags = strings.Join([]string{
173+
cfg.LdFlags = strings.Join([]string{
169174
"-L" + raw.LibDir,
170175
"-l" + raw.LibPy,
171176
raw.ShLibs,

bind/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestPythonConfig(t *testing.T) {
8585
},
8686
} {
8787
t.Run(tc.vm, func(t *testing.T) {
88-
cfg, err := getPythonConfig(tc.vm)
88+
cfg, err := GetPythonConfig(tc.vm)
8989
if err != nil {
9090
t.Fatal(err)
9191
}

cmd_build.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func runBuild(mode bind.BuildMode, odir, outname, cmdstr, vm, mainstr string, sy
120120
return err
121121
}
122122

123+
pycfg, err := bind.GetPythonConfig(vm)
124+
123125
if mode == bind.ModeExe {
124126
of, err := os.Create(buildname + ".h") // overwrite existing
125127
fmt.Fprintf(of, "typedef uint8_t bool;\n")
@@ -198,32 +200,32 @@ func runBuild(mode bind.BuildMode, odir, outname, cmdstr, vm, mainstr string, sy
198200
return err
199201
}
200202
cccmd := strings.TrimSpace(string(cccmdb))
201-
var cflags, ldflags []byte
202-
if runtime.GOOS != "windows" {
203-
fmt.Printf("%v-config --cflags\n", vm)
204-
cmd = exec.Command(vm+"-config", "--cflags") // TODO: need minor version!
205-
cflags, err = cmd.CombinedOutput()
206-
if err != nil {
207-
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cflags))
208-
return err
209-
}
210-
211-
fmt.Printf("%v-config --ldflags\n", vm)
212-
cmd = exec.Command(vm+"-config", "--ldflags")
213-
ldflags, err = cmd.CombinedOutput()
214-
if err != nil {
215-
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(ldflags))
216-
return err
217-
}
218-
}
203+
// var cflags, ldflags []byte
204+
// if runtime.GOOS != "windows" {
205+
// fmt.Printf("%v-config --cflags\n", vm)
206+
// cmd = exec.Command(vm+"-config", "--cflags") // TODO: need minor version!
207+
// cflags, err = cmd.CombinedOutput()
208+
// if err != nil {
209+
// fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cflags))
210+
// return err
211+
// }
212+
//
213+
// fmt.Printf("%v-config --ldflags\n", vm)
214+
// cmd = exec.Command(vm+"-config", "--ldflags")
215+
// ldflags, err = cmd.CombinedOutput()
216+
// if err != nil {
217+
// fmt.Printf("cmd had error: %v output:\n%v\n", err, string(ldflags))
218+
// return err
219+
// }
220+
// fmt.Printf("build cmd: %s\nldflags: %s\n", cmd, ldflags)
221+
// }
219222
extext := libExt
220223
if runtime.GOOS == "windows" {
221224
extext = ".pyd"
222225
}
223226
modlib := "_" + outname + extext
224227
gccargs := []string{outname + ".c", extraGccArgs, outname + "_go" + libExt, "-o", modlib}
225-
gccargs = append(gccargs, strings.Split(strings.TrimSpace(string(cflags)), " ")...)
226-
gccargs = append(gccargs, strings.Split(strings.TrimSpace(string(ldflags)), " ")...)
228+
gccargs = append(gccargs, strings.Fields(pycfg.AllFlags())...)
227229
gccargs = append(gccargs, "-fPIC", "--shared", "-Ofast")
228230
if !symbols {
229231
gccargs = append(gccargs, "-s")

0 commit comments

Comments
 (0)