Skip to content

Commit

Permalink
feat(cmd/rofl): Add initial support for ROFL app management
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Aug 20, 2024
1 parent 36e341c commit 53eddfc
Show file tree
Hide file tree
Showing 11 changed files with 825 additions and 3 deletions.
111 changes: 111 additions & 0 deletions build/cargo/cargo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Package cargo contains helper functions for building Rust applications using cargo.
package cargo

import (
"encoding/json"
"fmt"
"os/exec"
)

// Metadata is the cargo package metadata.
type Metadata struct {
Name string
Version string
}

// GetMetadata queries `cargo` for metadata of the package in the current working directory.
func GetMetadata() (*Metadata, error) {
cmd := exec.Command("cargo", "metadata", "--no-deps")
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("failed to initialize metadata process: %w", err)
}
if err = cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start metadata process: %w", err)
}

dec := json.NewDecoder(stdout)
var rawMeta struct {
Packages []struct {
Name string `json:"name"`
Version string `json:"version"`
} `json:"packages"`
}
if err = dec.Decode(&rawMeta); err != nil {
return nil, fmt.Errorf("malformed cargo metadata: %w", err)
}
if err = cmd.Wait(); err != nil {
return nil, fmt.Errorf("metadata process failed: %w", err)
}
if len(rawMeta.Packages) == 0 {
return nil, fmt.Errorf("no cargo packages found")
}

return &Metadata{
Name: rawMeta.Packages[0].Name,
Version: rawMeta.Packages[0].Version,
}, nil
}

// Build builds a Rust program using `cargo` in the current working directory.
func Build(release bool, target string) (string, error) {
args := []string{"build"}
if release {
args = append(args, "--release")
}
if target != "" {
args = append(args, "--target", target)
}
// Ensure the build process outputs JSON.
args = append(args, "--message-format", "json")

cmd := exec.Command("cargo", args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", fmt.Errorf("failed to initialize build process: %w", err)
}
if err = cmd.Start(); err != nil {
return "", fmt.Errorf("failed to start build process: %w", err)
}

var executable string
dec := json.NewDecoder(stdout)
for {
var output struct {
Reason string `json:"reason"`
PackageID string `json:"package_id,omitempty"`
Target struct {
Kind []string `json:"kind"`
} `json:"target,omitempty"`
Executable string `json:"executable,omitempty"`
Message struct {
Rendered string `json:"rendered"`
} `json:"message,omitempty"`
}
if err = dec.Decode(&output); err != nil {
break
}

switch output.Reason {
case "compiler-message":
fmt.Println(output.Message.Rendered)
case "compiler-artifact":
fmt.Printf("[built] %s\n", output.PackageID)
if len(output.Target.Kind) != 1 || output.Target.Kind[0] != "bin" {
continue
}

// Extract the last built executable.
executable = output.Executable
default:
}
}
if err = cmd.Wait(); err != nil {
return "", fmt.Errorf("build process failed: %w", err)
}

if executable == "" {
return "", fmt.Errorf("no executable generated")
}
return executable, nil
}
23 changes: 23 additions & 0 deletions build/sgxs/sgxs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Package sgxs contains helper functions for dealing with ELF and SGXS binaries.
package sgxs

import (
"os/exec"
"strconv"
)

// Elf2Sgxs converts an ELF binary built for the SGX ABI into an SGXS binary.
//
// It requires the `ftxsgx-elf2sgxs` utility to be installed.
func Elf2Sgxs(elfSgxPath, sgxsPath string, heapSize, stackSize, threads uint64) error {
args := []string{
elfSgxPath,
"--heap-size", strconv.FormatUint(heapSize, 10),
"--stack-size", strconv.FormatUint(stackSize, 10),
"--threads", strconv.FormatUint(threads, 10),
"--output", sgxsPath,
}

cmd := exec.Command("ftxsgx-elf2sgxs", args...)
return cmd.Run()
}
2 changes: 1 addition & 1 deletion cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
Expand Down
Loading

0 comments on commit 53eddfc

Please sign in to comment.