Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: go/build: add API to pick out cgo flags #52222

Open
ianlancetaylor opened this issue Apr 7, 2022 · 5 comments
Open

proposal: go/build: add API to pick out cgo flags #52222

ianlancetaylor opened this issue Apr 7, 2022 · 5 comments

Comments

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Apr 7, 2022

While most Go programmers use the go tool, there are alternative build systems such as Bazel. In order to build existing Go code, those build systems have to analyze build constraints in the same way as the go tool does. For most purposes that can be done by calling go/build.*Context.MatchFile. However, there is a case that is not handled by that API: #cgo directives in file that use cgo. A #cgo directive appears in the special comment ad looks like #cgo, an optional build constraint, a flag name, a colon, a list of flags. A build tool like Bazel needs a mechanism to take a file that uses cgo and return any #cgo flags appropriate for the current environment. Currently those tools must do this by recreating the build constraint evaluation process themselves. The go/build package already contains the code that does this for the go tool, but it is not exposed in a usable form; it is only exposed as part of creating a go/build.Package, but that assumes that directory layout used by the go tool but not by Bazel.

I propose that we add a new exported method to go/build similar to the existing saveCgo method.

// CgoFlags extracts #cgo flags from an import "C" comment that has already been parsed by the caller.
// Each element of the resulting slice will contain a flag name followed by the arguments for that flag.
// For example, given a comment containing
//
//     #cgo CFLAGS: -DDEBUG
//     #cgo linux CFLAGS: -DLINUX
//
// then on a Linux system the result will be [][]string{[]string{"CFLAGS", "-DDEBUG", "-DLINUX"}}.
//
// This method does not validate the #cgo flag names in any way, it simply returns them.
// It will return an error for a malformed #cgo line.
func (ctx *Context) CgoFlags(filepath string, cg *ast.CommentGroup) ([][]string, error)
@rsc
Copy link
Contributor

rsc commented Apr 13, 2022

/cc @matloob

@rsc
Copy link
Contributor

rsc commented Apr 13, 2022

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals (old) Apr 13, 2022
@rsc
Copy link
Contributor

rsc commented May 4, 2022

Waiting on @matloob to figure out an API proposal.

@rsc
Copy link
Contributor

rsc commented May 18, 2022

Let's put this on hold for an updated API proposal.

@rsc rsc moved this from Active to Hold in Proposals (old) May 18, 2022
@rsc
Copy link
Contributor

rsc commented May 18, 2022

Placed on hold.
— rsc for the proposal review group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Hold
Development

No branches or pull requests

2 participants