github.com/v2fly/tools@v0.100.0/go/internal/cgo/cgo_pkgconfig.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cgo
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"go/build"
    11  	exec "golang.org/x/sys/execabs"
    12  	"strings"
    13  )
    14  
    15  // pkgConfig runs pkg-config with the specified arguments and returns the flags it prints.
    16  func pkgConfig(mode string, pkgs []string) (flags []string, err error) {
    17  	cmd := exec.Command("pkg-config", append([]string{mode}, pkgs...)...)
    18  	out, err := cmd.CombinedOutput()
    19  	if err != nil {
    20  		s := fmt.Sprintf("%s failed: %v", strings.Join(cmd.Args, " "), err)
    21  		if len(out) > 0 {
    22  			s = fmt.Sprintf("%s: %s", s, out)
    23  		}
    24  		return nil, errors.New(s)
    25  	}
    26  	if len(out) > 0 {
    27  		flags = strings.Fields(string(out))
    28  	}
    29  	return
    30  }
    31  
    32  // pkgConfigFlags calls pkg-config if needed and returns the cflags
    33  // needed to build the package.
    34  func pkgConfigFlags(p *build.Package) (cflags []string, err error) {
    35  	if len(p.CgoPkgConfig) == 0 {
    36  		return nil, nil
    37  	}
    38  	return pkgConfig("--cflags", p.CgoPkgConfig)
    39  }