Commit 5dcb31b2 authored by Boris Nagaev's avatar Boris Nagaev Committed by Ian Lance Taylor

cmd/dist, cmd/go: add environment variable override for pkg-config

Allow overriding default name of `pkg-config` tool via environment
variable PKG_CONFIG (same as used by autoconf pkg.m4 macros). This
facilitates easy cross-compilation of cgo code.

Original patch against Go <= 1.4 was written by
xnox_canonical <dimitri.ledkov@canonical.com> in 2014.
Source: https://codereview.appspot.com/104960043/

Fixes #16253

Change-Id: I31c33ffc3ecbff65da31421e6188d092ab4fe7e4
Reviewed-on: https://go-review.googlesource.com/29991Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 6c5e377d
...@@ -53,6 +53,8 @@ For example: ...@@ -53,6 +53,8 @@ For example:
// #include <png.h> // #include <png.h>
import "C" import "C"
The default pkg-config tool may be changed by setting the PKG_CONFIG environment variable.
When building, the CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_FFLAGS and When building, the CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_FFLAGS and
CGO_LDFLAGS environment variables are added to the flags derived from CGO_LDFLAGS environment variables are added to the flags derived from
these directives. Package-specific flags should be set using the these directives. Package-specific flags should be set using the
......
...@@ -21,30 +21,31 @@ import ( ...@@ -21,30 +21,31 @@ import (
// The usual variables. // The usual variables.
var ( var (
goarch string goarch string
gobin string gobin string
gohostarch string gohostarch string
gohostos string gohostos string
goos string goos string
goarm string goarm string
go386 string go386 string
goroot string goroot string
goroot_final string goroot_final string
goextlinkenabled string goextlinkenabled string
gogcflags string // For running built compiler gogcflags string // For running built compiler
workdir string workdir string
tooldir string tooldir string
oldgoos string oldgoos string
oldgoarch string oldgoarch string
slash string slash string
exe string exe string
defaultcc string defaultcc string
defaultcflags string defaultcflags string
defaultldflags string defaultldflags string
defaultcxxtarget string defaultcxxtarget string
defaultcctarget string defaultcctarget string
rebuildall bool defaultpkgconfigtarget string
defaultclang bool rebuildall bool
defaultclang bool
vflag int // verbosity vflag int // verbosity
) )
...@@ -208,6 +209,12 @@ func xinit() { ...@@ -208,6 +209,12 @@ func xinit() {
} }
defaultcxxtarget = b defaultcxxtarget = b
b = os.Getenv("PKG_CONFIG")
if b == "" {
b = "pkg-config"
}
defaultpkgconfigtarget = b
// For tools being invoked but also for os.ExpandEnv. // For tools being invoked but also for os.ExpandEnv.
os.Setenv("GO386", go386) os.Setenv("GO386", go386)
os.Setenv("GOARCH", goarch) os.Setenv("GOARCH", goarch)
......
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
// package main // package main
// const defaultCC = <defaultcc> // const defaultCC = <defaultcc>
// const defaultCXX = <defaultcxx> // const defaultCXX = <defaultcxx>
// const defaultPkgConfig = <defaultpkgconfig>
// //
// It is invoked to write cmd/go/zdefaultcc.go // It is invoked to write cmd/go/zdefaultcc.go
// but we also write cmd/cgo/zdefaultcc.go // but we also write cmd/cgo/zdefaultcc.go
...@@ -29,8 +30,9 @@ func mkzdefaultcc(dir, file string) { ...@@ -29,8 +30,9 @@ func mkzdefaultcc(dir, file string) {
"package main\n"+ "package main\n"+
"\n"+ "\n"+
"const defaultCC = `%s`\n"+ "const defaultCC = `%s`\n"+
"const defaultCXX = `%s`\n", "const defaultCXX = `%s`\n"+
defaultcctarget, defaultcxxtarget) "const defaultPkgConfig = `%s`\n",
defaultcctarget, defaultcxxtarget, defaultpkgconfigtarget)
writefile(out, file, writeSkipSame) writefile(out, file, writeSkipSame)
......
...@@ -1104,6 +1104,8 @@ ...@@ -1104,6 +1104,8 @@
// Flags that cgo will pass to the compiler when linking. // Flags that cgo will pass to the compiler when linking.
// CXX // CXX
// The command to use to compile C++ code. // The command to use to compile C++ code.
// PKG_CONFIG
// Path to pkg-config tool.
// //
// Architecture-specific environment variables: // Architecture-specific environment variables:
// //
......
...@@ -1624,13 +1624,19 @@ func (b *builder) build(a *action) (err error) { ...@@ -1624,13 +1624,19 @@ func (b *builder) build(a *action) (err error) {
return nil return nil
} }
// pkgconfigCmd returns a pkg-config binary name
// defaultPkgConfig is defined in zdefaultcc.go, written by cmd/dist.
func (b *builder) pkgconfigCmd() string {
return envList("PKG_CONFIG", defaultPkgConfig)[0]
}
// Calls pkg-config if needed and returns the cflags/ldflags needed to build the package. // Calls pkg-config if needed and returns the cflags/ldflags needed to build the package.
func (b *builder) getPkgConfigFlags(p *Package) (cflags, ldflags []string, err error) { func (b *builder) getPkgConfigFlags(p *Package) (cflags, ldflags []string, err error) {
if pkgs := p.CgoPkgConfig; len(pkgs) > 0 { if pkgs := p.CgoPkgConfig; len(pkgs) > 0 {
var out []byte var out []byte
out, err = b.runOut(p.Dir, p.ImportPath, nil, "pkg-config", "--cflags", pkgs) out, err = b.runOut(p.Dir, p.ImportPath, nil, b.pkgconfigCmd(), "--cflags", pkgs)
if err != nil { if err != nil {
b.showOutput(p.Dir, "pkg-config --cflags "+strings.Join(pkgs, " "), string(out)) b.showOutput(p.Dir, b.pkgconfigCmd()+" --cflags "+strings.Join(pkgs, " "), string(out))
b.print(err.Error() + "\n") b.print(err.Error() + "\n")
err = errPrintedOutput err = errPrintedOutput
return return
...@@ -1638,9 +1644,9 @@ func (b *builder) getPkgConfigFlags(p *Package) (cflags, ldflags []string, err e ...@@ -1638,9 +1644,9 @@ func (b *builder) getPkgConfigFlags(p *Package) (cflags, ldflags []string, err e
if len(out) > 0 { if len(out) > 0 {
cflags = strings.Fields(string(out)) cflags = strings.Fields(string(out))
} }
out, err = b.runOut(p.Dir, p.ImportPath, nil, "pkg-config", "--libs", pkgs) out, err = b.runOut(p.Dir, p.ImportPath, nil, b.pkgconfigCmd(), "--libs", pkgs)
if err != nil { if err != nil {
b.showOutput(p.Dir, "pkg-config --libs "+strings.Join(pkgs, " "), string(out)) b.showOutput(p.Dir, b.pkgconfigCmd()+" --libs "+strings.Join(pkgs, " "), string(out))
b.print(err.Error() + "\n") b.print(err.Error() + "\n")
err = errPrintedOutput err = errPrintedOutput
return return
......
...@@ -465,6 +465,8 @@ Environment variables for use with cgo: ...@@ -465,6 +465,8 @@ Environment variables for use with cgo:
Flags that cgo will pass to the compiler when linking. Flags that cgo will pass to the compiler when linking.
CXX CXX
The command to use to compile C++ code. The command to use to compile C++ code.
PKG_CONFIG
Path to pkg-config tool.
Architecture-specific environment variables: Architecture-specific environment variables:
......
...@@ -47,6 +47,8 @@ ...@@ -47,6 +47,8 @@
# FC: Command line to run to compile Fortran code for GOARCH. # FC: Command line to run to compile Fortran code for GOARCH.
# This is used by cgo. Default is "gfortran". # This is used by cgo. Default is "gfortran".
# #
# PKG_CONFIG: Path to pkg-config tool. Default is "pkg-config".
#
# GO_DISTFLAGS: extra flags to provide to "dist bootstrap". # GO_DISTFLAGS: extra flags to provide to "dist bootstrap".
set -e set -e
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment