Difference between revisions of "Go package guidelines"

From ArchWiki
Jump to navigation Jump to search
m (Using go get (recommended))
m (Changed pronouns and verbs to match the noun. Spelling fix too.)
 
(53 intermediate revisions by 19 users not shown)
Line 1: Line 1:
[[Category:Package development]]
+
[[Category:Arch package guidelines]]
{{Package Guidelines}}
+
[[ja:Go パッケージガイドライン]]
 +
[[pt:Go package guidelines]]
 +
[[zh-hans:Go package guidelines]]
 +
{{Package guidelines}}
  
[[Wikipedia:Go (programming language)|Go]] is well supported on Arch Linux.
+
This document covers standards and guidelines on writing [[PKGBUILD]]s for [[Go]].
  
The {{Pkg|go}} package contains the '''go''' tool (for running {{Ic|go fix}}, {{Ic|go build}} etc). There is also the {{AUR|go-hg}} package in [[AUR]] and {{Pkg|gcc-go}} which provides {{Ic|gccgo}}.
+
== General guidelines ==
  
= General guidelines =
+
=== Package naming ===
  
== Naming ==
+
For [[Go]] library modules, use {{ic|go-''modulename''}}. Also use the prefix if the package provides a program that is strongly coupled to the Go ecosystem. For other applications, use only the program name.
* For applications written in Go, use the name of the application as the package name, in lowercase.
 
** Be creative if the name is already taken.
 
* For libraries written in Go, use {{Ic|go-''modulename''}}, in lowercase.
 
* If the name already starts with {{Ic|go-}}, don't call the package {{Ic|go-''go-modulename''}}, but just {{Ic|go-''modulename''}}.
 
* For PKGBUILDS that uses the "go" tool to download the package, only add "-git" to the package name if it's not built from a tarball or a from a tagged release (but from trunk/HEAD).
 
** Similarly for mercurial packages, only add "-hg" to the package name if it's not a release-revision.
 
** Extend this pattern for other version control systems.
 
** The go tool has its own logic for which branch or tag it should use. See {{Ic|go get --help}}.
 
* Consider adding the name of the author to the package name if there are several applications that are named the same, like {{AUR|dcpu16-kballard}}.
 
** In general, the most popular packages should be allowed to use the shortest or "best" name.
 
* Postfixes to the package names (like {{Ic|-hg}}, {{Ic|-git}} or {{Ic|-svn}}) are optional if there are no official releases from the project in question. On one hand, it's common to use them when the package downloads from a VCS. On the other hand, most Go projects don't have any release-tarballs, only the repo which is used for branching/tagging the official release, if it's not ''trunk''. Also, {{Ic|go get}}, which is the "official" way to install Go modules, uses the repositories directly. Use your better judgement.
 
  
== Packaging ==
+
{{Note|The package name should be entirely lowercase.}}
* Go projects are either just library files, just executables or both. Choose the appropriate way of packaging them. There are several examples below.
 
* Some Go applications or libraries have not been updated to the latest version of Go yet.
 
** Running {{Ic|go build -fix}} may often work, but it may have to be fixed by the developer. Report an issue upstream if this is the case.
 
* Several Go projects doesn't have a version number or a license file.
 
** Use license=('unknown') and report an issue to the developer if a license file is missing.
 
** Use version "0.1", "1" or the git-revision (or equivivalent for other version control systems) if the version number is missing.
 
** Alternatively, use the current date as the version number, in this form {{Ic|YYYYMMDD}}.
 
* Since Go applications are usually statically compiled, it's hard to envision reasons for packaging Go libraries instead of just Go applications.
 
  
= Sample PKGBUILDs =
+
=== Instructions for alternative dependency managers ===
  
== Sample PKGBUILD for an application written in Go ==
+
This is not needed for Go 1.11 and later, unless an alternative dependency manager is required by the Go project you are packaging.
  
