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
Этот коммит содержится в:
Ayke van Laethem 2022-10-21 19:41:47 +02:00 коммит произвёл Ron Evans
родитель 42e4a75319
коммит 3dbc4d5210

61
main.go
Просмотреть файл

@ -168,37 +168,44 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
return err return err
} }
if outpath == "" { if result.Binary != "" {
if strings.HasSuffix(pkgName, ".go") { // If result.Binary is set, it means there is a build output (elf, hex,
// A Go file was specified directly on the command line. // etc) that we need to move to the outpath. If it isn't set, it means
// Base the binary name off of it. // the build output was a .ll, .bc or .o file that has already been
outpath = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension() // written to outpath and so we don't need to do anything.
} else {
// Pick a default output path based on the main directory.
outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension()
}
}
if err := os.Rename(result.Binary, outpath); err != nil { if outpath == "" {
// Moving failed. Do a file copy. if strings.HasSuffix(pkgName, ".go") {
inf, err := os.Open(result.Binary) // A Go file was specified directly on the command line.
if err != nil { // Base the binary name off of it.
return err outpath = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension()
} } else {
defer inf.Close() // Pick a default output path based on the main directory.
outf, err := os.OpenFile(outpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension()
if err != nil { }
return err
} }
// Copy data to output file. if err := os.Rename(result.Binary, outpath); err != nil {
_, err = io.Copy(outf, inf) // Moving failed. Do a file copy.
if err != nil { inf, err := os.Open(result.Binary)
return err 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. // Copy data to output file.
return outf.Close() _, err = io.Copy(outf, inf)
if err != nil {
return err
}
// Check whether file writing was successful.
return outf.Close()
}
} }
// Move was successful. // Move was successful.