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/02 16:21:28 UTC

[mynewt-newt] branch master updated (a15dea0 -> 04a7530)

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

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


    from a15dea0  Include @<repo> in bin directories and filenames
     new 8f35dc9  Separate env var generation from builder
     new 773d97c  util: interactive shell: error on nonzero exit
     new dd85dc3  stage: Simplify sorting function
     new 9be9346  Run custom commands at build time
     new 04a7530  cmake: print warning if any custom commands

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 go.mod                      |   1 +
 go.sum                      |   2 +
 newt/builder/build.go       |  45 +++++++--
 newt/builder/buildutil.go   | 189 ++++++++++++++++++++++++++++++++++-
 newt/builder/cmake.go       |   8 ++
 newt/builder/extcmd.go      | 235 ++++++++++++++++++++++++++++++++++++++++++++
 newt/builder/load.go        |  92 +++++------------
 newt/builder/paths.go       |  32 ++++++
 newt/builder/targetbuild.go |  42 +++++++-
 newt/extcmd/extcmd.go       |  98 ++++++++++++++++++
 newt/pkg/localpackage.go    |  18 ++++
 newt/resolve/resolve.go     |  29 ++++++
 newt/stage/stage.go         |  80 ++++++---------
 util/util.go                | 130 +++++++++++++++++++++++-
 14 files changed, 868 insertions(+), 133 deletions(-)
 create mode 100644 newt/builder/extcmd.go
 create mode 100644 newt/extcmd/extcmd.go


[mynewt-newt] 01/05: Separate env var generation from builder

Posted by cc...@apache.org.
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 8f35dc970558a7743cada22be28f0888edaf6abe
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Thu Sep 19 10:05:26 2019 -0500

    Separate env var generation from builder
    
    Before commit: a `Builder` object was required to calculate the
    environment variables to pass to external scripts.
    
    This is a nuissance when no `Builder` is available (e.g., when loading
    an mfgimage, or for the upcomimg per-package commands feature).
    
    After commit: Environment variables are divided into three sets (basic,
    settings, slot).  The sets are divided according to the inputs required
    to generate them.  Now the appropriate variables can be calculated
    without a `Builder` object.
---
 newt/builder/buildutil.go | 134 ++++++++++++++++++++++++++++++++++++++++++++--
 newt/builder/load.go      |  89 ++++++++++--------------------
 2 files changed, 159 insertions(+), 64 deletions(-)

diff --git a/newt/builder/buildutil.go b/newt/builder/buildutil.go
index 6f23783..e06bd12 100644
--- a/newt/builder/buildutil.go
+++ b/newt/builder/buildutil.go
@@ -22,24 +22,29 @@ package builder
 import (
 	"bytes"
 	"sort"
+	"strconv"
 	"strings"
 
 	log "github.com/sirupsen/logrus"
 
 	"mynewt.apache.org/newt/newt/parse"
+	"mynewt.apache.org/newt/newt/pkg"
+	"mynewt.apache.org/newt/newt/project"
 	"mynewt.apache.org/newt/newt/resolve"
+	"mynewt.apache.org/newt/util"
 )
 
 func TestTargetName(testPkgName string) string {
 	return strings.Replace(testPkgName, "/", "_", -1)
 }
 
