-->

What build systems work with Go? [closed]

2019-04-04 17:43发布

问题:

I know that the Go source comes with a Makefile (It's in $GOROOT/doc) which I am using right now, but have other popular build systems added support for Go yet? Has anyone written build scripts for scons, waf etc...

What do you use to build your Go programs?

回答1:

I've been using scons; this is an example SConstruct file:

archs = {'amd64': '6', '386': '8', 'arm': '5',}

def gc(source, target, env, for_signature):
    targets = target[0]
    sources = ' '.join(str(s) for s in source)
    flags = ''
    for include in env.get('GOINCLUDE', []):
        flags += '-I %s ' % (include)
    return '%s -o %s %s %s' % (env['GOCOMPILER'], targets, flags, sources)

def ld(source, target, env, for_signature):
    targets = target[0]
    sources = ' '.join(str(s) for s in source)
    return '%s -o %s %s' % (env['GOLINKER'], targets, sources)

def _go_object_suffix(env, sources):
    return "." + archs[env['ENV']['GOARCH']]

def _go_program_prefix(env, sources):
    return env['PROGPREFIX']

def _go_program_suffix(env, sources):
    return env['PROGSUFFIX']

go_compiler = Builder(generator=gc,
                      suffix=_go_object_suffix,
                      src_suffix='.go',)
go_linker = Builder(generator=ld,
                    prefix=_go_program_prefix,
                    suffix=_go_program_suffix,)

# Create environment
import os
env = Environment(BUILDERS={'Go': go_compiler, 'GoProgram': go_linker},
                  ENV=os.environ,)
arch_prefix = archs[os.environ['GOARCH']]
env.SetDefault(GOCOMPILER=os.path.join(os.environ['GOBIN'], arch_prefix + 'g'))
env.SetDefault(GOLINKER=os.path.join(os.environ['GOBIN'], arch_prefix + 'l'))
# Build programs
# Modify this to suit your program
main_package = env.Go(target='main', source='main.go')
program = env.GoProgram(target='program', source=[main_package])


回答2:

You can find in Go Utils and Tools all the available build tools for Go.

But more of them are superseded by the "go build" command and the lack of Makefile with Go 1.
See "The go tool" blog post.

Go packages don't have any build configuration at all. There's no makefiles, no descriptions of dependencies etc.
How it works then? Everything is retrieved from the source code. To let the magic happen one thing has to be done first.

Even if Makefile can still be used, for pure Go source code, they can be removed (like in this code review for instance)



回答3:

I've built my own little tool called gobuild for that, and am still working on it. It should be able to compile most programs/libs that don't interfacing with C code without having to write any build-scripts/makefiles.



回答4:

I haven't written a large enough project yet to require a build system, so a simple build.sh is sufficient.

You can use $GOROOT, $GOARCH and $GOOS to determine what you need:

jurily@jurily ~ $ env | grep GO
GOARCH=amd64
GOROOT=/home/jurily/go
GOOS=linux

If it's enough for Go, it's enough for me.