Is it possible use Go build with extra build steps?
Asked Answered
C

3

8

What to do when go build is not enough and ones need to run extra commands along with go build? Does go tools have this use case covered? If so, what's the convention?

I noticed it's possible to pass extra flags to build tools with:

//#cgo pkg-config: glib-2.0 gobject-2.0 etc etc
import "C"

Is it possible to run extra commands or at least tell go build to use a Makefile?

Carlyn answered 10/12, 2014 at 14:2 Comment(0)
A
5

No. The go tool isn't intended to be a generic build system. There are some provisions made for cgo (like pkg-config), but it's not extendable.

in go1.4 there will be the generate command. This will let you run arbitrary commands for pre-processing source files, but it always has to be a separate step that is run explicitly. You can't hook it into go get, go build, or go install.

Many projects that require a more complicated build use a script or a Makefile, and eschew the general ability go get. Library packages however should strive to be get-able for simplicity in dependecy resolution.

Axis answered 10/12, 2014 at 16:31 Comment(1)
Would be too good to be true to be able to override go build with a makefile. Go generate seems promising, though.Carlyn
U
1

I don't believe you can add extra steps.

pkg-config is a special keyword for the built in build system.

Normally, more complex builds are accomplished with a makefile that calls go build at the appropriate step.

Udele answered 10/12, 2014 at 14:34 Comment(2)
but what would happen if a user go get a package that needs to run a Makefile?Carlyn
go get wouldn't really work in that case. There are a few packages where you have to git clone and run make to get them installed.Udele
R
1

For those who stumble across this question - it is currently already possible to create your own build preprocessing tools.

For this you can use a -toolexec flag to specify a tool that will run a custom program at each stage of the build: compile, asm, link.

A naive example:

go build -a -toolexec "yourtool someargs"

This will run yoortool for each build step by passing all the arguments to yourtool, so that you can do literally anything, as long as this tool have enough permissions.

There are multiple projects that utilizes this to autoinstrument go code with tracing, for example. But, unfortunately, there is little to no documentation on how to implement such tools yourself.

You can watch a video of Jon Bodners's talk, which describes the principle with examples in a simplified but still detailed way.

Replace answered 4/5 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.