哪些构建系统可与 Go 配合使用?

发布于 2024-08-11 01:17:42 字数 1436 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小镇女孩 2024-08-18 01:17:42

我一直在使用 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,)

# 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])

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])
晨与橙与城 2024-08-18 01:17:42

您可以在Go Utils and Tools中找到所有可用的 Go 构建工具。

但其中更多被“go build”命令取代,并且 Go 1 缺少 Makefile。
请参阅“go 工具”博客文章。

Go 包根本没有任何构建配置。没有 makefile,没有依赖关系的描述等。
那么它是如何运作的呢?一切都是从源代码中检索的。为了让奇迹发生,必须先完成一件事。

即使 Makefile 仍然可以使用,对于纯 Go 源代码,它们也可以被删除(就像在这个代码审查 例如)

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)

满意归宿 2024-08-18 01:17:42

我为此构建了自己的小工具,名为 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.

剑心龙吟 2024-08-18 01:17:42

我还没有编写足够大的项目来需要构建系统,因此一个简单的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文