github.com/metux/go-metabuild@v0.0.0-20240118143255-d9ed5ab697f9/engine/autoconf/probe/c_base.go (about)

     1  package probe
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/metux/go-metabuild/spec/check"
     8  	"github.com/metux/go-metabuild/util"
     9  	"github.com/metux/go-metabuild/util/compiler"
    10  )
    11  
    12  type ProbeCBase struct {
    13  	ProbeBase
    14  }
    15  
    16  func (p ProbeCBase) RunCheckCProg(cs util.CSource) error {
    17  	// check if host/target
    18  
    19  	src := p.Check.TestProgFile("c")
    20  	out := p.Check.TestProgFile("exe")
    21  
    22  	defer os.Remove(src)
    23  	defer os.Remove(out)
    24  
    25  	if err := cs.Write(src); err != nil {
    26  		return err
    27  	}
    28  
    29  	cc := compiler.NewCCompiler(p.Check.BuildConf.CompilerInfo(p.Check.ForBuild(), compiler.LangC), p.Check.TempDir())
    30  
    31  	args := compiler.CompilerArg{
    32  		Sources:    []string{src},
    33  		PkgImports: []compiler.PkgConfigInfo{},
    34  		Defines:    p.Check.EntryStrList("c/defines"),
    35  		Output:     out,
    36  	}
    37  
    38  	if err := cc.CompileExecutable(args); err != nil {
    39  		return fmt.Errorf("%w: %s", ErrCompileFailed, err)
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  func (p ProbeCBase) Headers() []string {
    46  	return p.Check.EntryStrList(check.KeyCHeader)
    47  }
    48  
    49  func (p ProbeCBase) Functions() []string {
    50  	return p.Check.EntryStrList(check.KeyCFunction)
    51  }
    52  
    53  func (p ProbeCBase) CSource() util.CSource {
    54  	return util.CSource{Includes: p.Headers()}
    55  }
    56  
    57  func (p ProbeCBase) Types() []string {
    58  	return p.Check.EntryStrList(check.KeyCType)
    59  }
    60  
    61  func MakeProbeCBase(chk Check) ProbeCBase {
    62  	return ProbeCBase{MakeProbeBase(chk)}
    63  }