Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/def_task [created] 744a4c152


newt; allow multiple init functions per-package.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/744a4c15
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/744a4c15
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/744a4c15

Branch: refs/heads/def_task
Commit: 744a4c152571263fb54d5242e68766222dce9ac7
Parents: 85da72b
Author: Marko Kiiskila <[email protected]>
Authored: Fri Jan 20 17:49:43 2017 -0800
Committer: Marko Kiiskila <[email protected]>
Committed: Fri Jan 20 17:49:43 2017 -0800

----------------------------------------------------------------------
 newt/pkg/localpackage.go | 30 ++++++++++++++++--------
 newt/sysinit/sysinit.go  | 53 +++++++++++++++++++++----------------------
 2 files changed, 46 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/744a4c15/newt/pkg/localpackage.go
----------------------------------------------------------------------
diff --git a/newt/pkg/localpackage.go b/newt/pkg/localpackage.go
index 2f0ad0b..b34bd59 100644
--- a/newt/pkg/localpackage.go
+++ b/newt/pkg/localpackage.go
@@ -27,6 +27,7 @@ import (
        "os"
        "path/filepath"
        "sort"
+       "strconv"
        "strings"
 
        log "github.com/Sirupsen/logrus"
@@ -65,8 +66,7 @@ type LocalPackage struct {
 
        // Package init function name and stage.  These are used to generate the
        // sysinit C file.
-       initFnName string
-       initStage  int
+       init map[string]int
 
        // Extra package-specific settings that don't come from syscfg.  For
        // example, SELFTEST gets set when the newt test command is used.
@@ -90,6 +90,7 @@ func NewLocalPackage(r *repo.Repo, pkgDir string) 
*LocalPackage {
                repo:             r,
                basePath:         filepath.ToSlash(filepath.Clean(pkgDir)),
                deps:             map[string]*Dependency{},
+               init:             map[string]int{},
                injectedSettings: map[string]string{},
        }
        return pkg
@@ -353,8 +354,21 @@ func (pkg *LocalPackage) Load() error {
                }
        }
 
-       pkg.initFnName = pkg.PkgV.GetString("pkg.init_function")
-       pkg.initStage = pkg.PkgV.GetInt("pkg.init_stage")
+       init := pkg.PkgV.GetStringMapString("pkg.init")
+       for name, stageStr := range init {
+               stage, err := strconv.ParseInt(stageStr, 10, 64)
+               if err != nil {
+                       return util.NewNewtError(fmt.Sprintf("Parsing pkg %s 
config: %s",
+                                                            pkg.FullName(), 
err.Error()))
+               }
+               pkg.init[name] = int(stage)
+       }
+       initFnName := pkg.PkgV.GetString("pkg.init_function")
+       initStage := pkg.PkgV.GetInt("pkg.init_stage")
+
+       if initFnName != "" {
+               pkg.init[initFnName] = initStage
+       }
 
        // Read the package description from the file
        pkg.desc, err = pkg.readDesc(pkg.PkgV)
@@ -375,12 +389,8 @@ func (pkg *LocalPackage) Load() error {
        return nil
 }
 
-func (pkg *LocalPackage) InitStage() int {
-       return pkg.initStage
-}
-
-func (pkg *LocalPackage) InitFnName() string {
-       return pkg.initFnName
+func (pkg *LocalPackage) Init() map[string]int {
+       return pkg.init
 }
 
 func (pkg *LocalPackage) InjectedSettings() map[string]string {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/744a4c15/newt/sysinit/sysinit.go
----------------------------------------------------------------------
diff --git a/newt/sysinit/sysinit.go b/newt/sysinit/sysinit.go
index 9835a3a..ddcc68b 100644
--- a/newt/sysinit/sysinit.go
+++ b/newt/sysinit/sysinit.go
@@ -35,12 +35,24 @@ import (
        "mynewt.apache.org/newt/util"
 )
 
-func buildStageMap(pkgs []*pkg.LocalPackage) map[int][]*pkg.LocalPackage {
-       sm := map[int][]*pkg.LocalPackage{}
+type initFunc struct {
+       stage int
+       name  string
+       pkg   *pkg.LocalPackage
+}
+
+func buildStageMap(pkgs []*pkg.LocalPackage) map[int][]*initFunc {
+       sm := map[int][]*initFunc{}
 
        for _, p := range pkgs {
-               stage := p.InitStage()
-               sm[stage] = append(sm[stage], p)
+               for name, stage := range p.Init() {
+                       initFunc := &initFunc{
+                               stage: stage,
+                               name: name,
+                               pkg: p,
+                       }
+                       sm[stage] = append(sm[stage], initFunc)
+               }
        }
 
        return sm
@@ -48,38 +60,26 @@ func buildStageMap(pkgs []*pkg.LocalPackage) 
map[int][]*pkg.LocalPackage {
 
 func writePrototypes(pkgs []*pkg.LocalPackage, w io.Writer) {
        sorted := pkg.SortLclPkgs(pkgs)
-       fmt.Fprintf(w, "void os_init(void);\n")
        for _, p := range sorted {
-               fmt.Fprintf(w, "void %s(void);\n", p.InitFnName())
+               init := p.Init()
+               for name, _ := range init {
+                       fmt.Fprintf(w, "void %s(void);\n", name);
+               }
        }
 }
 
-func writeStage(stage int, pkgs []*pkg.LocalPackage, w io.Writer) {
-       sorted := pkg.SortLclPkgs(pkgs)
-
+func writeStage(stage int, initFuncs []*initFunc, w io.Writer) {
        fmt.Fprintf(w, "    /*** Stage %d */\n", stage)
-       for i, p := range sorted {
-               fmt.Fprintf(w, "    /* %d.%d: %s */\n", stage, i, p.Name())
-               fmt.Fprintf(w, "    %s();\n", p.InitFnName())
+       for i, initFunc := range initFuncs {
+               fmt.Fprintf(w, "    /* %d.%d: %s */\n", stage, i, 
initFunc.pkg.Name())
+               fmt.Fprintf(w, "    %s();\n", initFunc.name)
        }
 }
 
-func onlyPkgsWithInit(pkgs []*pkg.LocalPackage) []*pkg.LocalPackage {
-       good := make([]*pkg.LocalPackage, 0, len(pkgs))
-       for _, p := range pkgs {
-               if p.InitFnName() != "" {
-                       good = append(good, p)
-               }
-       }
-
-       return good
-}
-
 func write(pkgs []*pkg.LocalPackage, isLoader bool,
        w io.Writer) {
 
-       goodPkgs := onlyPkgsWithInit(pkgs)
-       stageMap := buildStageMap(goodPkgs)
+       stageMap := buildStageMap(pkgs)
 
        i := 0
        stages := make([]int, len(stageMap))
@@ -97,7 +97,7 @@ func write(pkgs []*pkg.LocalPackage, isLoader bool,
                fmt.Fprintf(w, "#if !SPLIT_LOADER\n\n")
        }
 
-       writePrototypes(goodPkgs, w)
+       writePrototypes(pkgs, w)
 
        var fnName string
        if isLoader {
@@ -108,7 +108,6 @@ func write(pkgs []*pkg.LocalPackage, isLoader bool,
 
        fmt.Fprintf(w, "\n")
        fmt.Fprintf(w, "void\n%s(void)\n{\n", fnName)
-       fmt.Fprintf(w, "    os_init();\n")
 
        for _, s := range stages {
                fmt.Fprintf(w, "\n")

Reply via email to