golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.51k stars 17.6k forks source link

cmd/cgo: godefs doesn't work nicely with multiple input files #14919

Open mundaym opened 8 years ago

mundaym commented 8 years ago

It would be nice if the output of the command cgo -godefs with multiple input files resulted in a legal source file. Currently it duplicates the package declaration (and the generated comments) rendering the output useless without post-processing (since all the output goes straight to stdout):

$ cat a.go
package ex

/*
typedef int a;
*/
import "C"

type Atype C.a
$ cat b.go
package ex

/*
typedef int b;
*/
import "C"

type Btype C.b
$ go tool cgo -godefs a.go b.go
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs a.go b.go

package ex

type Atype int32
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs a.go b.go

package ex

type Btype int32

It would be nice if the output were instead:

$ go tool cgo -godefs a.go b.go
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs a.go b.go

package ex

type Atype int32

type Btype int32

This would be useful in situations where cgo -godefs requires extra/different information on different platforms. It is generally fairly easy to workaround though and the lack of this feature in previous versions of go might limit its usefulness.

minux commented 8 years ago

I don't know why there are so widespread use of an internal cgo feature that is only intended for bootstrapping Go on a new platform.

What do you use cgo -godefs for?

mundaym commented 8 years ago

My use case is the syscall package in std and maybe x/sys/unix. Both for the s390x port. We need to add some new struct types to types_linux.go and I haven't thought of a clean way to do it without either adding to the APIs on other platforms or adding a new autogenerated (z*.go) file. It probably isn't worth adding this feature just for this use case, but I thought I'd create an issue in case there was any other interest.