You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/03/09 21:23:17 UTC
[02/50] incubator-mynewt-newt git commit: Clean up bin / obj
directories.
Clean up bin / obj directories.
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/0a68684d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/0a68684d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/0a68684d
Branch: refs/heads/develop
Commit: 0a68684de0c5fad056083bcbc7ae70568ddfb11e
Parents: 58967ec
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Mar 7 16:17:03 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Mar 7 17:04:31 2016 -0800
----------------------------------------------------------------------
newt/builder/build.go | 96 ++++++++---------------------------------
newt/target/target.go | 9 ++++
newt/toolchain/compiler.go | 47 +++++++++-----------
newt/toolchain/deps.go | 20 +++------
4 files changed, 55 insertions(+), 117 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0a68684d/newt/builder/build.go
----------------------------------------------------------------------
diff --git a/newt/builder/build.go b/newt/builder/build.go
index 83e59b4..11221b6 100644
--- a/newt/builder/build.go
+++ b/newt/builder/build.go
@@ -21,12 +21,12 @@ package builder
import (
"bytes"
- "io/ioutil"
"os"
"path/filepath"
"mynewt.apache.org/newt/newt/cli"
"mynewt.apache.org/newt/newt/pkg"
+ "mynewt.apache.org/newt/newt/project"
"mynewt.apache.org/newt/newt/target"
"mynewt.apache.org/newt/newt/toolchain"
"mynewt.apache.org/newt/util"
@@ -190,11 +190,17 @@ func buildDir(srcDir string, c *toolchain.Compiler, t *target.Target,
return nil
}
+func (b *Builder) binDir() string {
+ return project.GetProject().Path() + "/bin/" + b.target.ShortName()
+}
+
+func (b *Builder) pkgBinDir(pkgName string) string {
+ return b.binDir() + "/" + pkgName
+}
+
// Generates the path+filename of the specified package's .a file.
func (b *Builder) archivePath(bpkg *BuildPackage) string {
- binDir := bpkg.BasePath() + "/bin/" + b.target.Package().Name()
- archiveFile := binDir + "/lib" + filepath.Base(bpkg.Name()) + ".a"
- return archiveFile
+ return b.pkgBinDir(bpkg.Name()) + "/" + filepath.Base(bpkg.Name()) + ".a"
}
func (b *Builder) elfPath() string {
@@ -203,8 +209,8 @@ func (b *Builder) elfPath() string {
return ""
}
- return appPkg.BasePath() + "/bin/" + b.target.Package().Name() + "/" +
- appPkg.Name() + ".elf"
+ return b.pkgBinDir(appPkg.Name()) + "/" +
+ filepath.Base(appPkg.Name()) + ".elf"
}
// Compiles and archives a package.
@@ -218,8 +224,7 @@ func (b *Builder) buildPackage(bpkg *BuildPackage,
}
c, err := toolchain.NewCompiler(compilerPkg.BasePath(),
- "default", // XXX
- b.target.Package().Name())
+ b.pkgBinDir(bpkg.Name()), "default") // XXX Use correct compiler def
if err != nil {
return err
}
@@ -262,8 +267,7 @@ func (b *Builder) linkApp(baseCi *toolchain.CompilerInfo,
compilerPkg *pkg.LocalPackage) error {
c, err := toolchain.NewCompiler(compilerPkg.BasePath(),
- "default", // XXX
- b.target.Package().Name())
+ b.pkgBinDir(b.target.App().Name()), "default") // XXX
if err != nil {
return err
}
@@ -346,75 +350,11 @@ func (b *Builder) Build() error {
return nil
}
-func cleanTree(rootPath string, tName string) error {
- // Find all the subdirectories of path that contain an "obj/" directory,
- // and remove that directory either altogether, or just the arch specific
- // directory.
- dirList, err := ioutil.ReadDir(rootPath)
- if err != nil {
- return util.NewNewtError(err.Error())
- }
-
- for _, node := range dirList {
- if node.IsDir() {
- if node.Name() == "obj" || node.Name() == "bin" {
- fullPath := filepath.Clean(rootPath + "/" + node.Name() + "/" +
- tName + "/")
- cli.StatusMessage(util.VERBOSITY_VERBOSE,
- "Cleaning directory %s\n", fullPath)
- os.RemoveAll(fullPath)
- } else {
- // recurse into the directory.
- err = cleanTree(rootPath+"/"+node.Name(), tName)
- if err != nil {
- return err
- }
- }
- }
- }
-
- return nil
-}
-
func (b *Builder) Clean() error {
- bspPkg := b.target.Bsp()
- if bspPkg != nil {
- b.AddPackage(bspPkg)
- }
-
- appPkg := b.target.App()
- if appPkg != nil {
- b.AddPackage(appPkg)
- }
-
- compilerPkg := b.target.Compiler()
- if compilerPkg != nil {
- b.AddPackage(compilerPkg)
- }
-
- if err := b.loadDeps(); err != nil {
- return err
- }
-
- for _, bpkg := range b.Packages {
- err := cleanTree(bpkg.BasePath(), b.target.Package().Name())
- if err != nil {
- return err
- }
- }
-
- binPath := b.elfPath()
- if binPath != "" {
- binDir := filepath.Dir(binPath)
- cli.StatusMessage(util.VERBOSITY_VERBOSE, "Cleaning directory %s\n",
- binDir)
- err := os.RemoveAll(binDir)
- if err != nil {
- return err
- }
- }
-
- return nil
+ path := b.binDir()
+ cli.StatusMessage(util.VERBOSITY_VERBOSE, "Cleaning directory %s\n", path)
+ err := os.RemoveAll(path)
+ return err
}
func (b *Builder) Init(target *target.Target) error {
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0a68684d/newt/target/target.go
----------------------------------------------------------------------
diff --git a/newt/target/target.go b/newt/target/target.go
index a2113e3..3b6dfc6 100644
--- a/newt/target/target.go
+++ b/newt/target/target.go
@@ -22,6 +22,7 @@ package target
import (
"io/ioutil"
"os"
+ "path/filepath"
"sort"
"strings"
@@ -96,6 +97,14 @@ func (target *Target) Package() *pkg.LocalPackage {
return target.basePkg
}
+func (target *Target) Name() string {
+ return target.basePkg.Name()
+}
+
+func (target *Target) ShortName() string {
+ return filepath.Base(target.Name())
+}
+
func (target *Target) Compiler() *pkg.LocalPackage {
dep, _ := pkg.NewDependency(nil, target.CompilerName)
mypkg := project.GetProject().ResolveDependency(dep).(*pkg.LocalPackage)
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0a68684d/newt/toolchain/compiler.go
----------------------------------------------------------------------
diff --git a/newt/toolchain/compiler.go b/newt/toolchain/compiler.go
index 3a3571a..cb3017c 100644
--- a/newt/toolchain/compiler.go
+++ b/newt/toolchain/compiler.go
@@ -46,7 +46,6 @@ type CompilerInfo struct {
}
type Compiler struct {
- TargetName string
ObjPathList map[string]bool
LinkerScript string
@@ -59,6 +58,7 @@ type Compiler struct {
ocPath string
ldResolveCircularDeps bool
ldMapFile bool
+ dstDir string
info CompilerInfo
}
@@ -80,18 +80,18 @@ func (ci *CompilerInfo) AddCompilerInfo(newCi *CompilerInfo) {
ci.Aflags = append(ci.Aflags, newCi.Aflags...)
}
-func NewCompiler(compilerDir string, cDef string,
- tName string) (*Compiler, error) {
+func NewCompiler(compilerDir string, dstDir string,
+ cDef string) (*Compiler, error) {
c := &Compiler{
- TargetName: tName,
ObjPathList: map[string]bool{},
+ dstDir: filepath.Clean(dstDir),
}
c.depTracker = NewDepTracker(c)
cli.StatusMessage(cli.VERBOSITY_VERBOSE,
- "Loading compiler %s, target %s, def %s\n", compilerDir, tName, cDef)
+ "Loading compiler %s, def %s\n", compilerDir, cDef)
err := c.load(compilerDir, cDef)
if err != nil {
return nil, err
@@ -137,6 +137,10 @@ func (c *Compiler) AddInfo(info *CompilerInfo) {
c.info.AddCompilerInfo(info)
}
+func (c *Compiler) DstDir() string {
+ return c.dstDir
+}
+
// Skips compilation of the specified C or assembly file, but adds the name of
// the object file that would have been generated to the compiler's list of
// object files. This function is used when the object file is already up to
@@ -144,10 +148,8 @@ func (c *Compiler) AddInfo(info *CompilerInfo) {
// still be remembered so that it gets linked in to the final library or
// executable.
func (c *Compiler) SkipSourceFile(srcFile string) error {
- wd, _ := os.Getwd()
- objDir := wd + "/obj/" + c.TargetName + "/"
- objFile := objDir + strings.TrimSuffix(srcFile, filepath.Ext(srcFile)) +
- ".o"
+ objFile := c.dstDir + "/" +
+ strings.TrimSuffix(srcFile, filepath.Ext(srcFile)) + ".o"
c.ObjPathList[filepath.ToSlash(objFile)] = true
// Update the dependency tracker with the object file's modification time.
@@ -192,10 +194,8 @@ func (c *Compiler) lflagsString() string {
func (c *Compiler) CompileFileCmd(file string,
compilerType int) (string, error) {
- wd, _ := os.Getwd()
- objDir := wd + "/obj/" + c.TargetName + "/"
objFile := strings.TrimSuffix(file, filepath.Ext(file)) + ".o"
- objPath := filepath.ToSlash(objDir + objFile)
+ objPath := filepath.ToSlash(c.dstDir + "/" + objFile)
var cmd string
@@ -218,14 +218,12 @@ func (c *Compiler) CompileFileCmd(file string,
//
// @param file The name of the source file.
func (c *Compiler) GenDepsForFile(file string) error {
- wd, _ := os.Getwd()
- objDir := wd + "/obj/" + c.TargetName + "/"
-
- if cli.NodeNotExist(objDir) {
- os.MkdirAll(objDir, 0755)
+ if cli.NodeNotExist(c.dstDir) {
+ os.MkdirAll(c.dstDir, 0755)
}
- depFile := objDir + strings.TrimSuffix(file, filepath.Ext(file)) + ".d"
+ depFile := c.dstDir + "/" +
+ strings.TrimSuffix(file, filepath.Ext(file)) + ".d"
depFile = filepath.ToSlash(depFile)
var cmd string
@@ -263,16 +261,13 @@ func writeCommandFile(dstFile string, cmd string) error {
// @param file The filename of the source file to compile.
// @param compilerType One of the COMPILER_TYPE_[...] constants.
func (c *Compiler) CompileFile(file string, compilerType int) error {
- wd, _ := os.Getwd()
- objDir := wd + "/obj/" + c.TargetName + "/"
-
- if cli.NodeNotExist(objDir) {
- os.MkdirAll(objDir, 0755)
+ if cli.NodeNotExist(c.dstDir) {
+ os.MkdirAll(c.dstDir, 0755)
}
objFile := strings.TrimSuffix(file, filepath.Ext(file)) + ".o"
- objPath := objDir + objFile
+ objPath := c.dstDir + "/" + objFile
c.ObjPathList[filepath.ToSlash(objPath)] = true
cmd, err := c.CompileFileCmd(file, compilerType)
@@ -418,9 +413,9 @@ func (c *Compiler) RecursiveCompile(cType int, ignDirs []string) error {
}
switch cType {
- case 0:
+ case COMPILER_TYPE_C:
return c.CompileC()
- case 1:
+ case COMPILER_TYPE_ASM:
return c.CompileAs()
default:
return NewNewtError("Wrong compiler type specified to RecursiveCompile")
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0a68684d/newt/toolchain/deps.go
----------------------------------------------------------------------
diff --git a/newt/toolchain/deps.go b/newt/toolchain/deps.go
index b2ddc06..01e74de 100644
--- a/newt/toolchain/deps.go
+++ b/newt/toolchain/deps.go
@@ -22,7 +22,6 @@ package toolchain
import (
"bytes"
"io/ioutil"
- "os"
"path/filepath"
"strings"
"time"
@@ -115,11 +114,7 @@ func commandHasChanged(dstFile string, cmd string) bool {
return true
}
- rc := bytes.Compare(prevCmd, []byte(cmd)) != 0
- if rc {
- cli.StatusMessage(util.VERBOSITY_VERBOSE, "oldcmd=%s\nnewcmd=%s\n", prevCmd, cmd)
- }
- return rc
+ return bytes.Compare(prevCmd, []byte(cmd)) != 0
}
// Determines if the specified C or assembly file needs to be built. A compile
@@ -132,13 +127,11 @@ func commandHasChanged(dstFile string, cmd string) bool {
// the object file.
func (tracker *DepTracker) CompileRequired(srcFile string,
compilerType int) (bool, error) {
- wd, _ := os.Getwd()
- objDir := wd + "/obj/" + tracker.compiler.TargetName + "/"
- objFile := objDir + strings.TrimSuffix(srcFile, filepath.Ext(srcFile)) +
- ".o"
- depFile := objDir + strings.TrimSuffix(srcFile, filepath.Ext(srcFile)) +
- ".d"
+ objFile := tracker.compiler.DstDir() + "/" +
+ strings.TrimSuffix(srcFile, filepath.Ext(srcFile)) + ".o"
+ depFile := tracker.compiler.DstDir() + "/" +
+ strings.TrimSuffix(srcFile, filepath.Ext(srcFile)) + ".d"
// If the object was previously built with a different set of options, a
// rebuild is necessary.
@@ -165,7 +158,8 @@ func (tracker *DepTracker) CompileRequired(srcFile string,
// If the object doesn't exist or is older than the source file, a build is
// required; no need to check dependencies.
if srcModTime.After(objModTime) {
- cli.StatusMessage(util.VERBOSITY_VERBOSE, "%s - rebuild required; source newer than obj\n", srcFile)
+ cli.StatusMessage(util.VERBOSITY_VERBOSE, "%s - rebuild required; "+
+ "source newer than obj\n", srcFile)
return true, nil
}