doufan3408
2013-02-27 04:13
浏览 18
已采纳

Debian的Packaging Go应用程序

How can I put my Go binary into a Debian package? Since Go is statically linked, I just have a single executable--I don't need a lot of complicated project metadata information. Is there a simple way to package the executable and resource files without going through the trauma of debuild?

I've looked all over for existing questions; however, all of my research turns up questions/answers about a .deb file containing the golang development environment (i.e., what you would get if you do sudo apt-get install golang-go).

图片转代码服务由CSDN问答提供 功能建议

如何将Go二进制文件放入Debian软件包中? 由于Go是静态链接的,所以我只有一个可执行文件-我不需要很多复杂的项目元数据信息。 是否有一种简单的方法来打包可执行文件和资源文件,而不会经历 debuild 的痛苦?

我到处都在寻找现有的问题; 但是,我所有的研究都提出了有关包含golang开发环境的.deb文件的问题/答案(即,如果您执行 sudo apt-get install golang-go 会得到什么)。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dtwxmn8741 2013-02-27 07:50
    已采纳

    Well. I think the only "trauma" of debuild is that it runs lintian after building the package, and it's lintian who tries to spot problems with your package.

    So there are two ways to combat the situation:

    • Do not use debuild: this tool merely calls dpkg-buildpackage which really does the necessary powerlifting. The usual call to build a binary package is dpkg-buildpackage -us -uc -b. You still might call debuild for other purposes, like debuild clean for instance.
    • Add the so-called "lintian override" which can be used to make lintian turn a blind eye to selected problems with your package which, you insist, are not problems.

    Both approaches imply that you do not attempt to build your application by the packaging tools but rather treat it as a blob which is just wrapped to a package. This would require slightly abstraining from the normal way debian/rules work (to not attempt to build anything).

    Another solution which might be possible (and is really way more Debian-ish) is to try to use gcc-go (plus gold for linking): since it's a GCC front-end, this tool produces a dynamically-linked application (which links against libgo or something like this). I, personally, have no experience with it yet, and would only consider using it if you intend to try to push your package into the Debian proper.

    Regarding the general question of packaging Go programs for Debian, you might find the following resources useful:

    • This thread started on go-nuts by one of Go for Debian packagers.
    • In particular, the first post in that thread links to this discussion on debian-devel.
    • The second thread on debian-devel regarding that same problem (it's a logical continuation of the former thread).

    Update on 2015-10-15.

    (Since this post appears to still be searched and found and studied by people I've decided to update it to better reflec the current state of affairs.)

    Since then the situation with packaging Go apps and packages got improved dramatically, and it's possible to build a Debian package using "classic" Go (the so-called gc suite originating from Google) rather than gcc-go. And there exist a good infrastructure for packages as well.

    The key tool to use when debianizing a Go program now is dh-golang described here.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzi3756 2013-02-28 10:19

    I've just been looking into this myself, and I'm basically there.

    Synopsis

    By 'borrowing' from the 'package' branch from one of Canonical's existing Go projects, you can build your package with dpkg-buildpackage.

    1. install dependencies and grab a 'package' branch from another repo.

       # I think this list of packages is enough. May need dpkg-dev aswell.
       sudo apt-get install bzr debhelper build-essential golang-go
       bzr branch lp:~niemeyer/cobzr/package mypackage-build
       cd mypackage-build
      
    2. Edit the metadata.

      • edit debian/control file (name, version, source). You may need to change the golang-stable dependency to golang-go.
      • The debian/control file is the manifest. Note the 'build dependencies' (Build-Depends: debhelper (>= 7.0.50~), golang-stable) and the 3 architectures. Using Ubuntu (without the gophers ppa), I had to change golang-stable to golang-go.
      • edit debian/rules file (put your package name in place of cobzr).
      • The debian/rules file is basically a 'make' file, and it shows how the package is built. In this case they are relying heavily on debhelper. Here they set up GOPATH, and invoke 'go install'. Here's the magic 'go install' line:

      cd $(GOPATH)/src && find * -name '*.go' -exec dirname {} \; | xargs -n1 go install

      • Also update the copyright file, readme, licence, etc.
    3. Put your source inside the src folder. e.g.

       git clone https://github.com/yourgithubusername/yourpackagename src/github.com/yourgithubusername/yourpackagename
      

      or e.g.2

       cp .../yourpackage/ src/
      
    4. build the package

       # -us -uc skips package signing.
       dpkg-buildpackage -us -uc
      

      This should produce a binary .deb file for your architecture, plus the 'source deb' (.tgz) and the source deb description file (.dsc).

    More details

    So, I realised that Canonical (the Ubuntu people) are using Go, and building .deb packages for some of their Go projects. Ubuntu is based on Debian, so for the most part the same approach should apply to both distributions (dependency names may vary slightly).

    You'll find a few Go-based packages in Ubuntu's Launchpad repositories. So far I've found cobzr (git-style branching for bzr) and juju-core (a devops project, being ported from Python).

    Both of these projects have both a 'trunk' and a 'package' branch, and you can see the debian/ folder inside the package branch. The 2 most important files here are debian/control and debian/rules - I have linked to 'browse source'.

    Finally

    Something I haven't covered is cross-compiling your package (to the other 2 architectures of the 3, 386/arm/amd64). Cross-compiling isn't too tricky in go (you need to build the toolchain for each target platform, and then set some ENV vars during 'go build'), and I've been working on a cross-compiler utility myself. Eventually I'll hopefully add .deb support into my utility, but first I need to crystallize this task.

    Good luck. If you make any progress then please update my answer or add a comment. Thanks

    评论
    解决 无用
    打赏 举报
  • dousi4148 2015-02-09 09:03

    Building deb or rpm packages from Go Applications is also very easy with fpm.

    Grab it from rubygems:

    gem install fpm
    

    After building you binary, e.g. foobar, you can package it like this:

    fpm -s dir -t deb -n foobar -v 0.0.1 foobar=/usr/bin/
    

    fpm supports all sorts of advanced packaging options.

    评论
    解决 无用
    打赏 举报
  • douwei1904 2015-09-09 15:42

    I recently discovered https://packager.io/ - I'm quite happy with what they're doing. Maybe open up one of the packages to see what they're doing?

    评论
    解决 无用
    打赏 举报
  • douchengjue9892 2016-05-12 23:12

    There is an official Debian policy document describing the packaging procedure for Go: https://go-team.pages.debian.net/packaging.html

    For libraries: Use dh-make-golang to create a package skeleton. Name your package with a name derived from import path, with a -dev suffix, e.g. golang-github-lib-pq-dev. Specify the dependencies ont Depends: line. (These are source dependencies for building, not binary dependencies for running, since Go statically links all source.)

    Installing the library package will install its source code to /usr/share/golang/src (possibly, the compiled libraries could go into .../pkg). Building depending Go packages will use the artifacts from those system-wide locations.

    For executables: Use dh-golang to create the package. Specify dependencies in Build-Depends: line (see above regarding packaging the dependencies).

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题