func (t *TargetBuilder) envVarsForCmd(sf stage.StageFunc, userSrcDir string,
	userIncDir string, workDir string) (map[string]string, error) {
	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(),
		BuildProfile: t.target.BuildProfile,
		AppName:      t.appPkg.FullName(),
		BuildName:    buildName,
		UserSrcDir:   userSrcDir,
		UserIncDir:   userIncDir,
		WorkDir:      workDir,
	}
	uenv := UserEnvVars(p)
	for k, v := range uenv {
		env[k] = v
	}
	c, err := t.NewCompiler("", "")
	if err != nil {
		return nil, err
	}
	tenv := ToolchainEnvVars(c)
	for k, v := range tenv {
		env[k] = v
	}
	return env, nil
}
envVarsForCmd calculates the set of environment variables to export for the
specified external command.
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"
)
func replaceArtifactsIfChanged(oldDir string, newDir string) error {
	eq, err := util.DirsAreEqual(oldDir, newDir)
	if err != nil {
		return err
	}
	if eq {
		return nil
	}
	log.Debugf("changes detected; replacing %s with %s", oldDir, newDir)
	os.RemoveAll(oldDir)
	if err := util.MoveDir(newDir, oldDir); err != nil {
		return err
	}
	return nil
}
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
}