-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/rofl): Add initial support for ROFL app management
- Loading branch information
Showing
8 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.