Skip to content

Commit d266a4c

Browse files
committed
ref: move string literals to constants
Signed-off-by: leongross <[email protected]>
1 parent b5626e7 commit d266a4c

File tree

5 files changed

+71
-66
lines changed

5 files changed

+71
-66
lines changed

compileopts/config.go

+47-43
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ func (c *Config) GC() string {
104104
if c.Target.GC != "" {
105105
return c.Target.GC
106106
}
107-
return "conservative"
107+
return GCConservative
108108
}
109109

110110
// NeedsStackObjects returns true if the compiler should insert stack objects
111111
// that can be traced by the garbage collector.
112112
func (c *Config) NeedsStackObjects() bool {
113113
switch c.GC() {
114-
case "conservative", "custom", "precise":
114+
case GCConservative, GCCustom, GCPrecise:
115115
for _, tag := range c.BuildTags() {
116116
if tag == "tinygo.wasm" {
117117
return true
@@ -134,7 +134,7 @@ func (c *Config) Scheduler() string {
134134
return c.Target.Scheduler
135135
}
136136
// Fall back to none.
137-
return "none"
137+
return SchedulerNone
138138
}
139139

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

152152
// OptLevels returns the optimization level (0-2), size level (0-2), and inliner
153153
// threshold as used in the LLVM optimization pipeline.
154154
func (c *Config) OptLevel() (level string, speedLevel, sizeLevel int) {
155155
switch c.Options.Opt {
156-
case "none", "0":
156+
case OptNone, "0":
157157
return "O0", 0, 0
158-
case "1":
158+
case Opt1:
159159
return "O1", 1, 0
160-
case "2":
160+
case Opt2:
161161
return "O2", 2, 0
162-
case "s":
162+
case Opts:
163163
return "Os", 2, 1
164-
case "z":
164+
case Optz:
165165
return "Oz", 2, 2 // default
166166
default:
167167
// This is not shown to the user: valid choices are already checked as
@@ -181,7 +181,7 @@ func (c *Config) PanicStrategy() string {
181181
// automatically at compile time, if possible. If it is false, no attempt is
182182
// made.
183183
func (c *Config) AutomaticStackSize() bool {
184-
if c.Target.AutoStackSize != nil && c.Scheduler() == "tasks" {
184+
if c.Target.AutoStackSize != nil && c.Scheduler() == SchedulerTasks {
185185
return *c.Target.AutoStackSize
186186
}
187187
return false
@@ -218,10 +218,10 @@ func (c *Config) RP2040BootPatch() bool {
218218
// vs thumb* vs arm64.
219219
func CanonicalArchName(triple string) string {
220220
arch := strings.Split(triple, "-")[0]
221-
if arch == "arm64" {
221+
if arch == ArchArm64 {
222222
return "aarch64"
223223
}
224-
if strings.HasPrefix(arch, "arm") || strings.HasPrefix(arch, "thumb") {
224+
if strings.HasPrefix(arch, ArchArm) || strings.HasPrefix(arch, "thumb") {
225225
return "arm"
226226
}
227227
if arch == "mipsel" {
@@ -252,7 +252,7 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {
252252
}
253253

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

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

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

290290
// CFlags returns the flags to pass to the C compiler. This is necessary for CGo
291291
// preprocessing.
292292
func (c *Config) CFlags(libclang bool) []string {
293293
var cflags []string
294294
for _, flag := range c.Target.CFlags {
295-
cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
295+
cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get(TinyGoRoot)))
296296
}
297297
resourceDir := goenv.ClangResourceDir(libclang)
298298
if resourceDir != "" {
@@ -306,13 +306,13 @@ func (c *Config) CFlags(libclang bool) []string {
306306
}
307307
switch c.Target.Libc {
308308
case "darwin-libSystem":
309-
root := goenv.Get("TINYGOROOT")
309+
root := goenv.Get(TinyGoRoot)
310310
cflags = append(cflags,
311311
"-nostdlibinc",
312312
"-isystem", filepath.Join(root, "lib/macos-minimal-sdk/src/usr/include"),
313313
)
314314
case "picolibc":
315-
root := goenv.Get("TINYGOROOT")
315+
root := goenv.Get(TinyGoRoot)
316316
picolibcDir := filepath.Join(root, "lib", "picolibc", "newlib", "libc")
317317
path, _ := c.LibcPath("picolibc")
318318
cflags = append(cflags,
@@ -322,7 +322,7 @@ func (c *Config) CFlags(libclang bool) []string {
322322
"-isystem", filepath.Join(picolibcDir, "tinystdio"),
323323
)
324324
case "musl":
325-
root := goenv.Get("TINYGOROOT")
325+
root := goenv.Get(TinyGoRoot)
326326
path, _ := c.LibcPath("musl")
327327
arch := MuslArchitecture(c.Triple())
328328
cflags = append(cflags,
@@ -332,14 +332,14 @@ func (c *Config) CFlags(libclang bool) []string {
332332
"-isystem", filepath.Join(root, "lib", "musl", "include"),
333333
)
334334
case "wasi-libc":
335-
root := goenv.Get("TINYGOROOT")
335+
root := goenv.Get(TinyGoRoot)
336336
cflags = append(cflags,
337337
"-nostdlibinc",
338338
"-isystem", root+"/lib/wasi-libc/sysroot/include")
339339
case "wasmbuiltins":
340340
// nothing to add (library is purely for builtins)
341341
case "mingw-w64":
342-
root := goenv.Get("TINYGOROOT")
342+
root := goenv.Get(TinyGoRoot)
343343
path, _ := c.LibcPath("mingw-w64")
344344
cflags = append(cflags,
345345
"-nostdlibinc",
@@ -385,7 +385,7 @@ func (c *Config) CFlags(libclang bool) []string {
385385
// (like the one for the compiler runtime), but this represents the majority of
386386
// the flags.
387387
func (c *Config) LDFlags() []string {
388-
root := goenv.Get("TINYGOROOT")
388+
root := goenv.Get(TinyGoRoot)
389389
// Merge and adjust LDFlags.
390390
var ldflags []string
391391
for _, flag := range c.Target.LDFlags {
@@ -426,37 +426,41 @@ func (c *Config) Debug() bool {
426426
// BinaryFormat returns an appropriate binary format, based on the file
427427
// extension and the configured binary format in the target JSON file.
428428
func (c *Config) BinaryFormat(ext string) string {
429+
if len(ext) > 1 {
430+
ext = ext[1:] // remove leading '.'
431+
}
432+
429433
switch ext {
430-
case ".bin", ".gba", ".nro":
434+
case BinFormatBin, BinFormatGba, BinFormatNro:
431435
// The simplest format possible: dump everything in a raw binary file.
432436
if c.Target.BinaryFormat != "" {
433437
return c.Target.BinaryFormat
434438
}
435-
return "bin"
436-
case ".img":
439+
return BinFormatBin
440+
case BinFormatImg:
437441
// Image file. Only defined for the ESP32 at the moment, where it is a
438442
// full (runnable) image that can be used in the Espressif QEMU fork.
439443
if c.Target.BinaryFormat != "" {
440-
return c.Target.BinaryFormat + "-img"
444+
return c.Target.BinaryFormat + "-" + BinFormatImg
441445
}
442-
return "bin"
443-
case ".hex":
446+
return BinFormatBin
447+
case BinFormatHex:
444448
// Similar to bin, but includes the start address and is thus usually a
445449
// better format.
446-
return "hex"
447-
case ".uf2":
450+
return BinFormatHex
451+
case BinFormatUf2:
448452
// Special purpose firmware format, mainly used on Adafruit boards.
449453
// More information:
450454
// https://github.com/Microsoft/uf2
451-
return "uf2"
452-
case ".zip":
455+
return BinFormatUf2
456+
case BinFormatZip:
453457
if c.Target.BinaryFormat != "" {
454458
return c.Target.BinaryFormat
455459
}
456-
return "zip"
460+
return BinFormatZip
457461
default:
458462
// Use the ELF format for unrecognized file formats.
459-
return "elf"
463+
return BinExtElf
460464
}
461465
}
462466

@@ -468,16 +472,16 @@ func (c *Config) Programmer() (method, openocdInterface string) {
468472
case "":
469473
// No configuration supplied.
470474
return c.Target.FlashMethod, c.Target.OpenOCDInterface
471-
case "openocd", "msd", "command":
475+
case ProgOpenOCD, ProgMSD, ProgCommand:
472476
// The -programmer flag only specifies the flash method.
473477
return c.Options.Programmer, c.Target.OpenOCDInterface
474-
case "bmp":
478+
case ProgBMP:
475479
// The -programmer flag only specifies the flash method.
476480
return c.Options.Programmer, ""
477481
default:
478482
// The -programmer flag specifies something else, assume it specifies
479483
// the OpenOCD interface name.
480-
return "openocd", c.Options.Programmer
484+
return ProgOpenOCD, c.Options.Programmer
481485
}
482486
}
483487

@@ -570,7 +574,7 @@ func (c *Config) Emulator(format, binary string) ([]string, error) {
570574
}
571575
var emulator []string
572576
for _, s := range parts {
573-
s = strings.ReplaceAll(s, "{root}", goenv.Get("TINYGOROOT"))
577+
s = strings.ReplaceAll(s, "{root}", goenv.Get(TinyGoRoot))
574578
// Allow replacement of what's usually /tmp except notably Windows.
575579
s = strings.ReplaceAll(s, "{tmpDir}", os.TempDir())
576580
s = strings.ReplaceAll(s, "{"+format+"}", binary)

compileopts/options.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
)
99

1010
var (
11-
validGCOptions = []string{"none", "leaking", "conservative", "custom", "precise"}
12-
validSchedulerOptions = []string{"none", "tasks", "asyncify"}
13-
validSerialOptions = []string{"none", "uart", "usb", "rtt"}
14-
validPrintSizeOptions = []string{"none", "short", "full"}
15-
validPanicStrategyOptions = []string{"print", "trap"}
16-
validOptOptions = []string{"none", "0", "1", "2", "s", "z"}
11+
validGCOptions = []string{GCNone, GCLeaking, GCConservative, GCCustom, GCPrecise}
12+
validSchedulerOptions = []string{SchedulerNone, SchedulerTasks, SchedulerAsyncify}
13+
validSerialOptions = []string{SerialNone, SerialUART, SerialUSB, SerialRTT}
14+
validPrintSizeOptions = []string{SizeNone, SizeShort, SizeFull}
15+
validPanicStrategyOptions = []string{PanicPrint, PanicTrap}
16+
validOptOptions = []string{OptNone, Opt1, Opt2, Opt3, Opts, Optz}
1717
)
1818

1919
// Options contains extra options to give to the compiler. These options are
@@ -26,18 +26,18 @@ type Options struct {
2626
GOMIPS string // environment variable (only used with GOARCH=mips and GOARCH=mipsle)
2727
Directory string // working dir, leave it unset to use the current working dir
2828
Target string
29-
Opt string
30-
GC string
31-
PanicStrategy string
29+
Opt string // optimization level. may be O0, O1, O2, O3, Os, or Oz
30+
GC string // garbage collection strategy. may be
31+
PanicStrategy string // panic strategy. may be print, or trap
3232
Scheduler string
3333
StackSize uint64 // goroutine stack size (if none could be automatically determined)
3434
Serial string
3535
Work bool // -work flag to print temporary build directory
3636
InterpTimeout time.Duration
37-
PrintIR bool
38-
DumpSSA bool
39-
VerifyIR bool
40-
SkipDWARF bool
37+
PrintIR bool // provide the build in llvm intermediate representation (LLVM-IR)
38+
DumpSSA bool // provide the build in single static assignment (ssa)
39+
VerifyIR bool // verify the generate IR via llvm.VerifyModule
40+
SkipDWARF bool // do not generate DWARF debug information
4141
PrintCommands func(cmd string, args ...string) `json:"-"`
4242
Semaphore chan struct{} `json:"-"` // -p flag controls cap
4343
Debug bool

compileopts/options_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ func TestVerifyOptions(t *testing.T) {
6464
{
6565
name: "SchedulerOptionNone",
6666
opts: compileopts.Options{
67-
Scheduler: "none",
67+
Scheduler: compileopts.SchedulerNone,
6868
},
6969
},
7070
{
7171
name: "SchedulerOptionTasks",
7272
opts: compileopts.Options{
73-
Scheduler: "tasks",
73+
Scheduler: compileopts.SchedulerTasks,
7474
},
7575
},
7676
{

compileopts/target.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
194194
return nil, fmt.Errorf("%s : %w", options.Target, err)
195195
}
196196

197-
if spec.Scheduler == "asyncify" {
197+
if spec.Scheduler == SchedulerAsyncify {
198198
spec.ExtraFiles = append(spec.ExtraFiles, "src/internal/task/task_asyncify_wasm.S")
199199
}
200200

@@ -246,7 +246,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
246246
GOARCH: options.GOARCH,
247247
BuildTags: []string{options.GOOS, options.GOARCH},
248248
GC: "precise",
249-
Scheduler: "tasks",
249+
Scheduler: SchedulerTasks,
250250
Linker: "cc",
251251
DefaultStackSize: 1024 * 64, // 64kB
252252
GDB: []string{"gdb"},
@@ -440,7 +440,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
440440
)
441441
case "wasip1":
442442
spec.GC = "" // use default GC
443-
spec.Scheduler = "asyncify"
443+
spec.Scheduler = SchedulerAsyncify
444444
spec.Linker = "wasm-ld"
445445
spec.RTLib = "compiler-rt"
446446
spec.Libc = "wasi-libc"

0 commit comments

Comments
 (0)