github.com/metux/go-metabuild@v0.0.0-20240118143255-d9ed5ab697f9/spec/buildconf/pkgconfig.go (about)

     1  package buildconf
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/metux/go-metabuild/util"
     8  	"github.com/metux/go-metabuild/util/compiler"
     9  )
    10  
    11  // suffixes for pkg-config
    12  const (
    13  	KeyPkgSpec          = Key("pkgspec")
    14  	KeyPkgName          = Key("pkg")
    15  	KeyPkgVersion       = Key("version")
    16  	KeyPkgCflagsShared  = Key("shared/cflags")
    17  	KeyPkgCflagsStatic  = Key("static/cflags")
    18  	KeyPkgLdflagsShared = Key("shared/ldflags")
    19  	KeyPkgLdflagsStatic = Key("static/ldflags")
    20  )
    21  
    22  // subtree of pkgconfig subtree underneath the build/host subtree
    23  const (
    24  	KeyPkgSub = Key("pkg")
    25  )
    26  
    27  func (bc BuildConf) PkgConfigSub(build bool, pkg string) SpecObj {
    28  	return bc.SubForBuild(build, KeyPkgSub.AppendStr(pkg))
    29  }
    30  
    31  func (bc BuildConf) SetPkgConfig(build bool, id string, pi compiler.PkgConfigInfo) error {
    32  	sub := bc.PkgConfigSub(build, id)
    33  	sub.EntryPutStr(KeyPkgSpec, pi.PkgSpec)
    34  	sub.EntryPutStr(KeyPkgName, pi.Name)
    35  	sub.EntryPutStr(KeyPkgVersion, pi.Version)
    36  	sub.EntryPutStrList(KeyPkgCflagsShared, pi.SharedCflags)
    37  	sub.EntryPutStrList(KeyPkgCflagsStatic, pi.StaticCflags)
    38  	sub.EntryPutStrList(KeyPkgLdflagsShared, pi.SharedLdflags)
    39  	sub.EntryPutStrList(KeyPkgLdflagsStatic, pi.StaticLdflags)
    40  	sub.EntryPutStrMap("variables", pi.Variables)
    41  
    42  	// write out to config
    43  	bc.ConfigBool("HAVE_PKG_"+id, pi.Name != "")
    44  	pkg_prefix := fmt.Sprintf("PKG_%s_%s_", util.ValIf(build, "BUILD", "HOST"), strings.ToUpper(id))
    45  	bc.ConfigStrList(pkg_prefix+"STATIC_CFLAGS", pi.StaticCflags)
    46  	bc.ConfigStrList(pkg_prefix+"STATIC_LDFLAGS", pi.StaticLdflags)
    47  	bc.ConfigStr(pkg_prefix+"VERSION", pi.Version)
    48  	bc.ConfigStrList(pkg_prefix+"SHARED_CFLAGS", pi.SharedCflags)
    49  	bc.ConfigStrList(pkg_prefix+"SHARED_LDFLAGS", pi.SharedLdflags)
    50  
    51  	return nil
    52  }
    53  
    54  func PkgconfLoad(sub SpecObj, id string) compiler.PkgConfigInfo {
    55  	return compiler.PkgConfigInfo{
    56  		Id:            id,
    57  		PkgSpec:       sub.EntryStr(KeyPkgSpec),
    58  		Name:          sub.EntryStr(KeyPkgName),
    59  		Version:       sub.EntryStr(KeyPkgVersion),
    60  		SharedCflags:  sub.EntryStrList(KeyPkgCflagsShared),
    61  		StaticCflags:  sub.EntryStrList(KeyPkgCflagsStatic),
    62  		SharedLdflags: sub.EntryStrList(KeyPkgLdflagsShared),
    63  		StaticLdflags: sub.EntryStrList(KeyPkgLdflagsStatic),
    64  	}
    65  }
    66  
    67  func (bc BuildConf) PkgConfig(build bool, id string) compiler.PkgConfigInfo {
    68  	pkg := PkgconfLoad(bc.PkgConfigSub(build, id), id)
    69  	if !pkg.Valid() {
    70  		// try to get it from target platform
    71  		if !build {
    72  			pkg = PkgconfLoad(bc.EntrySpec(KeyTargetPlatform.Append(KeyPkgSub).AppendStr(id)), id)
    73  		} // FIXME: also should have @host-platform ?
    74  	}
    75  	return pkg
    76  }