@@ -104,14 +104,14 @@ func (c *Config) GC() string {
104
104
if c .Target .GC != "" {
105
105
return c .Target .GC
106
106
}
107
- return "conservative"
107
+ return GCConservative
108
108
}
109
109
110
110
// NeedsStackObjects returns true if the compiler should insert stack objects
111
111
// that can be traced by the garbage collector.
112
112
func (c * Config ) NeedsStackObjects () bool {
113
113
switch c .GC () {
114
- case "conservative" , "custom" , "precise" :
114
+ case GCConservative , GCCustom , GCPrecise :
115
115
for _ , tag := range c .BuildTags () {
116
116
if tag == "tinygo.wasm" {
117
117
return true
@@ -134,7 +134,7 @@ func (c *Config) Scheduler() string {
134
134
return c .Target .Scheduler
135
135
}
136
136
// Fall back to none.
137
- return "none"
137
+ return SchedulerNone
138
138
}
139
139
140
140
// Serial returns the serial implementation for this build configuration: uart,
@@ -146,22 +146,22 @@ func (c *Config) Serial() string {
146
146
if c .Target .Serial != "" {
147
147
return c .Target .Serial
148
148
}
149
- return "none"
149
+ return SerialNone
150
150
}
151
151
152
152
// OptLevels returns the optimization level (0-2), size level (0-2), and inliner
153
153
// threshold as used in the LLVM optimization pipeline.
154
154
func (c * Config ) OptLevel () (level string , speedLevel , sizeLevel int ) {
155
155
switch c .Options .Opt {
156
- case "none" , "0" :
156
+ case OptNone , "0" :
157
157
return "O0" , 0 , 0
158
- case "1" :
158
+ case Opt1 :
159
159
return "O1" , 1 , 0
160
- case "2" :
160
+ case Opt2 :
161
161
return "O2" , 2 , 0
162
- case "s" :
162
+ case Opts :
163
163
return "Os" , 2 , 1
164
- case "z" :
164
+ case Optz :
165
165
return "Oz" , 2 , 2 // default
166
166
default :
167
167
// This is not shown to the user: valid choices are already checked as
@@ -181,7 +181,7 @@ func (c *Config) PanicStrategy() string {
181
181
// automatically at compile time, if possible. If it is false, no attempt is
182
182
// made.
183
183
func (c * Config ) AutomaticStackSize () bool {
184
- if c .Target .AutoStackSize != nil && c .Scheduler () == "tasks" {
184
+ if c .Target .AutoStackSize != nil && c .Scheduler () == SchedulerTasks {
185
185
return * c .Target .AutoStackSize
186
186
}
187
187
return false
@@ -218,10 +218,10 @@ func (c *Config) RP2040BootPatch() bool {
218
218
// vs thumb* vs arm64.
219
219
func CanonicalArchName (triple string ) string {
220
220
arch := strings .Split (triple , "-" )[0 ]
221
- if arch == "arm64" {
221
+ if arch == ArchArm64 {
222
222
return "aarch64"
223
223
}
224
- if strings .HasPrefix (arch , "arm" ) || strings .HasPrefix (arch , "thumb" ) {
224
+ if strings .HasPrefix (arch , ArchArm ) || strings .HasPrefix (arch , "thumb" ) {
225
225
return "arm"
226
226
}
227
227
if arch == "mipsel" {
@@ -252,7 +252,7 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {
252
252
}
253
253
254
254
// 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 )
256
256
if _ , err := os .Stat (precompiledDir ); err == nil {
257
257
// Found a precompiled library for this OS/architecture. Return the path
258
258
// directly.
@@ -261,38 +261,38 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {
261
261
262
262
// No precompiled library found. Determine the path name that will be used
263
263
// in the build cache.
264
- return filepath .Join (goenv .Get ("GOCACHE" ), name + "-" + archname ), false
264
+ return filepath .Join (goenv .Get (GolangCache ), name + "-" + archname ), false
265
265
}
266
266
267
267
// 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).
269
269
func (c * Config ) DefaultBinaryExtension () string {
270
270
parts := strings .Split (c .Triple (), "-" )
271
271
if parts [0 ] == "wasm32" {
272
272
// WebAssembly files always have the .wasm file extension.
273
- return ".wasm"
273
+ return "." + BinExtWasm
274
274
}
275
- if len (parts ) >= 3 && parts [2 ] == "windows" {
275
+ if len (parts ) >= 3 && parts [2 ] == OsWindows {
276
276
// Windows uses .exe.
277
- return ".exe"
277
+ return "." + BinExtExe
278
278
}
279
279
if len (parts ) >= 3 && parts [2 ] == "unknown" {
280
280
// There appears to be a convention to use the .elf file extension for
281
281
// ELF files intended for microcontrollers. I'm not aware of the origin
282
282
// of this, it's just something that is used by many projects.
283
283
// I think it's a good tradition, so let's keep it.
284
- return ".elf"
284
+ return "." + BinExtElf
285
285
}
286
286
// Linux, MacOS, etc, don't use a file extension. Use it as a fallback.
287
- return ""
287
+ return BinExtNone
288
288
}
289
289
290
290
// CFlags returns the flags to pass to the C compiler. This is necessary for CGo
291
291
// preprocessing.
292
292
func (c * Config ) CFlags (libclang bool ) []string {
293
293
var cflags []string
294
294
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 )))
296
296
}
297
297
resourceDir := goenv .ClangResourceDir (libclang )
298
298
if resourceDir != "" {
@@ -306,13 +306,13 @@ func (c *Config) CFlags(libclang bool) []string {
306
306
}
307
307
switch c .Target .Libc {
308
308
case "darwin-libSystem" :
309
- root := goenv .Get ("TINYGOROOT" )
309
+ root := goenv .Get (TinyGoRoot )
310
310
cflags = append (cflags ,
311
311
"-nostdlibinc" ,
312
312
"-isystem" , filepath .Join (root , "lib/macos-minimal-sdk/src/usr/include" ),
313
313
)
314
314
case "picolibc" :
315
- root := goenv .Get ("TINYGOROOT" )
315
+ root := goenv .Get (TinyGoRoot )
316
316
picolibcDir := filepath .Join (root , "lib" , "picolibc" , "newlib" , "libc" )
317
317
path , _ := c .LibcPath ("picolibc" )
318
318
cflags = append (cflags ,
@@ -322,7 +322,7 @@ func (c *Config) CFlags(libclang bool) []string {
322
322
"-isystem" , filepath .Join (picolibcDir , "tinystdio" ),
323
323
)
324
324
case "musl" :
325
- root := goenv .Get ("TINYGOROOT" )
325
+ root := goenv .Get (TinyGoRoot )
326
326
path , _ := c .LibcPath ("musl" )
327
327
arch := MuslArchitecture (c .Triple ())
328
328
cflags = append (cflags ,
@@ -332,14 +332,14 @@ func (c *Config) CFlags(libclang bool) []string {
332
332
"-isystem" , filepath .Join (root , "lib" , "musl" , "include" ),
333
333
)
334
334
case "wasi-libc" :
335
- root := goenv .Get ("TINYGOROOT" )
335
+ root := goenv .Get (TinyGoRoot )
336
336
cflags = append (cflags ,
337
337
"-nostdlibinc" ,
338
338
"-isystem" , root + "/lib/wasi-libc/sysroot/include" )
339
339
case "wasmbuiltins" :
340
340
// nothing to add (library is purely for builtins)
341
341
case "mingw-w64" :
342
- root := goenv .Get ("TINYGOROOT" )
342
+ root := goenv .Get (TinyGoRoot )
343
343
path , _ := c .LibcPath ("mingw-w64" )
344
344
cflags = append (cflags ,
345
345
"-nostdlibinc" ,
@@ -385,7 +385,7 @@ func (c *Config) CFlags(libclang bool) []string {
385
385
// (like the one for the compiler runtime), but this represents the majority of
386
386
// the flags.
387
387
func (c * Config ) LDFlags () []string {
388
- root := goenv .Get ("TINYGOROOT" )
388
+ root := goenv .Get (TinyGoRoot )
389
389
// Merge and adjust LDFlags.
390
390
var ldflags []string
391
391
for _ , flag := range c .Target .LDFlags {
@@ -426,37 +426,41 @@ func (c *Config) Debug() bool {
426
426
// BinaryFormat returns an appropriate binary format, based on the file
427
427
// extension and the configured binary format in the target JSON file.
428
428
func (c * Config ) BinaryFormat (ext string ) string {
429
+ if len (ext ) > 1 {
430
+ ext = ext [1 :] // remove leading '.'
431
+ }
432
+
429
433
switch ext {
430
- case ".bin" , ".gba" , ".nro" :
434
+ case BinFormatBin , BinFormatGba , BinFormatNro :
431
435
// The simplest format possible: dump everything in a raw binary file.
432
436
if c .Target .BinaryFormat != "" {
433
437
return c .Target .BinaryFormat
434
438
}
435
- return "bin"
436
- case ".img" :
439
+ return BinFormatBin
440
+ case BinFormatImg :
437
441
// Image file. Only defined for the ESP32 at the moment, where it is a
438
442
// full (runnable) image that can be used in the Espressif QEMU fork.
439
443
if c .Target .BinaryFormat != "" {
440
- return c .Target .BinaryFormat + "-img"
444
+ return c .Target .BinaryFormat + "-" + BinFormatImg
441
445
}
442
- return "bin"
443
- case ".hex" :
446
+ return BinFormatBin
447
+ case BinFormatHex :
444
448
// Similar to bin, but includes the start address and is thus usually a
445
449
// better format.
446
- return "hex"
447
- case ".uf2" :
450
+ return BinFormatHex
451
+ case BinFormatUf2 :
448
452
// Special purpose firmware format, mainly used on Adafruit boards.
449
453
// More information:
450
454
// https://github.com/Microsoft/uf2
451
- return "uf2"
452
- case ".zip" :
455
+ return BinFormatUf2
456
+ case BinFormatZip :
453
457
if c .Target .BinaryFormat != "" {
454
458
return c .Target .BinaryFormat
455
459
}
456
- return "zip"
460
+ return BinFormatZip
457
461
default :
458
462
// Use the ELF format for unrecognized file formats.
459
- return "elf"
463
+ return BinExtElf
460
464
}
461
465
}
462
466
@@ -468,16 +472,16 @@ func (c *Config) Programmer() (method, openocdInterface string) {
468
472
case "" :
469
473
// No configuration supplied.
470
474
return c .Target .FlashMethod , c .Target .OpenOCDInterface
471
- case "openocd" , "msd" , "command" :
475
+ case ProgOpenOCD , ProgMSD , ProgCommand :
472
476
// The -programmer flag only specifies the flash method.
473
477
return c .Options .Programmer , c .Target .OpenOCDInterface
474
- case "bmp" :
478
+ case ProgBMP :
475
479
// The -programmer flag only specifies the flash method.
476
480
return c .Options .Programmer , ""
477
481
default :
478
482
// The -programmer flag specifies something else, assume it specifies
479
483
// the OpenOCD interface name.
480
- return "openocd" , c .Options .Programmer
484
+ return ProgOpenOCD , c .Options .Programmer
481
485
}
482
486
}
483
487
@@ -570,7 +574,7 @@ func (c *Config) Emulator(format, binary string) ([]string, error) {
570
574
}
571
575
var emulator []string
572
576
for _ , s := range parts {
573
- s = strings .ReplaceAll (s , "{root}" , goenv .Get ("TINYGOROOT" ))
577
+ s = strings .ReplaceAll (s , "{root}" , goenv .Get (TinyGoRoot ))
574
578
// Allow replacement of what's usually /tmp except notably Windows.
575
579
s = strings .ReplaceAll (s , "{tmpDir}" , os .TempDir ())
576
580
s = strings .ReplaceAll (s , "{" + format + "}" , binary )
0 commit comments