Skip to content

Commit

Permalink
add a -no-make flag, which prevents overwriting the generated Makefil…
Browse files Browse the repository at this point in the history
…e when run from the Makefile. add extra -Wno-error -Wno-implicit-function-declaration CFLAGS to fix Xcode 12 and make actual errors more visible
  • Loading branch information
Randall C. O'Reilly committed Sep 20, 2020
1 parent 6d8fa23 commit b9902d7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
31 changes: 24 additions & 7 deletions bind/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ build:
# this will fail but is needed to generate the .c file that then allows go build to work
- $(PYTHON) build.py >/dev/null 2>&1
# generate %[1]s_go.h from %[1]s.go -- unfortunately no way to build .h only
$(GOBUILD) -buildmode=c-shared -o %[1]s_go$(LIBEXT) >/dev/null 2>&1
$(GOBUILD) -buildmode=c-shared -o %[1]s_go$(LIBEXT)
# use pybindgen to build the %[1]s.c file which are the CPython wrappers to cgo wrappers..
# note: pip install pybindgen to get pybindgen if this fails
$(PYTHON) build.py
Expand Down Expand Up @@ -457,6 +457,9 @@ var thePyGen *pyGen
// before e.g., thePyGen is present.
var NoWarn = false

// NoMake turns off generation of Makefiles
var NoMake = false

// GenPyBind generates a .go file, build.py file to enable pybindgen to create python bindings,
// and wrapper .py file(s) that are loaded as the interface to the package with shadow
// python-side classes
Expand Down Expand Up @@ -538,11 +541,15 @@ func (g *pyGen) genPre() {
g.gofile = &printer{buf: new(bytes.Buffer), indentEach: []byte("\t")}
g.leakfile = &printer{buf: new(bytes.Buffer), indentEach: []byte("\t")}
g.pybuild = &printer{buf: new(bytes.Buffer), indentEach: []byte("\t")}
g.makefile = &printer{buf: new(bytes.Buffer), indentEach: []byte("\t")}
if !NoMake {
g.makefile = &printer{buf: new(bytes.Buffer), indentEach: []byte("\t")}
}
g.genGoPreamble()
g.genLeaksPreamble()
g.genPyBuildPreamble()
g.genMakefile()
if !NoMake {
g.genMakefile()
}
oinit, err := os.Create(filepath.Join(g.odir, "__init__.py"))
g.err.Add(err)
err = oinit.Close()
Expand All @@ -562,11 +569,13 @@ func (g *pyGen) genOut() {
g.pybuild.Printf("\nmod.generate(open('%v.c', 'w'))\n\n", g.outname)
g.gofile.Printf("\n\n")
g.genLeaksPostamble()
g.makefile.Printf("\n\n")
g.genPrintOut(g.outname+".go", g.gofile)
g.genPrintOut("patch-leaks.go", g.leakfile)
g.genPrintOut("build.py", g.pybuild)
g.genPrintOut("Makefile", g.makefile)
if !NoMake {
g.makefile.Printf("\n\n")
g.genPrintOut("Makefile", g.makefile)
}
}

func (g *pyGen) genPkgWrapOut() {
Expand Down Expand Up @@ -611,10 +620,12 @@ func (g *pyGen) genGoPreamble() {
if err != nil {
panic(err)
}
// this is critical to avoid pybindgen errors:
exflags := " -Wno-error -Wno-implicit-function-declaration -Wno-int-conversion"
pkgcfg := fmt.Sprintf(`
#cgo CFLAGS: %s
#cgo LDFLAGS: %s
`, pycfg.cflags, pycfg.ldflags)
`, pycfg.cflags+exflags, pycfg.ldflags)

return pkgcfg
}()
Expand Down Expand Up @@ -688,7 +699,13 @@ func CmdStrToMakefile(cmdstr string) string {
spidx := strings.Index(cmdstr[oidx:], " ")
cmdstr = cmdstr[:oidx] + cmdstr[oidx+spidx+1:]
}
return cmdstr
cmds := strings.Fields(cmdstr)
ncmds := make([]string, 0, len(cmds)+1)
ncmds = append(ncmds, cmds[:2]...)
ncmds = append(ncmds, "-no-make")
ncmds = append(ncmds, cmds[2:]...)

return strings.Join(ncmds, " ")
}

func (g *pyGen) genMakefile() {
Expand Down
3 changes: 3 additions & 0 deletions cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ex:
cmd.Flag.String("main", "", "code string to run in the go main() function in the cgo library")
cmd.Flag.Bool("symbols", true, "include symbols in output")
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")
return cmd
}

Expand All @@ -56,9 +57,11 @@ func gopyRunCmdBuild(cmdr *commander.Command, args []string) error {
vm = cmdr.Flag.Lookup("vm").Value.Get().(string)
symbols = cmdr.Flag.Lookup("symbols").Value.Get().(bool)
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
nomake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
)

bind.NoWarn = nowarn
bind.NoMake = nomake

cmdstr := argStr()

Expand Down
3 changes: 3 additions & 0 deletions cmd_exe.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ex:
cmd.Flag.String("desc", "", "short description of project (long comes from README.md)")
cmd.Flag.String("url", "https://github.com/go-python/gopy", "home page for project")
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")

return cmd
}
Expand All @@ -78,11 +79,13 @@ func gopyRunCmdExe(cmdr *commander.Command, args []string) error {
desc = cmdr.Flag.Lookup("desc").Value.Get().(string)
url = cmdr.Flag.Lookup("url").Value.Get().(string)
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
nomake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
)

cmdstr := argStr()

bind.NoWarn = nowarn
bind.NoMake = nomake

if name == "" {
path := args[0]
Expand Down
3 changes: 3 additions & 0 deletions cmd_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ex:
cmd.Flag.String("name", "", "name of output package (otherwise name of first package is used)")
cmd.Flag.String("main", "", "code string to run in the go main() function in the cgo library")
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")
return cmd
}

Expand All @@ -53,13 +54,15 @@ func gopyRunCmdGen(cmdr *commander.Command, args []string) error {
name = cmdr.Flag.Lookup("name").Value.Get().(string)
mainstr = cmdr.Flag.Lookup("main").Value.Get().(string)
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
nomake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
)

if vm == "" {
vm = "python"
}

bind.NoWarn = nowarn
bind.NoMake = nomake

for _, path := range args {
pkg, err := newPackage(path)
Expand Down
3 changes: 3 additions & 0 deletions cmd_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ex:
cmd.Flag.String("desc", "", "short description of project (long comes from README.md)")
cmd.Flag.String("url", "https://github.com/go-python/gopy", "home page for project")
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")

return cmd
}
Expand All @@ -76,11 +77,13 @@ func gopyRunCmdPkg(cmdr *commander.Command, args []string) error {
desc = cmdr.Flag.Lookup("desc").Value.Get().(string)
url = cmdr.Flag.Lookup("url").Value.Get().(string)
nowarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
nomake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
)

cmdstr := argStr()

bind.NoWarn = nowarn
bind.NoMake = nomake

if name == "" {
path := args[0]
Expand Down

0 comments on commit b9902d7

Please sign in to comment.