Browse Source

builder: refactor Build function to not use a callback

The only reason a callback was used, was so that the temporary directory
gets removed once `Build` returns. But that is honestly a really bad
reason: the parent function can simply create a temporary function and
remove it when it returns. It wasn't worth the code complexity that this
callback created.

This change should not cause any observable differences in behavior (it
should be a non-functional change).

I have no reason to do this now, but this unclean code has been bugging
me and I just wanted to get it fixed.
pull/3232/head
Ayke van Laethem 2 years ago
committed by Ron Evans
parent
commit
f866d5cc38
  1. 103
      builder/build.go
  2. 69
      main.go

103
builder/build.go

@ -42,8 +42,8 @@ type BuildResult struct {
// information. Used for GDB for example. // information. Used for GDB for example.
Executable string Executable string
// A path to the output binary. It will be removed after Build returns, so // A path to the output binary. It is stored in the tmpdir directory of the
// if it should be kept it must be copied or moved away. // Build function, so if it should be kept it must be copied or moved away.
// It is often the same as Executable, but differs if the output format is // It is often the same as Executable, but differs if the output format is
// .hex for example (instead of the usual ELF). // .hex for example (instead of the usual ELF).
Binary string Binary string
@ -94,23 +94,16 @@ type packageAction struct {
// //
// The error value may be of type *MultiError. Callers will likely want to check // The error value may be of type *MultiError. Callers will likely want to check
// for this case and print such errors individually. // for this case and print such errors individually.
func Build(pkgName, outpath string, config *compileopts.Config, action func(BuildResult) error) error { func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildResult, error) {
// Read the build ID of the tinygo binary. // Read the build ID of the tinygo binary.
// Used as a cache key for package builds. // Used as a cache key for package builds.
compilerBuildID, err := ReadBuildID() compilerBuildID, err := ReadBuildID()
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
// Create a temporary directory for intermediary files.
dir, err := os.MkdirTemp("", "tinygo")
if err != nil {
return err
}
if config.Options.Work { if config.Options.Work {
fmt.Printf("WORK=%s\n", dir) fmt.Printf("WORK=%s\n", tmpdir)
} else {
defer os.RemoveAll(dir)
} }
// Look up the build cache directory, which is used to speed up incremental // Look up the build cache directory, which is used to speed up incremental
@ -119,7 +112,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
if cacheDir == "off" { if cacheDir == "off" {
// Use temporary build directory instead, effectively disabling the // Use temporary build directory instead, effectively disabling the
// build cache. // build cache.
cacheDir = dir cacheDir = tmpdir
} }
// Check for a libc dependency. // Check for a libc dependency.
@ -129,40 +122,40 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
var libcDependencies []*compileJob var libcDependencies []*compileJob
switch config.Target.Libc { switch config.Target.Libc {
case "darwin-libSystem": case "darwin-libSystem":
job := makeDarwinLibSystemJob(config, dir) job := makeDarwinLibSystemJob(config, tmpdir)
libcDependencies = append(libcDependencies, job) libcDependencies = append(libcDependencies, job)
case "musl": case "musl":
job, unlock, err := Musl.load(config, dir) job, unlock, err := Musl.load(config, tmpdir)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
defer unlock() defer unlock()
libcDependencies = append(libcDependencies, dummyCompileJob(filepath.Join(filepath.Dir(job.result), "crt1.o"))) libcDependencies = append(libcDependencies, dummyCompileJob(filepath.Join(filepath.Dir(job.result), "crt1.o")))
libcDependencies = append(libcDependencies, job) libcDependencies = append(libcDependencies, job)
case "picolibc": case "picolibc":
libcJob, unlock, err := Picolibc.load(config, dir) libcJob, unlock, err := Picolibc.load(config, tmpdir)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
defer unlock() defer unlock()
libcDependencies = append(libcDependencies, libcJob) libcDependencies = append(libcDependencies, libcJob)
case "wasi-libc": case "wasi-libc":
path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a") path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a")
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?") return BuildResult{}, errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?")
} }
libcDependencies = append(libcDependencies, dummyCompileJob(path)) libcDependencies = append(libcDependencies, dummyCompileJob(path))
case "mingw-w64": case "mingw-w64":
_, unlock, err := MinGW.load(config, dir) _, unlock, err := MinGW.load(config, tmpdir)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
unlock() unlock()
libcDependencies = append(libcDependencies, makeMinGWExtraLibs(dir)...) libcDependencies = append(libcDependencies, makeMinGWExtraLibs(tmpdir)...)
case "": case "":
// no library specified, so nothing to do // no library specified, so nothing to do
default: default:
return fmt.Errorf("unknown libc: %s", config.Target.Libc) return BuildResult{}, fmt.Errorf("unknown libc: %s", config.Target.Libc)
} }
optLevel, sizeLevel, _ := config.OptLevels() optLevel, sizeLevel, _ := config.OptLevels()
@ -188,7 +181,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// address spaces, etc). // address spaces, etc).
machine, err := compiler.NewTargetMachine(compilerConfig) machine, err := compiler.NewTargetMachine(compilerConfig)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
defer machine.Dispose() defer machine.Dispose()
@ -197,11 +190,11 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
Sizes: compiler.Sizes(machine), Sizes: compiler.Sizes(machine),
}) })
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
err = lprogram.Parse() err = lprogram.Parse()
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
// Create the *ssa.Program. This does not yet build the entire SSA of the // Create the *ssa.Program. This does not yet build the entire SSA of the
@ -270,7 +263,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
} }
} }
job.result, err = createEmbedObjectFile(string(data), hexSum, name, pkg.OriginalDir(), dir, compilerConfig) job.result, err = createEmbedObjectFile(string(data), hexSum, name, pkg.OriginalDir(), tmpdir, compilerConfig)
return err return err
}, },
} }
@ -284,7 +277,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
for _, imported := range pkg.Pkg.Imports() { for _, imported := range pkg.Pkg.Imports() {
job, ok := packageActionIDJobs[imported.Path()] job, ok := packageActionIDJobs[imported.Path()]
if !ok { if !ok {
return fmt.Errorf("package %s imports %s but couldn't find dependency", pkg.ImportPath, imported.Path()) return BuildResult{}, fmt.Errorf("package %s imports %s but couldn't find dependency", pkg.ImportPath, imported.Path())
} }
importedPackages = append(importedPackages, job) importedPackages = append(importedPackages, job)
actionIDDependencies = append(actionIDDependencies, job) actionIDDependencies = append(actionIDDependencies, job)
@ -370,7 +363,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Packages are compiled independently anyway. // Packages are compiled independently anyway.
for _, cgoHeader := range pkg.CGoHeaders { for _, cgoHeader := range pkg.CGoHeaders {
// Store the header text in a temporary file. // Store the header text in a temporary file.
f, err := os.CreateTemp(dir, "cgosnippet-*.c") f, err := os.CreateTemp(tmpdir, "cgosnippet-*.c")
if err != nil { if err != nil {
return err return err
} }
@ -579,17 +572,17 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Run jobs to produce the LLVM module. // Run jobs to produce the LLVM module.
err := runJobs(programJob, config.Options.Semaphore) err := runJobs(programJob, config.Options.Semaphore)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
// Generate output. // Generate output.
switch outext { switch outext {
case ".o": case ".o":
llvmBuf, err := machine.EmitToMemoryBuffer(mod, llvm.ObjectFile) llvmBuf, err := machine.EmitToMemoryBuffer(mod, llvm.ObjectFile)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
defer llvmBuf.Dispose() defer llvmBuf.Dispose()
return os.WriteFile(outpath, llvmBuf.Bytes(), 0666) return BuildResult{}, os.WriteFile(outpath, llvmBuf.Bytes(), 0666)
case ".bc": case ".bc":
var buf llvm.MemoryBuffer var buf llvm.MemoryBuffer
if config.UseThinLTO() { if config.UseThinLTO() {
@ -598,10 +591,10 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
buf = llvm.WriteBitcodeToMemoryBuffer(mod) buf = llvm.WriteBitcodeToMemoryBuffer(mod)
} }
defer buf.Dispose() defer buf.Dispose()
return os.WriteFile(outpath, buf.Bytes(), 0666) return BuildResult{}, os.WriteFile(outpath, buf.Bytes(), 0666)
case ".ll": case ".ll":
data := []byte(mod.String()) data := []byte(mod.String())
return os.WriteFile(outpath, data, 0666) return BuildResult{}, os.WriteFile(outpath, data, 0666)
default: default:
panic("unreachable") panic("unreachable")
} }
@ -612,7 +605,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// run all jobs in parallel as far as possible. // run all jobs in parallel as far as possible.
// Add job to write the output object file. // Add job to write the output object file.
objfile := filepath.Join(dir, "main.o") objfile := filepath.Join(tmpdir, "main.o")
outputObjectFileJob := &compileJob{ outputObjectFileJob := &compileJob{
description: "generate output file", description: "generate output file",
dependencies: []*compileJob{programJob}, dependencies: []*compileJob{programJob},
@ -635,7 +628,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Prepare link command. // Prepare link command.
linkerDependencies := []*compileJob{outputObjectFileJob} linkerDependencies := []*compileJob{outputObjectFileJob}
executable := filepath.Join(dir, "main") executable := filepath.Join(tmpdir, "main")
if config.GOOS() == "windows" { if config.GOOS() == "windows" {
executable += ".exe" executable += ".exe"
} }
@ -645,9 +638,9 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// Add compiler-rt dependency if needed. Usually this is a simple load from // Add compiler-rt dependency if needed. Usually this is a simple load from
// a cache. // a cache.
if config.Target.RTLib == "compiler-rt" { if config.Target.RTLib == "compiler-rt" {
job, unlock, err := CompilerRT.load(config, dir) job, unlock, err := CompilerRT.load(config, tmpdir)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
defer unlock() defer unlock()
linkerDependencies = append(linkerDependencies, job) linkerDependencies = append(linkerDependencies, job)
@ -661,7 +654,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
job := &compileJob{ job := &compileJob{
description: "compile extra file " + path, description: "compile extra file " + path,
run: func(job *compileJob) error { run: func(job *compileJob) error {
result, err := compileAndCacheCFile(abspath, dir, config.CFlags(), config.UseThinLTO(), config.Options.PrintCommands) result, err := compileAndCacheCFile(abspath, tmpdir, config.CFlags(), config.UseThinLTO(), config.Options.PrintCommands)
job.result = result job.result = result
return err return err
}, },
@ -679,7 +672,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
job := &compileJob{ job := &compileJob{
description: "compile CGo file " + abspath, description: "compile CGo file " + abspath,
run: func(job *compileJob) error { run: func(job *compileJob) error {
result, err := compileAndCacheCFile(abspath, dir, pkg.CFlags, config.UseThinLTO(), config.Options.PrintCommands) result, err := compileAndCacheCFile(abspath, tmpdir, pkg.CFlags, config.UseThinLTO(), config.Options.PrintCommands)
job.result = result job.result = result
return err return err
}, },
@ -722,7 +715,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
ldflags = append(ldflags, "--strip-debug") ldflags = append(ldflags, "--strip-debug")
} else { } else {
// Other linkers may have different flags. // Other linkers may have different flags.
return errors.New("cannot remove debug information: unknown linker: " + config.Target.Linker) return BuildResult{}, errors.New("cannot remove debug information: unknown linker: " + config.Target.Linker)
} }
} }
@ -879,7 +872,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
// is simpler and cannot be parallelized. // is simpler and cannot be parallelized.
err = runJobs(linkJob, config.Options.Semaphore) err = runJobs(linkJob, config.Options.Semaphore)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
// Get an Intel .hex file or .bin file from the .elf file. // Get an Intel .hex file or .bin file from the .elf file.
@ -890,40 +883,40 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
case "hex", "bin": case "hex", "bin":
// Extract raw binary, either encoding it as a hex file or as a raw // Extract raw binary, either encoding it as a hex file or as a raw
// firmware file. // firmware file.
tmppath = filepath.Join(dir, "main"+outext) tmppath = filepath.Join(tmpdir, "main"+outext)
err := objcopy(executable, tmppath, outputBinaryFormat) err := objcopy(executable, tmppath, outputBinaryFormat)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
case "uf2": case "uf2":
// Get UF2 from the .elf file. // Get UF2 from the .elf file.
tmppath = filepath.Join(dir, "main"+outext) tmppath = filepath.Join(tmpdir, "main"+outext)
err := convertELFFileToUF2File(executable, tmppath, config.Target.UF2FamilyID) err := convertELFFileToUF2File(executable, tmppath, config.Target.UF2FamilyID)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
case "esp32", "esp32-img", "esp32c3", "esp8266": case "esp32", "esp32-img", "esp32c3", "esp8266":
// Special format for the ESP family of chips (parsed by the ROM // Special format for the ESP family of chips (parsed by the ROM
// bootloader). // bootloader).
tmppath = filepath.Join(dir, "main"+outext) tmppath = filepath.Join(tmpdir, "main"+outext)
err := makeESPFirmareImage(executable, tmppath, outputBinaryFormat) err := makeESPFirmareImage(executable, tmppath, outputBinaryFormat)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
case "nrf-dfu": case "nrf-dfu":
// special format for nrfutil for Nordic chips // special format for nrfutil for Nordic chips
tmphexpath := filepath.Join(dir, "main.hex") tmphexpath := filepath.Join(tmpdir, "main.hex")
err := objcopy(executable, tmphexpath, "hex") err := objcopy(executable, tmphexpath, "hex")
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
tmppath = filepath.Join(dir, "main"+outext) tmppath = filepath.Join(tmpdir, "main"+outext)
err = makeDFUFirmwareImage(config.Options, tmphexpath, tmppath) err = makeDFUFirmwareImage(config.Options, tmphexpath, tmppath)
if err != nil { if err != nil {
return err return BuildResult{}, err
} }
default: default:
return fmt.Errorf("unknown output binary format: %s", outputBinaryFormat) return BuildResult{}, fmt.Errorf("unknown output binary format: %s", outputBinaryFormat)
} }
// If there's a module root, use that. // If there's a module root, use that.
@ -933,13 +926,13 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
moduleroot = lprogram.MainPkg().Root moduleroot = lprogram.MainPkg().Root
} }
return action(BuildResult{ return BuildResult{
Executable: executable, Executable: executable,
Binary: tmppath, Binary: tmppath,
MainDir: lprogram.MainPkg().Dir, MainDir: lprogram.MainPkg().Dir,
ModuleRoot: moduleroot, ModuleRoot: moduleroot,
ImportPath: lprogram.MainPkg().ImportPath, ImportPath: lprogram.MainPkg().ImportPath,
}) }, nil
} }
// createEmbedObjectFile creates a new object file with the given contents, for // createEmbedObjectFile creates a new object file with the given contents, for

69
main.go

@ -155,7 +155,19 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
return nil return nil
} }
return builder.Build(pkgName, outpath, config, func(result builder.BuildResult) error { // Create a temporary directory for intermediary files.
tmpdir, err := os.MkdirTemp("", "tinygo")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
// Do the build.
result, err := builder.Build(pkgName, outpath, tmpdir, config)
if err != nil {
return err
}
if outpath == "" { if outpath == "" {
if strings.HasSuffix(pkgName, ".go") { if strings.HasSuffix(pkgName, ".go") {
// A Go file was specified directly on the command line. // A Go file was specified directly on the command line.
@ -187,12 +199,11 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
// Check whether file writing was successful. // Check whether file writing was successful.
return outf.Close() return outf.Close()
} else { }
// Move was successful. // Move was successful.
return nil return nil
} }
})
}
// Test runs the tests in the given package. Returns whether the test passed and // Test runs the tests in the given package. Returns whether the test passed and
// possibly an error if the test failed to run. // possibly an error if the test failed to run.
@ -371,7 +382,19 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
return errors.New("unknown flash method: " + flashMethod) return errors.New("unknown flash method: " + flashMethod)
} }
return builder.Build(pkgName, fileExt, config, func(result builder.BuildResult) error { // Create a temporary directory for intermediary files.
tmpdir, err := os.MkdirTemp("", "tinygo")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
// Build the binary.
result, err := builder.Build(pkgName, fileExt, tmpdir, config)
if err != nil {
return err
}
// do we need port reset to put MCU into bootloader mode? // do we need port reset to put MCU into bootloader mode?
if config.Target.PortReset == "true" && flashMethod != "openocd" { if config.Target.PortReset == "true" && flashMethod != "openocd" {
port, err := getDefaultPort(port, config.Target.SerialPort) port, err := getDefaultPort(port, config.Target.SerialPort)
@ -385,7 +408,7 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
} }
} }
// this flashing method copies the binary data to a Mass Storage Device (msd) // Flash the binary to the MCU.
switch flashMethod { switch flashMethod {
case "", "command": case "", "command":
// Create the command. // Create the command.
@ -424,6 +447,7 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
return &commandError{"failed to flash", result.Binary, err} return &commandError{"failed to flash", result.Binary, err}
} }
case "msd": case "msd":
// this flashing method copies the binary data to a Mass Storage Device (msd)
switch fileExt { switch fileExt {
case ".uf2": case ".uf2":
err := flashUF2UsingMSD(config.Target.FlashVolume, result.Binary, config.Options) err := flashUF2UsingMSD(config.Target.FlashVolume, result.Binary, config.Options)
@ -480,7 +504,6 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
return Monitor("", options) return Monitor("", options)
} }
return nil return nil
})
} }
// Debug compiles and flashes a program to a microcontroller (just like Flash) // Debug compiles and flashes a program to a microcontroller (just like Flash)
@ -506,8 +529,20 @@ func Debug(debugger, pkgName string, ocdOutput bool, options *compileopts.Option
return err return err
} }
// Create a temporary directory for intermediary files.
tmpdir, err := os.MkdirTemp("", "tinygo")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
// Build the binary to debug.
format, fileExt := config.EmulatorFormat() format, fileExt := config.EmulatorFormat()
return builder.Build(pkgName, fileExt, config, func(result builder.BuildResult) error { result, err := builder.Build(pkgName, fileExt, tmpdir, config)
if err != nil {
return err
}
// Find a good way to run GDB. // Find a good way to run GDB.
gdbInterface, openocdInterface := config.Programmer() gdbInterface, openocdInterface := config.Programmer()
switch gdbInterface { switch gdbInterface {
@ -694,7 +729,6 @@ func Debug(debugger, pkgName string, ocdOutput bool, options *compileopts.Option
return &commandError{"failed to run " + cmdName + " with", result.Executable, err} return &commandError{"failed to run " + cmdName + " with", result.Executable, err}
} }
return nil return nil
})
} }
// Run compiles and runs the given program. Depending on the target provided in // Run compiles and runs the given program. Depending on the target provided in
@ -767,8 +801,20 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
env = environmentVars env = environmentVars
} }
// Create a temporary directory for intermediary files.
tmpdir, err := os.MkdirTemp("", "tinygo")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
// Build the binary to be run.
format, fileExt := config.EmulatorFormat() format, fileExt := config.EmulatorFormat()
return builder.Build(pkgName, fileExt, config, func(result builder.BuildResult) error { result, err := builder.Build(pkgName, fileExt, tmpdir, config)
if err != nil {
return err
}
// If needed, set a timeout on the command. This is done in tests so // If needed, set a timeout on the command. This is done in tests so
// they don't waste resources on a stalled test. // they don't waste resources on a stalled test.
var ctx context.Context var ctx context.Context
@ -820,7 +866,7 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
if config.Options.PrintCommands != nil { if config.Options.PrintCommands != nil {
config.Options.PrintCommands(cmd.Path, cmd.Args...) config.Options.PrintCommands(cmd.Path, cmd.Args...)
} }
err := run(cmd, result) err = run(cmd, result)
if err != nil { if err != nil {
if ctx != nil && ctx.Err() == context.DeadlineExceeded { if ctx != nil && ctx.Err() == context.DeadlineExceeded {
stdout.Write([]byte(fmt.Sprintf("--- timeout of %s exceeded, terminating...\n", timeout))) stdout.Write([]byte(fmt.Sprintf("--- timeout of %s exceeded, terminating...\n", timeout)))
@ -829,7 +875,6 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
return &commandError{"failed to run compiled binary", result.Binary, err} return &commandError{"failed to run compiled binary", result.Binary, err}
} }
return nil return nil
})
} }
func touchSerialPortAt1200bps(port string) (err error) { func touchSerialPortAt1200bps(port string) (err error) {

Loading…
Cancel
Save