英文:
What build systems work with Go?
问题
我知道Go源代码附带一个Makefile(在$GOROOT/doc
中),我现在正在使用它,但是其他流行的构建系统是否已经支持Go了?是否有人为scons、waf等编写了构建脚本?
你用什么来构建你的Go程序?
英文:
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
得分: 10
我一直在使用scons; 这是一个示例的SConstruct
文件:
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,)
# 创建环境
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'))
# 构建程序
# 根据您的程序进行修改
main_package = env.Go(target='main', source='main.go')
program = env.GoProgram(target='program', source=[main_package])
英文:
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
得分: 3
你可以在**Go Utils and Tools**中找到所有可用的Go构建工具。
但是,其中更多的工具已被"go build
"命令取代,并且在Go 1中缺少Makefile。
请参阅"The go tool"博文。
Go包根本没有任何构建配置。没有makefile,没有依赖描述等。
那么它是如何工作的呢?一切都是从源代码中获取的。为了让魔法发生,首先必须做一件事。
即使Makefile仍然可以使用,对于纯Go源代码,它们可以被移除(例如在这个code review中)
英文:
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
得分: 2
我已经建立了一个叫做gobuild的小工具来实现这个功能,并且仍在继续开发中。它应该能够编译大多数不涉及C代码接口的程序/库,而无需编写任何构建脚本/Makefile。
英文:
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
得分: -1
我还没有写过足够大的项目来需要构建系统,所以一个简单的 build.sh
就足够了。
你可以使用 $GOROOT
、$GOARCH
和 $GOOS
来确定你需要的内容:
jurily@jurily ~ $ env | grep GO
GOARCH=amd64
GOROOT=/home/jurily/go
GOOS=linux
如果对于 Go 来说足够了,那对于我来说也足够了。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论