From 84618c45eb75958283db84a4763ac0a16b2fbb1a Mon Sep 17 00:00:00 2001 From: m-chichikalov Date: Thu, 13 Jun 2019 20:41:18 -0400 Subject: [PATCH] Added supporting suctom TargetSpec json file via -target flag. --- main.go | 2 +- target.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 8d240c06..3c331523 100644 --- a/main.go +++ b/main.go @@ -588,8 +588,8 @@ func main() { panicStrategy := flag.String("panic", "print", "panic strategy (abort, trap)") printIR := flag.Bool("printir", false, "print LLVM IR") dumpSSA := flag.Bool("dumpssa", false, "dump internal Go SSA") - target := flag.String("target", "", "LLVM target") tags := flag.String("tags", "", "a space-separated list of extra build tags") + target := flag.String("target", "", "LLVM target | .json file with TargetSpec") printSize := flag.String("size", "", "print sizes (none, short, full)") nodebug := flag.Bool("no-debug", false, "disable DWARF debug symbol generation") ocdOutput := flag.Bool("ocd-output", false, "print OCD daemon output during debug") diff --git a/target.go b/target.go index d732e716..53255f8b 100644 --- a/target.go +++ b/target.go @@ -104,10 +104,18 @@ func (spec *TargetSpec) load(r io.Reader) error { return nil } -// loadFromName loads the given target from the targets/ directory inside the -// compiler sources. -func (spec *TargetSpec) loadFromName(name string) error { - path := filepath.Join(sourceDir(), "targets", strings.ToLower(name)+".json") +// loadFromGivenStr loads the TargetSpec from the given string that could be: +// - targets/ directory inside the compiler sources +// - a relative or absolute path to custom (project specific) target specification .json file; +// the Inherits[] could contain the files from target folder (ex. stm32f4disco) +// as well as path to custom files (ex. myAwesomeProject.json) +func (spec *TargetSpec) loadFromGivenStr(str string) error { + path := "" + if strings.HasSuffix(str, ".json") { + path, _ = filepath.Abs(str) + } else { + path = filepath.Join(sourceDir(), "targets", strings.ToLower(str)+".json") + } fp, err := os.Open(path) if err != nil { return err @@ -122,7 +130,7 @@ func (spec *TargetSpec) resolveInherits() error { newSpec := &TargetSpec{} for _, name := range spec.Inherits { subtarget := &TargetSpec{} - err := subtarget.loadFromName(name) + err := subtarget.loadFromGivenStr(name) if err != nil { return err } @@ -172,7 +180,7 @@ func LoadTarget(target string) (*TargetSpec, error) { // See whether there is a target specification for this target (e.g. // Arduino). spec := &TargetSpec{} - err := spec.loadFromName(target) + err := spec.loadFromGivenStr(target) if err == nil { // Successfully loaded this target from a built-in .json file. Make sure // it includes all parents as specified in the "inherits" key.