Difference between revisions of "Go package guidelines"

From ArchWiki
Jump to navigation Jump to search
(Minor changes)
m (Changed pronouns and verbs to match the noun. Spelling fix too.)
 
(70 intermediate revisions by 20 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) and there is also the {{AUR|gccgo}} package which provides {{Ic|gccgo}} and {{AUR|go-hg}} in [[AUR]].
+
== 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 the ''_gitroot'' and ''_gitname'' variables are used.
 
** Similarly for mercurial packages, only add "-hg" to the package name if the ''_hgroot'' and ''_hgname'' variables are used.
 
** 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 applications are either just library files, just executables or both. Choose an appropriate way of packaging them. There are several examples below.
 
* Some Go applications or libraries have not been updated to Go 1 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}}.
 
  
= 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
  
   source /etc/profile.d/go.sh
+
   # the dependencies can be fetched here if needed
   go build
+
  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 go 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>}}
  
  source /etc/profile.d/go.sh
+
{{Note|For the sake of brevity, these flags are omitted in the examples below.}}
  go build -o "$pkgname"
 
}
 
  
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 ==
+
{{bc|<nowiki>
 
+
build(){
{{Note| Not all libraries are upgraded to work with Go1 yet! If in doubt, try installing with {{Ic|go get}} first, before packaging.}}
+
  cd "$pkgname-$pkgver"
 
+
  go build .
=== Using ''go get'' '''(recommended)''' ===
+
}
 
+
</nowiki>}}
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).
+
=== Old Go projects (for Go <1.11) ===
  
If this doesn't work, test with {{Ic|go get}} first. Change ''mercurial'' to ''git'', if that's what your package uses.
+
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}}.
  
{{Note| Remove {{Ic|/...}} if the PKGBUILD fails!}}
+
{{Note|These builds should have {{ic|$GOPATH}} stripped from the binary.}}
  
{{bc|<nowiki># Maintainer: NAME <EMAIL>
+
{{bc|<nowiki>
 +
prepare(){
 +
  mkdir -p gopath/src/github.com/pkgbuild-example
 +
  ln -rTsf $pkgname-$pkgver gopath/src/github.com/pkgbuild-example/$pkgname
 +
  export GOPATH="$srcdir"/gopath
  
pkgname=go-net
+
  # the dependencies can be fetched here if needed
pkgver=20120515
+
  cd gopath/src/github.com/pkgbuild-example/$pkgname
pkgrel=1
+
   dep ensure
pkgdesc="Extra network libraries for Go (dict, spdy, websocket)"
 
arch=('x86_64' 'i686')
 
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() {
+
build(){
   source /etc/profile.d/go.sh
+
   export GOPATH="$srcdir"/gopath
   GOPATH="$GOPATH:$srcdir" go test -v -x ${_gourl}/...
+
  cd gopath/src/github.com/pkgbuild-example/$pkgname
 +
   go install -v .
 
}
 
}
 +
</nowiki>}}
  
package() {
+
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/...}}
  source /etc/profile.d/go.sh
 
  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.
 
 
 
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
  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
 
 
}
 
}
 +
</nowiki>}}
  
# vim:set ts=2 sw=2 et:</nowiki>}}
+
=== PKGBUILD with GOPATH and dep ===
  
=== For libraries (not executables) released as tarballs ===
+
{{bc|<nowiki>
 
+
pkgname=foo
{{Note|This way is deprecated and won't work properly after the latest update of Go.}}
+
pkgver=0.0.1
 
 
{{bc|<nowiki># Maintainer: NAME <EMAIL>
 
 
 
pkgname=go-PACKAGE NAME
 
pkgver=1.2.3
 
 
pkgrel=1
 
pkgrel=1
pkgdesc='Example library for Go package creation guidelines'
+
pkgdesc='Go PKGBUILD Example'
arch=('i686' 'x86_64')
+
arch=('x86_64')
url='http://bitbucket.org/vlasovskikh/go-aur-example'
+
url='https://example.org/$pkgname'
license=('MIT')
+
license=('GPL')
makedepends=('go' 'libSOMELIBRARY')
+
makedepends=('go-pie' 'dep')
options=('!strip')
+
source=("$url/$pkgname-$pkgver.tar.gz")
source=("https://bitbucket.org/vlasovskikh/go-aur-example/downloads/$pkgname-$pkgver.tar.gz")
+
sha256sums=('1337deadbeef')
md5sums=('810311689a73879a96bc9b29ebbfc347')
 
  
build() {
+
prepare(){
   remoteprefix="bitbucket.org/vlasovskikh/$pkgname"
+
   mkdir -p gopath/src/example.org/foo
  libname="SOMELIBRARY"
+
   ln -rTsf $pkgname-$pkgver gopath/src/example.org/foo
  cd "$srcdir"
+
   export GOPATH="$srcdir"/gopath
  install -d "src/$remoteprefix"
+
   cd gopath/src/example.org/foo
   cp -aT "$pkgname-$pkgver" "src/$remoteprefix"
+
   dep ensure
   GOPATH="$srcdir" go install "$remoteprefix/$libname"
 
   destdir="$pkgdir/usr/lib/go"
 
  install -d "$destdir" "$destdir/src/pkg"
 
  cp -aT pkg "$destdir/pkg"
 
   cp -aT src "$destdir/src/pkg"
 
 
}
 
}
 
# vim:set ts=2 sw=2 et:
 
</nowiki>}}
 
 
Thanks to Andrey Vlasovskikh for this one.
 
 
See also:
 
* https://bitbucket.org/vlasovskikh/go-aur-example
 
 
== Sample PKGBUILDs for Go libraries that also includes executables ==
 
 
=== Using ''go get'' '''(recommended)''' ===
 
 
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| Try removing {{Ic|/...}} if this PKGBUILD should fail.}}
 
 
{{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; 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>}}
+
== Example packages ==
 
 
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
 
}
 
 
 
package() {
 
  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