From df7db445e951ff7fdfd1f1e09830d60df16baeeb Mon Sep 17 00:00:00 2001
From: Kirill Smelkov <kirr@nexedi.com>
Date: Mon, 26 Mar 2018 11:52:58 +0200
Subject: [PATCH] golang: Hook-in pkg-config for Cgo support

The most convenient way to discover CFLAGS/LDFLAGS when building Cgo bits is
via pkg-config:

    https://golang.org/cmd/cgo/

For this to work let's add pkg-config to be available there in gowork out of
the box, and provide users with a way to specify which C packages they want to
be there on gowork's $PKG_CONFIG_PATH, e.g. this way:

    [gowork]
    cpkgpath =
        ${sqlite3:location}/lib/pkgconfig
        ${zlib:location}/lib/pkgconfig

In buildout sources cpkgpath is specified as multiline - not one line with ":"
delimiters - with the idea that it should be easy to amend gowork.cpkgpath with "+="
in different places.
---
 component/golang/buildout.cfg | 16 ++++++++++++++--
 component/golang/goenv.sh.in  |  7 +++++--
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/component/golang/buildout.cfg b/component/golang/buildout.cfg
index 204841ea5..129fa6ed4 100644
--- a/component/golang/buildout.cfg
+++ b/component/golang/buildout.cfg
@@ -3,6 +3,7 @@
 extends =
   ../findutils/buildout.cfg
   ../git/buildout.cfg
+  ../pkgconfig/buildout.cfg
 
 parts = gowork
 
@@ -64,7 +65,15 @@ environment-extra =
 #       github.com/pkg/profile          \
 #       golang.org/x/perf/cmd/benchstat
 #
-# it is possible to specify Go build flags used for compilation e.g. this way:
+# 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
@@ -80,6 +89,9 @@ golang  = ${golang19:location}
 # no special build flags by default
 buildflags =
 
+# empty pkg-config path by default
+cpkgpath =
+
 # everything is done by dependent parts
 recipe  = plone.recipe.command
 command = :
@@ -93,7 +105,7 @@ recipe	= slapos.recipe.template
 url     = ${:_profile_base_location_}/goenv.sh.in
 output	= ${gowork:directory}/env.sh
 depends = ${gowork.mkdir:recipe}
-md5sum	= a9a265135931b3da53f4392870748264
+md5sum	= 7a067a3974c446c3eaa0e82818ba1adb
 
 [gowork.mkdir]
 # NOTE do not use slapos.cookbook:mkdirectory here - if anything in software (not instance)
diff --git a/component/golang/goenv.sh.in b/component/golang/goenv.sh.in
index 0a409372d..4c0e98b4b 100644
--- a/component/golang/goenv.sh.in
+++ b/component/golang/goenv.sh.in
@@ -3,13 +3,16 @@
 
 # ---- 8< ---- (buildout substitution here)
 # PATH so that go & friends work out of the box
-export PATH=${gowork:golang}/bin:${git:location}/bin:${buildout:bin-directory}:$PATH
+export PATH=${gowork:golang}/bin:${git:location}/bin:${pkgconfig:location}/bin:${buildout:bin-directory}:$PATH
 X=${gowork:directory}
+
+export PKG_CONFIG_PATH=$(echo -n "${gowork:cpkgpath}" |tr '\n' ':'):$PKG_CONFIG_PATH
 # ---- 8< ----
 
 export GOPATH=$X:$GOPATH
 export PATH=$X/bin:$PATH
 export PS1="(`basename $X`) $PS1"
 
-# strip trailing : from $GOPATH
+# strip trailing : from $GOPATH, $PKG_CONFIG_PATH
 GOPATH=$${GOPATH%:}
+PKG_CONFIG_PATH=$${PKG_CONFIG_PATH%:}
-- 
2.30.9