output files from build of swig file

145 views
Skip to first unread message

andrew...@miracl.com

unread,
Oct 14, 2016, 8:31:46 AM10/14/16
to golang-nuts
Hi,

Im trying to build a swig wrapper to a c library using go build. I know the swig interface file is good as it builds correctly with the swig command and generated the expected go wrapper file and the wrapper c file. I know it has built because if I change the include paths in my  CGO_CFLAGS environment variable the build fails to find the c library headers and fails. So it looks like it is building ok, but I dont see the go wrapper file generated anywhere. Is this created as a temporary file and then discarded after the build completes? Is there any way of forcing go build to output the swig wrappers in a specified directory?

Thanks

Andy

Ian Lance Taylor

unread,
Oct 14, 2016, 9:33:23 AM10/14/16
to andrew...@miracl.com, golang-nuts
There is no way to tell `go build` to put the SWIG output in a
specific place. But you can use the -work option to preserve the
temporary directory where the SWIG outputs are generated, and the -x
option to see the exact commands that `go build` is executing.

Ian

andrew...@miracl.com

unread,
Oct 17, 2016, 7:11:41 AM10/17/16
to golang-nuts, andrew...@miracl.com
Thanks for confirming my suspicions. 

I have a follow-up question, how can I instrument my go code so that I can set the include path that is used to resolve '%include' statements in my swig code. Currently I have relative paths in my swig file, and I would like to put '/usr/include' on the search path for the swig compilation. Is this possible?

Ian Lance Taylor

unread,
Oct 17, 2016, 12:50:44 PM10/17/16
to andrew...@miracl.com, golang-nuts
On Mon, Oct 17, 2016 at 4:11 AM, <andrew...@miracl.com> wrote:
>
> I have a follow-up question, how can I instrument my go code so that I can
> set the include path that is used to resolve '%include' statements in my
> swig code. Currently I have relative paths in my swig file, and I would like
> to put '/usr/include' on the search path for the swig compilation. Is this
> possible?

There is no general support for passing arguments to SWIG, but any -I
options found in the CGO_CFLAGS environment variable (see
https://golang.org/cmd/cgo) will be passed to SWIG.

Ian

andrew...@miracl.com

unread,
Oct 17, 2016, 4:17:54 PM10/17/16
to golang-nuts, andrew...@miracl.com
Thanks for confirming, I realised this by digging into the go build code. 

// Run SWIG on one SWIG input file.
  3553  func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
  3554          cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.cflags(p, true)
  3555          var cflags []string
  3556          if cxx {
  3557                  cflags = stringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)
  3558          } else {
  3559                  cflags = stringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)
  3560          }
  3561  
  3562          n := 5 // length of ".swig"
  3563          if cxx {
  3564                  n = 8 // length of ".swigcxx"
  3565          }
  3566          base := file[:len(file)-n]
  3567          goFile := base + ".go"
  3568          gccBase := base + "_wrap."
  3569          gccExt := "c"
  3570          if cxx {
  3571                  gccExt = "cxx"
  3572          }
  3573  
  3574          _, gccgo := buildToolchain.(gccgoToolchain)
  3575  
  3576          // swig
  3577          args := []string{
  3578                  "-go",
  3579                  "-cgo",
  3580                  "-intgosize", intgosize,
  3581                  "-module", base,
  3582                  "-o", obj + gccBase + gccExt,
  3583                  "-outdir", obj,
  3584          }
  3585  
  3586          for _, f := range cflags {
  3587                  if len(f) > 3 && f[:2] == "-I" {
  3588                          args = append(args, f)
  3589                  }
  3590          }
  3591  
  3592          if gccgo {
  3593                  args = append(args, "-gccgo")
  3594                  if pkgpath := gccgoPkgpath(p); pkgpath != "" {
  3595                          args = append(args, "-go-pkgpath", pkgpath)
  3596                  }
  3597          }
  3598          if cxx {
  3599                  args = append(args, "-c++")
  3600          }
  3601  
  3602          out, err := b.runOut(p.Dir, p.ImportPath, nil, "swig", args, file)
  3603          if err != nil {
  3604                  if len(out) > 0 {
  3605                          if bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo")) {
  3606                                  return "", "", errors.New("must have SWIG version >= 3.0.6")
  3607                          }
  3608                          b.showOutput(p.Dir, p.ImportPath, b.processOutput(out)) // swig error
  3609                          return "", "", errPrintedOutput
  3610                  }
  3611                  return "", "", err
  3612          }
  3613          if len(out) > 0 {
  3614                  b.showOutput(p.Dir, p.ImportPath, b.processOutput(out)) // swig warning
  3615          }
  3616  
  3617          return obj + goFile, obj + gccBase + gccExt, nil
  3618  }
Reply all
Reply to author
Forward
0 new messages