{{bc|<nowiki># Maintainer: NAME <EMAIL>
+
When preparing the sources before building, the following may be needed:
  
pkgname=PACKAGE NAME
+
* Create a directory {{ic|$srcdir/gopath}} for [[Go#$GOPATH|$GOPATH]] and copy over the source to this directory.
pkgver=1.2.3
+
* It should be noted that this step might not be needed if the project provides a {{ic|Makefile}} for the project that sets this up.
pkgrel=1
 
pkgdesc="PACKAGE DESCRIPTION"
 
arch=('x86_64' 'i686')
 
url="http://SERVER/$pkgname/"
 
license=('MIT')
 
makedepends=('go')
 
options=('!strip' '!emptydirs')
 
source=("http://SERVER/$pkgname/$pkgname-$pkgver.tar.gz")
 
sha256sums=('00112233445566778899aabbccddeeff')
 
  
build() {
+
{{bc|<nowiki>
   cd "$srcdir/$pkgname-$pkgver"
+
prepare(){
 +
   mkdir -p gopath/src/github.com/pkgbuild-example
 +
  ln -rTsf $pkgname-$pkgver gopath/src/github.com/pkgbuild-example/$pkgname
  
   go build
+
   # the dependencies can be fetched here if needed
 +
  cd gopath/src/github.com/pkgbuild-example/$pkgname
 +
  dep ensure
 
}
 
}
 +
</nowiki>}}
  
package() {
+
== Building ==
  cd "$srcdir/$pkgname-$pkgver"
 
  
  install -Dm755 "$pkgname-$pkgver" "$pkgdir/usr/bin/$pkgname"
+
There are two go packages in the repositories that you can build towards; {{Pkg|go}} and {{Pkg|go-pie}}. All packages should preferably be built towards {{Pkg|go-pie}} as this enables us to deliver secure binaries. However, as upstream might have bugs, building towards {{Pkg|go}} should be a last resort.
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
 
}
 
  
# vim:set ts=2 sw=2 et:</nowiki>}}
+
=== Flags and build options ===
  
=== Sample packages ===
+
Most Makefiles written for go applications do not respect the {{ic|LDFLAGS}} provided by build systems, this causes Go binaries to not be compiled with RELRO. This needs to be patched into the Makefile, or the Makefile should be omitted. If there is an Makefile involved, you have 3 options to support RELRO.
* {{Pkg|gendesk}}
 
* {{AUR|dcpu16}}
 
  
== Sample PKGBUILD for when only a single source file is available ==
+
* Patch Makefile
 +
* Skip Makefile completely and use {{ic|go build}}
 +
* Export {{ic|GOFLAGS}} - This is less desirable as we are dropping flags from {{ic|LDFLAGS}}.
  
{{bc|<nowiki># Maintainer: NAME <EMAIL>
+
For [https://reproducible-builds.org/ reproducible builds] it's important that the binaries are stripped of the build path using the {{ic|-trimpath}} flags.
  
pkgname=PACKAGE NAME
+
{{bc|<nowiki>
pkgver=1.2.3
+
# LDFLAGS into the GOFLAGS env variable.
pkgrel=1
+
export GOFLAGS="-gcflags=all=-trimpath=${PWD} -asmflags=all=-trimpath=${PWD} -ldflags=-extldflags=-zrelro -ldflags=-extldflags=-znow"
pkgdesc="PACKAGE DESCRIPTION"
 
arch=('x86_64' 'i686')
 
url="http://SERVER/$pkgname/"
 
license=('GPL3')
 
makedepends=('go')
 
options=('!strip' '!emptydirs')
 
source=("http://SERVER/$pkgname/$pkgname.go")
 
sha256sums=('00112233445566778899aabbccddeeff')
 
  
build() {
+
# or alternatively use LDFLAGS defined in go build.
  cd "$srcdir"
+
go build \
 +
    -gcflags "all=-trimpath=${PWD}" \
 +
    -asmflags "all=-trimpath=${PWD}" \
 +
    -ldflags "-extldflags ${LDFLAGS}" \
 +
    .
 +
</nowiki>}}
  
  go build -o "$pkgname"
+
{{Note|For the sake of brevity, these flags are omitted in the examples below.}}
}
 
  
package() {
+
=== Modern Go projects (for Go >=1.11) ===
  cd "$srcdir"
 
  
  install -Dm755 "$pkgname" "$pkgdir/usr/bin/$pkgname"
+
Go 1.11 introduces [https://github.com/golang/go/wiki/Modules go modules]. This omits the need to setup {{ic|GOPATH}} and we can build directly in the directory.
}
 
  
# vim:set ts=2 sw=2 et:</nowiki>}}
+
Projects like these have a {{ic|go.mod}} and a {{ic|go.sum}} file.
  
=== Sample packages ===
+
{{Note| These builds needs {{ic|$PWD}} stripped from the binary.}}
* {{AUR|gorun}}
 
  
== Sample PKGBUILDs for Go libraries (uncommon) ==
+
{{bc|<nowiki>
 
+
build(){
{{Warning| There's not really any point in packaging libraries until Go supports dynamic libraries! }}
+
  cd "$pkgname-$pkgver"
 
+
  go build .
{{Note| Not all libraries are upgraded to work with Go1 yet! If in doubt, try installing with {{Ic|go get}} first, before packaging.}}
+
}
 
+
</nowiki>}}
=== Using ''go get'' ===
 
 
 
This is the recommended way, instead of the method below.
 
 
 
Here's a way that relies on {{Ic|go get}}.
 
 
 
You probably won't need to modify the build() or package() functions at all, only the variables at the top (pkgname etc).
 
  
If this doesn't work, test with {{Ic|go get}} first. Change ''mercurial'' to ''git'', if that's what your package uses.
+
=== Old Go projects (for Go <1.11) ===
  
{{Note| Remove {{Ic|/...}} if the PKGBUILD fails!}}
+
When building go packages with [[Go#$GOPATH|$GOPATH]] there are a few problems one can encounter. Usually the project delivers a {{ic|Makefile}} that can be used and should be used. There are cases where you still need to setup a {{ic|$GOPATH}} in the [[PKGBUILD]]. The following snippet sets up an appropriate {{ic|GOPATH}} inside {{ic|$srcdir/gopath}} one can use to build packages. A new directory is used as some go dependency managers do weird things if they discover the project in the root of the {{ic|$GOPATH}}.
  
{{bc|<nowiki># Maintainer: NAME <EMAIL>
+
{{Note|These builds should have {{ic|$GOPATH}} stripped from the binary.}}
  
pkgname=go-net
+
{{bc|<nowiki>
pkgver=20120515
+
prepare(){
pkgrel=1
+
  mkdir -p gopath/src/github.com/pkgbuild-example
pkgdesc="Extra network libraries for Go (dict, spdy, websocket)"
+
  ln -rTsf $pkgname-$pkgver gopath/src/github.com/pkgbuild-example/$pkgname
arch=('x86_64' 'i686')
+
   export GOPATH="$srcdir"/gopath
url="http://code.google.com/p/go"
 
license=('BSD')
 
depends=('go')
 
makedepends=('mercurial')
 
options=('!strip' '!emptydirs')
 
_gourl=code.google.com/p/go.net
 
 
 
build() {
 
  cd "$srcdir"
 
   GOPATH="$srcdir" go get -fix -v -x ${_gourl}/...
 
}
 
  
check() {
+
  # the dependencies can be fetched here if needed
   source /etc/profile.d/go.sh
+
   cd gopath/src/github.com/pkgbuild-example/$pkgname
   GOPATH="$GOPATH:$srcdir" go test -v -x ${_gourl}/...
+
   dep ensure
 
}
 
}
  
package() {
+
build(){
   source /etc/profile.d/go.sh
+
   export GOPATH="$srcdir"/gopath
  mkdir -p "$pkgdir/$GOPATH"
+
   cd gopath/src/github.com/pkgbuild-example/$pkgname
  cp -Rv --preserve=timestamps ${srcdir}/{src,pkg} "$pkgdir/$GOPATH"
+
   go install -v .
 
 
  # Package license (if available)
 
   for f in LICENSE COPYING LICENSE.* COPYING.*; do
 
    if [ -e "$srcdir/src/$_gourl/$f" ]; then
 
      install -Dm644 "$srcdir/src/$_gourl/$f" \
 
        "$pkgdir/usr/share/licenses/$pkgname/$f"
 
    fi
 
   done
 
 
}
 
}
 +
</nowiki>}}
  
# vim:set ts=2 sw=2 et:</nowiki>}}
+
Note that {{ic|install}} and {{ic|build}} and can do recursive builds if you have a {{ic|cmd/}} subdirectory with multiple binaries: {{ic|go install -v github.com/pkgbuild-example/cmd/...}}
 
 
Thanks to Rémy Oudompheng‎ for this one.
 
 
 
Add "|| return 0" after the {{Ic|go test}} line if you wish to force the test to succeed. It's recommended to report the issue upstream to the relevant developers instead.
 
  
==== Sample packages ====
+
=== Notes about dependencies and dependency managers ===
* {{AUR|go-web}}
 
  
==== Troubleshooting ====
+
Go packages currently use the {{ic|vendor/}} folder to handle dependencies in projects. These are usually managed by one of several dependency manager projects. If one is used for the package, this step should be performed in the [[Creating packages#prepare()|prepare()]] function of the [[PKGBUILD]].
* If you get the error that there are two packages found, both package {{Ic|main}} and another package, try removing the corresponding .go file for the {{Ic|main}} package in the {{Ic|build()}} function. Alternatively, move it elsewhere when building and then copy it to {{Ic|/usr/share/doc/$pkgname/examples}} in the {{Ic|package()}} function.
 
  
=== Using ''go get''===
+
Dependencies are normally managed by the {{ic|go mod}} command, that comes with Go 1.11 or later.
  
Here's another way that relies on {{Ic|go get}}. It can be nice to have if the {{Ic|go get}} method above doesn't work.
+
There are also alternative dependency managers:
  
You probably won't need to modify the build() or package() functions at all, only the variables at the top (pkgname etc).
+
* {{Pkg|dep}}
 +
* {{Pkg|godep}}
 +
* {{Pkg|glide}}
  
If this doesn't work, test with {{Ic|go get}} first.
+
== Sample PKGBUILDs ==
  
{{bc|<nowiki># Maintainer: NAME <EMAIL>
+
=== Basic PKGBUILD ===
  
pkgname=go-PACKAGENAME
+
{{bc|<nowiki>
pkgver=1.2.3
+
pkgname=foo
 +
pkgver=0.0.1
 
pkgrel=1
 
pkgrel=1
pkgdesc="PACKAGE DESCRIPTION"
+
pkgdesc='Go PKGBUILD Example'
arch=('x86_64' 'i686')
+
arch=('x86_64')
url="http://SERVER/$pkgname/"
+
url="https://example.org/$pkgname"
license=('MIT')
+
license=('GPL')
makedepends=('go' 'git')
+
makedepends=('go-pie')
options=('!strip' '!emptydirs')
+
source=("$url/$pkgname-$pkgver.tar.gz")
_gourl=SERVER.NET/PATH/MODULENAME
+
sha256sums=('1337deadbeef')
  
 
build() {
 
build() {
   cd "$srcdir"
+
   cd $pkgname-$pkgver
  export GOROOT=/usr/lib/go
+
   go build \
 
+
     -gcflags "all=-trimpath=$PWD" \
  rm -rf build
+
     -asmflags "all=-trimpath=$PWD" \
   mkdir -p build/go
+
     -ldflags "-extldflags $LDFLAGS" \
  cd build/go
+
    -o $pkgname .
 
 
  for f in "$GOROOT/"*; do
 
     ln -s "$f"
 
  done
 
 
 
  rm pkg
 
  mkdir pkg
 
  cd pkg
 
 
 
  for f in "$GOROOT/pkg/"*; do
 
     ln -s "$f"
 
  done
 
 
 
  platform=`for f in "$GOROOT/pkg/"*; do echo \`basename $f\`; done|grep linux`
 
 
 
  rm "$platform"
 
  mkdir "$platform"
 
  cd "$platform"
 
 
 
  for f in "$GOROOT/pkg/$platform/"*.h; do
 
     ln -s "$f"
 
  done
 
 
 
  export GOROOT="$srcdir/build/go"
 
  export GOPATH="$srcdir/build"
 
 
 
  go get -fix "$_gourl"
 
 
}
 
}
  
 
package() {
 
package() {
   cd "$srcdir"
+
   cd $pkgname-$pkgver
 +
  install -Dm755 $pkgname "$pkgdir"/usr/bin/$pkgname
 +
}
 +
</nowiki>}}
  
  source /etc/profile.d/go.sh
+
=== PKGBUILD with GOPATH and dep ===
  export GOROOT="$GOPATH"
 
  
  # Package go package files
+
{{bc|<nowiki>
  for f in "$srcdir/build/go/pkg/"* "$srcdir/build/pkg/"*; do
+
pkgname=foo
    # If it's a directory
+
pkgver=0.0.1
    if [ -d "$f" ]; then
+
pkgrel=1
      cd "$f"
+
pkgdesc='Go PKGBUILD Example'
      mkdir -p "$pkgdir/$GOROOT/pkg/`basename $f`"
+
arch=('x86_64')
      for z in *; do
+
url='https://example.org/$pkgname'
        # Check if the directory name matches
+
license=('GPL')
        if [ "$z" == `echo $_gourl | cut -d/ -f1` ]; then
+
makedepends=('go-pie' 'dep')
          cp -r $z "$pkgdir/$GOROOT/pkg/`basename $f`"
+
source=("$url/$pkgname-$pkgver.tar.gz")
        fi
+
sha256sums=('1337deadbeef')
      done
 
      cd ..
 
    fi
 
  done
 
  
  # Package source files
+
prepare(){
   if [ -d "$srcdir/build/src" ]; then
+
   mkdir -p gopath/src/example.org/foo
    mkdir -p "$pkgdir/$GOROOT/src/pkg"
+
  ln -rTsf $pkgname-$pkgver gopath/src/example.org/foo
    cp -r "$srcdir/build/src/"* "$pkgdir/$GOROOT/src/pkg/"
+
   export GOPATH="$srcdir"/gopath
    find "$pkgdir" -depth -type d -name .git -exec rm -r {} \;
+
  cd gopath/src/example.org/foo
  fi
+
   dep ensure
 
 
   # Package license (if available)
 
  for f in LICENSE COPYING LICENSE.* COPYING.*; do
 
    if [ -e "$srcdir/build/src/$_gourl/$f" ]; then
 
      install -Dm644 "$srcdir/build/src/$_gourl/$f" \
 
        "$pkgdir/usr/share/licenses/$pkgname/$f"
 
    fi
 
   done
 
 
}
 
}
 
# vim:set ts=2 sw=2 et:</nowiki>}}
 
 
== Sample PKGBUILDs for Go libraries that also includes executables ==
 
 
=== Using ''go get'' ===
 
 
This is the recommended way, instead of the method below.
 
 
Here's a way that relies on go get.
 
 
You probably won't need to modify the build() or package() functions at all, only the variables at the top (pkgname etc).
 
 
If this doesn't work, test with go get first.
 
 
{{Note| Remove {{Ic|/...}} if the PKGBUILD fails!}}
 
 
{{bc|<nowiki># Maintainer: NAME <EMAIL>
 
 
pkgname=codesearch
 
pkgver=20120515
 
pkgrel=1
 
pkgdesc="Code indexing and search written in Go"
 
arch=('x86_64' 'i686')
 
url="http://code.google.com/p/codesearch"
 
license=('BSD')
 
depends=('go')
 
makedepends=('mercurial')
 
options=('!strip' '!emptydirs')
 
_gourl=code.google.com/p/codesearch
 
  
 
build() {
 
build() {
   cd "$srcdir"
+
   export GOPATH="$srcdir"/gopath
   GOPATH="$srcdir" go get -fix -v -x ${_gourl}/...
+
  cd gopath/src/example.org/foo
 +
   go install \
 +
    -gcflags "all=-trimpath=$GOPATH" \
 +
    -asmflags "all=-trimpath=$GOPATH" \
 +
    -ldflags "-extldflags $LDFLAGS" \
 +
    -v ./...
 
}
 
}
  
 
check() {
 
check() {
   source /etc/profile.d/go.sh
+
   export GOPATH="$srcdir"/gopath
   GOPATH="$GOPATH:$srcdir" go test -v -x ${_gourl}/...
+
  cd gopath/src/example.org/foo
 +
   go test ./...
 
}
 
}
  
 
package() {
 
package() {
  source /etc/profile.d/go.sh
+
   install -Dm755 gopath/bin/$pkgname "$pkgdir"/usr/bin/$pkgname
  mkdir -p "${pkgdir}/usr/bin"
 
   install -p -m755 ${srcdir}/bin/* "$pkgdir/usr/bin"
 
 
 
  mkdir -p "$pkgdir/$GOPATH"
 
  cp -Rv --preserve=timestamps ${srcdir}/{src,pkg} "$pkgdir/$GOPATH"
 
 
 
  # Package license (if available)
 
  for f in LICENSE COPYING LICENSE.* COPYING.*; do
 
    if [ -e "$srcdir/src/$_gourl/$f" ]; then
 
      install -Dm644 "$srcdir/src/$_gourl/$f" \
 
        "$pkgdir/usr/share/licenses/$pkgname/$f"
 
    fi
 
  done
 
}
 
 
 
# vim:set ts=2 sw=2 et:</nowiki>}}
 
 
 
Thanks to Rémy Oudompheng‎ for this one.
 
 
 
=== Using ''go get'' ===
 
 
 
Here's another way that relies on {{Ic|go get}}.
 
 
 
You probably won't need to modify the build() or package() functions at all, only the variables at the top (pkgname etc).
 
 
 
If this doesn't work, test with {{Ic|go get}} first.
 
 
 
{{bc|<nowiki># Maintainer: NAME <EMAIL>
 
 
 
pkgname=PACKAGE NAME
 
pkgver=1.2.3
 
pkgrel=1
 
pkgdesc="PACKAGE DESCRIPTION"
 
arch=('x86_64' 'i686')
 
url="http://SERVER/$pkgname/"
 
license=('MIT')
 
makedepends=('go' 'git')
 
options=('!strip' '!emptydirs')
 
_gourl=SERVER.NET/PATH/MODULENAME
 
 
 
build() {
 
  cd "$srcdir"
 
 
 
  export GOROOT=/usr/lib/go
 
 
 
  rm -rf build
 
  mkdir -p build/go
 
  cd build/go
 
 
 
  for f in "$GOROOT/"*; do
 
    ln -s "$f"
 
  done
 
 
 
  rm pkg
 
  mkdir pkg
 
  cd pkg
 
 
 
  for f in "$GOROOT/pkg/"*; do
 
    ln -s "$f"
 
  done
 
 
 
  platform=`for f in "$GOROOT/pkg/"*; do echo \`basename $f\`; done|grep linux`
 
 
 
  rm "$platform"
 
  mkdir "$platform"
 
  cd "$platform"
 
 
 
  for f in "$GOROOT/pkg/$platform/"*.h; do
 
    ln -s "$f"
 
  done
 
 
 
  export GOROOT="$srcdir/build/go"
 
  export GOPATH="$srcdir/build"
 
 
 
  go get -fix "$_gourl"
 
 
 
  # Prepare executable
 
  if [ -d "$srcdir/build/src" ]; then
 
    cd "$srcdir/build/src/$_gourl"
 
    go build -o "$srcdir/build/$pkgname"
 
  else
 
    echo 'Old sources for a previous version of this package are already present!'
 
    echo 'Build in a chroot or uninstall the previous version.'
 
    return 1
 
  fi
 
 
}
 
}
 +
</nowiki>}}
  
package() {
+
== Example packages ==
  cd "$srcdir"
 
 
 
  source /etc/profile.d/go.sh
 
  export GOROOT="$GOPATH"
 
 
 
  # Package go package files
 
  for f in "$srcdir/build/go/pkg/"* "$srcdir/build/pkg/"*; do
 
    # If it's a directory
 
    if [ -d "$f" ]; then
 
      cd "$f"
 
      mkdir -p "$pkgdir/$GOROOT/pkg/`basename $f`"
 
      for z in *; do
 
        # Check if the directory name matches
 
        if [ "$z" == `echo $_gourl | cut -d/ -f1` ]; then
 
          cp -r $z "$pkgdir/$GOROOT/pkg/`basename $f`"
 
        fi
 
      done
 
      cd ..
 
    fi
 
  done
 
 
 
  # Package source files
 
  if [ -d "$srcdir/build/src" ]; then
 
    mkdir -p "$pkgdir/$GOROOT/src/pkg"
 
    cp -r "$srcdir/build/src/"* "$pkgdir/$GOROOT/src/pkg/"
 
    find "$pkgdir" -depth -type d -name .git -exec rm -r {} \;
 
  fi
 
 
 
  # Package license (if available)
 
  for f in LICENSE COPYING; do
 
    if [ -e "$srcdir/build/src/$_gourl/$f" ]; then
 
      install -Dm644 "$srcdir/build/src/$_gourl/$f" \
 
        "$pkgdir/usr/share/licenses/$pkgname/$f"
 
    fi
 
  done
 
 
 
  # Package executables
 
  if [ -e "$srcdir/build/$pkgname" ]; then
 
    install -Dm755 "$srcdir/build/$pkgname" \
 
      "$pkgdir/usr/bin/$pkgname"
 
  fi
 
}
 
  
# vim:set ts=2 sw=2 et:</nowiki>}}
+
* {{Pkg|dep}}
 +
* {{Pkg|gopass}}
 +
* {{Pkg|delve}}
 +
* {{Pkg|gitea}}
 +
* {{Pkg|git-lfs}}

Latest revision as of 15:55, 23 January 2019

Package creation guidelines

32-bitCLRCrossEclipseElectronFree PascalGNOMEGoHaskellJavaKDEKernelLispMinGWNode.jsNonfreeOCamlPerlPHPPythonRRubyRustVCSWebWine

This document covers standards and guidelines on writing PKGBUILDs for Go.

General guidelines

Package naming

For Go library modules, use go-modulename. Also use the prefix if the package provides a program that is strongly coupled to the Go ecosystem. For other applications, use only the program name.

Note: The package name should be entirely lowercase.

Instructions for alternative dependency managers

This is not needed for Go 1.11 and later, unless an alternative dependency manager is required by the Go project you are packaging.

When preparing the sources before building, the following may be needed:

  • Create a directory $srcdir/gopath for $GOPATH and copy over the source to this directory.
  • It should be noted that this step might not be needed if the project provides a Makefile for the project that sets this up.
prepare(){
  mkdir -p gopath/src/github.com/pkgbuild-example
  ln -rTsf $pkgname-$pkgver gopath/src/github.com/pkgbuild-example/$pkgname

  # the dependencies can be fetched here if needed
  cd gopath/src/github.com/pkgbuild-example/$pkgname
  dep ensure
}

Building

There are two go packages in the repositories that you can build towards; go and go-pie. All packages should preferably be built towards go-pie as this enables us to deliver secure binaries. However, as upstream might have bugs, building towards go should be a last resort.

Flags and build options

Most Makefiles written for go applications do not respect the LDFLAGS provided by build systems, this causes Go binaries to not be compiled with RELRO. This needs to be patched into the Makefile, or the Makefile should be omitted. If there is an Makefile involved, you have 3 options to support RELRO.

  • Patch Makefile
  • Skip Makefile completely and use go build
  • Export GOFLAGS - This is less desirable as we are dropping flags from LDFLAGS.

For reproducible builds it's important that the binaries are stripped of the build path using the -trimpath flags.

# LDFLAGS into the GOFLAGS env variable.
export GOFLAGS="-gcflags=all=-trimpath=${PWD} -asmflags=all=-trimpath=${PWD} -ldflags=-extldflags=-zrelro -ldflags=-extldflags=-znow"

# or alternatively use LDFLAGS defined in go build.
go build \
    -gcflags "all=-trimpath=${PWD}" \
    -asmflags "all=-trimpath=${PWD}" \
    -ldflags "-extldflags ${LDFLAGS}" \
    .
Note: For the sake of brevity, these flags are omitted in the examples below.

Modern Go projects (for Go >=1.11)

Go 1.11 introduces go modules. This omits the need to setup GOPATH and we can build directly in the directory.

Projects like these have a go.mod and a go.sum file.

Note: These builds needs $PWD stripped from the binary.
build(){
  cd "$pkgname-$pkgver"
  go build .
}

Old Go projects (for Go <1.11)

When building go packages with $GOPATH there are a few problems one can encounter. Usually the project delivers a Makefile that can be used and should be used. There are cases where you still need to setup a $GOPATH in the PKGBUILD. The following snippet sets up an appropriate GOPATH inside $srcdir/gopath one can use to build packages. A new directory is used as some go dependency managers do weird things if they discover the project in the root of the $GOPATH.

Note: These builds should have $GOPATH stripped from the binary.
prepare(){
  mkdir -p gopath/src/github.com/pkgbuild-example
  ln -rTsf $pkgname-$pkgver gopath/src/github.com/pkgbuild-example/$pkgname
  export GOPATH="$srcdir"/gopath

  # the dependencies can be fetched here if needed
  cd gopath/src/github.com/pkgbuild-example/$pkgname
  dep ensure
}

build(){
  export GOPATH="$srcdir"/gopath
  cd gopath/src/github.com/pkgbuild-example/$pkgname
  go install -v .
}

Note that install and build and can do recursive builds if you have a cmd/ subdirectory with multiple binaries: go install -v github.com/pkgbuild-example/cmd/...

Notes about dependencies and dependency managers

Go packages currently use the vendor/ folder to handle dependencies in projects. These are usually managed by one of several dependency manager projects. If one is used for the package, this step should be performed in the prepare() function of the PKGBUILD.

Dependencies are normally managed by the go mod command, that comes with Go 1.11 or later.

There are also alternative dependency managers:

Sample PKGBUILDs

Basic PKGBUILD

pkgname=foo
pkgver=0.0.1
pkgrel=1
pkgdesc='Go PKGBUILD Example'
arch=('x86_64')
url="https://example.org/$pkgname"
license=('GPL')
makedepends=('go-pie')
source=("$url/$pkgname-$pkgver.tar.gz")
sha256sums=('1337deadbeef')

build() {
  cd $pkgname-$pkgver
  go build \
    -gcflags "all=-trimpath=$PWD" \
    -asmflags "all=-trimpath=$PWD" \
    -ldflags "-extldflags $LDFLAGS" \
    -o $pkgname .
}

package() {
  cd $pkgname-$pkgver
  install -Dm755 $pkgname "$pkgdir"/usr/bin/$pkgname
}

PKGBUILD with GOPATH and dep

pkgname=foo
pkgver=0.0.1
pkgrel=1
pkgdesc='Go PKGBUILD Example'
arch=('x86_64')
url='https://example.org/$pkgname'
license=('GPL')
makedepends=('go-pie' 'dep')
source=("$url/$pkgname-$pkgver.tar.gz")
sha256sums=('1337deadbeef')

prepare(){
  mkdir -p gopath/src/example.org/foo
  ln -rTsf $pkgname-$pkgver gopath/src/example.org/foo
  export GOPATH="$srcdir"/gopath
  cd gopath/src/example.org/foo
  dep ensure
}

build() {
  export GOPATH="$srcdir"/gopath
  cd gopath/src/example.org/foo
  go install \
    -gcflags "all=-trimpath=$GOPATH" \
    -asmflags "all=-trimpath=$GOPATH" \
    -ldflags "-extldflags $LDFLAGS" \
    -v ./...
}

check() {
  export GOPATH="$srcdir"/gopath
  cd gopath/src/example.org/foo
  go test ./...
}

package() {
  install -Dm755 gopath/bin/$pkgname "$pkgdir"/usr/bin/$pkgname
}

Example packages