# Go language - https://golang.org/
[buildout]
extends =
  ../findutils/buildout.cfg
  ../gcc/buildout.cfg
  ../git/buildout.cfg
  ../pkgconfig/buildout.cfg

parts = gowork go

# ---- Go builds itself ----

[golang-common]
recipe = slapos.recipe.cmmi
shared = true
configure-command = :
location = @@LOCATION@@
make-binary =
make-targets= cd src && ./all.bash && cp -alf .. ${:location}
# some testdata files have an issue with slapos.extension.strip.
post-install = ${findutils:location}/bin/find ${:location}/src -type d -name testdata -exec rm -rf {} \; || true
environment =
  GOROOT_FINAL=${:location}
  ${:environment-extra}

[golang14]
<= golang-common
# https://golang.org/doc/install/source#bootstrapFromSource
url = https://dl.google.com/go/go1.4-bootstrap-20171003.tar.gz
md5sum = dbf727a4b0e365bf88d97cbfde590016
environment-extra =
make-targets= cd src && ./make.bash && cp -alf .. ${:location}


[golang1.12]
<= golang-common
url = https://golang.org/dl/go1.12.17.src.tar.gz
md5sum = 6b607fc795391dc609ffd79ebf41f080

# go1.12 needs go1.4 to bootstrap
environment-extra =
  GOROOT_BOOTSTRAP=${golang14:location}

[golang1.13]
<= golang-common
url = https://golang.org/dl/go1.13.15.src.tar.gz
md5sum = 4f4af14d88352a62761a9dcedf863ac0

# go1.13 needs go1.4 to bootstrap
environment-extra =
  GOROOT_BOOTSTRAP=${golang14:location}

[golang1.14]
<= golang-common
url = https://golang.org/dl/go1.14.15.src.tar.gz
md5sum = 1065b705a0d330136084f4e1628baf82

# go1.14 needs go1.4 to bootstrap
environment-extra =
  GOROOT_BOOTSTRAP=${golang14:location}

[golang1.15]
<= golang-common
url = https://golang.org/dl/go1.15.8.src.tar.gz
md5sum = 7e4b8580bfe25ffc51501b1dc3d2d8ce

# go1.15 needs go1.4 to bootstrap
environment-extra =
  GOROOT_BOOTSTRAP=${golang14:location}

[golang1.16]
<= golang-common
url = https://golang.org/dl/go1.16.src.tar.gz
md5sum = b767f7e81d886b4208874ea953db2ce3

# go1.16 needs go1.4 to bootstrap
environment-extra =
  GOROOT_BOOTSTRAP=${golang14:location}


# ---- infrastructure to build Go workspaces / projects ----

# gowork is the top-level section that defines Go workspace.
#
# It specifies global settings that are used to build Go programs:
#
# - Go toolchain to use, e.g.
#
#   [gowork]
#   golang = ${golang1.15:location}
#
# - For Cgo support pkg-config is made pre-available by gowork, and users
#   should list paths where to search for pkg-config files, e.g. this way:
#
#   [gowork]
#   cpkgpath =
#       ${sqlite3:location}/lib/pkgconfig
#       ${zlib:location}/lib/pkgconfig
#
# - It is also possible to specify Go build flags used for compilation e.g. this way:
#
#   [gowork]
#   buildflags = -race
#
#
# Users can also add `install` field to [gowork] to request Go programs to be
# automatically installed, for example:
#
#   [gowork]
#   install =
#       lab.nexedi.com/kirr/neo/go/...@v0.0.0-20210103165133-f3effa6c535f
#       golang.org/x/tools/gopls@v0.4.3
#       ${helloweb:location}/go:./...
#
#
# ${go:exe} is standalone executable that runs go in activated gowork environment.
[go]
recipe  = slapos.recipe.template:jinja2
exe     = ${buildout:bin-directory}/go
rendered= ${:exe}
mode    = 755
template= inline:
  #!/bin/sh -e
  . ${gowork:env.sh}
  exec go "$@"

[gowork]
directory = ${gowork.dir:directory}
src	= ${gowork.dir:src}
bin	= ${gowork.dir:bin}
depends = ${gowork.goinstall:recipe}

# go version used for the workspace (possible to override in applications)
golang  = ${golang1.15:location}

# no special build flags by default
buildflags =

# empty pkg-config path by default
cpkgpath =

# by default don't `go install` anything
install =

# everything is done by dependent parts
recipe  = plone.recipe.command
command = :

# env.sh for compiling and running go programs
env.sh  = ${gowork-env.sh:output}

[gowork-env.sh]
recipe	= slapos.recipe.template
url     = ${:_profile_base_location_}/goenv.sh.in
output	= ${gowork.dir:directory}/env.sh
depends = ${gowork.dir:recipe}
md5sum	= 6efdf5d63381c240c7d206d7939a63f7

[gowork.dir]
directory = ${buildout:directory}/go.work
src	= ${:directory}/src
bin	= ${:directory}/bin
# NOTE do not use slapos.cookbook:mkdirectory here - if anything in software (not instance)
# uses slapos.cookbook:* in recipe - slapos.cookbook will get compiled against system
# libxml/libxslt or fail to bootstrap at all if those are not present.
recipe  = plone.recipe.command
command = mkdir -p ${:directory}
update-command = ${:command}
stop-on-error = true

# install go programs
# clients can put program list to install to gowork:install
[gowork.goinstall]
recipe  = plone.recipe.command
command = bash -c ". ${gowork:env.sh} &&
	for x in $(echo -n '${gowork:install}' |tr '\n' ' '); do
		case "\$x" in

		# external module, e.g. golang.org/x/tools/gopls@v0.4.3
		*@*)
			echo GOMOD \$x
			GO111MODULE=on go install ${gowork:buildflags} -v \$x
			;;

		# locally-cloned module source, e.g. <module-src-location>:./...
		*:*)
			echo GOMODSRC \$x
			dir=\"\$${x%%:*}\"
			arg=\"\$${x#*:}\"
			(cd \$dir && GO111MODULE=on go install ${gowork:buildflags} -v \$arg)
			;;

		# non-module
		*)
			echo GOPKG \$x
			GO111MODULE=off go install ${gowork:buildflags} -v \$x
			;;
		esac
	done
	"
update-command = ${:command}
stop-on-error = true


[git-repository]
recipe  = slapos.recipe.build:gitclone
git-executable = ${git:location}/bin/git

# a go package should:
# 1) <= go-git-package
# 2) provide go.importpath
# 3) provide repository (which is not the same as importpath in general case)
#
# the list of go packages for a go workspace state can be automatically
# generated with the help of gowork-snapshot tool.
[go-git-package]
<= git-repository
location = ${gowork.dir:src}/${:go.importpath}