Skip to content

Commit 3a32210

Browse files
committed
Repackage tinygo as an RPK plugin
something quick and hacky without help args Signed-off-by: Tyler Rockwood <[email protected]>
1 parent d01d859 commit 3a32210

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

main.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,130 @@ func getListOfPackages(pkgs []string, options *compileopts.Options) ([]string, e
13861386
return pkgNames, nil
13871387
}
13881388

1389+
// This is a pure addition to upstream tinygo.
1390+
//
1391+
// It provides a hardcoded `build` command and autocomplete support for rpk
13891392
func main() {
1393+
if len(os.Args) > 1 {
1394+
// Early command processing, before commands are interpreted by the Go flag
1395+
// library.
1396+
switch os.Args[1] {
1397+
case "clang", "ld.lld", "wasm-ld":
1398+
err := builder.RunTool(os.Args[1], os.Args[2:]...)
1399+
if err != nil {
1400+
fmt.Fprintln(os.Stderr, err)
1401+
os.Exit(1)
1402+
}
1403+
os.Exit(0)
1404+
}
1405+
}
1406+
1407+
command := "build"
1408+
1409+
opt := flag.String("opt", "z", "optimization level: 0, 1, 2, s, z")
1410+
gc := flag.String("gc", "conservative", "garbage collector to use (none, leaking, conservative)")
1411+
panicStrategy := flag.String("panic", "print", "panic strategy (print, trap)")
1412+
scheduler := flag.String("scheduler", "none", "which scheduler to use (none, tasks, asyncify)")
1413+
work := flag.Bool("work", false, "print the name of the temporary build directory and do not delete this directory on exit")
1414+
interpTimeout := flag.Duration("interp-timeout", 180*time.Second, "interp optimization pass timeout")
1415+
var tags buildutil.TagsFlag
1416+
flag.Var(&tags, "tags", "a space-separated list of extra build tags")
1417+
target := flag.String("target", "wasi", "chip/board name or JSON target specification file")
1418+
var stackSize uint64
1419+
flag.Func("stack-size", "goroutine stack size (if unknown at compile time)", func(s string) error {
1420+
size, err := bytesize.Parse(s)
1421+
stackSize = uint64(size)
1422+
return err
1423+
})
1424+
printSize := flag.String("size", "", "print sizes (none, short, full)")
1425+
printStacks := flag.Bool("print-stacks", false, "print stack sizes of goroutines")
1426+
printAllocsString := flag.String("print-allocs", "", "regular expression of functions for which heap allocations should be printed")
1427+
printCommands := flag.Bool("x", false, "Print commands")
1428+
parallelism := flag.Int("p", runtime.GOMAXPROCS(0), "the number of build jobs that can run in parallel")
1429+
nodebug := flag.Bool("no-debug", false, "strip debug information")
1430+
programmer := flag.String("programmer", "", "which hardware programmer to use")
1431+
ldflags := flag.String("ldflags", "", "Go link tool compatible ldflags")
1432+
llvmFeatures := flag.String("llvm-features", "", "comma separated LLVM features to enable")
1433+
flagJSON := flag.Bool("json", false, "print data in JSON format")
1434+
outpath := flag.String("o", "", "output filename")
1435+
// strip the .rpk.managed-tinygo prefix
1436+
flag.CommandLine.Parse(os.Args[1:])
1437+
globalVarValues, err := parseGoLinkFlag(*ldflags)
1438+
if err != nil {
1439+
fmt.Fprintln(os.Stderr, err)
1440+
os.Exit(1)
1441+
}
1442+
1443+
var printAllocs *regexp.Regexp
1444+
if *printAllocsString != "" {
1445+
printAllocs, err = regexp.Compile(*printAllocsString)
1446+
if err != nil {
1447+
fmt.Fprintln(os.Stderr, err)
1448+
os.Exit(1)
1449+
}
1450+
}
1451+
options := &compileopts.Options{
1452+
GOOS: goenv.Get("GOOS"),
1453+
GOARCH: goenv.Get("GOARCH"),
1454+
GOARM: goenv.Get("GOARM"),
1455+
Target: *target,
1456+
StackSize: stackSize,
1457+
Opt: *opt,
1458+
GC: *gc,
1459+
PanicStrategy: *panicStrategy,
1460+
Scheduler: *scheduler,
1461+
Serial: "",
1462+
Work: *work,
1463+
InterpTimeout: *interpTimeout,
1464+
PrintIR: false,
1465+
DumpSSA: false,
1466+
VerifyIR: false,
1467+
SkipDWARF: false,
1468+
Semaphore: make(chan struct{}, *parallelism),
1469+
Debug: !*nodebug,
1470+
PrintSizes: *printSize,
1471+
PrintStacks: *printStacks,
1472+
PrintAllocs: printAllocs,
1473+
Tags: []string(tags),
1474+
TestConfig: compileopts.TestConfig{},
1475+
GlobalValues: globalVarValues,
1476+
Programmer: *programmer,
1477+
OpenOCDCommands: nil,
1478+
LLVMFeatures: *llvmFeatures,
1479+
PrintJSON: *flagJSON,
1480+
Monitor: false,
1481+
BaudRate: 115200,
1482+
Timeout: 20 * time.Second,
1483+
}
1484+
if *printCommands {
1485+
options.PrintCommands = printCommand
1486+
}
1487+
err = options.Verify()
1488+
if err != nil {
1489+
fmt.Fprintln(os.Stderr, err.Error())
1490+
usage(command)
1491+
os.Exit(1)
1492+
}
1493+
pkgName := "."
1494+
if flag.NArg() == 1 {
1495+
pkgName = filepath.ToSlash(flag.Arg(0))
1496+
} else if flag.NArg() > 1 {
1497+
fmt.Fprintln(os.Stderr, "build only accepts a single positional argument: package name, but multiple were specified")
1498+
usage(command)
1499+
os.Exit(1)
1500+
}
1501+
if options.Target == "" && filepath.Ext(*outpath) == ".wasm" {
1502+
options.Target = "wasm"
1503+
}
1504+
1505+
err = Build(pkgName, *outpath, options)
1506+
handleCompilerError(err)
1507+
fmt.Println("build successful")
1508+
fmt.Println("deploy your wasm function to a topic:")
1509+
fmt.Println("\trpk wasm deploy")
1510+
}
1511+
1512+
func upstreamMain() {
13901513
if len(os.Args) < 2 {
13911514
fmt.Fprintln(os.Stderr, "No command-line arguments supplied.")
13921515
usage("")

0 commit comments

Comments
 (0)