You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/10/24 16:40:59 UTC

[mynewt-newt] 02/03: mfg: Support the "extra_manifest" field per target

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit 06c440f87674d0b86b097c19874045a5d2c3d8d2
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Oct 18 17:11:43 2019 -0700

    mfg: Support the "extra_manifest" field per target
    
    This new field allows an mfgimage definition to specify an
    "extra_manifest" map for each embedded target.  This map is passed
    through unchanged to the resulting mfg manifest JSON file with the name
    "extra".  This field is useful if the user needs to convey additional
    information to some system that knows how to parse a manufacturing
    manifest file.
    
    For example, the following `mfg.yml` snippet:
    
        mfg.targets:
            -
                name: 'targets/blinky_dev'
                area: FLASH_AREA_IMAGE_0
                offset: 0
                extra_manifest:
                    map:
                        int1: 5
                        int2: 100
                        str: "abc"
                    sequence:
                        - 8
                        - 9
                        - 10
    
    yields the following `manifest.json` snippet:
    
        "targets": [
          {
            "name": "targets/blinky_dev",
            "offset": 49152,
            "image_path": "targets/0/image.img",
            "hex_path": "targets/0/image.hex",
            "manifest_path": "targets/0/manifest.json",
            "extra": {
              "map": {
                "int1": 5,
                "int2": 100,
                "str": "abc"
              },
              "sequence": [
                8,
                9,
                10
              ]
            }
          }
        ],
---
 newt/mfg/build.go  | 22 ++++++++++++----------
 newt/mfg/decode.go | 48 +++++++++++++++++++++++++++++++++++++++++++++---
 newt/mfg/emit.go   | 15 +++++++++------
 3 files changed, 66 insertions(+), 19 deletions(-)

diff --git a/newt/mfg/build.go b/newt/mfg/build.go
index df09865..5142f1f 100644
--- a/newt/mfg/build.go
+++ b/newt/mfg/build.go
@@ -42,11 +42,12 @@ import (
 )
 
 type MfgBuildTarget struct {
-	Target  *target.Target
-	Area    flash.FlashArea
-	Offset  int
-	IsBoot  bool
-	BinPath string
+	Target        *target.Target
+	Area          flash.FlashArea
+	Offset        int
+	IsBoot        bool
+	BinPath       string
+	ExtraManifest map[string]interface{}
 }
 
 type MfgBuildRaw struct {
@@ -222,11 +223,12 @@ func newMfgBuildTarget(dt DecodedTarget,
 	isBoot := parse.ValueIsTrue(man.Syscfg["BOOT_LOADER"])
 
 	return MfgBuildTarget{
-		Target:  t,
-		Area:    area,
-		Offset:  dt.Offset,
-		IsBoot:  isBoot,
-		BinPath: targetSrcBinPath(t, isBoot),
+		Target:        t,
+		Area:          area,
+		Offset:        dt.Offset,
+		IsBoot:        isBoot,
+		BinPath:       targetSrcBinPath(t, isBoot),
+		ExtraManifest: dt.ExtraManifest,
 	}, nil
 }
 
diff --git a/newt/mfg/decode.go b/newt/mfg/decode.go
index 802e88d..d204274 100644
--- a/newt/mfg/decode.go
+++ b/newt/mfg/decode.go
@@ -33,9 +33,10 @@ import (
 const OFFSET_END = -1
 
 type DecodedTarget struct {
-	Name   string
-	Area   string
-	Offset int
+	Name          string
+	Area          string
+	Offset        int
+	ExtraManifest map[string]interface{}
 }
 
 type DecodedRaw struct {
@@ -110,6 +111,40 @@ func decodeBoolDflt(kv map[string]interface{}, key string,
 	}
 }
 
+func decodeExtra(kv map[string]interface{},
+	key string) (map[string]interface{}, error) {
+
+	extra := kv[key]
+	if extra == nil {
+		return nil, nil
+	}
+
+	if _, ok := extra.(map[interface{}]interface{}); !ok {
+		return nil, util.FmtNewtError(
+			"invalid \"%s\" field: must be a map", key)
+	}
+
+	// If `extra` contains any maps, they were unmarshalled as
+	// `map[interface{}]interface{}`.  These need to be converted to
+	// `map[string]interface{}` to be JSON compatible.
+	var fixMap func(m map[string]interface{})
+	fixMap = func(m map[string]interface{}) {
+		for k, v := range m {
+			if _, ok := v.(map[interface{}]interface{}); ok {
+				sm := cast.ToStringMap(v)
+				fixMap(sm)
+
+				m[k] = sm
+			}
+		}
+	}
+
+	extraMap := cast.ToStringMap(extra)
+	fixMap(extraMap)
+
+	return extraMap, nil
+}
+
 func decodeTarget(yamlTarget interface{}) (DecodedTarget, error) {
 	dt := DecodedTarget{}
 
@@ -146,6 +181,13 @@ func decodeTarget(yamlTarget interface{}) (DecodedTarget, error) {
 	}
 	dt.Offset = offsetInt
 
+	extra, err := decodeExtra(kv, "extra_manifest")
+	if err != nil {
+		return dt, util.FmtNewtError(
+			"in target entry %s: %s", dt.Name, err.Error())
+	}
+	dt.ExtraManifest = extra
+
 	return dt, nil
 }
 
diff --git a/newt/mfg/emit.go b/newt/mfg/emit.go
index ed45ee3..0d84caa 100644
--- a/newt/mfg/emit.go
+++ b/newt/mfg/emit.go
@@ -50,12 +50,13 @@ type CpEntry struct {
 }
 
 type MfgEmitTarget struct {
-	Name         string
-	Offset       int
-	IsBoot       bool
-	BinPath      string
-	ElfPath      string
-	ManifestPath string
+	Name          string
+	Offset        int
+	IsBoot        bool
+	BinPath       string
+	ElfPath       string
+	ManifestPath  string
+	ExtraManifest map[string]interface{}
 }
 
 type MfgEmitRaw struct {
@@ -121,6 +122,7 @@ func newMfgEmitTarget(bt MfgBuildTarget) (MfgEmitTarget, error) {
 		ElfPath: targetSrcElfPath(bt.Target),
 		ManifestPath: builder.ManifestPath(bt.Target.Name(),
 			builder.BUILD_NAME_APP, bt.Target.App().Name()),
+		ExtraManifest: bt.ExtraManifest,
 	}, nil
 }
 
@@ -367,6 +369,7 @@ func (me *MfgEmitter) emitManifest() ([]byte, error) {
 			Name:         t.Name,
 			ManifestPath: MfgTargetManifestPath(i),
 			Offset:       t.Offset,
+			Extra:        t.ExtraManifest,
 		}
 
 		if t.IsBoot {