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

     1  package global
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/metux/go-magicdict/magic"
     7  
     8  	"github.com/metux/go-metabuild/spec/buildconf"
     9  	"github.com/metux/go-metabuild/spec/cache"
    10  	"github.com/metux/go-metabuild/spec/check"
    11  	"github.com/metux/go-metabuild/spec/features"
    12  	"github.com/metux/go-metabuild/spec/generate"
    13  	"github.com/metux/go-metabuild/spec/target"
    14  	"github.com/metux/go-metabuild/util/specobj"
    15  )
    16  
    17  type Global struct {
    18  	specobj.SpecObj
    19  }
    20  
    21  func (g Global) GetChecks() check.CheckList {
    22  	chklist := g.EntrySpecList(KeyConfigureChecks)
    23  	out := make(check.CheckList, 0, len(chklist))
    24  	for _, ent := range chklist {
    25  		chk := check.Check{
    26  			SpecObj:   ent,
    27  			Cache:     g.GetCache(),
    28  			BuildConf: g.BuildConf()}
    29  		chk.Init()
    30  		out = append(out, chk)
    31  	}
    32  	return out
    33  }
    34  
    35  func (g Global) GetGenerates() generate.GenerateList {
    36  	bc := g.BuildConf()
    37  	list := g.EntrySpecList(KeyConfigureGenerate)
    38  	out := make(generate.GenerateList, 0, len(list))
    39  	for _, ent := range list {
    40  		g := generate.Generate{SpecObj: ent, BuildConf: bc}
    41  		g.Init()
    42  		out = append(out, g)
    43  	}
    44  	return out
    45  }
    46  
    47  func (g Global) GetTargetObjects() map[string]target.TargetObject {
    48  	bc := g.BuildConf()
    49  	c := g.GetCache()
    50  	m := make(map[string]target.TargetObject)
    51  	// FIXME: use SubSpecList / EntrySpecMap
    52  	for _, n := range g.EntryKeys(KeyTargetObjects) {
    53  		m[string(n)] = target.MakeTargetObject(g.EntrySpec(KeyTargetObjects.Append(n)), n, bc, c)
    54  	}
    55  	return m
    56  }
    57  
    58  func (g Global) BuildConf() buildconf.BuildConf {
    59  	return buildconf.BuildConf{SpecObj: g.EntrySpec(KeyBuildConf), Features: g.GetFeatureMap()}
    60  }
    61  
    62  func (g Global) GetCache() cache.Cache {
    63  	return cache.NewCache(g.EntrySpec(KeyCache))
    64  }
    65  
    66  func (g Global) GetFeatureMap() features.FeatureMap {
    67  	return features.FeatureMap{g.EntrySpec(KeyFeatures)}
    68  }
    69  
    70  func (g Global) Init() {
    71  	// init buildconf
    72  	bc := g.BuildConf()
    73  	bc.Init()
    74  
    75  	// init checks
    76  	for _, ent := range g.EntrySpecList(KeyConfigureChecks) {
    77  		chk := check.Check{
    78  			SpecObj:   ent,
    79  			Cache:     g.GetCache(),
    80  			BuildConf: g.BuildConf()}
    81  		chk.Init()
    82  	}
    83  
    84  	// init generates
    85  	for _, ent := range g.EntrySpecList(KeyConfigureGenerate) {
    86  		g := generate.Generate{SpecObj: ent, BuildConf: bc}
    87  		g.Init()
    88  	}
    89  
    90  	// init features
    91  	fm := g.GetFeatureMap()
    92  	fm.Init()
    93  }
    94  
    95  func (g Global) PostConfig() {
    96  	for _, ent := range g.GetTargetObjects() {
    97  		ent.LoadTargetDefaults()
    98  	}
    99  }
   100  
   101  func LoadGlobal(fn string, dflt string) (Global, error) {
   102  	md, err := magic.YamlLoad(fn, dflt)
   103  	g := Global{SpecObj: specobj.NewSpecObj(md)}
   104  
   105  	absfn, _ := filepath.Abs(fn)
   106  	g.DefaultPutStr(KeySysConfigPath, fn)
   107  	g.DefaultPutStr(KeySysConfigDir, filepath.Dir(fn))
   108  	g.DefaultPutStr(KeySysConfigAbsDir, filepath.Dir(absfn))
   109  	g.DefaultPutStr(KeySysConfigAbsPath, absfn)
   110  	g.DefaultPutStr(KeySysConfigBase, filepath.Base(fn))
   111  
   112  	absdflt, _ := filepath.Abs(dflt)
   113  	g.DefaultPutStr(KeySysSettingsPath, dflt)
   114  	g.DefaultPutStr(KeySysSettingsDir, filepath.Dir(dflt))
   115  	g.DefaultPutStr(KeySysSettingsAbsDir, filepath.Dir(absdflt))
   116  	g.DefaultPutStr(KeySysSettingsAbsPath, absdflt)
   117  	g.DefaultPutStr(KeySysSettingsBase, filepath.Base(dflt))
   118  
   119  	g.Init()
   120  
   121  	return g, err
   122  }