Skip to content
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

feat(cmd/rofl): Add initial support for ROFL app management #265

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
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
Loading