-func (b *Builder) FeatureString() string {
+// FeatureString converts a syscfg map to a string.  The string is a
+// space-separate list of "enabled" settings.
+func FeatureString(settings map[string]string) string {
 	var buffer bytes.Buffer
 
-	settingMap := b.cfg.SettingValues()
-	featureSlice := make([]string, 0, len(settingMap))
-	for k, v := range settingMap {
+	featureSlice := make([]string, 0, len(settings))
+	for k, v := range settings {
 		if parse.ValueIsTrue(v) {
 			featureSlice = append(featureSlice, k)
 		}
@@ -53,6 +58,7 @@ func (b *Builder) FeatureString() string {
 
 		buffer.WriteString(feature)
 	}
+
 	return buffer.String()
 }
 
@@ -126,3 +132,123 @@ func logDepInfo(res *resolve.Resolution) {
 		log.Debugf("%s", RevdepGraphText(rdg))
 	}
 }
+
+// binBasePath calculates the relative path of the "application binary"
+// directory.  Examples:
+//     * bin/targets/my_blinky_sim/app
+//     * bin/targets/splitty-nrf52dk/loader
+func (b *Builder) binBasePath() (string, error) {
+	if b.appPkg == nil {
+		return "", util.NewNewtError("app package not specified")
+	}
+
+	// Convert the binary path from absolute to relative.  This is required for
+	// Windows compatibility.
+	return util.TryRelPath(b.AppBinBasePath()), nil
+}
+
+// BasicEnvVars calculates the basic set of environment variables passed to all
+// external scripts.  `binBase` is the result of calling `binBasePath()`.
+func BasicEnvVars(binBase string, bspPkg *pkg.BspPackage) map[string]string {
+	coreRepo := project.GetProject().FindRepo("apache-mynewt-core")
+	bspPath := bspPkg.BasePath()
+
+	return map[string]string{
+		"CORE_PATH":           coreRepo.Path(),
+		"BSP_PATH":            bspPath,
+		"BIN_BASENAME":        binBase,
+		"BIN_ROOT":            BinRoot(),
+		"MYNEWT_PROJECT_ROOT": ProjectRoot(),
+	}
+}
+
+// SettingsEnvVars calculates the syscfg set of environment variables required
+// by image loading scripts.
+func SettingsEnvVars(settings map[string]string) map[string]string {
+	env := map[string]string{}
+
+	// Add all syscfg settings to the environment with the MYNEWT_VAL_ prefix.
+	for k, v := range settings {
+		env["MYNEWT_VAL_"+k] = v
+	}
+
+	if parse.ValueIsTrue(settings["BOOT_LOADER"]) {
+		env["BOOT_LOADER"] = "1"
+	}
+
+	env["FEATURES"] = FeatureString(settings)
+
+	return env
+}
+
+// SlotEnvVars calculates the image-slot set of environment variables required
+// by image loading scripts.  Pass a negative `imageSlot` value if the target
+// is a boot loader.
+func SlotEnvVars(bspPkg *pkg.BspPackage,
+	imageSlot int) (map[string]string, error) {
+
+	env := map[string]string{}
+
+	var flashTargetArea string
+	if imageSlot < 0 {
+		flashTargetArea = "FLASH_AREA_BOOTLOADER"
+	} else {
+		env["IMAGE_SLOT"] = strconv.Itoa(imageSlot)
+		switch imageSlot {
+		case 0:
+			flashTargetArea = "FLASH_AREA_IMAGE_0"
+		case 1:
+			flashTargetArea = "FLASH_AREA_IMAGE_1"
+		default:
+			return nil, util.FmtNewtError(
+				"invalid image slot: have=%d want=0or1", imageSlot)
+		}
+	}
+
+	tgtArea := bspPkg.FlashMap.Areas[flashTargetArea]
+	if tgtArea.Name == "" {
+		return nil, util.FmtNewtError(
+			"No flash target area %s", flashTargetArea)
+	}
+
+	env["FLASH_OFFSET"] = "0x" + strconv.FormatInt(int64(tgtArea.Offset), 16)
+	env["FLASH_AREA_SIZE"] = "0x" + strconv.FormatInt(int64(tgtArea.Size), 16)
+
+	return env, nil
+}
+
+// EnvVars calculates the full set of environment variables passed to external
+// scripts.
+func (b *Builder) EnvVars(imageSlot int) (map[string]string, error) {
+	bspPkg := b.targetBuilder.bspPkg
+	settings := b.cfg.SettingValues()
+
+	binBasePath, err := b.binBasePath()
+	if err != nil {
+		return nil, err
+	}
+
+	// Calculate all three sets of environment variables: basic, settings, and
+	// slot.  Then merge the three sets into one.
+
+	env := BasicEnvVars(binBasePath, bspPkg)
+	setEnv := SettingsEnvVars(settings)
+
+	if parse.ValueIsTrue(settings["BOOT_LOADER"]) {
+		imageSlot = -1
+	}
+
+	slotEnv, err := SlotEnvVars(bspPkg, imageSlot, settings)
+	if err != nil {
+		return nil, err
+	}
+
+	for k, v := range setEnv {
+		env[k] = v
+	}
+	for k, v := range slotEnv {
+		env[k] = v
+	}
+
+	return env, nil
+}
diff --git a/newt/builder/load.go b/newt/builder/load.go
index 9d76060..940a934 100644
--- a/newt/builder/load.go
+++ b/newt/builder/load.go
@@ -24,11 +24,9 @@ import (
 	"os"
 	"os/signal"
 	"sort"
-	"strconv"
 	"strings"
 	"syscall"
 
-	"mynewt.apache.org/newt/newt/parse"
 	"mynewt.apache.org/newt/newt/pkg"
 	"mynewt.apache.org/newt/newt/project"
 	"mynewt.apache.org/newt/util"
@@ -117,40 +115,36 @@ func RunOptionalCheck(checkScript string, env []string) error {
 	return nil
 }
 
-func Load(binBaseName string, bspPkg *pkg.BspPackage,
+func Load(binBasePath string, bspPkg *pkg.BspPackage,
 	extraEnvSettings map[string]string) error {
 
 	if bspPkg.DownloadScript == "" {
 		return nil
 	}
 
-	bspPath := bspPkg.BasePath()
+	env := BasicEnvVars(binBasePath, bspPkg)
+	for k, v := range extraEnvSettings {
+		env[k] = v
+	}
 
-	sortedKeys := make([]string, 0, len(extraEnvSettings))
-	for k, _ := range extraEnvSettings {
+	sortedKeys := make([]string, 0, len(env))
+	for k, _ := range env {
 		sortedKeys = append(sortedKeys, k)
 	}
 	sort.Strings(sortedKeys)
 
-	env := []string{}
+	envSlice := []string{}
 	for _, key := range sortedKeys {
-		env = append(env, fmt.Sprintf("%s=%s", key, extraEnvSettings[key]))
+		envSlice = append(envSlice, fmt.Sprintf("%s=%s", key, env[key]))
 	}
 
-	coreRepo := project.GetProject().FindRepo("apache-mynewt-core")
-	env = append(env, fmt.Sprintf("CORE_PATH=%s", coreRepo.Path()))
-	env = append(env, fmt.Sprintf("BSP_PATH=%s", bspPath))
-	env = append(env, fmt.Sprintf("BIN_BASENAME=%s", binBaseName))
-	env = append(env, fmt.Sprintf("BIN_ROOT=%s", BinRoot()))
-	env = append(env, fmt.Sprintf("MYNEWT_PROJECT_ROOT=%s", ProjectRoot()))
-
-	RunOptionalCheck(bspPkg.OptChkScript, env)
-	// bspPath, binBaseName are passed in command line for backwards
+	RunOptionalCheck(bspPkg.OptChkScript, envSlice)
+	// bspPath, binBasePath are passed in command line for backwards
 	// compatibility
 	cmd := []string{
 		bspPkg.DownloadScript,
-		bspPath,
-		binBaseName,
+		bspPkg.BasePath(),
+		binBasePath,
 	}
 
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "Load command: %s\n",
@@ -159,7 +153,7 @@ func Load(binBaseName string, bspPkg *pkg.BspPackage,
 	for _, v := range env {
 		util.StatusMessage(util.VERBOSITY_VERBOSE, "* %s\n", v)
 	}
-	if _, err := util.ShellCommand(cmd, env); err != nil {
+	if _, err := util.ShellCommand(cmd, envSlice); err != nil {
 		return err
 	}
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "Successfully loaded image.\n")
@@ -178,50 +172,27 @@ func (b *Builder) Load(imageSlot int, extraJtagCmd string) error {
 		return err
 	}
 
-	envSettings := map[string]string{
-		"IMAGE_SLOT": strconv.Itoa(imageSlot),
-		"FEATURES":   b.FeatureString(),
+	env, err := b.EnvVars(imageSlot)
+	if err != nil {
+		return err
 	}
+
 	if extraJtagCmd != "" {
-		envSettings["EXTRA_JTAG_CMD"] = extraJtagCmd
+		env["EXTRA_JTAG_CMD"] = extraJtagCmd
 	}
-	settings := b.cfg.SettingValues()
 
-	var flashTargetArea string
-	if parse.ValueIsTrue(settings["BOOT_LOADER"]) {
-		envSettings["BOOT_LOADER"] = "1"
-
-		flashTargetArea = "FLASH_AREA_BOOTLOADER"
+	if _, ok := env["BOOT_LOADER"]; ok {
 		util.StatusMessage(util.VERBOSITY_DEFAULT,
 			"Loading bootloader\n")
 	} else {
-		if imageSlot == 0 {
-			flashTargetArea = "FLASH_AREA_IMAGE_0"
-		} else if imageSlot == 1 {
-			flashTargetArea = "FLASH_AREA_IMAGE_1"
-		}
 		util.StatusMessage(util.VERBOSITY_DEFAULT,
 			"Loading %s image into slot %d\n", b.buildName, imageSlot+1)
 	}
 
-	bspPkg := b.targetBuilder.bspPkg
-	tgtArea := bspPkg.FlashMap.Areas[flashTargetArea]
-	if tgtArea.Name == "" {
-		return util.NewNewtError(fmt.Sprintf("No flash target area %s\n",
-			flashTargetArea))
-	}
-	envSettings["FLASH_OFFSET"] = "0x" + strconv.FormatInt(int64(tgtArea.Offset), 16)
-	envSettings["FLASH_AREA_SIZE"] = "0x" + strconv.FormatInt(int64(tgtArea.Size), 16)
-
-	// Add all syscfg settings to the environment with the MYNEWT_VAL_ prefix.
-	for k, v := range settings {
-		envSettings["MYNEWT_VAL_"+k] = v
-	}
-
 	// Convert the binary path from absolute to relative.  This is required for
 	// compatibility with unix-in-windows environemnts (e.g., cygwin).
 	binPath := util.TryRelPath(b.AppBinBasePath())
-	if err := Load(binPath, b.targetBuilder.bspPkg, envSettings); err != nil {
+	if err := Load(binPath, b.targetBuilder.bspPkg, env); err != nil {
 		return err
 	}
 
@@ -250,15 +221,15 @@ func (b *Builder) debugBin(binPath string, extraJtagCmd string, reset bool,
 	}
 
 	bspPath := b.bspPkg.rpkg.Lpkg.BasePath()
-	binBaseName := binPath
-	featureString := b.FeatureString()
+	binBasePath := binPath
+	featureString := FeatureString(b.cfg.SettingValues())
 	bspPkg := b.targetBuilder.bspPkg
 
 	coreRepo := project.GetProject().FindRepo("apache-mynewt-core")
 	envSettings := []string{
 		fmt.Sprintf("CORE_PATH=%s", coreRepo.Path()),
 		fmt.Sprintf("BSP_PATH=%s", bspPath),
-		fmt.Sprintf("BIN_BASENAME=%s", binBaseName),
+		fmt.Sprintf("BIN_BASENAME=%s", binBasePath),
 		fmt.Sprintf("FEATURES=%s", featureString),
 		fmt.Sprintf("MYNEWT_PROJECT_ROOT=%s", ProjectRoot()),
 	}
@@ -276,10 +247,10 @@ func (b *Builder) debugBin(binPath string, extraJtagCmd string, reset bool,
 	os.Chdir(project.GetProject().Path())
 
 	RunOptionalCheck(bspPkg.OptChkScript, envSettings)
-	// bspPath, binBaseName are passed in command line for backwards
+	// bspPath, binBasePath are passed in command line for backwards
 	// compatibility
 	cmdLine := []string{
-		b.targetBuilder.bspPkg.DebugScript, bspPath, binBaseName,
+		b.targetBuilder.bspPkg.DebugScript, bspPath, binBasePath,
 	}
 
 	fmt.Printf("%s\n", cmdLine)
@@ -287,12 +258,10 @@ func (b *Builder) debugBin(binPath string, extraJtagCmd string, reset bool,
 }
 
 func (b *Builder) Debug(extraJtagCmd string, reset bool, noGDB bool) error {
-	if b.appPkg == nil {
-		return util.NewNewtError("app package not specified")
+	binPath, err := b.binBasePath()
+	if err != nil {
+		return err
 	}
 
-	// Convert the binary path from absolute to relative.  This is required for
-	// Windows compatibility.
-	binPath := util.TryRelPath(b.AppBinBasePath())
 	return b.debugBin(binPath, extraJtagCmd, reset, noGDB)
 }


[mynewt-newt] 04/05: Run custom commands at build time

Posted by cc...@apache.org.
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 9be9346655b3fb936f610f923438653b31897d2c
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Sep 20 12:17:49 2019 -0500

    Run custom commands at build time
    
    This commit adds the ability to run custom commands at build time.
    
    This PR adds the ability to run custom commands at build time.
    
    A package specifies custom commands in its `pkg.yml` file.  There are
    three types of commands:
    1. pre_build_cmds (run before the build)
    2. pre_link_cmds (run after compilation, before linking)
    3. post_build_cmds (run after the build)
    
    Example (apps/blinky/pkg.yml):
    ```
    pkg.pre_build_cmds:
        scripts/pre_build1.sh: 100
        scripts/pre_build2.sh: 200
    
    pkg.pre_link_cmds:
        scripts/pre_link.sh: 500
    
    pkg.post_build_cmds:
        scripts/post_build.sh: 100
    ```
    
    For each command, the string on the left specifies the command to run.
    The number on the right indicates the command's relative ordering.
    
    When newt builds this example, it performs the following sequence:
    
    * scripts/pre_build1.sh
    * scripts/pre_build2.sh
    * [compile]
    * scripts/pre_link.sh
    * [link]
    * scripts/post_build.sh
    
    If other packages specify custom commands, those commands would also be
    executed during the above sequence.  For example, if another package
    specifies a pre command with an ordering of 150, that command would run
    immediately after `pre_build1.sh`.  In the case of a tie, the commands
    are run in lexicographic order.
    
    All commands are run from the project's base directory.  In the above
    example, the `scripts` directory is a sibling of `targets`.
    
    See <https://github.com/apache/mynewt-newt/pull/335> for details.
---
 go.mod                      |   1 +
 go.sum                      |   2 +
 newt/builder/build.go       |  45 +++++++--
 newt/builder/buildutil.go   |  57 ++++++++++-
 newt/builder/extcmd.go      | 235 ++++++++++++++++++++++++++++++++++++++++++++
 newt/builder/load.go        |  13 +--
 newt/builder/paths.go       |  32 ++++++
 newt/builder/targetbuild.go |  42 +++++++-
 newt/extcmd/extcmd.go       |  98 ++++++++++++++++++
 newt/pkg/localpackage.go    |  18 ++++
 newt/resolve/resolve.go     |  29 ++++++
 newt/stage/stage.go         |   6 +-
 util/util.go                | 118 ++++++++++++++++++++++
 13 files changed, 672 insertions(+), 24 deletions(-)

diff --git a/go.mod b/go.mod
index 5744c4d..e0368d0 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.12
 require (
 	github.com/NickBall/go-aes-key-wrap v0.0.0-20170929221519-1c3aa3e4dfc5
 	github.com/apache/mynewt-artifact v0.0.3
+	github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
 	github.com/sirupsen/logrus v1.4.2
 	github.com/spf13/cast v1.3.0
 	github.com/spf13/cobra v0.0.5
diff --git a/go.sum b/go.sum
index d591003..de1f0bd 100644
--- a/go.sum
+++ b/go.sum
@@ -16,6 +16,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
+github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
 github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
diff --git a/newt/builder/build.go b/newt/builder/build.go
index 1e3c392..d23d44d 100644
--- a/newt/builder/build.go
+++ b/newt/builder/build.go
@@ -83,10 +83,16 @@ func NewBuilder(
 		}
 	}
 
-	// Create a pseudo build package for the generated sysinit code.
+	// Create the pseudo build packages.
 	if _, err := b.addSysinitBpkg(); err != nil {
 		return nil, err
 	}
+	if _, err := b.addUserPreBuildBpkg(); err != nil {
+		return nil, err
+	}
+	if _, err := b.addUserPreLinkBpkg(); err != nil {
+		return nil, err
+	}
 
 	for api, rpkg := range apiMap {
 		bpkg := b.PkgMap[rpkg]
@@ -504,6 +510,10 @@ func (b *Builder) PrepBuild() error {
 	baseCi.Includes = append(baseCi.Includes,
 		GeneratedIncludeDir(b.targetPkg.rpkg.Lpkg.FullName()))
 
+	// All packages have access to the user generated header directory.
+	baseCi.Includes = append(baseCi.Includes,
+		UserPreBuildIncludeDir(b.targetPkg.rpkg.Lpkg.Name()))
+
 	// Let multiplatform libraries know that a Mynewt binary is being build.
 	baseCi.Cflags = append(baseCi.Cflags, "-DMYNEWT=1")
 
@@ -518,17 +528,40 @@ func (b *Builder) AddCompilerInfo(info *toolchain.CompilerInfo) {
 	b.compilerInfo.AddCompilerInfo(info)
 }
 
-func (b *Builder) addSysinitBpkg() (*BuildPackage, error) {
-	lpkg := pkg.NewLocalPackage(b.targetPkg.rpkg.Lpkg.Repo().(*repo.Repo),
-		GeneratedBaseDir(b.targetPkg.rpkg.Lpkg.FullName()))
-	lpkg.SetName(pkg.ShortName(b.targetPkg.rpkg.Lpkg) + "-sysinit-" +
-		b.buildName)
+// addPseudoBpkg creates a dynamic build package and adds it to the builder.
+func (b *Builder) addPseudoBpkg(name string,
+	dir string) (*BuildPackage, error) {
+
+	lpkg := pkg.NewLocalPackage(b.targetPkg.rpkg.Lpkg.Repo().(*repo.Repo), dir)
+	lpkg.SetName(fmt.Sprintf(
+		"%s-%s", pkg.ShortName(b.targetPkg.rpkg.Lpkg), name))
 	lpkg.SetType(pkg.PACKAGE_TYPE_GENERATED)
 
 	rpkg := resolve.NewResolvePkg(lpkg)
 	return b.addPackage(rpkg)
 }
 
+// addSysinitBpkg adds the pseudo sysinit build package to the builder.
+func (b *Builder) addSysinitBpkg() (*BuildPackage, error) {
+	name := fmt.Sprintf("%s-%s", "sysinit", b.buildName)
+	dir := GeneratedBaseDir(b.targetPkg.rpkg.Lpkg.FullName())
+	return b.addPseudoBpkg(name, dir)
+}
+
+// addUserPreBuildBpkg adds the pseudo user build package to the builder.  The
+// user build package contains inputs emitted by external scripts.
+func (b *Builder) addUserPreBuildBpkg() (*BuildPackage, error) {
+	return b.addPseudoBpkg("user-pre-build",
+		UserPreBuildDir(b.targetPkg.rpkg.Lpkg.FullName()))
+}
+
+// addUserPreLinkBpkg adds the pseudo user build package to the builder.  The
+// user build package contains inputs emitted by external scripts.
+func (b *Builder) addUserPreLinkBpkg() (*BuildPackage, error) {
+	return b.addPseudoBpkg("user-pre-link",
+		UserPreLinkDir(b.targetPkg.rpkg.Lpkg.FullName()))
+}
+
 // Runs build jobs while any remain.  On failure, signals the other workers to
 // stop via the stop channel.  On error, the error object is signaled via the
 // results channel.  On successful completion, nil is signaled via the results
diff --git a/newt/builder/buildutil.go b/newt/builder/buildutil.go
index e06bd12..e330f36 100644
--- a/newt/builder/buildutil.go
+++ b/newt/builder/buildutil.go
@@ -21,6 +21,7 @@ package builder
 
 import (
 	"bytes"
+	"fmt"
 	"sort"
 	"strconv"
 	"strings"
@@ -217,6 +218,42 @@ func SlotEnvVars(bspPkg *pkg.BspPackage,
 	return env, nil
 }
 
+type UserEnvParams struct {
+	Lpkg       *pkg.LocalPackage
+	TargetName string // Short name
+	AppName    string
+	BuildName  string // "app" or "loader"
+	UserSrcDir string // "" if none
+	UserIncDir string // "" if none
+	WorkDir    string
+}
+
+// UserEnvVars calculates the set of environment variables required by external
+// user scripts.
+func UserEnvVars(params UserEnvParams) map[string]string {
+	m := map[string]string{}
+
+	m["MYNEWT_APP_BIN_DIR"] = FileBinDir(
+		params.TargetName, params.BuildName, params.AppName)
+	m["MYNEWT_PKG_BIN_ARCHIVE"] = ArchivePath(
+		params.TargetName, params.BuildName, params.Lpkg.FullName(),
+		params.Lpkg.Type())
+	m["MYNEWT_PKG_BIN_DIR"] = PkgBinDir(
+		params.TargetName, params.BuildName, params.Lpkg.FullName(),
+		params.Lpkg.Type())
+	m["MYNEWT_PKG_NAME"] = params.Lpkg.FullName()
+	m["MYNEWT_USER_WORK_DIR"] = params.WorkDir
+
+	if params.UserSrcDir != "" {
+		m["MYNEWT_USER_SRC_DIR"] = params.UserSrcDir
+	}
+	if params.UserIncDir != "" {
+		m["MYNEWT_USER_INCLUDE_DIR"] = params.UserIncDir
+	}
+
+	return m
+}
+
 // EnvVars calculates the full set of environment variables passed to external
 // scripts.
 func (b *Builder) EnvVars(imageSlot int) (map[string]string, error) {
@@ -238,7 +275,7 @@ func (b *Builder) EnvVars(imageSlot int) (map[string]string, error) {
 		imageSlot = -1
 	}
 
-	slotEnv, err := SlotEnvVars(bspPkg, imageSlot, settings)
+	slotEnv, err := SlotEnvVars(bspPkg, imageSlot)
 	if err != nil {
 		return nil, err
 	}
@@ -252,3 +289,21 @@ func (b *Builder) EnvVars(imageSlot int) (map[string]string, error) {
 
 	return env, nil
 }
+
+// EnvVarsToSlice converts an environment variable map into a slice of strings
+// suitable for "shell command" functions defined in `util` (e.g.,
+// util.ShellCommand).
+func EnvVarsToSlice(env map[string]string) []string {
+	keys := make([]string, 0, len(env))
+	for k, _ := range env {
+		keys = append(keys, k)
+	}
+	sort.Strings(keys)
+
+	slice := make([]string, 0, len(env))
+	for _, key := range keys {
+		slice = append(slice, fmt.Sprintf("%s=%s", key, env[key]))
+	}
+
+	return slice
+}
diff --git a/newt/builder/extcmd.go b/newt/builder/extcmd.go
new file mode 100644
index 0000000..624a025
--- /dev/null
+++ b/newt/builder/extcmd.go
@@ -0,0 +1,235 @@
+package builder
+
+import (
+	"io/ioutil"
+	"os"
+	"os/exec"
+
+	"github.com/kballard/go-shellquote"
+	log "github.com/sirupsen/logrus"
+	"mynewt.apache.org/newt/newt/stage"
+	"mynewt.apache.org/newt/util"
+)
+
+// replaceArtifactsIfChanged compares the artifacts just produced (temp
+// directory) to those from the previous build (user bin directory).  If they
+// are different, it replaces the old with the new so that they get relinked
+// during this build.
+func replaceArtifactsIfChanged(oldDir string, newDir string) error {
+	eq, err := util.DirsAreEqual(oldDir, newDir)
+	if err != nil {
+		return err
+	}
+
+	if eq {
+		// No changes detected.
+		return nil
+	}
+
+	log.Debugf("changes detected; replacing %s with %s", oldDir, newDir)
+	os.RemoveAll(oldDir)
+	if err := os.Rename(newDir, oldDir); err != nil {
+		return util.ChildNewtError(err)
+	}
+
+	return nil
+}
+
+// createTempUserDirs creates a set of temporary directories for holding build
+// inputs.  It returns:
+//     * base-dir
+//     * src-dir
+//     * include-dir
+func createTempUserDirs(label string) (string, string, string, error) {
+	tmpDir, err := ioutil.TempDir("", "mynewt-user-"+label)
+	if err != nil {
+		return "", "", "", util.ChildNewtError(err)
+	}
+	log.Debugf("created user %s dir: %s", label, tmpDir)
+
+	tmpSrcDir := UserTempSrcDir(tmpDir)
+	log.Debugf("creating user %s src dir: %s", label, tmpSrcDir)
+	if err := os.MkdirAll(tmpSrcDir, 0755); err != nil {
+		os.RemoveAll(tmpDir)
+		return "", "", "", util.ChildNewtError(err)
+	}
+
+	tmpIncDir := UserTempIncludeDir(tmpDir)
+	log.Debugf("creating user %s include dir: %s", label, tmpIncDir)
+	if err := os.MkdirAll(tmpIncDir, 0755); err != nil {
+		os.RemoveAll(tmpDir)
+		return "", "", "", util.ChildNewtError(err)
+	}
+
+	return tmpDir, tmpSrcDir, tmpIncDir, nil
+}
+
+// envVarsForCmd calculates the set of environment variables to export for the
+// specified external command.
+func (t *TargetBuilder) envVarsForCmd(sf stage.StageFunc, userSrcDir string,
+	userIncDir string, workDir string) (map[string]string, error) {
+
+	// Determine whether the owning package is part of the loader or the app.
+	slot := 0
+	buildName := "app"
+
+	if t.LoaderBuilder != nil {
+		rpkg := t.res.LpkgRpkgMap[sf.Pkg]
+		if rpkg == nil {
+			return nil, util.FmtNewtError(
+				"resolution missing expected package: %s", sf.Pkg.FullName())
+		}
+
+		if t.LoaderBuilder.PkgMap[rpkg] != nil {
+			buildName = "loader"
+		} else {
+			slot = 1
+		}
+	}
+
+	env, err := t.AppBuilder.EnvVars(slot)
+	if err != nil {
+		return nil, err
+	}
+
+	p := UserEnvParams{
+		Lpkg:       sf.Pkg,
+		TargetName: t.target.FullName(),
+		AppName:    t.appPkg.FullName(),
+		BuildName:  buildName,
+		UserSrcDir: userSrcDir,
+		UserIncDir: userIncDir,
+		WorkDir:    workDir,
+	}
+	uenv := UserEnvVars(p)
+
+	for k, v := range uenv {
+		env[k] = v
+	}
+
+	return env, nil
+}
+
+// execExtCmds executes a set of user scripts.
+func (t *TargetBuilder) execExtCmds(sf stage.StageFunc, userSrcDir string,
+	userIncDir string, workDir string) error {
+
+	env, err := t.envVarsForCmd(sf, userSrcDir, userIncDir, workDir)
+	if err != nil {
+		return err
+	}
+
+	envs := EnvVarsToSlice(env)
+	toks, err := shellquote.Split(sf.Name)
+	if err != nil {
+		return util.FmtNewtError(
+			"invalid command string: \"%s\": %s", sf.Name, err.Error())
+	}
+
+	// If the command is in the user's PATH, expand it to its real location.
+	cmd, err := exec.LookPath(toks[0])
+	if err == nil {
+		toks[0] = cmd
+	}
+
+	util.StatusMessage(util.VERBOSITY_DEFAULT, "Executing %s\n", sf.Name)
+	if err := util.ShellInteractiveCommand(toks, envs, true); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// execPreBuildCmds runs the target's set of pre-build user commands.  It is an
+// error if any command fails (exits with a nonzero status).
+func (t *TargetBuilder) execPreBuildCmds(workDir string) error {
+	// Create temporary directories where scripts can put build inputs.
+	tmpDir, tmpSrcDir, tmpIncDir, err := createTempUserDirs("pre-build")
+	if err != nil {
+		return err
+	}
+	defer func() {
+		log.Debugf("removing user pre-build dir: %s", tmpDir)
+		os.RemoveAll(tmpDir)
+	}()
+
+	for _, sf := range t.res.PreBuildCmdCfg.StageFuncs {
+		if err := t.execExtCmds(sf, tmpSrcDir, tmpIncDir, workDir); err != nil {
+			return err
+		}
+	}
+
+	srcDir := UserPreBuildSrcDir(t.target.FullName())
+	if err := replaceArtifactsIfChanged(srcDir, tmpSrcDir); err != nil {
+		return err
+	}
+
+	incDir := UserPreBuildIncludeDir(t.target.FullName())
+	if err := replaceArtifactsIfChanged(incDir, tmpIncDir); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// execPreLinkCmds runs the target's set of post-build user commands.  It is
+// an error if any command fails (exits with a nonzero status).
+func (t *TargetBuilder) execPreLinkCmds(workDir string) error {
+	// Create temporary directories where scripts can put build inputs.
+	tmpDir, tmpSrcDir, _, err := createTempUserDirs("pre-link")
+	if err != nil {
+		return err
+	}
+	defer func() {
+		log.Debugf("removing user pre-link dir: %s", tmpDir)
+		os.RemoveAll(tmpDir)
+	}()
+
+	for _, sf := range t.res.PreLinkCmdCfg.StageFuncs {
+		if err := t.execExtCmds(sf, tmpSrcDir, "", workDir); err != nil {
+			return err
+		}
+	}
+
+	srcDir := UserPreLinkSrcDir(t.target.FullName())
+	err = replaceArtifactsIfChanged(srcDir, tmpSrcDir)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// execPostBuildCmds runs the target's set of post-build user commands.  It is
+// an error if any command fails (exits with a nonzero status).
+func (t *TargetBuilder) execPostBuildCmds(workDir string) error {
+	for _, sf := range t.res.PostBuildCmdCfg.StageFuncs {
+		if err := t.execExtCmds(sf, "", "", workDir); err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
+// makeUserDir creates a temporary directory where scripts can put build
+// inputs.
+func makeUserDir() (string, error) {
+	tmpDir, err := ioutil.TempDir("", "mynewt-user")
+	if err != nil {
+		return "", util.ChildNewtError(err)
+	}
+	log.Debugf("created user dir: %s", tmpDir)
+
+	return tmpDir, nil
+}
+
+func makeUserWorkDir() (string, error) {
+	tmpDir, err := ioutil.TempDir("", "mynewt-user-work")
+	if err != nil {
+		return "", util.ChildNewtError(err)
+	}
+	log.Debugf("created user work dir: %s", tmpDir)
+
+	return tmpDir, nil
+}
diff --git a/newt/builder/load.go b/newt/builder/load.go
index 940a934..a166b8c 100644
--- a/newt/builder/load.go
+++ b/newt/builder/load.go
@@ -23,7 +23,6 @@ import (
 	"fmt"
 	"os"
 	"os/signal"
-	"sort"
 	"strings"
 	"syscall"
 
@@ -126,17 +125,7 @@ func Load(binBasePath string, bspPkg *pkg.BspPackage,
 	for k, v := range extraEnvSettings {
 		env[k] = v
 	}
-
-	sortedKeys := make([]string, 0, len(env))
-	for k, _ := range env {
-		sortedKeys = append(sortedKeys, k)
-	}
-	sort.Strings(sortedKeys)
-
-	envSlice := []string{}
-	for _, key := range sortedKeys {
-		envSlice = append(envSlice, fmt.Sprintf("%s=%s", key, env[key]))
-	}
+	envSlice := EnvVarsToSlice(env)
 
 	RunOptionalCheck(bspPkg.OptChkScript, envSlice)
 	// bspPath, binBasePath are passed in command line for backwards
diff --git a/newt/builder/paths.go b/newt/builder/paths.go
index 2fb078e..cc5c2d3 100644
--- a/newt/builder/paths.go
+++ b/newt/builder/paths.go
@@ -63,6 +63,38 @@ func SysinitArchivePath(targetName string) string {
 	return GeneratedBinDir(targetName) + "/sysinit.a"
 }
 
+func UserBaseDir(targetName string) string {
+	return BinRoot() + "/" + targetName + "/user"
+}
+
+func UserPreBuildDir(targetName string) string {
+	return UserBaseDir(targetName) + "/pre_build"
+}
+
+func UserPreLinkDir(targetName string) string {
+	return UserBaseDir(targetName) + "/pre_link"
+}
+
+func UserPreBuildSrcDir(targetName string) string {
+	return UserPreBuildDir(targetName) + "/src"
+}
+
+func UserPreBuildIncludeDir(targetName string) string {
+	return UserPreBuildDir(targetName) + "/include"
+}
+
+func UserPreLinkSrcDir(targetName string) string {
+	return UserPreLinkDir(targetName) + "/src"
+}
+
+func UserTempSrcDir(tempDir string) string {
+	return tempDir + "/src"
+}
+
+func UserTempIncludeDir(tempDir string) string {
+	return tempDir + "/include"
+}
+
 func PkgSyscfgPath(pkgPath string) string {
 	return pkgPath + "/" + pkg.SYSCFG_YAML_FILENAME
 }
diff --git a/newt/builder/targetbuild.go b/newt/builder/targetbuild.go
index 3b1604d..c5145a1 100644
--- a/newt/builder/targetbuild.go
+++ b/newt/builder/targetbuild.go
@@ -330,7 +330,22 @@ func (t *TargetBuilder) PrepBuild() error {
 		return err
 	}
 
-	var err error
+	// Create directories where user scripts can write artifacts to incorporate
+	// into the build.
+
+	err := os.MkdirAll(UserPreBuildSrcDir(t.target.FullName()), 0755)
+	if err != nil {
+		return util.NewNewtError(err.Error())
+	}
+	err = os.MkdirAll(UserPreBuildIncludeDir(t.target.FullName()), 0755)
+	if err != nil {
+		return util.NewNewtError(err.Error())
+	}
+	err = os.MkdirAll(UserPreLinkSrcDir(t.target.FullName()), 0755)
+	if err != nil {
+		return util.NewNewtError(err.Error())
+	}
+
 	if t.res.LoaderSet != nil {
 		t.LoaderBuilder, err = NewBuilder(t, BUILD_NAME_LOADER,
 			t.res.LoaderSet.Rpkgs, t.res.ApiMap, t.res.Cfg)
@@ -483,7 +498,6 @@ func (t *TargetBuilder) Build() error {
 		return err
 	}
 
-	/* Build the Apps */
 	project.ResetDeps(t.AppList)
 
 	if err := t.bspPkg.Reload(t.AppBuilder.cfg.SettingValues()); err != nil {
@@ -497,6 +511,20 @@ func (t *TargetBuilder) Build() error {
 		}
 	}
 
+	workDir, err := makeUserWorkDir()
+	if err != nil {
+		return err
+	}
+	defer func() {
+		log.Debugf("removing user work dir: %s", workDir)
+		os.RemoveAll(workDir)
+	}()
+
+	// Execute the set of pre-build user scripts.
+	if err := t.execPreBuildCmds(workDir); err != nil {
+		return err
+	}
+
 	if err := t.AppBuilder.Build(); err != nil {
 		return err
 	}
@@ -511,11 +539,21 @@ func (t *TargetBuilder) Build() error {
 		linkerScripts = t.bspPkg.Part2LinkerScripts
 	}
 
+	// Execute the set of pre-link user scripts.
+	if err := t.execPreLinkCmds(workDir); err != nil {
+		return err
+	}
+
 	/* Link the app. */
 	if err := t.AppBuilder.Link(linkerScripts); err != nil {
 		return err
 	}
 
+	// Execute the set of post-build user scripts.
+	if err := t.execPostBuildCmds(workDir); err != nil {
+		return err
+	}
+
 	return nil
 }
 
diff --git a/newt/extcmd/extcmd.go b/newt/extcmd/extcmd.go
new file mode 100644
index 0000000..68cfc65
--- /dev/null
+++ b/newt/extcmd/extcmd.go
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package extcmd
+
+import (
+	"fmt"
+
+	"mynewt.apache.org/newt/newt/pkg"
+	"mynewt.apache.org/newt/newt/stage"
+	"mynewt.apache.org/newt/newt/syscfg"
+)
+
+// ExtCmdCfg represents an ordered list of external commands that get run
+// during the build (aka "user scripts").
+type ExtCmdCfg struct {
+	// Used in diagnostic messages only (example: "pre_build_cmds").
+	Name string
+
+	// Sorted in call order (stage-num,command-string).
+	StageFuncs []stage.StageFunc
+
+	// Strings describing errors encountered while parsing the extcmd config.
+	InvalidSettings []string
+}
+
+// GetMapFn retrieves a set of ordered external commands from a package.  For
+// example, one GetMapFn instance might retrieve a package's pre-build
+// commands.
+type GetMapFn func(
+	lpkg *pkg.LocalPackage, settings map[string]string) map[string]string
+
+func (ecfg *ExtCmdCfg) readOnePkg(lpkg *pkg.LocalPackage, cfg *syscfg.Cfg,
+	getMapCb GetMapFn) {
+
+	settings := cfg.AllSettingsForLpkg(lpkg)
+	cmdMap := getMapCb(lpkg, settings)
+
+	for name, stageStr := range cmdMap {
+		sf, err := stage.NewStageFunc(name, stageStr, lpkg, cfg)
+		if err != nil {
+			text := fmt.Sprintf("%s: %s", lpkg.FullName(), err.Error())
+			ecfg.InvalidSettings = append(ecfg.InvalidSettings, text)
+		} else {
+			ecfg.StageFuncs = append(ecfg.StageFuncs, sf)
+		}
+	}
+}
+
+// Read constructs an external command configuration from a full set of
+// packages.
+func Read(name string, lpkgs []*pkg.LocalPackage, cfg *syscfg.Cfg,
+	getMapCb GetMapFn) ExtCmdCfg {
+
+	ecfg := ExtCmdCfg{
+		Name: name,
+	}
+
+	for _, lpkg := range lpkgs {
+		ecfg.readOnePkg(lpkg, cfg, getMapCb)
+	}
+
+	stage.SortStageFuncs(ecfg.StageFuncs, ecfg.Name)
+
+	return ecfg
+}
+
+// If any errors were encountered while parsing extcmd definitions, this
+// function returns a string indicating the errors.  If no errors were
+// encountered, "" is returned.
+func (ecfg *ExtCmdCfg) ErrorText() string {
+	str := ""
+
+	if len(ecfg.InvalidSettings) > 0 {
+		str += fmt.Sprintf("Invalid %s definitions detected:", ecfg.Name)
+		for _, e := range ecfg.InvalidSettings {
+			str += "\n    " + e
+		}
+	}
+
+	return str
+}
diff --git a/newt/pkg/localpackage.go b/newt/pkg/localpackage.go
index fcd2e68..d1f0088 100644
--- a/newt/pkg/localpackage.go
+++ b/newt/pkg/localpackage.go
@@ -377,6 +377,24 @@ func (pkg *LocalPackage) DownFuncs(
 	return pkg.PkgY.GetValStringMapString("pkg.down", settings)
 }
 
+func (pkg *LocalPackage) PreBuildCmds(
+	settings map[string]string) map[string]string {
+
+	return pkg.PkgY.GetValStringMapString("pkg.pre_build_cmds", settings)
+}
+
+func (pkg *LocalPackage) PreLinkCmds(
+	settings map[string]string) map[string]string {
+
+	return pkg.PkgY.GetValStringMapString("pkg.pre_link_cmds", settings)
+}
+
+func (pkg *LocalPackage) PostBuildCmds(
+	settings map[string]string) map[string]string {
+
+	return pkg.PkgY.GetValStringMapString("pkg.post_build_cmds", settings)
+}
+
 func (pkg *LocalPackage) InjectedSettings() map[string]string {
 	return pkg.injectedSettings
 }
diff --git a/newt/resolve/resolve.go b/newt/resolve/resolve.go
index 07fa1de..0cb6c98 100644
--- a/newt/resolve/resolve.go
+++ b/newt/resolve/resolve.go
@@ -26,6 +26,7 @@ import (
 
 	log "github.com/sirupsen/logrus"
 
+	"mynewt.apache.org/newt/newt/extcmd"
 	"mynewt.apache.org/newt/newt/flashmap"
 	"mynewt.apache.org/newt/newt/logcfg"
 	"mynewt.apache.org/newt/newt/newtutil"
@@ -67,6 +68,9 @@ type Resolver struct {
 	lcfg             logcfg.LCfg
 	sysinitCfg       sysinit.SysinitCfg
 	sysdownCfg       sysdown.SysdownCfg
+	preBuildCmdCfg   extcmd.ExtCmdCfg
+	preLinkCmdCfg    extcmd.ExtCmdCfg
+	postBuildCmdCfg  extcmd.ExtCmdCfg
 
 	// [api-name][api-supplier]
 	apiConflicts map[string]map[*ResolvePackage]struct{}
@@ -119,6 +123,9 @@ type Resolution struct {
 	LCfg            logcfg.LCfg
 	SysinitCfg      sysinit.SysinitCfg
 	SysdownCfg      sysdown.SysdownCfg
+	PreBuildCmdCfg  extcmd.ExtCmdCfg
+	PreLinkCmdCfg   extcmd.ExtCmdCfg
+	PostBuildCmdCfg extcmd.ExtCmdCfg
 	ApiMap          map[string]*ResolvePackage
 	UnsatisfiedApis map[string][]*ResolvePackage
 	ApiConflicts    []ApiConflict
@@ -987,6 +994,22 @@ func (r *Resolver) resolveDepsAndCfg() error {
 	r.sysinitCfg = sysinit.Read(lpkgs, &r.cfg)
 	r.sysdownCfg = sysdown.Read(lpkgs, &r.cfg)
 
+	r.preBuildCmdCfg = extcmd.Read("pre_build_cmds", lpkgs, &r.cfg,
+		func(lpkg *pkg.LocalPackage,
+			settings map[string]string) map[string]string {
+			return lpkg.PreBuildCmds(settings)
+		})
+	r.preLinkCmdCfg = extcmd.Read("pre_link_cmds", lpkgs, &r.cfg,
+		func(lpkg *pkg.LocalPackage,
+			settings map[string]string) map[string]string {
+			return lpkg.PreLinkCmds(settings)
+		})
+	r.postBuildCmdCfg = extcmd.Read("post_build_cmds", lpkgs, &r.cfg,
+		func(lpkg *pkg.LocalPackage,
+			settings map[string]string) map[string]string {
+			return lpkg.PostBuildCmds(settings)
+		})
+
 	// Log the final syscfg.
 	r.cfg.Log()
 
@@ -1085,6 +1108,9 @@ func ResolveFull(
 	res.LCfg = r.lcfg
 	res.SysinitCfg = r.sysinitCfg
 	res.SysdownCfg = r.sysdownCfg
+	res.PreBuildCmdCfg = r.preBuildCmdCfg
+	res.PreLinkCmdCfg = r.preLinkCmdCfg
+	res.PostBuildCmdCfg = r.postBuildCmdCfg
 
 	// Determine which package satisfies each API and which APIs are
 	// unsatisfied.
@@ -1203,6 +1229,9 @@ func (res *Resolution) ErrorText() string {
 	str += res.LCfg.ErrorText()
 	str += res.SysinitCfg.ErrorText()
 	str += res.SysdownCfg.ErrorText()
+	str += res.PreBuildCmdCfg.ErrorText()
+	str += res.PreLinkCmdCfg.ErrorText()
+	str += res.PostBuildCmdCfg.ErrorText()
 
 	str = strings.TrimSpace(str)
 	if str != "" {
diff --git a/newt/stage/stage.go b/newt/stage/stage.go
index 7ad4e90..dce33c3 100644
--- a/newt/stage/stage.go
+++ b/newt/stage/stage.go
@@ -59,9 +59,9 @@ func NewStageFunc(name string, textVal string,
 
 	// Ensure setting resolves to an integer.
 	if _, err := vs.IntVal(); err != nil {
-		return StageFunc{}, util.FmtNewtError("Invalid stage setting: %s=%s; "+
-			"value does not resolve to an integer",
-			name, textVal)
+		return StageFunc{}, util.FmtNewtError("Invalid setting: \"%s: %s\"; "+
+			"value \"%s\" does not resolve to an integer",
+			name, textVal, textVal)
 	}
 
 	sf := StageFunc{
diff --git a/util/util.go b/util/util.go
index a008a2e..aa46495 100644
--- a/util/util.go
+++ b/util/util.go
@@ -786,3 +786,121 @@ func MarshalJSONStringer(sr fmt.Stringer) ([]byte, error) {
 
 	return j, nil
 }
+
+// readDirRecursive recursively reads the contents of a directory.  It returns
+// [dir-paths],[file-paths].  All returned strings are relative to the provided
+// base directory.
+func readDirRecursive(path string) ([]string, []string, error) {
+	var dirs []string
+	var files []string
+
+	var iter func(crumbs string) error
+	iter = func(crumbs string) error {
+		var crumbsPath string
+		if crumbs != "" {
+			crumbsPath = "/" + crumbs
+		}
+
+		f, err := os.Open(path + crumbsPath)
+		if err != nil {
+			return ChildNewtError(err)
+		}
+		defer f.Close()
+
+		infos, err := f.Readdir(-1)
+		if err != nil {
+			return ChildNewtError(err)
+		}
+
+		for _, info := range infos {
+			name := fmt.Sprintf("%s/%s", crumbs, info.Name())
+
+			if info.IsDir() {
+				dirs = append(dirs, name)
+				if err := iter(name); err != nil {
+					return err
+				}
+			} else {
+				files = append(files, name)
+			}
+		}
+
+		return nil
+	}
+
+	if err := iter(""); err != nil {
+		return nil, nil, err
+	}
+
+	return dirs, files, nil
+}
+
+// DirsAreEqual compares the contents of two directories.  Directories are
+// equal if 1) their subdirectory structures are identical, and 2) they contain
+// the exact same set of files (same names and contents).
+func DirsAreEqual(dira string, dirb string) (bool, error) {
+	dirsa, filesa, err := readDirRecursive(dira)
+	if err != nil {
+		return false, err
+	}
+
+	dirsb, filesb, err := readDirRecursive(dirb)
+	if err != nil {
+		return false, err
+	}
+
+	if len(dirsa) != len(dirsb) || len(filesa) != len(filesb) {
+		return false, nil
+	}
+
+	// Returns the intersection of two sets of strings.
+	intersection := func(a []string, b []string) map[string]struct{} {
+		ma := make(map[string]struct{}, len(a))
+		for _, p := range a {
+			ma[p] = struct{}{}
+		}
+
+		isect := map[string]struct{}{}
+		for _, p := range b {
+			if _, ok := ma[p]; ok {
+				isect[p] = struct{}{}
+			}
+		}
+
+		return isect
+	}
+
+	// If the intersection lengths are equal, both directories have the same
+	// structure.
+
+	isectDirs := intersection(dirsa, dirsb)
+	if len(isectDirs) != len(dirsa) {
+		return false, nil
+	}
+
+	isectFiles := intersection(filesa, filesb)
+	if len(isectFiles) != len(filesa) {
+		return false, nil
+	}
+
+	// Finally, compare the contents of files in each directory.
+	for _, p := range filesa {
+		patha := fmt.Sprintf("%s/%s", dira, p)
+		bytesa, err := ioutil.ReadFile(patha)
+		if err != nil {
+			return false, ChildNewtError(err)
+		}
+
+		pathb := fmt.Sprintf("%s/%s", dirb, p)
+		unchanged, err := FileContains(bytesa, pathb)
+		if err != nil {
+			return false, err
+		}
+
+		if !unchanged {
+			return false, nil
+		}
+	}
+
+	return true, nil
+}


[mynewt-newt] 03/05: stage: Simplify sorting function

Posted by cc...@apache.org.
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 dd85dc3c8e8be93496cbf0ea1fdc189413410284
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Sep 20 16:31:47 2019 -0500

    stage: Simplify sorting function
    
    It is simpler to just use `sort.Slice()` rather than create a sorter
    type.
---
 newt/stage/stage.go | 74 +++++++++++++++++++----------------------------------
 1 file changed, 26 insertions(+), 48 deletions(-)

diff --git a/newt/stage/stage.go b/newt/stage/stage.go
index ea8bad1..7ad4e90 100644
--- a/newt/stage/stage.go
+++ b/newt/stage/stage.go
@@ -73,58 +73,36 @@ func NewStageFunc(name string, textVal string,
 	return sf, nil
 }
 
-type stageFuncSorter struct {
-	// Used in logging; either "sysinit" or "sysdown".
-	funcType string
-	// Array of functions to be sorted.
-	fns []StageFunc
-}
-
-func (s stageFuncSorter) Len() int {
-	return len(s.fns)
-}
-
-func (s stageFuncSorter) Swap(i, j int) {
-	s.fns[i], s.fns[j] = s.fns[j], s.fns[i]
-}
+// SortStageFuncs performs an in-place sort of the provided StageFunc slice.
+func SortStageFuncs(sfs []StageFunc, entryType string) {
+	sort.Slice(sfs, func(i int, j int) bool {
+		a := sfs[i]
+		b := sfs[j]
+
+		inta, _ := a.Stage.IntVal()
+		intb, _ := b.Stage.IntVal()
+
+		// 1: Sort by stage number.
+		if inta < intb {
+			return true
+		} else if intb < inta {
+			return false
+		}
 
-func (s stageFuncSorter) Less(i, j int) bool {
-	a := s.fns[i]
-	b := s.fns[j]
+		// 2: Sort by entry name.
+		switch strings.Compare(a.Name, b.Name) {
+		case -1:
+			return true
+		case 1:
+			return false
+		}
 
-	inta, _ := a.Stage.IntVal()
-	intb, _ := b.Stage.IntVal()
+		// Same stage and function name?
+		log.Warnf("Warning: Identical %s entries detected: %s",
+			entryType, a.Name)
 
-	// 1: Sort by stage number.
-	if inta < intb {
 		return true
-	} else if intb < inta {
-		return false
-	}
-
-	// 2: Sort by function name.
-	switch strings.Compare(a.Name, b.Name) {
-	case -1:
-		return true
-	case 1:
-		return false
-	}
-
-	// Same stage and function name?
-	log.Warnf("Warning: Identical %s functions detected: %s",
-		s.funcType, a.Name)
-
-	return true
-}
-
-// SortStageFuncs performs an in-place sort of the provided StageFunc slice.
-func SortStageFuncs(unsorted []StageFunc, funcType string) {
-	s := stageFuncSorter{
-		funcType: funcType,
-		fns:      unsorted,
-	}
-
-	sort.Sort(s)
+	})
 }
 
 func (f *StageFunc) ReturnTypeString() string {


[mynewt-newt] 02/05: util: interactive shell: error on nonzero exit

Posted by cc...@apache.org.
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 773d97c070ccbc2541feb0ebcd9374cb87d02f87
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Sep 20 14:19:44 2019 -0500

    util: interactive shell: error on nonzero exit
    
    Prior to this change, ShellInteractiveCommand() would never return an
    error.
    
    Now, this function returns an error if the specified command exits with
    a nonzero status.  This change makes the function consistent with the
    other "shell command" functions in the util package.
---
 util/util.go | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/util/util.go b/util/util.go
index 6b59fe7..a008a2e 100644
--- a/util/util.go
+++ b/util/util.go
@@ -405,12 +405,18 @@ func ShellInteractiveCommand(cmdStr []string, env []string, flagBlock bool) erro
 	}
 
 	// Release and exit
-	_, err = proc.Wait()
+	state, err := proc.Wait()
+	signal.Stop(c)
+
 	if err != nil {
-		signal.Stop(c)
 		return NewNewtError(err.Error())
 	}
-	signal.Stop(c)
+	if state.ExitCode() != 0 {
+		return FmtNewtError(
+			"command %v exited with nonzero status %d",
+			cmdStr, state.ExitCode())
+	}
+
 	return nil
 }
 


[mynewt-newt] 05/05: cmake: print warning if any custom commands

Posted by cc...@apache.org.
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 04a7530e8dde4723ab71f5d712291d4c36d81f84
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Wed Sep 25 10:40:45 2019 -0700

    cmake: print warning if any custom commands
    
    We don't currently support custom commands in cmake output.  Print a
    warning if the user runs the "target cmake" command on a target that
    specified custom commands.
---
 newt/builder/cmake.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/newt/builder/cmake.go b/newt/builder/cmake.go
index 49fbd7a..031b628 100644
--- a/newt/builder/cmake.go
+++ b/newt/builder/cmake.go
@@ -312,6 +312,14 @@ func (t *TargetBuilder) CMakeTargetBuilderWrite(w io.Writer, targetCompiler *too
 		return err
 	}
 
+	if len(t.res.PreBuildCmdCfg.StageFuncs) > 0 ||
+		len(t.res.PreLinkCmdCfg.StageFuncs) > 0 ||
+		len(t.res.PostBuildCmdCfg.StageFuncs) > 0 {
+
+		util.OneTimeWarning(
+			"custom commands not included in cmake output (unsupported)")
+	}
+
 	/* Build the Apps */
 	project.ResetDeps(t.AppList)