Skip to content

Commit

Permalink
ref: move string literals to constants
Browse files Browse the repository at this point in the history
Signed-off-by: leongross <leon.gross@9elements.com>
  • Loading branch information
leongross committed Sep 19, 2024
1 parent b5626e7 commit d266a4c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 66 deletions.
90 changes: 47 additions & 43 deletions compileopts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func (c *Config) GC() string {
if c.Target.GC != "" {
return c.Target.GC
}
return "conservative"
return GCConservative
}

// NeedsStackObjects returns true if the compiler should insert stack objects
// that can be traced by the garbage collector.
func (c *Config) NeedsStackObjects() bool {
switch c.GC() {
case "conservative", "custom", "precise":
case GCConservative, GCCustom, GCPrecise:
for _, tag := range c.BuildTags() {
if tag == "tinygo.wasm" {
return true
Expand All @@ -134,7 +134,7 @@ func (c *Config) Scheduler() string {
return c.Target.Scheduler
}
// Fall back to none.
return "none"
return SchedulerNone
}

// Serial returns the serial implementation for this build configuration: uart,
Expand All @@ -146,22 +146,22 @@ func (c *Config) Serial() string {
if c.Target.Serial != "" {
return c.Target.Serial
}
return "none"
return SerialNone
}

// OptLevels returns the optimization level (0-2), size level (0-2), and inliner
// threshold as used in the LLVM optimization pipeline.
func (c *Config) OptLevel() (level string, speedLevel, sizeLevel int) {
switch c.Options.Opt {
case "none", "0":
case OptNone, "0":
return "O0", 0, 0
case "1":
case Opt1:
return "O1", 1, 0
case "2":
case Opt2:
return "O2", 2, 0
case "s":
case Opts:
return "Os", 2, 1
case "z":
case Optz:
return "Oz", 2, 2 // default
default:
// This is not shown to the user: valid choices are already checked as
Expand All @@ -181,7 +181,7 @@ func (c *Config) PanicStrategy() string {
// automatically at compile time, if possible. If it is false, no attempt is
// made.
func (c *Config) AutomaticStackSize() bool {
if c.Target.AutoStackSize != nil && c.Scheduler() == "tasks" {
if c.Target.AutoStackSize != nil && c.Scheduler() == SchedulerTasks {
return *c.Target.AutoStackSize
}
return false
Expand Down Expand Up @@ -218,10 +218,10 @@ func (c *Config) RP2040BootPatch() bool {
// vs thumb* vs arm64.
func CanonicalArchName(triple string) string {
arch := strings.Split(triple, "-")[0]
if arch == "arm64" {
if arch == ArchArm64 {
return "aarch64"
}
if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") {
if strings.HasPrefix(arch, ArchArm) || strings.HasPrefix(arch, "thumb") {
return "arm"
}
if arch == "mipsel" {
Expand Down Expand Up @@ -252,7 +252,7 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {
}

// Try to load a precompiled library.
precompiledDir := filepath.Join(goenv.Get("TINYGOROOT"), "pkg", archname, name)
precompiledDir := filepath.Join(goenv.Get(TinyGoRoot), "pkg", archname, name)
if _, err := os.Stat(precompiledDir); err == nil {
// Found a precompiled library for this OS/architecture. Return the path
// directly.
Expand All @@ -261,38 +261,38 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {

// No precompiled library found. Determine the path name that will be used
// in the build cache.
return filepath.Join(goenv.Get("GOCACHE"), name+"-"+archname), false
return filepath.Join(goenv.Get(GolangCache), name+"-"+archname), false
}

// DefaultBinaryExtension returns the default extension for binaries, such as
// .exe, .wasm, or no extension (depending on the target).
// .exe, .wasm, .elf or no extension (depending on the target).
func (c *Config) DefaultBinaryExtension() string {
parts := strings.Split(c.Triple(), "-")
if parts[0] == "wasm32" {
// WebAssembly files always have the .wasm file extension.
return ".wasm"
return "." + BinExtWasm
}
if len(parts) >= 3 && parts[2] == "windows" {
if len(parts) >= 3 && parts[2] == OsWindows {
// Windows uses .exe.
return ".exe"
return "." + BinExtExe
}
if len(parts) >= 3 && parts[2] == "unknown" {
// There appears to be a convention to use the .elf file extension for
// ELF files intended for microcontrollers. I'm not aware of the origin
// of this, it's just something that is used by many projects.
// I think it's a good tradition, so let's keep it.
return ".elf"
return "." + BinExtElf
}
// Linux, MacOS, etc, don't use a file extension. Use it as a fallback.
return ""
return BinExtNone
}

// CFlags returns the flags to pass to the C compiler. This is necessary for CGo
// preprocessing.
func (c *Config) CFlags(libclang bool) []string {
var cflags []string
for _, flag := range c.Target.CFlags {
cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get(TinyGoRoot)))
}
resourceDir := goenv.ClangResourceDir(libclang)
if resourceDir != "" {
Expand All @@ -306,13 +306,13 @@ func (c *Config) CFlags(libclang bool) []string {
}
switch c.Target.Libc {
case "darwin-libSystem":
root := goenv.Get("TINYGOROOT")
root := goenv.Get(TinyGoRoot)
cflags = append(cflags,
"-nostdlibinc",
"-isystem", filepath.Join(root, "lib/macos-minimal-sdk/src/usr/include"),
)
case "picolibc":
root := goenv.Get("TINYGOROOT")
root := goenv.Get(TinyGoRoot)
picolibcDir := filepath.Join(root, "lib", "picolibc", "newlib", "libc")
path, _ := c.LibcPath("picolibc")
cflags = append(cflags,
Expand All @@ -322,7 +322,7 @@ func (c *Config) CFlags(libclang bool) []string {
"-isystem", filepath.Join(picolibcDir, "tinystdio"),
)
case "musl":
root := goenv.Get("TINYGOROOT")
root := goenv.Get(TinyGoRoot)
path, _ := c.LibcPath("musl")
arch := MuslArchitecture(c.Triple())
cflags = append(cflags,
Expand All @@ -332,14 +332,14 @@ func (c *Config) CFlags(libclang bool) []string {
"-isystem", filepath.Join(root, "lib", "musl", "include"),
)
case "wasi-libc":
root := goenv.Get("TINYGOROOT")
root := goenv.Get(TinyGoRoot)
cflags = append(cflags,
"-nostdlibinc",
"-isystem", root+"/lib/wasi-libc/sysroot/include")
case "wasmbuiltins":
// nothing to add (library is purely for builtins)
case "mingw-w64":
root := goenv.Get("TINYGOROOT")
root := goenv.Get(TinyGoRoot)
path, _ := c.LibcPath("mingw-w64")
cflags = append(cflags,
"-nostdlibinc",
Expand Down Expand Up @@ -385,7 +385,7 @@ func (c *Config) CFlags(libclang bool) []string {
// (like the one for the compiler runtime), but this represents the majority of
// the flags.
func (c *Config) LDFlags() []string {
root := goenv.Get("TINYGOROOT")
root := goenv.Get(TinyGoRoot)
// Merge and adjust LDFlags.
var ldflags []string
for _, flag := range c.Target.LDFlags {
Expand Down Expand Up @@ -426,37 +426,41 @@ func (c *Config) Debug() bool {
// BinaryFormat returns an appropriate binary format, based on the file
// extension and the configured binary format in the target JSON file.
func (c *Config) BinaryFormat(ext string) string {
if len(ext) > 1 {
ext = ext[1:] // remove leading '.'
}

switch ext {
case ".bin", ".gba", ".nro":
case BinFormatBin, BinFormatGba, BinFormatNro:
// The simplest format possible: dump everything in a raw binary file.
if c.Target.BinaryFormat != "" {
return c.Target.BinaryFormat
}
return "bin"
case ".img":
return BinFormatBin
case BinFormatImg:
// Image file. Only defined for the ESP32 at the moment, where it is a
// full (runnable) image that can be used in the Espressif QEMU fork.
if c.Target.BinaryFormat != "" {
return c.Target.BinaryFormat + "-img"
return c.Target.BinaryFormat + "-" + BinFormatImg
}
return "bin"
case ".hex":
return BinFormatBin
case BinFormatHex:
// Similar to bin, but includes the start address and is thus usually a
// better format.
return "hex"
case ".uf2":
return BinFormatHex
case BinFormatUf2:
// Special purpose firmware format, mainly used on Adafruit boards.
// More information:
// https://github.com/Microsoft/uf2
return "uf2"
case ".zip":
return BinFormatUf2
case BinFormatZip:
if c.Target.BinaryFormat != "" {
return c.Target.BinaryFormat
}
return "zip"
return BinFormatZip
default:
// Use the ELF format for unrecognized file formats.
return "elf"
return BinExtElf
}
}

Expand All @@ -468,16 +472,16 @@ func (c *Config) Programmer() (method, openocdInterface string) {
case "":
// No configuration supplied.
return c.Target.FlashMethod, c.Target.OpenOCDInterface
case "openocd", "msd", "command":
case ProgOpenOCD, ProgMSD, ProgCommand:
// The -programmer flag only specifies the flash method.
return c.Options.Programmer, c.Target.OpenOCDInterface
case "bmp":
case ProgBMP:
// The -programmer flag only specifies the flash method.
return c.Options.Programmer, ""
default:
// The -programmer flag specifies something else, assume it specifies
// the OpenOCD interface name.
return "openocd", c.Options.Programmer
return ProgOpenOCD, c.Options.Programmer
}
}

Expand Down Expand Up @@ -570,7 +574,7 @@ func (c *Config) Emulator(format, binary string) ([]string, error) {
}
var emulator []string
for _, s := range parts {
s = strings.ReplaceAll(s, "{root}", goenv.Get("TINYGOROOT"))
s = strings.ReplaceAll(s, "{root}", goenv.Get(TinyGoRoot))
// Allow replacement of what's usually /tmp except notably Windows.
s = strings.ReplaceAll(s, "{tmpDir}", os.TempDir())
s = strings.ReplaceAll(s, "{"+format+"}", binary)
Expand Down
26 changes: 13 additions & 13 deletions compileopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

var (
validGCOptions = []string{"none", "leaking", "conservative", "custom", "precise"}
validSchedulerOptions = []string{"none", "tasks", "asyncify"}
validSerialOptions = []string{"none", "uart", "usb", "rtt"}
validPrintSizeOptions = []string{"none", "short", "full"}
validPanicStrategyOptions = []string{"print", "trap"}
validOptOptions = []string{"none", "0", "1", "2", "s", "z"}
validGCOptions = []string{GCNone, GCLeaking, GCConservative, GCCustom, GCPrecise}

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: GCNone

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: GCLeaking

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: GCConservative

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: GCCustom

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: GCPrecise

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: GCNone

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: GCLeaking

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: GCConservative

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: GCCustom

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: GCPrecise

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: GCNone

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: GCLeaking

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: GCConservative

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: GCCustom

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: GCPrecise

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: GCNone

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: GCLeaking

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: GCConservative

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: GCCustom

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: GCPrecise

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: GCNone

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: GCLeaking

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: GCConservative

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: GCCustom

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: GCPrecise

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: GCNone

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: GCLeaking

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: GCConservative

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: GCCustom

Check failure on line 11 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: GCPrecise
validSchedulerOptions = []string{SchedulerNone, SchedulerTasks, SchedulerAsyncify}

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: SchedulerNone

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: SchedulerTasks

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: SchedulerAsyncify

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: SchedulerNone

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: SchedulerTasks

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: SchedulerAsyncify

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: SchedulerNone

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: SchedulerTasks

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: SchedulerAsyncify

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: SchedulerNone

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: SchedulerTasks

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: SchedulerAsyncify

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: SchedulerNone

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: SchedulerTasks

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: SchedulerAsyncify

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: SchedulerNone

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: SchedulerTasks

Check failure on line 12 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: SchedulerAsyncify
validSerialOptions = []string{SerialNone, SerialUART, SerialUSB, SerialRTT}

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: SerialNone

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (17)

undefined: SerialUART

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: SerialNone

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (16)

undefined: SerialUART

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: SerialNone

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / homebrew-install (18)

undefined: SerialUART

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: SerialNone

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

undefined: SerialUART

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: SerialNone

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

undefined: SerialUART

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: SerialNone

Check failure on line 13 in compileopts/options.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: SerialUART
validPrintSizeOptions = []string{SizeNone, SizeShort, SizeFull}
validPanicStrategyOptions = []string{PanicPrint, PanicTrap}
validOptOptions = []string{OptNone, Opt1, Opt2, Opt3, Opts, Optz}
)

// Options contains extra options to give to the compiler. These options are
Expand All @@ -26,18 +26,18 @@ type Options struct {
GOMIPS string // environment variable (only used with GOARCH=mips and GOARCH=mipsle)
Directory string // working dir, leave it unset to use the current working dir
Target string
Opt string
GC string
PanicStrategy string
Opt string // optimization level. may be O0, O1, O2, O3, Os, or Oz
GC string // garbage collection strategy. may be
PanicStrategy string // panic strategy. may be print, or trap
Scheduler string
StackSize uint64 // goroutine stack size (if none could be automatically determined)
Serial string
Work bool // -work flag to print temporary build directory
InterpTimeout time.Duration
PrintIR bool
DumpSSA bool
VerifyIR bool
SkipDWARF bool
PrintIR bool // provide the build in llvm intermediate representation (LLVM-IR)
DumpSSA bool // provide the build in single static assignment (ssa)
VerifyIR bool // verify the generate IR via llvm.VerifyModule
SkipDWARF bool // do not generate DWARF debug information
PrintCommands func(cmd string, args ...string) `json:"-"`
Semaphore chan struct{} `json:"-"` // -p flag controls cap
Debug bool
Expand Down
4 changes: 2 additions & 2 deletions compileopts/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func TestVerifyOptions(t *testing.T) {
{
name: "SchedulerOptionNone",
opts: compileopts.Options{
Scheduler: "none",
Scheduler: compileopts.SchedulerNone,
},
},
{
name: "SchedulerOptionTasks",
opts: compileopts.Options{
Scheduler: "tasks",
Scheduler: compileopts.SchedulerTasks,
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
return nil, fmt.Errorf("%s : %w", options.Target, err)
}

if spec.Scheduler == "asyncify" {
if spec.Scheduler == SchedulerAsyncify {
spec.ExtraFiles = append(spec.ExtraFiles, "src/internal/task/task_asyncify_wasm.S")
}

Expand Down Expand Up @@ -246,7 +246,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
GOARCH: options.GOARCH,
BuildTags: []string{options.GOOS, options.GOARCH},
GC: "precise",
Scheduler: "tasks",
Scheduler: SchedulerTasks,
Linker: "cc",
DefaultStackSize: 1024 * 64, // 64kB
GDB: []string{"gdb"},
Expand Down Expand Up @@ -440,7 +440,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
)
case "wasip1":
spec.GC = "" // use default GC
spec.Scheduler = "asyncify"
spec.Scheduler = SchedulerAsyncify
spec.Linker = "wasm-ld"
spec.RTLib = "compiler-rt"
spec.Libc = "wasi-libc"
Expand Down
Loading

0 comments on commit d266a4c

Please sign in to comment.