Skip to content

Allow implementing different WIT worlds in wasip2 target #4934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,11 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
}

// Run wasm-tools for component-model binaries
wasiPackage := strings.ReplaceAll(config.Target.WASIPackage, "{root}", goenv.Get("TINYGOROOT"))
if config.Options.WASIPackage != "" {
wasiPackage = config.Options.WASIPackage
}

witPackage := strings.ReplaceAll(config.Target.WITPackage, "{root}", goenv.Get("TINYGOROOT"))
if config.Options.WITPackage != "" {
witPackage = config.Options.WITPackage
Expand All @@ -922,7 +927,37 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
if config.Options.WITWorld != "" {
witWorld = config.Options.WITWorld
}

if witPackage != "" && witWorld != "" {
if wasiPackage != "" {

fmt.Println("Embedding WASI component. Wasi package: ", wasiPackage)

// wasm-tools component embed -w wasi:cli/command
// $$(tinygo env TINYGOROOT)/lib/wasi-cli/wit/ main.wasm -o embedded.wasm
componentEmbedInputFile := result.Binary
result.Binary = result.Executable + ".wasm-component-embed-1"
args := []string{
"component",
"embed",
wasiPackage,
componentEmbedInputFile,
"-o", result.Binary,
}

wasmtools := goenv.Get("WASMTOOLS")
if config.Options.PrintCommands != nil {
config.Options.PrintCommands(wasmtools, args...)
}
cmd := exec.Command(wasmtools, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Run()
if err != nil {
return fmt.Errorf("`wasm-tools component embed` failed: %w", err)
}
}

// wasm-tools component embed -w wasi:cli/command
// $$(tinygo env TINYGOROOT)/lib/wasi-cli/wit/ main.wasm -o embedded.wasm
Expand Down
1 change: 1 addition & 0 deletions compileopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Options struct {
Monitor bool
BaudRate int
Timeout time.Duration
WASIPackage string // pass trough to wasm-tools component first embed invocation
WITPackage string // pass through to wasm-tools component embed invocation
WITWorld string // pass through to wasm-tools component embed -w option
ExtLDFlags []string
Expand Down
1 change: 1 addition & 0 deletions compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type TargetSpec struct {
JLinkDevice string `json:"jlink-device,omitempty"`
CodeModel string `json:"code-model,omitempty"`
RelocationModel string `json:"relocation-model,omitempty"`
WASIPackage string `json:"wasi-package,omitempty"`
WITPackage string `json:"wit-package,omitempty"`
WITWorld string `json:"wit-world,omitempty"`
}
Expand Down
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,19 +1285,19 @@ microcontrollers, it is common to use the .elf file extension to indicate a
linked ELF file is generated. For Linux, it is common to build binaries with no
extension at all.`

usageRun = `Run the program, either directly on the host or in an emulated environment
usageRun = `Run the program, either directly on the host or in an emulated environment
(depending on -target).`

usageFlash = `Flash the program to a microcontroller. Some common flags are described below.

-target={name}:
-target={name}:
Specifies the type of microcontroller that is used. The name of the
microcontroller is given on the individual pages for each board type
listed under Microcontrollers
(https://tinygo.org/docs/reference/microcontrollers/).
Examples: "arduino-nano", "d1mini", "xiao".

-monitor:
-monitor:
Start the serial monitor (see below) immediately after
flashing. However, some microcontrollers need a split second
or two to configure the serial port after flashing, and
Expand Down Expand Up @@ -1347,7 +1347,7 @@ instead of the expected \n (also known as ^J, NL, or LF) to indicate
end-of-line. You may be able to get around this problem by hitting Control-J in
tinygo monitor to transmit the \n end-of-line character.`

usageGdb = `Build the program, optionally flash it to a microcontroller if it is a remote
usageGdb = `Build the program, optionally flash it to a microcontroller if it is a remote
target, and drop into a GDB shell. From there you can set breakpoints, start the
program with "run" or "continue" ("run" for a local program, continue for
on-chip debugging), single-step, show a backtrace, break and resume the program
Expand Down Expand Up @@ -1631,8 +1631,9 @@ func main() {
flag.StringVar(&outpath, "o", "", "output filename")
}

var witPackage, witWorld string
var witPackage, witWorld, wasiPackage string
if command == "help" || command == "build" || command == "test" || command == "run" {
flag.StringVar(&wasiPackage, "wasi-package", "", "wasi package for wasm component embedding")
flag.StringVar(&witPackage, "wit-package", "", "wit package for wasm component embedding")
flag.StringVar(&witWorld, "wit-world", "", "wit world for wasm component embedding")
}
Expand Down Expand Up @@ -1720,6 +1721,7 @@ func main() {
Monitor: *monitor,
BaudRate: *baudrate,
Timeout: *timeout,
WASIPackage: wasiPackage,
WITPackage: witPackage,
WITWorld: witWorld,
}
Expand Down
1 change: 1 addition & 0 deletions targets/wasip2.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"src/runtime/asm_tinygowasm.S"
],
"emulator": "wasmtime run --wasm component-model -Sinherit-network -Sallow-ip-name-lookup --dir={tmpDir}::/tmp {}",
"wasi-package": "{root}/lib/wasi-cli/wit/",
"wit-package": "{root}/lib/wasi-cli/wit/",
"wit-world": "wasi:cli/command"
}
Loading