From 5b34713d41b7cbf091da20cd85b061fbadb794f3 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 16 Apr 2019 15:49:31 +0200 Subject: [PATCH] main: fix linker script includes when running outside TINYGOROOT This commit adds the TinyGo root directory (`TINYGOROOT`) to the linker script `-L` search path, so that linker scripts can be found when running `tinygo` outside of the TinyGo root. This was already working before when using an external linker by setting the working directory, but this is not possible when using the internal linker. However, by adding the root directory to the linker search path (`-L`), it can now find these linker scripts. fixes #265 --- linker-builtin.go | 3 +-- linker-external.go | 3 +-- main.go | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/linker-builtin.go b/linker-builtin.go index ba63f827..c8987938 100644 --- a/linker-builtin.go +++ b/linker-builtin.go @@ -22,7 +22,7 @@ import "C" // Link invokes a linker with the given name and flags. // // This version uses the built-in linker when trying to use lld. -func Link(dir, linker string, flags ...string) error { +func Link(linker string, flags ...string) error { switch linker { case "ld.lld", commands["ld.lld"]: flags = append([]string{"tinygo:" + linker}, flags...) @@ -60,7 +60,6 @@ func Link(dir, linker string, flags ...string) error { cmd := exec.Command(linker, flags...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - cmd.Dir = dir return cmd.Run() } } diff --git a/linker-external.go b/linker-external.go index d0c8126a..446b6808 100644 --- a/linker-external.go +++ b/linker-external.go @@ -13,10 +13,9 @@ import ( // Link invokes a linker with the given name and arguments. // // This version always runs the linker as an external command. -func Link(dir, linker string, flags ...string) error { +func Link(linker string, flags ...string) error { cmd := exec.Command(linker, flags...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - cmd.Dir = dir return cmd.Run() } diff --git a/main.go b/main.go index 1ac46f4d..28cf34f3 100644 --- a/main.go +++ b/main.go @@ -191,7 +191,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act // Prepare link command. executable := filepath.Join(dir, "main") tmppath := executable // final file - ldflags := append(spec.LDFlags, "-o", executable, objfile) + ldflags := append(spec.LDFlags, "-o", executable, objfile, "-L", sourceDir()) if spec.RTLib == "compiler-rt" { ldflags = append(ldflags, librt) } @@ -229,9 +229,9 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act // Link the object files together. if linker, ok := commands[spec.Linker]; ok { - err = Link(sourceDir(), linker, ldflags...) + err = Link(linker, ldflags...) } else { - err = Link(sourceDir(), spec.Linker, ldflags...) + err = Link(spec.Linker, ldflags...) } if err != nil { return &commandError{"failed to link", executable, err}