From 3dbc4d52105f4209ece1332f0272f293745ac0bf Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 21 Oct 2022 19:41:47 +0200 Subject: [PATCH] main: fix outputting .ll files This command didn't work anymore since the refactor in #3211. The reason it doesn't work anymore is that before the refactor, the code in the callback wasn't executed for .ll, .bc and .o files but after the refactor it is, which causes a spurious error. This commit fixes this oversight. Example that didn't work anymore and is fixed with this change: tinygo build -o test.ll examples/serial --- main.go | 61 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index b6b22e31..31fb9f24 100644 --- a/main.go +++ b/main.go @@ -168,37 +168,44 @@ func Build(pkgName, outpath string, options *compileopts.Options) error { return err } - if outpath == "" { - if strings.HasSuffix(pkgName, ".go") { - // A Go file was specified directly on the command line. - // Base the binary name off of it. - outpath = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension() - } else { - // Pick a default output path based on the main directory. - outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension() - } - } + if result.Binary != "" { + // If result.Binary is set, it means there is a build output (elf, hex, + // etc) that we need to move to the outpath. If it isn't set, it means + // the build output was a .ll, .bc or .o file that has already been + // written to outpath and so we don't need to do anything. - if err := os.Rename(result.Binary, outpath); err != nil { - // Moving failed. Do a file copy. - inf, err := os.Open(result.Binary) - if err != nil { - return err - } - defer inf.Close() - outf, err := os.OpenFile(outpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) - if err != nil { - return err + if outpath == "" { + if strings.HasSuffix(pkgName, ".go") { + // A Go file was specified directly on the command line. + // Base the binary name off of it. + outpath = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension() + } else { + // Pick a default output path based on the main directory. + outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension() + } } - // Copy data to output file. - _, err = io.Copy(outf, inf) - if err != nil { - return err - } + if err := os.Rename(result.Binary, outpath); err != nil { + // Moving failed. Do a file copy. + inf, err := os.Open(result.Binary) + if err != nil { + return err + } + defer inf.Close() + outf, err := os.OpenFile(outpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) + if err != nil { + return err + } - // Check whether file writing was successful. - return outf.Close() + // Copy data to output file. + _, err = io.Copy(outf, inf) + if err != nil { + return err + } + + // Check whether file writing was successful. + return outf.Close() + } } // Move was successful.